forked from seanstappas/neural-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_neural_network.py
161 lines (129 loc) · 7.44 KB
/
test_neural_network.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
import unittest
import numpy as np
from copy import deepcopy
from neural_network import NeuralNetwork
def get_approx_loss_gradient(nn, input_vectors, output_vectors, layer, row, col, epsilon=1e-6):
saved_weights = nn.get_weights()
weights = deepcopy(saved_weights)
weights[layer][row, col] += epsilon
nn.set_weights(weights)
loss1 = nn.get_loss(input_vectors, output_vectors)
weights = deepcopy(saved_weights)
weights[layer][row, col] -= epsilon
nn.set_weights(weights)
loss2 = nn.get_loss(input_vectors, output_vectors)
nn.set_weights(saved_weights)
return (loss1 - loss2) / (2 * epsilon)
class TestNeuralNetwork(unittest.TestCase):
def test_nn_with_single_layer_predicts_the_input_for_1D_array(self):
nn = NeuralNetwork((3,))
input_vector = np.array([[1, 1, 2]])
np.testing.assert_array_equal(nn.predict(input_vector), input_vector)
def test_nn_with_single_layer_predicts_the_input_for_2D_array(self):
nn = NeuralNetwork((3,))
input_vectors = np.array([[1, 1, 2], [2, 0, 1]])
np.testing.assert_array_equal(nn.predict(input_vectors), input_vectors)
def test_nn_with_single_layer_predicts_the_input_for_3D_array(self):
nn = NeuralNetwork((3,))
input_vectors = np.array([[[1, 1, 2]], [[2, 0, 1]]])
np.testing.assert_array_equal(nn.predict(input_vectors), input_vectors)
def test_nn_with_logistic_sigmoid_has_correct_loss_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic')
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss(nn, input_vector, output_vector)
def test_nn_with_logistic_sigmoid_has_correct_loss_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic')
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss(nn, input_vectors, output_vectors)
def test_nn_with_tanh_sigmoid_has_correct_loss_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh')
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss(nn, input_vector, output_vector)
def test_nn_with_tanh_sigmoid_has_correct_loss_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh')
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss(nn, input_vectors, output_vectors)
def test_nn_with_logistic_sigmoid_has_correct_loss_gradient_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic')
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss_gradient(nn, input_vector, output_vector)
def test_nn_with_logistic_sigmoid_has_correct_loss_gradient_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic')
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss_gradient(nn, input_vectors, output_vectors)
def test_nn_with_tanh_sigmoid_has_correct_loss_gradient_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh')
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss_gradient(nn, input_vector, output_vector)
def test_nn_with_tanh_sigmoid_has_correct_loss_gradient_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh')
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss_gradient(nn, input_vectors, output_vectors)
def test_nn_with_logistic_sigmoid_and_decay_has_correct_loss_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic', weight_decay=0.1)
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss(nn, input_vector, output_vector)
def test_nn_with_logistic_sigmoid_and_decay_has_correct_loss_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic', weight_decay=0.1)
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss(nn, input_vectors, output_vectors)
def test_nn_with_tanh_sigmoid_and_decay_has_correct_loss_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh', weight_decay=0.1)
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss(nn, input_vector, output_vector)
def test_nn_with_tanh_sigmoid_and_decay_has_correct_loss_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh', weight_decay=0.1)
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss(nn, input_vectors, output_vectors)
def test_nn_with_logistic_sigmoid_and_decay_has_correct_loss_gradient_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic', weight_decay=0.1)
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss_gradient(nn, input_vector, output_vector)
def test_nn_with_logistic_sigmoid_and_decay_has_correct_loss_gradient_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='logistic', weight_decay=0.1)
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss_gradient(nn, input_vectors, output_vectors)
def test_nn_with_tanh_sigmoid_and_decay_has_correct_loss_gradient_for_single_example(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh', weight_decay=0.1)
input_vector = np.array([[1, 1]])
output_vector = np.array([[2]])
self._test_loss_gradient(nn, input_vector, output_vector)
def test_nn_with_tanh_sigmoid_and_decay_has_correct_loss_gradient_for_multiple_examples(self):
nn = NeuralNetwork((2, 5, 1), sigmoid='tanh', weight_decay=0.1)
input_vectors = np.array([[[1, 1]], [[-1, 2]]])
output_vectors = np.array([[[2]], [[5]]])
self._test_loss_gradient(nn, input_vectors, output_vectors)
def test_nn_with_invalid_sigmoid_throws_error(self):
with self.assertRaises(ValueError):
nn = NeuralNetwork((2, 5, 1), sigmoid='invalid')
def _test_loss(self, nn, input_vectors, output_vectors):
prediction = nn.predict(input_vectors)
sum_of_weights_squared = 0.0
for weight in nn.get_weights():
sum_of_weights_squared += np.sum(weight ** 2)
self.assertAlmostEqual(nn.get_loss(input_vectors, output_vectors), 0.5 * np.sum(
(output_vectors - prediction) ** 2) / prediction.shape[0] + 0.5 * nn._weight_decay * sum_of_weights_squared)
def _test_loss_gradient(self, nn, input_vectors, output_vectors):
weights = nn.get_weights()
for layer in range(len(weights)):
for row in range(weights[layer].shape[0]):
for col in range(weights[layer].shape[1]):
gradient = nn.get_loss_gradient(input_vectors, output_vectors)
approx_gradient = get_approx_loss_gradient(nn, input_vectors, output_vectors, layer, row, col)
self.assertAlmostEqual(gradient[layer][row, col], approx_gradient)
if __name__ == '__main__':
unittest.main()