-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.py
241 lines (158 loc) · 9.48 KB
/
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
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
import numpy
# TODO: Add custom activation function. Providing activation function together with gradient is enough.
# TODO: Add custom network layers.
class neural_network:
# activation function
def sigmoid(self, x):
return 1.0 / (1.0 + numpy.exp(numpy.negative(x)))
# gradient of sigmoid
def sigmoid_gradient(self, x):
return self.sigmoid(x) * (1.0 - self.sigmoid(x))
# build network using sizes of layers
def __init__(self, layers):
self.network_layers_size = layers
self.network_layers_count = len(self.network_layers_size)
# create network
self.network_weights = []
self.network_weights_bias = []
self.network_weights_delta = []
self.network_weights_bias_delta = []
self.network_weights_update = []
self.network_weights_bias_update = []
self.network_layers = []
self.network_layers_activation = []
self.network_layers_error = []
self.network_input = None
# create layers
for i in range(self.network_layers_count - 1):
self.network_layers.append(None)
self.network_layers_activation.append(None)
self.network_layers_error.append(None)
# create weights
for i in range(self.network_layers_count - 1):
network_weights_shape = (self.network_layers_size[i], self.network_layers_size[i + 1])
self.network_weights.append(numpy.matrix(numpy.random.uniform(-2.0 / numpy.sqrt(self.network_layers_size[i]), 2.0 / numpy.sqrt(self.network_layers_size[i]), size = network_weights_shape)))
self.network_weights_delta.append(numpy.matrix(numpy.zeros(network_weights_shape)))
self.network_weights_update.append(numpy.matrix(numpy.zeros(network_weights_shape)))
# create bias
for i in range(self.network_layers_count - 1):
self.network_weights_bias.append(numpy.matrix(numpy.random.uniform(-2.0 / numpy.sqrt(self.network_layers_size[i]), 2.0 / numpy.sqrt(self.network_layers_size[i]), size = self.network_layers_size[i+1])))
self.network_weights_bias_delta.append(numpy.matrix(numpy.zeros(self.network_layers_size[i+1])))
self.network_weights_bias_update.append(numpy.matrix(numpy.zeros(self.network_layers_size[i+1])))
# calculate output values
def propagate_forward(self, input):
self.network_input = input
self.network_layers_activation[0] = input * self.network_weights[0]
self.network_layers_activation[0] += self.network_weights_bias[0]
self.network_layers[0] = self.sigmoid(self.network_layers_activation[0])
for i in range(1, self.network_layers_count - 1):
self.network_layers_activation[i] = self.network_layers[i - 1] * self.network_weights[i]
self.network_layers_activation[i] += self.network_weights_bias[i]
self.network_layers[i] = self.sigmoid(self.network_layers_activation[i])
return self.network_layers[-1] # last element
def _propagate_backward(self, error):
# compute gradient explicitly
for i in range(len(self.network_layers)-2, -1, -1):
self.network_layers_error[i] = numpy.zeros(self.network_layers[i].shape)
for j in range(self.network_layers[i].shape[1]):
for k in range(self.network_layers[i+1].shape[1]):
self.network_layers_error[i][(0, j)] += self.network_weights[i+1][j, k] * self.network_layers_error[i+1][(0, k)]
self.network_layers_error[i] *= self.sigmoid_gradient(numpy.array(self.network_layers_activation[i]))
def propagate_backward(self, error):
# compute gradient using matrix multiplication
self.network_layers_error[-1] = error * self.sigmoid_gradient(numpy.array(self.network_layers_activation[-1]))
for i in range(self.network_layers_count - 3, -1, -1):
self.network_layers_error[i] = self.network_layers_error[i + 1] * numpy.transpose(self.network_weights[i + 1])
self.network_layers_error[i] = numpy.asarray(self.network_layers_error[i]) * self.sigmoid_gradient(numpy.array(self.network_layers_activation[i]))
def update_delta(self):
self.network_weights_delta[0] += numpy.outer(self.network_input, self.network_layers_error[0])
self.network_weights_bias_delta[0] += self.network_layers_error[0]
for i in range(1, self.network_layers_count - 1):
self.network_weights_delta[i] += numpy.outer(self.network_layers[i-1], self.network_layers_error[i])
self.network_weights_bias_delta[i] += self.network_layers_error[i]
def update_weights(self, learning_rate, momentum = 1.0):
for i in range(0, self.network_layers_count - 1):
self.network_weights_update[i] = learning_rate * -self.network_weights_delta[i] + momentum * self.network_weights_update[i]
self.network_weights_bias_update[i] = learning_rate * -self.network_weights_bias_delta[i] + momentum * self.network_weights_bias_update[i]
self.network_weights[i] += self.network_weights_update[i]
self.network_weights_bias[i] += self.network_weights_bias_update[i]
def reset_weights_delta(self):
for i in range(self.network_layers_count - 1):
network_weights_shape = (self.network_layers_size[i], self.network_layers_size[i + 1])
self.network_weights_delta[i] = numpy.matrix(numpy.zeros(network_weights_shape))
for i in range(self.network_layers_count - 1):
self.network_weights_bias_delta[i] = numpy.matrix(numpy.zeros(self.network_layers_size[i+1]))
def get_weights_delta(self):
delta_weights = []
delta_weights_bias = []
for i in range(0, self.network_layers_count - 1):
delta_weights.append(numpy.sum(numpy.abs(self.network_weights_delta[i])))
delta_weights_bias.append(numpy.sum(numpy.abs(self.network_weights_bias_delta[i])))
return (delta_weights, delta_weights_bias)
class neural_network_test:
def run_basic_test(self):
input_size = 4
nn = neural_network([input_size, 2])
input = numpy.matrix(numpy.random.normal(size=input_size))
output = nn.propagate_forward(input)
#TODO: check formulae manually
def run_gradient_test(self):
print('Starting gradient test')
input_height = 16
input_width = 32
input_size = input_width * input_height
output_classes = 10
network_layers = [input_size, 64, 32, output_classes]
epsilon = 0.0005
h = 0.0005
print('Neural network layers: {}'.format(network_layers))
print('Gradient epsilon: {}'.format(epsilon))
nn = neural_network(network_layers)
input = numpy.matrix(numpy.random.normal(size=input_size))
nn.propagate_forward(input)
error = numpy.ones((1, output_classes))
nn.propagate_backward(error)
total = 0
passed = 0
nn.update_delta()
for i in range(nn.network_layers_count - 1):
for j in range(nn.network_weights[i].shape[0]):
for k in range(nn.network_weights[i].shape[1]):
w = nn.network_weights[i][(j, k)]
nn.network_weights[i][(j, k)] = w - h
output_a = nn.propagate_forward(input)
nn.network_weights[i][(j, k)] = w + h
output_b = nn.propagate_forward(input)
gradient_weight = (output_b - output_a) / (2 * h)
nn.network_weights[i][(j, k)] = w
delta = abs(nn.network_weights_delta[i][(j, k)] - numpy.sum(gradient_weight))
if delta > epsilon:
print('Test case for {}-th weight [{},{}] failed: Calculated = {} Estimated = {} Delta = {}'.format(i, j, k, nn.network_weights_delta[i][(j, k)], numpy.sum(gradient_weight), delta))
else:
passed += 1
print('Test case for {}-th weight [{},{}] passed'.format(i, j, k))
total += 1
# compute gradient with respect to i-th input
input_gradient = numpy.matrix(numpy.zeros(input.shape))
for i in range(input_gradient.shape[1]):
# compute gradient df = (f(x+h) - f(x-h)) / h
x = input[(0, i)]
input[0, i] = x - h
output_a = nn.propagate_forward(input)
input[0, i] = x + h
output_b = nn.propagate_forward(input)
gradient = (output_b - output_a) / (2 * h)
input[0, i] = x
for j in range(nn.network_layers[0].shape[1]):
input_gradient[0, i] += nn.network_weights[0][i, j] * nn.network_layers_error[0][0, j]
delta = abs(input_gradient[0, i] - numpy.sum(gradient))
if delta > epsilon:
print('Test case for {}-th input failed: Calculated = {} Estimated = {} Delta = {}'.format(i, input_gradient[0, j], numpy.sum(gradient), delta))
else:
passed += 1
print('Test case for {}-th input passed'.format(i))
total += 1
print('Passed {} tests from {}'.format(passed, total))
print
test = neural_network.neural_network_test()
test.run_gradient_test()