-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nn.py
215 lines (167 loc) · 8.11 KB
/
test_nn.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
import math
import numpy as np
import pytest
import nn
@pytest.fixture(autouse=True)
def set_seed():
np.random.seed(42)
@pytest.mark.timeout(2)
def test_predict():
input_matrix = np.array([[13, -16],
[-14, 17],
[15, -18]])
target_binary_predictions = np.array([[0, 1],
[1, 0],
[0, 1]])
Omega_0 = np.array([[.1, -.3, .5],
[-.2, .4, -.6]])
Omega_1 = np.array([[-.7, .10],
[.8, -.11],
[-.9, .12]])
# h<layer_index>_<activation_node_index><input_instance_index>
[[h1_11, h1_12],
[h1_21, h1_22]] = h1 = [
[s(13 * .1 + -14 * -.3 + 15 * .5), s(-16 * .1 + 17 * -.3 - 18 * .5)],
[s(13 * -.2 + -14 * .4 + 15 * -.6), s(-16 * -.2 + 17 * .4 + -18 * -.6)]]
expected_raw_output = np.array([
[s(h1_11 * -.7 + h1_21 * .10), s(h1_12 * -.7 + h1_22 * .10)],
[s(h1_11 * .8 + h1_21 * -.11), s(h1_12 * .8 + h1_22 * -.11)],
[s(h1_11 * -.9 + h1_21 * .12), s(h1_12 * -.9 + h1_22 * .12)]])
net = nn.SimpleNetwork(Omega_0, Omega_1)
model_raw_predictions = net.predict(input_matrix)
np.testing.assert_allclose(model_raw_predictions, expected_raw_output)
model_binary_predictions = net.predict_zero_one(input_matrix)
np.testing.assert_array_equal(model_binary_predictions,
target_binary_predictions)
@pytest.mark.timeout(2)
def test_gradients():
input_matrix = np.array([[0, 1, 0, 1],
[0, 0, 1, 1]])
target_output_matrix = np.array([[1, 0, 1, 0],
[1, 1, 0, 0]])
weights_0 = np.array([[0.1, -0.5],
[0.3, -0.3],
[0.5, -0.1]])
weights_1 = np.array([[0.2, 0.4, 0.6],
[-0.2, -0.4, -0.6]])
# f0 : pre-activation at layer 0
# f<layer_index>_<pre-activation_node_index><input_instance_index>
[[f0_11, f0_12, f0_13, f0_14],
[f0_21, f0_22, f0_23, f0_24],
[f0_31, f0_32, f0_33, f0_34]] = f0 = [
[.1 * 0 + -.5 * 0, .1 * 1 + -.5 * 0, .1 * 0 + -.5 * 1, .1 * 1 + -.5 * 1],
[.3 * 0 + -.3 * 0, .3 * 1 + -.3 * 0, .1 * 0 + -.3 * 1, .3 * 1 + -.3 * 1],
[.5 * 0 + -.1 * 0, .5 * 1 + -.1 * 0, .5 * 0 + -.1 * 1, .5 * 1 + -.1 * 1]]
# h1 : activation at layer 1
# h<layer_index>_<activation_node_index><input_instance_index>
[[h1_11, h1_12, h1_13, h1_14],
[h1_21, h1_22, h1_23, h1_24],
[h1_31, h1_32, h1_33, h1_34]] = [[s(z) for z in row] for row in f0]
# f1 : pre-activation at layer 1
# f<layer_index>_<pre-activation_node_index><input_instance_index>
[[f1_11, f1_12, f1_13, f1_14],
[f1_21, f1_22, f1_23, f1_24]] = f1 = [
[h1_11 * .2 + h1_21 * .4 + h1_31 * .6,
h1_12 * .2 + h1_22 * .4 + h1_32 * .6,
h1_13 * .2 + h1_23 * .4 + h1_33 * .6,
h1_14 * .2 + h1_24 * .4 + h1_34 * .6],
[h1_11 * -.2 + h1_21 * -.4 + h1_31 * -.6,
h1_12 * -.2 + h1_22 * -.4 + h1_32 * -.6,
h1_13 * -.2 + h1_23 * -.4 + h1_33 * -.6,
h1_14 * -.2 + h1_24 * -.4 + h1_34 * -.6]]
# mo : model output
# mo_<model_output_layer_node_index><input_instance_index>
[[mo_11, mo_12, mo_13, mo_14],
[mo_21, mo_22, mo_23, mo_24]] = [[s(x) for x in row] for row in f1]
# dlf1 = derivative of loss w.r.t. f1 (pre-activation of model output)
# dlf<layer_index>_<pre-activation_node_index><input_instance_index>
[[dlf1_11, dlf1_12, dlf1_13, dlf1_14],
[dlf1_21, dlf1_22, dlf1_23, dlf1_24]] = [
[2 * sg(f1_11) * (mo_11 - 1),
2 * sg(f1_12) * (mo_12 - 0),
2 * sg(f1_13) * (mo_13 - 1),
2 * sg(f1_14) * (mo_14 - 0)],
[2 * sg(f1_21) * (mo_21 - 1),
2 * sg(f1_22) * (mo_22 - 1),
2 * sg(f1_23) * (mo_23 - 0),
2 * sg(f1_24) * (mo_24 - 0)]]
# grad_l1_arr = the gradient at layer 1
# Gradient matrix is same shape as weight (Omega) matrix at layer 1
grad_l1_arr = np.array(
[[(dlf1_11 * h1_11 + dlf1_12 * h1_12 + dlf1_13 * h1_13 + dlf1_14 * h1_14) / 4,
(dlf1_11 * h1_21 + dlf1_12 * h1_22 + dlf1_13 * h1_23 + dlf1_14 * h1_24) / 4,
(dlf1_11 * h1_31 + dlf1_12 * h1_32 + dlf1_13 * h1_33 + dlf1_14 * h1_34) / 4],
[(dlf1_21 * h1_11 + dlf1_22 * h1_12 + dlf1_23 * h1_13 + dlf1_24 * h1_14) / 4,
(dlf1_21 * h1_21 + dlf1_22 * h1_22 + dlf1_23 * h1_23 + dlf1_24 * h1_24) / 4,
(dlf1_21 * h1_31 + dlf1_22 * h1_32 + dlf1_23 * h1_33 + dlf1_24 * h1_34) / 4]])
# dlf0 = derivative of loss w.r.t. f0 (pre-activation of hidden layer 1)
# dlf<layer_index>_<activation_node_index><input_instance_index>
[[dlf0_11, dlf0_12, dlf0_13, dlf0_14],
[dlf0_21, dlf0_22, dlf0_23, dlf0_24],
[dlf0_31, dlf0_32, dlf0_33, dlf0_34]] = [
[sg(f0_11) * (.2 * dlf1_11 + -.2 * dlf1_21),
sg(f0_12) * (.2 * dlf1_12 + -.2 * dlf1_22),
sg(f0_13) * (.2 * dlf1_13 + -.2 * dlf1_23),
sg(f0_14) * (.2 * dlf1_14 + -.2 * dlf1_24)],
[sg(f0_21) * (.4 * dlf1_11 + -.4 * dlf1_21),
sg(f0_22) * (.4 * dlf1_12 + -.4 * dlf1_22),
sg(f0_23) * (.4 * dlf1_13 + -.4 * dlf1_23),
sg(f0_24) * (.4 * dlf1_14 + -.4 * dlf1_24)],
[sg(f0_31) * (.6 * dlf1_11 + -.6 * dlf1_21),
sg(f0_32) * (.6 * dlf1_12 + -.6 * dlf1_22),
sg(f0_33) * (.6 * dlf1_13 + -.6 * dlf1_23),
sg(f0_34) * (.6 * dlf1_14 + -.6 * dlf1_24)]]
# grad_l1_arr = the gradient at layer 0
# Gradient matrix is same shape as weight (Omega) matrix at layer 0
grad_l0_arr = np.array(
[[(0 * dlf0_11 + 1 * dlf0_12 + 0 * dlf0_13 + 1 * dlf0_14) / 4,
(0 * dlf0_11 + 0 * dlf0_12 + 1 * dlf0_13 + 1 * dlf0_14) / 4],
[(0 * dlf0_21 + 1 * dlf0_22 + 0 * dlf0_23 + 1 * dlf0_24) / 4,
(0 * dlf0_21 + 0 * dlf0_22 + 1 * dlf0_23 + 1 * dlf0_24) / 4],
[(0 * dlf0_31 + 1 * dlf0_32 + 0 * dlf0_33 + 1 * dlf0_34) / 4,
(0 * dlf0_31 + 0 * dlf0_32 + 1 * dlf0_33 + 1 * dlf0_34) / 4]])
net = nn.SimpleNetwork(weights_0, weights_1)
[input_to_hidden_gradient,
hidden_to_output_gradient] = net.gradients(input_matrix, target_output_matrix)
np.testing.assert_allclose(hidden_to_output_gradient, grad_l1_arr)
np.testing.assert_allclose(input_to_hidden_gradient, grad_l0_arr)
@pytest.mark.timeout(2)
def test_train_greater_than_half():
inputs = np.random.uniform(size=(1, 100))
outputs = (inputs > 0.5).astype(int)
test_inputs = np.array([[0., 0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.]])
test_outputs = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
net = nn.SimpleNetwork.random(1, 5, 5, 1)
assert len(net.gradients(inputs, outputs)) == 3
net.train(inputs, outputs, iterations=1000, learning_rate=1)
assert np.count_nonzero(net.predict_zero_one(test_inputs) == test_outputs) >= 9
@pytest.mark.timeout(2)
def test_train_learning_rate():
inputs = np.array([[0, 0, 1, 1],
[0, 1, 0, 1],
[1, 1, 1, 1]])
outputs = np.array([[0, 1, 1, 1]])
net = nn.SimpleNetwork.random(3, 3, 1)
net.train(inputs, outputs, iterations=400, learning_rate=0.01)
assert np.any(net.predict_zero_one(inputs) != outputs)
net = nn.SimpleNetwork.random(3, 3, 1)
net.train(inputs, outputs, iterations=400, learning_rate=1)
assert np.all(net.predict_zero_one(inputs) == outputs)
@pytest.mark.timeout(2)
def test_train_xor():
inputs = np.array([[0, 0, 1, 1],
[0, 1, 0, 1],
[1, 1, 1, 1]])
outputs = np.array([[0, 1, 1, 0]])
all_weights = [np.array([[0.0346, 0.8939, 0.5309],
[-0.4352, -0.5579, 0.3724],
[-0.6657, -0.2151, 0.2361]]),
np.array([[-0.2157, -1.2187, 0.9407]])]
net = nn.SimpleNetwork(*all_weights)
assert len(net.gradients(inputs, outputs)) == 2
net.train(inputs, outputs, iterations=1000, learning_rate=0.5)
assert np.all(net.predict_zero_one(inputs) == outputs)
def s(x):
return 1 / (1 + math.exp(-x))
def sg(x):
return s(x) * (1 - s(x))