-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathneural_network_test.py
316 lines (248 loc) · 10.2 KB
/
neural_network_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from assertpy import assert_that
import pytest
from neural_network import CachedNodeData
from neural_network import ConstantNode
from neural_network import InputNode
from neural_network import L2ErrorNode
from neural_network import LinearNode
from neural_network import NeuralNetwork
from neural_network import Node
from neural_network import ReluNode
from neural_network import SigmoidNode
def single_linear_relu(input_nodes, initial_weights=None):
return ReluNode(LinearNode(input_nodes, initial_weights=initial_weights))
def single_linear_relu_network(node_count, initial_weights):
input_nodes = InputNode.make_input_nodes(node_count)
relu_node = single_linear_relu(
input_nodes, initial_weights=initial_weights)
error_node = L2ErrorNode(relu_node)
return NeuralNetwork(relu_node, input_nodes, error_node=error_node)
def test_node_missing_output():
node = Node()
with pytest.raises(Exception):
node.output
def test_cache_repr():
cache = CachedNodeData()
cache.output = 1
cache.local_gradient = 2
cache.global_gradient = 3
cache.local_parameter_gradient = 4
cache.global_parameter_gradient = 5
expect = (
"CachedNodeData(output=1, "
"local_gradient=2, "
"global_gradient=3, "
"local_parameter_gradient=4, "
"global_parameter_gradient=5)"
)
assert_that(repr(cache)).is_equal_to(expect)
cache = eval(repr(cache))
assert_that(repr(cache)).is_equal_to(expect)
def test_linear_node_bad_initialization():
input_nodes = InputNode.make_input_nodes(3)
inputs = [1, 2, 3]
initial_weights = [4, 3, 2, 1, 1]
with pytest.raises(Exception):
linear_node = LinearNode(
input_nodes, initial_weights=initial_weights)
def test_sigmoid_node_empty_parameters():
node = SigmoidNode()
assert_that(node.compute_local_parameter_gradient()).is_empty()
assert_that(node.compute_global_parameter_gradient()).is_empty()
def test_pretty_print():
const = ConstantNode()
input_node = InputNode(0)
sigmoid = SigmoidNode(const)
sigmoid.evaluate([])
relu = ReluNode(input_node)
relu.evaluate([2])
assert_that(sigmoid.pretty_print()).is_equal_to(
"Sigmoid output=0.73\n Constant(1)\n")
assert_that(relu.pretty_print()).is_equal_to(
"Relu output=2.00\n InputNode(0) output = 2.00\n")
network = single_linear_relu_network(3, [-20, 3, 2, 1])
network.evaluate([1, 2, 3])
network.compute_error([1, 2, 3], 1)
assert_that(network.pretty_print()).is_equal_to(
"""Relu output=0.00
Linear weights=-20.00,3.00,2.00,1.00 gradient=0.00,0.00,0.00,0.00 output=-10.00
Constant(1)
InputNode(0) output = 1.00
InputNode(1) output = 2.00
InputNode(2) output = 3.00
""")
def test_input_output():
node = InputNode(0)
assert_that(node.compute_output([3])).is_equal_to(3)
assert_that(node.compute_output([-4])).is_equal_to(-4)
def test_relu_evaluate_negative():
input_node = InputNode(0)
relu = ReluNode(input_node)
assert_that(relu.evaluate([-2])).is_equal_to(0)
def test_relu_evaluate_positive():
input_node = InputNode(0)
relu = ReluNode(input_node)
assert_that(relu.evaluate([3])).is_equal_to(3)
def test_relu_local_gradient_positive():
input_node = InputNode(0)
relu = ReluNode(input_node)
relu.evaluate([3])
assert_that(relu.local_gradient_for_argument(input_node)).is_equal_to(1)
def test_relu_local_gradient_negative():
input_node = InputNode(0)
relu = ReluNode(input_node)
relu.evaluate([-3])
assert_that(relu.local_gradient_for_argument(input_node)).is_equal_to(0)
def test_relu_local_parameter_gradient_empty():
input_node = InputNode(0)
relu = ReluNode(input_node)
relu.evaluate([3])
assert_that(len(relu.local_parameter_gradient)).is_equal_to(0)
def test_linear_evaluate():
input_nodes = InputNode.make_input_nodes(3)
inputs = [1, 2, 3]
initial_weights = [4, 3, 2, 1]
linear_node = LinearNode(input_nodes, initial_weights=initial_weights)
assert_that(linear_node.evaluate(inputs)
).is_equal_to(4*1 + 3*1 + 2*2 + 3*1)
def test_linear_local_gradient():
input_nodes = InputNode.make_input_nodes(3)
initial_weights = [4, 3, 2, 1]
linear_node = LinearNode(input_nodes, initial_weights=initial_weights)
assert_that(linear_node.local_gradient).is_equal_to([4, 3, 2, 1])
def test_linear_local_parameter_gradient():
input_nodes = InputNode.make_input_nodes(3)
inputs = [1, 2, 3]
initial_weights = [4, 3, 2, 1]
linear_node = LinearNode(input_nodes, initial_weights=initial_weights)
linear_node.evaluate(inputs)
assert_that(linear_node.local_parameter_gradient).is_equal_to([1, 1, 2, 3])
def test_linear_with_relu_evaluate():
input_nodes = InputNode.make_input_nodes(3)
inputs = [1, 2, 3]
initial_weights = [-20, 3, 2, 1]
linear_node = LinearNode(input_nodes, initial_weights=initial_weights)
relu_node = ReluNode(linear_node)
assert_that(relu_node.evaluate(inputs)).is_equal_to(0)
assert_that(linear_node.output).is_equal_to(-10)
def test_neural_network_evaluate():
network = single_linear_relu_network(3, [-20, 3, 2, 1])
assert_that(network.evaluate([1, 2, 3])).is_equal_to(0)
def test_neural_network_error():
input_node = InputNode(0)
relu = ReluNode(input_node)
network = NeuralNetwork(relu, [input_node])
inputs = [-2]
label = 1
assert_that(network.evaluate(inputs)).is_equal_to(0)
assert_that(network.compute_error(inputs, label)).is_equal_to(1)
def test_neural_network_reset():
network = single_linear_relu_network(2, [3, 2, 1])
assert_that(network.evaluate([2, -2])).is_equal_to(5)
assert network.evaluate([6, -2]) != 5
def test_neural_network_errors_on_dataset():
network = single_linear_relu_network(2, [3, 2, 1])
dataset = [((2, -2), 5), ((6, -2), 5)]
assert_that(network.error_on_dataset(dataset)).is_close_to(0.5, 1e-9)
def test_neural_network_gradients():
input_nodes = InputNode.make_input_nodes(2)
initial_weights = [3, 2, 1]
linear_node = LinearNode(input_nodes, initial_weights=initial_weights)
relu_node = ReluNode(linear_node)
error_node = L2ErrorNode(relu_node)
network = NeuralNetwork(relu_node, input_nodes, error_node=error_node)
example = [2, -2]
label = 1
'''
l(w, x): linear node
r(z): relu node
f(w, x) = r(l(w, x))
E(w, x, y): (r(l(w, x)) - y) ^ 2
'''
# f(w, x) = 5
# E(w, x, y) = 16
assert_that(network.evaluate(example)).is_equal_to(5)
assert relu_node.output > 0
assert_that(network.compute_error(example, label)).is_equal_to(16)
# ∂E/∂E = 1, ∂E/∂f = 8
assert_that(error_node.global_gradient).is_equal_to(1)
assert_that(error_node.local_gradient).is_equal_to([8])
# ∂E/∂z = 8, ∂r/∂z = 1
assert_that(relu_node.global_gradient).is_equal_to(8)
assert_that(relu_node.local_gradient).is_equal_to([1])
assert_that(relu_node.global_parameter_gradient).is_equal_to([])
assert_that(relu_node.local_parameter_gradient).is_equal_to([])
# ∂E/∂l = 8, ∂l/∂x_i = [3, 2, 1]
assert_that(linear_node.global_gradient).is_equal_to(8)
assert_that(linear_node.local_gradient).is_equal_to([3, 2, 1])
# ∂l/∂w_i = [1, 2, -2], ∂E/∂w_i = [8, 16, -16]
assert_that(linear_node.local_parameter_gradient).is_equal_to([1, 2, -2])
assert_that(linear_node.global_parameter_gradient).is_equal_to(
[8, 16, -16])
def test_neural_network_backpropagation_step():
input_nodes = InputNode.make_input_nodes(2)
initial_weights = [3, 2, 1]
linear_node = LinearNode(input_nodes, initial_weights=initial_weights)
relu_node = ReluNode(linear_node)
error_node = L2ErrorNode(relu_node)
network = NeuralNetwork(relu_node, input_nodes, error_node=error_node)
example = [2, -2]
label = 1
step_size = 0.5
network.backpropagation_step(example, label, step_size=step_size)
new_weights = [-1.0, -6.0, 9.0]
# ∂E/∂w_i = [8, 16, -16], delta is [-4, -8, 8]
assert_that(linear_node.weights).is_equal_to(new_weights)
def test_neural_network_gradients2():
input_nodes = InputNode.make_input_nodes(1)
# bias: 0
# weights: [2]
initial_weights_f = [0, 2]
linear_node_f = LinearNode(input_nodes, initial_weights=initial_weights_f)
# bias: 0
# weights: [3]
initial_weights_h = [0, 3]
linear_node_h = LinearNode([linear_node_f], initial_weights=initial_weights_h)
error_node = L2ErrorNode(linear_node_h)
network = NeuralNetwork(linear_node_h, input_nodes, error_node=error_node)
example = [2]
label = 1
step_size = 0.5
'''
h(w_h, x): linear node
f(w_f, x): linear node
E(w_h, w_f, x, y): (h(w_h, f(w_h, x)) - y) ^ 2
'''
network.backpropagation_step(example, label, step_size=step_size)
# h(w, x) = 12
# E(w_h, w_f, x, y) = 121
# assert_that(network.evaluate(example)).is_equal_to(12)
# assert_that(network.compute_error(example, label)).is_equal_to(121)
# ∂E/∂E = 1, ∂E/∂h = 22
assert_that(error_node.global_gradient).is_equal_to(1)
assert_that(error_node.local_gradient).is_equal_to([22])
# w_h = -41
assert_that(linear_node_h.parameters[1]).is_equal_to(-41)
# ∂E/∂h = 22
assert_that(linear_node_h.global_gradient).is_equal_to(22)
# ∂h/∂w_h = 4
assert_that(linear_node_h.local_parameter_gradient[1]).is_equal_to(4)
# ∂E/∂w_h = 88
assert_that(linear_node_h.global_parameter_gradient[1]).is_equal_to(88)
# expected: ∂h/∂f = 3
# reality: ∂h/∂f = -41
assert_that(linear_node_h.local_gradient[1]).is_equal_to(3)
# expected: w_f = -64
# reality: w_f = 904
assert_that(linear_node_f.parameters[1]).is_equal_to(-64)
# expected: ∂E/∂f = 66
# reality: ∂E/∂f = -902
assert_that(linear_node_f.global_gradient).is_equal_to(66)
# ∂f/∂w_f = 2
assert_that(linear_node_f.local_parameter_gradient[1]).is_equal_to(2)
# expected: ∂E/∂w_f = 132
# reality: ∂E/∂w_f = -1804
assert_that(linear_node_f.global_parameter_gradient[1]).is_equal_to(132)
# expected: ∂f/∂x = 2
# reality: ∂f/∂x = 904
assert_that(linear_node_f.local_gradient[1]).is_equal_to(2)