-
Notifications
You must be signed in to change notification settings - Fork 154
/
autodiff_test.py
191 lines (148 loc) · 6.63 KB
/
autodiff_test.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import autodiff as ad
import numpy as np
def test_identity():
x2 = ad.Variable(name = "x2")
y = x2
grad_x2, = ad.gradients(y, [x2])
executor = ad.Executor([y, grad_x2])
x2_val = 2 * np.ones(3)
y_val, grad_x2_val= executor.run(feed_dict = {x2 : x2_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x2_val)
assert np.array_equal(grad_x2_val, np.ones_like(x2_val))
def test_add_by_const():
x2 = ad.Variable(name = "x2")
y = 5 + x2
grad_x2, = ad.gradients(y, [x2])
executor = ad.Executor([y, grad_x2])
x2_val = 2 * np.ones(3)
y_val, grad_x2_val= executor.run(feed_dict = {x2 : x2_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x2_val + 5)
assert np.array_equal(grad_x2_val, np.ones_like(x2_val))
def test_mul_by_const():
x2 = ad.Variable(name = "x2")
y = 5 * x2
grad_x2, = ad.gradients(y, [x2])
executor = ad.Executor([y, grad_x2])
x2_val = 2 * np.ones(3)
y_val, grad_x2_val= executor.run(feed_dict = {x2 : x2_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x2_val * 5)
assert np.array_equal(grad_x2_val, np.ones_like(x2_val) * 5)
def test_add_two_vars():
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
y = x2 + x3
grad_x2, grad_x3 = ad.gradients(y, [x2, x3])
executor = ad.Executor([y, grad_x2, grad_x3])
x2_val = 2 * np.ones(3)
x3_val = 3 * np.ones(3)
y_val, grad_x2_val, grad_x3_val = executor.run(feed_dict = {x2: x2_val, x3: x3_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x2_val + x3_val)
assert np.array_equal(grad_x2_val, np.ones_like(x2_val))
assert np.array_equal(grad_x3_val, np.ones_like(x3_val))
def test_mul_two_vars():
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
y = x2 * x3
grad_x2, grad_x3 = ad.gradients(y, [x2, x3])
executor = ad.Executor([y, grad_x2, grad_x3])
x2_val = 2 * np.ones(3)
x3_val = 3 * np.ones(3)
y_val, grad_x2_val, grad_x3_val = executor.run(feed_dict = {x2: x2_val, x3: x3_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x2_val * x3_val)
assert np.array_equal(grad_x2_val, x3_val)
assert np.array_equal(grad_x3_val, x2_val)
def test_add_mul_mix_1():
x1 = ad.Variable(name = "x1")
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
y = x1 + x2 * x3 * x1
grad_x1, grad_x2, grad_x3 = ad.gradients(y, [x1, x2, x3])
executor = ad.Executor([y, grad_x1, grad_x2, grad_x3])
x1_val = 1 * np.ones(3)
x2_val = 2 * np.ones(3)
x3_val = 3 * np.ones(3)
y_val, grad_x1_val, grad_x2_val, grad_x3_val = executor.run(feed_dict = {x1 : x1_val, x2: x2_val, x3 : x3_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x1_val + x2_val * x3_val)
assert np.array_equal(grad_x1_val, np.ones_like(x1_val) + x2_val * x3_val)
assert np.array_equal(grad_x2_val, x3_val * x1_val)
assert np.array_equal(grad_x3_val, x2_val * x1_val)
def test_add_mul_mix_2():
x1 = ad.Variable(name = "x1")
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
x4 = ad.Variable(name = "x4")
y = x1 + x2 * x3 * x4
grad_x1, grad_x2, grad_x3, grad_x4 = ad.gradients(y, [x1, x2, x3, x4])
executor = ad.Executor([y, grad_x1, grad_x2, grad_x3, grad_x4])
x1_val = 1 * np.ones(3)
x2_val = 2 * np.ones(3)
x3_val = 3 * np.ones(3)
x4_val = 4 * np.ones(3)
y_val, grad_x1_val, grad_x2_val, grad_x3_val, grad_x4_val = executor.run(feed_dict = {x1 : x1_val, x2: x2_val, x3 : x3_val, x4 : x4_val})
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, x1_val + x2_val * x3_val * x4_val)
assert np.array_equal(grad_x1_val, np.ones_like(x1_val))
assert np.array_equal(grad_x2_val, x3_val * x4_val)
assert np.array_equal(grad_x3_val, x2_val * x4_val)
assert np.array_equal(grad_x4_val, x2_val * x3_val)
def test_add_mul_mix_3():
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
z = x2 * x2 + x2 + x3 + 3
y = z * z + x3
grad_x2, grad_x3 = ad.gradients(y, [x2, x3])
executor = ad.Executor([y, grad_x2, grad_x3])
x2_val = 2 * np.ones(3)
x3_val = 3 * np.ones(3)
y_val, grad_x2_val, grad_x3_val = executor.run(feed_dict = {x2: x2_val, x3: x3_val})
z_val = x2_val * x2_val + x2_val + x3_val + 3
expected_yval = z_val * z_val + x3_val
expected_grad_x2_val = 2 * (x2_val * x2_val + x2_val + x3_val + 3) * (2 * x2_val + 1)
expected_grad_x3_val = 2 * (x2_val * x2_val + x2_val + x3_val + 3) + 1
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, expected_yval)
assert np.array_equal(grad_x2_val, expected_grad_x2_val)
assert np.array_equal(grad_x3_val, expected_grad_x3_val)
def test_grad_of_grad():
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
y = x2 * x2 + x2 * x3
grad_x2, grad_x3 = ad.gradients(y, [x2, x3])
grad_x2_x2, grad_x2_x3 = ad.gradients(grad_x2, [x2, x3])
executor = ad.Executor([y, grad_x2, grad_x3, grad_x2_x2, grad_x2_x3])
x2_val = 2 * np.ones(3)
x3_val = 3 * np.ones(3)
y_val, grad_x2_val, grad_x3_val, grad_x2_x2_val, grad_x2_x3_val = executor.run(feed_dict = {x2: x2_val, x3: x3_val})
expected_yval = x2_val * x2_val + x2_val * x3_val
expected_grad_x2_val = 2 * x2_val + x3_val
expected_grad_x3_val = x2_val
expected_grad_x2_x2_val = 2 * np.ones_like(x2_val)
expected_grad_x2_x3_val = 1 * np.ones_like(x2_val)
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, expected_yval)
assert np.array_equal(grad_x2_val, expected_grad_x2_val)
assert np.array_equal(grad_x3_val, expected_grad_x3_val)
assert np.array_equal(grad_x2_x2_val, expected_grad_x2_x2_val)
assert np.array_equal(grad_x2_x3_val, expected_grad_x2_x3_val)
def test_matmul_two_vars():
x2 = ad.Variable(name = "x2")
x3 = ad.Variable(name = "x3")
y = ad.matmul_op(x2, x3)
grad_x2, grad_x3 = ad.gradients(y, [x2, x3])
executor = ad.Executor([y, grad_x2, grad_x3])
x2_val = np.array([[1, 2], [3, 4], [5, 6]]) # 3x2
x3_val = np.array([[7, 8, 9], [10, 11, 12]]) # 2x3
y_val, grad_x2_val, grad_x3_val = executor.run(feed_dict = {x2: x2_val, x3: x3_val})
expected_yval = np.matmul(x2_val, x3_val)
expected_grad_x2_val = np.matmul(np.ones_like(expected_yval), np.transpose(x3_val))
expected_grad_x3_val = np.matmul(np.transpose(x2_val), np.ones_like(expected_yval))
assert isinstance(y, ad.Node)
assert np.array_equal(y_val, expected_yval)
assert np.array_equal(grad_x2_val, expected_grad_x2_val)
assert np.array_equal(grad_x3_val, expected_grad_x3_val)