-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
67 lines (57 loc) · 1.41 KB
/
Copy pathtest.py
File metadata and controls
67 lines (57 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""测试"""
import numpy as np
from reverseAD import *
# x = var('x')
# y = var('y')
#
# z = log(x) + x * y
# z = x + y
# z = x + 1 + y
# z = 1 + x + y
# z = x - y
# z = 1 - x - y
# z = x - y - 1
# z = x * y
# z = 3 * x * y
# z = x / y
# z = x / 3 + y
# z = 3 / x + y
# z = log(x) + y
# z = x @ y
# values = {x: np.array([[1, 2], [3, 4], [5, 6]]), y: np.array([[7, 8, 9], [10, 11, 12]])}
# z = sumOp(x) + sumOp(y)
# values = {x: np.asarray([1, 2, 3]), y: np.asarray([4, 5, 6])}
# z = 1 / (1 + exp(-x)) + y # sigmoid
# values = {x: np.asarray(2), y: np.asarray(5)}
#
# print(f"z = {z}")
#
# grads = gradient(z, [x, y])
# print(f"grad_x: {grads[0]}\ngrad_y: {grads[1]}")
#
# executor = Executor([z] + grads)
# res = executor.run(values)
# print(f"z = {res[0]}")
# print(f"grad_x: {res[1]}\ngrad_y: {res[2]}")
# print(res)
# # softmax
# def test_softmax():
#
# x = var("x")
# z = exp(x) / sumOp(exp(x))
# print(f"z = {z}")
# grads = gradient(z, [x])
# print(f"grad_x: {grads[0]}")
# executor = Executor([z] + grads)
# values = {x: np.asarray([2, 3, 5])}
# res = executor.run(values)
# """
# softmax求导方法
# 对于i==j ∂softmax(x)/∂xi = softmax(xi)(1-softmax(xi)
# 对于i!=j ∂softmax(x)/∂xi = -softmax(xj)softmax(xi)
# """
# res[1][np.argmax(res[0])] = 1 - res[1][np.argmax(res[0])]
# print(f"z = {res[0]}")
# print(f"grad_x: {res[1]}")
#
# test_softmax()