-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
428 lines (348 loc) · 17.5 KB
/
main.py
File metadata and controls
428 lines (348 loc) · 17.5 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import numpy as np
import nnfs
from nnfs.datasets import spiral_data
class Layer_Dense:
def __init__(self, n_inputs, n_neurons, weight_regularizer_l1=0, weight_regularizer_l2=0, bias_regularizer_l1=0, bias_regularizer_l2=0):
"""Note that we’re initializing weights
to be (inputs, neurons), rather than ( neurons, inputs) . We’re doing this ahead instead of
transposing every time we perform a forward pass, as explained in the previous chapter."""
self.weights = 0.01 * np.random.randn(n_inputs, n_neurons)
self.biases = np.zeros((1, n_neurons))
self.weight_regularizer_l1=weight_regularizer_l1
self.weight_regularizer_l2=weight_regularizer_l2
self.bias_regularizer_l1=bias_regularizer_l1
self.bias_regularizer_l2=bias_regularizer_l2
def forward(self, inputs):
self.output = np.dot(inputs, self.weights) + self.biases
self.inputs = inputs
def backward(self, dvalues):
self.dweights = np.dot(self.inputs.T, dvalues)
self.dbiases = np.sum(dvalues, axis=0, keepdims=True)
#gradient on values
#regularization
#l1 weights
if self.weight_regularizer_l1 > 0:
dL1 = np.ones_like(self.weights)
dL1[self.weights < 0] = -1
self.dweights += self.weight_regularizer_l1 * dL1
#l2 weights
if self.weight_regularizer_l2 > 0:
self.dweights += 2 * self.weight_regularizer_l2 * self.weights
#l1 biases
if self.bias_regularizer_l1 > 0:
dL1 = np.ones_like(self.biases)
dL1[self.biases < 0] = -1
self.dbiases += self.bias_regularizer_l1 * dL1
#l2 biases
if self.bias_regularizer_l2 > 0:
self.dbiases += 2 * self.bias_regularizer_l2 * self.biases
self.dinputs=np.dot(dvalues, self.weights.T)
class Layer_Dropout:
def __init__(self, rate):
#storage rate
self.rate = 1 - rate
def forward(self, inputs):
self.inputs = inputs
self.binary_mask = np.random.binomial(1, self.rate, size=inputs.shape) / self.rate
self.output = inputs * self.binary_mask
def backward(self, dvalues):
self.dinputs = dvalues * self.binary_mask
class Activation_ReLU:
def forward(self, inputs):
self.inputs = inputs #saving input values
self.output = np.maximum(0, inputs)
def backward(self, dvalues):
self.dinputs = dvalues.copy()
self.dinputs[self.inputs <= 0] = 0
class Activation_Softmax:
def forward(self, inputs):
self.inputs = inputs
#subtract max of activations to prevent exploding values
#thanks to normalisation, we can subtract any value from all of the inputs and the output will not be changed
exp_values = np.exp(inputs - np.max(inputs, axis = 1, keepdims = True))
#get normalised prob
probabilities = exp_values / np.sum(exp_values, axis = 1, keepdims = True)
self.output = probabilities
def backward(self, dvalues):
self.dinputs = np.empty_like(dvalues) #uninitialised array
#iterate over pairs out outputs and gradients
for index, (single_output, single_dvalues) in enumerate(zip(self.output, dvalues)): #index from enumerate
single_output = single_output.reshape(-1,1) #row to column vector
jacobian_matrix = np.diagflat(single_output) - np.dot(single_output, single_output.T)
self.dinputs[index] = np.dot(jacobian_matrix, single_dvalues)
class Loss:
def regularization_loss(self, layer):
regularization_loss = 0
#l1 weights, when factor is greater than 0
if layer.weight_regularizer_l1 > 0:
regularization_loss += layer.weight_regularizer_l1 * np.sum(np.abs(layer.weights))
#l2 weights
if layer.weight_regularizer_l2 > 0:
regularization_loss += layer.weight_regularizer_l2 * np.sum(layer.weights * layer.weights)
#l1 biases
if layer.bias_regularizer_l1 > 0:
regularization_loss += layer.bias_regularizer_l1 * np.sum(np.abs(layer.biases))
#l2 biases
if layer.bias_regularizer_l2 > 0:
regularization_loss += layer.bias_regularizer_l2 * np.sum(layer.biases * layer.biases)
return regularization_loss
def calculate(self, output, y):
sample_losses = self.forward(output, y)
data_loss = np.mean(sample_losses)
return data_loss
class Loss_CategoricalCrossentropy(Loss):
def forward(self, y_pred, y_true):
#no of samples in batch
samples = len(y_pred)
y_pred_clipped = np.clip(y_pred, 1e-7, 1 - 1e-7)
#single dimensional - sparse (just class index)
if len(y_true.shape) == 1:
#getting confidences of indexes of ground truth
correct_confidences = y_pred_clipped[range(samples), y_true]
#list of list - one hot
elif len(y_true.shape) == 2:
correct_confidences = np.sum(y_pred_clipped * y_true, axis = 1)
negative_log_likelihoods = -np.log(correct_confidences)
return negative_log_likelihoods
def backward(self, dvalues, y_true):
samples = len(dvalues)
labels = len(dvalues[0]) #number of labels in each sample - getting count from first
#if sparse, then convert to one hot
if len(y_true.shape) == 1:
y_true = np.eye(labels)[y_true]
#calculate gradients
self.dinputs = -y_true / dvalues
self.dinputs = self.dinputs / samples
class Loss_BinaryCrossentropy(Loss):
def forward(self, y_pred, y_true):
y_pred_clipped = np.clip(y_pred, 1e-7, 1 - 1e-7)
sample_losses = -(y_true * np.log(y_pred_clipped) + (1 - y_true) * np.log(1 - y_pred_clipped))
sample_losses = np.mean(sample_losses, axis = -1)
return sample_losses
def backward(self, dvalues, y_true):
samples = len(dvalues)
outputs = len(dvalues[0])
clipped_dvalues = np.clip(dvalues, 1e-7, 1 - 1e-7)
self.dinputs = -(y_true / clipped_dvalues - (1 - y_true) / (1 - clipped_dvalues)) / outputs
self.dinputs = self.dinputs / samples
#more efficient backprop implementation that does both at once
class Activation_Softmax_Loss_CategoricalCrossentropy():
def __init__(self):
self.activation = Activation_Softmax()
self.loss = Loss_CategoricalCrossentropy()
def forward(self, inputs, y_true):
self.activation.forward(inputs)
self.output = self.activation.output
return self.loss.calculate(self.output, y_true)
#more efficient backwards pass
def backward(self, dvalues, y_true):
samples = len(dvalues)
#turns one hot coded into discrete values
if len(y_true.shape) == 2:
y_true = np.argmax(y_true, axis = 1)
self.dinputs = dvalues.copy()
#calculating gradients
self.dinputs[range(samples), y_true] -= 1
#normalising gradients
self.dinputs = self.dinputs / samples
class Activation_Sigmoid:
def forward(self, inputs):
self.inputs = inputs
self.output = 1 / (1 + np.exp(-inputs))
def backward(self, dvalues):
self.dinputs = dvalues * (1 - self.output) * self.output
class Optimizer_SGD():
def __init__(self, learning_rate=1.0, decay=0., momentum = 0.):
self.learning_rate = learning_rate
self.current_learning_rate = learning_rate
self.decay = decay
self.iterations = 0
self.momentum = momentum
def pre_update_params(self):
if self.decay:
self.current_learning_rate = self.learning_rate * (1. / (1. + self.decay * self.iterations))
def update_params(self, layer):
if self.momentum:
if not hasattr(layer, "weight_momentums"):
layer.weight_momentums = np.zeros_like(layer.weights)
layer.bias_momentums = np.zeros_like(layer.biases)
weight_updates = self.momentum * layer.weight_momentums - self.current_learning_rate * layer.dweights
layer.weight_momentums = weight_updates
bias_updates = self.momentum * layer.bias_momentums - self.current_learning_rate * layer.dbiases
layer.bias_momentums = bias_updates
else:
weight_updates = -self.current_learning_rate * layer.dweights
bias_updates = -self.current_learning_rate * layer.dbiases
layer.weights += weight_updates
layer.biases += bias_updates
def post_update_params(self):
self.iterations += 1
class Optimizer_Adagrad():
def __init__(self, learning_rate=1.0, decay=0., epsilon = 1e-7):
self.learning_rate = learning_rate
self.current_learning_rate = learning_rate
self.decay = decay
self.iterations = 0
self.epsilon = epsilon #so we can never divide by zero
def pre_update_params(self):
if self.decay:
self.current_learning_rate = self.learning_rate * (1. / (1. + self.decay * self.iterations))
def update_params(self, layer):
if not hasattr(layer, "weight_cache"):
layer.weight_cache = np.zeros_like(layer.weights)
layer.bias_cache = np.zeros_like(layer.biases)
#updating cache with squared current gradients
layer.weight_cache += layer.dweights**2
layer.bias_cache += layer.dbiases**2
layer.weights += -self.current_learning_rate * layer.dweights / (np.sqrt(layer.weight_cache) + self.epsilon)
layer.biases += -self.current_learning_rate * layer.dbiases / (np.sqrt(layer.bias_cache) + self.epsilon)
def post_update_params(self):
self.iterations += 1
class Optimizer_RMSprop():
def __init__(self, learning_rate=0.001, decay=0., epsilon=1e-7, rho=0.9):
self.learning_rate = learning_rate
self.current_learning_rate = learning_rate
self.decay = decay
self.iterations = 0
self.epsilon = epsilon #so we can never divide by zero
self.rho = rho
def pre_update_params(self):
if self.decay:
self.current_learning_rate = self.learning_rate * (1. / (1. + self.decay * self.iterations))
def update_params(self, layer):
if not hasattr(layer, "weight_cache"):
layer.weight_cache = np.zeros_like(layer.weights)
layer.bias_cache = np.zeros_like(layer.biases)
#updating cache with movin average of the cahce - so learning does not stall
layer.weight_cache = self.rho * layer.weight_cache + (1 - self.rho) * layer.dweights**2
layer.bias_cache = self.rho * layer.bias_cache + (1 - self.rho) * layer.dbiases**2
layer.weights += -self.current_learning_rate * layer.dweights / (np.sqrt(layer.weight_cache) + self.epsilon)
layer.biases += -self.current_learning_rate * layer.dbiases / (np.sqrt(layer.bias_cache) + self.epsilon)
def post_update_params(self):
self.iterations += 1
class Optimizer_Adam():
def __init__(self, learning_rate=0.001, decay=0., epsilon=1e-7, beta_1=0.9, beta_2=0.99):
self.learning_rate = learning_rate
self.current_learning_rate = learning_rate
self.decay = decay
self.iterations = 0
self.epsilon = epsilon #so we can never divide by zero
self.beta_1 = beta_1
self.beta_2 = beta_2
def pre_update_params(self):
if self.decay:
self.current_learning_rate = self.learning_rate * (1. / (1. + self.decay * self.iterations))
def update_params(self, layer):
if not hasattr(layer, "weight_cache"):
layer.weight_momentums = np.zeros_like(layer.weights)
layer.weight_cache = np.zeros_like(layer.weights)
layer.bias_momentums = np.zeros_like(layer.biases)
layer.bias_cache = np.zeros_like(layer.biases)
#updating momentum with current gradients
layer.weight_momentums = self.beta_1 * layer.weight_momentums + (1 - self.beta_1) * layer.dweights
layer.bias_momentums = self.beta_1 * layer.bias_momentums + (1 - self.beta_1) * layer.dbiases
#correting momentum - self.iteration is 0 at first pass, increment to start at 1
weight_momentums_corrected = layer.weight_momentums / (1 - self.beta_1 ** (self.iterations + 1))
bias_momentums_corrected = layer.bias_momentums / (1 - self.beta_1 ** (self.iterations + 1))
#updating cache with squared current gradients
layer.weight_cache = self.beta_2 * layer.weight_cache + (1 - self.beta_2) * layer.dweights**2
layer.bias_cache = self.beta_2 * layer.bias_cache + (1 - self.beta_2) * layer.dbiases**2
#get corrected cache
weight_cache_corrected = layer.weight_cache / (1 - self.beta_2 ** (self.iterations + 1))
bias_cache_corrected = layer.bias_cache / (1 - self.beta_2 ** (self.iterations + 1))
layer.weights += -self.current_learning_rate * weight_momentums_corrected / (np.sqrt(weight_cache_corrected) + self.epsilon)
layer.biases += -self.current_learning_rate * bias_momentums_corrected / (np.sqrt(bias_cache_corrected) + self.epsilon)
def post_update_params(self):
self.iterations += 1
if __name__ == "__main__":
nnfs.init()
X, y = spiral_data(samples=1000, classes=3)
"""#2 input features and 3 output values
dense1 = Layer_Dense(2,3)
activation1 = Activation_ReLU()
dense2 = Layer_Dense(3,3)
loss_activation = Activation_Softmax_Loss_CategoricalCrossentropy()
dense1.forward(X)
activation1.forward(dense1.output)
dense2.forward(activation1.output)
loss = loss_activation.forward(dense2.output, y)
print (loss_activation.output[: 5 ])
print ( 'loss:', loss)
predictions = np.argmax(loss_activation.output, axis = 1 )
if len (y.shape) == 2 :
y = np.argmax(y, axis = 1 )
accuracy = np.mean(predictions == y)
print(f"acc: {accuracy}")
loss_activation.backward(loss_activation.output, y)
dense2.backward(loss_activation.dinputs)
activation1.backward(dense2.dinputs)
dense1.backward(activation1.dinputs)
print(dense1.dweights)
print(dense1.dbiases)
print(dense2.dweights)
print(dense2.dbiases)"""
"""softmax_outputs = np.array([[ 0.7 , 0.1 , 0.2 ],
[ 0.1 , 0.5 , 0.4 ],
[ 0.02 , 0.9 , 0.08 ]])
class_targets = np.array([0, 1, 1])
softmax_loss = Activation_Softmax_Loss_CategoricalCrossentropy()
softmax_loss.backward(softmax_outputs, class_targets)
dvalues1 = softmax_loss.dinputs
activation = Activation_Softmax()
activation.output = softmax_outputs
loss = Loss_CategoricalCrossentropy()
loss.backward(softmax_outputs, class_targets)
activation.backward(loss.dinputs)
dvalues2 = activation.dinputs
print(dvalues1)
print(dvalues2)"""
dense1 = Layer_Dense(2, 512, weight_regularizer_l2=5e-4, bias_regularizer_l2=5e-4)
activation1 = Activation_ReLU()
dropout1 = Layer_Dropout(0.1) # want to keep 90% of neurons in each pass
dense2 = Layer_Dense(512, 3)
loss_activation = Activation_Softmax_Loss_CategoricalCrossentropy()
#optimizer = Optimizer_SGD(decay=1e-3, momentum=0.9) #max 93.3% acc
#optimizer = Optimizer_Adagrad(decay=1e-4) #max 86% acc
#optimizer = Optimizer_RMSprop(learning_rate=0.02, decay=1e-5, rho=0.999)
optimizer = Optimizer_Adam(learning_rate=0.05, decay=5e-5)
for epoch in range(10001):
dense1.forward(X)
activation1.forward(dense1.output)
dropout1.forward(activation1.output)
dense2.forward(dropout1.output)
data_loss = loss_activation.forward(dense2.output, y)
regularization_loss = loss_activation.loss.regularization_loss(dense1) + loss_activation.loss.regularization_loss(dense2)
loss = data_loss + regularization_loss
predictions = np.argmax(loss_activation.output, axis = 1 )
if len(y.shape) == 2 :
y = np.argmax(y, axis = 1 )
accuracy = np.mean(predictions == y)
if not epoch % 100:
print(f"epoch: {epoch}," +
f"acc: {accuracy:.3f}," +
f"loss: {loss:.3f} (" +
f"data_loss: {data_loss:.3f},"
f"reg_loss: {regularization_loss:.3f}),"
f'lr: {optimizer.current_learning_rate}')
loss_activation.backward(loss_activation.output, y)
dense2.backward(loss_activation.dinputs)
dropout1.backward(dense2.dinputs)
activation1.backward(dropout1.dinputs)
dense1.backward(activation1.dinputs)
optimizer.pre_update_params()
optimizer.update_params(dense1)
optimizer.update_params(dense2)
optimizer.post_update_params()
#validation of the model
#test ds
X_test, y_test = spiral_data(samples=100, classes=3)
dense1.forward(X_test)
activation1.forward(dense1.output)
dense2.forward(activation1.output)
loss = loss_activation.forward(dense2.output, y_test)
#calculate accuracies
predictions = np.argmax(loss_activation.output, axis = 1)
if len(y_test.shape) == 2:
y_test = np.argmax(y_test, axis=1)
accuracy = np.mean(predictions==y_test)
print(f"validation, acc: {accuracy:.3f}, loss: {loss:.3f}")