-
Notifications
You must be signed in to change notification settings - Fork 1
/
Builder.py
685 lines (564 loc) · 25.5 KB
/
Builder.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# Our Full Code Expanding
import numpy as np
import nnfs
import os
import cv2
import pickle
import copy
nnfs.init()
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):
# Initialize weights and biases
self.weights = 0.01 * np.random.randn(n_inputs, n_neurons) # 0.10 to scale, define as inp, neur so no transpose
self.biases = np.zeros((1, n_neurons))
# L1 and L2
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, training):
self.inputs = inputs
# Calculate output values from inputs, weights, and biases
self.output = np.dot(inputs, self.weights) + self.biases
def backward(self, dvalues):
# Gradient on parameters
self.dweights = np.dot(self.inputs.T, dvalues)
self.dbiases = np.sum(dvalues, axis=0, keepdims=True)
# Gradient on regularization
if self.weight_regularizer_l1 > 0:
dL1 = np.ones_like(self.weights)
dL1[self.weights < 0] = -1
self.dweights += self.weight_regularizer_l1 * dL1
if self.weight_regularizer_l2 > 0:
self.dweights += 2 * self.weight_regularizer_l2 * self.weights
if self.bias_regularizer_l1 > 0:
dL1 = np.ones_like(self.biases)
dL1[self.biases < 0] = -1
self.dbiases += self.bias_regularizer_l1 * dL1
if self.bias_regularizer_l2 > 0:
self.dbiases += 2 * self.bias_regularizer_l2 * self.biases
# Gradient on values
self.dinputs = np.dot(dvalues, self.weights.T)
def get_parameters(self):
return self.weights, self.biases
def set_parameters(self, weights, biases):
self.weights = weights
self.biases = biases
class Layer_Dropout:
def __init__(self, rate):
# Store rate
self.rate = 1 - rate
def forward(self, inputs, training):
self.inputs = inputs
# If not in training mode, return values
if not training:
self.output = inputs.copy()
return
# Create mask
self.binary_mask = np.random.binomial(1, self.rate, size=inputs.shape) / self.rate
# Apply mask to output values
self.output = inputs * self.binary_mask
def backward(self, dvalues):
# Gradient on values
self.dinputs = dvalues * self.binary_mask
class Layer_Input:
def forward(self, inputs, training):
self.output = inputs
class Activation_ReLU:
def forward(self, inputs, training):
self.inputs = inputs
self.output = np.maximum(0, inputs)
def backward(self, dvalues):
self.dinputs = dvalues.copy()
# Zero gradient where input is negative
self.dinputs[self.inputs <= 0] = 0
def predictions(self, outputs):
return outputs
class Activation_Softmax:
def forward(self, inputs, training):
# Get un-normalized probabilities
exp_values = np.exp(inputs - np.max(inputs, axis=1, keepdims=True))
# Normalize them
probabilities = exp_values / np.sum(exp_values, axis=1, keepdims=True)
self.output = probabilities
def backward(self, dvalues):
# Create uninitialized array
self.dinputs = np.empty_like(dvalues)
# Enumerate outputs and gradients
for index, (single_output, single_dvalues) in enumerate(zip(self.output, dvalues)):
# Flatten output array
single_output = single.output.reshape(-1, 1)
# Calculate Jacobian matrix of the output
jacobian_matrix = np.diagflat(single_output) - np.dot(single_output, single_output.T)
# Calculate sample-wise gradient, and add to array of sample gradients
self.dinputs[index] = np.dot(jacobian_matrix, single_dvalues)
def predictions(self, outputs):
return np.argmax(outputs, axis=1)
class Activation_Sigmoid:
def forward(self, inputs, training):
self.inputs = inputs
self.output = 1 / (1 + np.exp(-inputs))
def backward(self, dvalues):
self.dinputs = dvalues * (1 - self.output) * self.output
def predictions(self, outputs):
return (outputs > 0.5) * 1
class Activation_Linear:
def forward(self, inputs, training):
self.inputs = inputs
self.output = inputs
def backward(self, dvalues):
# derivative is 1 b/c 1 * dvalues = dvalues
self.dinputs = dvalues.copy()
def predictions(self, outputs):
return outputs
class Optimizer_SGD:
def __init__(self, learning_rate=1., 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., 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
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)
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
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)
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.999):
self.learning_rate = learning_rate
self.current_learning_rate = learning_rate
self.decay = decay
self.iterations = 0
self.epsilon = epsilon
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)
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
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))
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
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
class Loss:
# Calculate the data and regularization losses given model output and true values
def remember_trainable_layers(self, trainable_layers):
self.trainable_layers = trainable_layers
def calculate(self, output, y, *, include_regularization=False):
sample_losses = self.forward(output, y)
data_loss = np.mean(sample_losses)
self.accumulated_sum += np.sum(sample_losses)
self.accumulated_count += len(sample_losses)
if not include_regularization:
return data_loss
return data_loss, self.regularization_loss()
def regularization_loss(self):
regularization_loss = 0
for layer in self.trainable_layers:
if layer.weight_regularizer_l1 > 0:
regularization_loss += layer.weight_regularizer_l1 * np.sum(np.abs(layer.weights))
if layer.weight_regularizer_l2 > 0:
regularization_loss += layer.weight_regularizer_l2 * np.sum(layer.weights * layer.weights)
if layer.bias_regularizer_l1 > 0:
regularization_loss += layer.bias_regularizer_l1 * np.sum(np.abs(layer.biases))
if layer.bias_regularizer_l2 > 0:
regularization_loss += layer.bias_regularizer_l2 * np.sum(layer.biases * layer.biases)
return regularization_loss
def calculate_accumulated(self, *, include_regularization=False):
data_loss = self.accumulated_sum / self.accumulated_count
if not include_regularization:
return data_loss
return data_loss, self.regularization_loss()
def new_pass(self):
self.accumulated_sum = 0
self.accumulated_count = 0
class Loss_CategoricalCrossentropy(Loss):
def forward(self, y_pred, y_true):
samples = len(y_pred)
# Clip data to prevent division by 0, and clip both pos and neg to not drag mean
y_pred_clipped = np.clip(y_pred, 1e-7, 1 - 1e-7)
# Probabilities for target values - if categorical labels
if len(y_true.shape) == 1:
correct_confidences = y_pred_clipped[range(samples), y_true]
# If one-hot encoded labels
elif len(y_true.shape) == 2:
correct_confidences = np.sum(y_pred_clipped * y_true, axis=1)
# Losses
negative_log_likelihoods = -np.log(correct_confidences)
return negative_log_likelihoods
def backward(self, dvalues, y_true):
samples = len(dvalues)
labels = len(dvalues[0])
# If labels are sparse, one-hot encode
if len(y_true.shape) == 1:
y_true = np.eye(labels)[y_true]
# Calculate gradient
self.dinputs = -y_true / dvalues
# Normalize gradient
self.dinputs = self.dinputs / samples
class Loss_BinaryCrossentropy(Loss):
def forward(self, y_pred, y_true):
# Clip data to prevent division by 0
y_pred_clipped = np.clip(y_pred, 1e-7, 1 - 1e-7)
# Calculate sample wise loss
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])
# Clip to prevent division by 0
clipped_dvalues = np.clip(dvalues, 1e-7, 1 - 1e-7)
# Calculate gradient
self.dinputs = -(y_true / clipped_dvalues - (1 - y_true) / (1 - clipped_dvalues)) / outputs
# Normalize gradient
self.dinputs = self.dinputs / samples
class Loss_MeanSquaredError(Loss):
def forward(self, y_pred, y_true):
sample_losses = np.mean((y_true - y_pred) ** 2, axis=-1)
return sample_losses
def backward(self, dvalues, y_true):
samples = len(dvalues)
outputs = len(dvalues[0])
# Gradient on values
self.dinputs = -2 * (y_true - dvalues) / outputs
# Normalize gradient
self.dinputs = self.dinputs / samples
class Loss_MeanAbsoluteError(Loss):
def forward(self, y_pred, y_true):
sample_losses = np.mean(np.abs(y_true - y_pred), axis=-1)
return sample_losses
def backward(self, dvalues, y_true):
samples = len(dvalues)
outputs = len(dvalues[0])
# Calculate gradient
self.dinputs = np.sign(y_true - dvalues) / outputs
# Normalize gradient
self.dinputs = self.dinputs / samples
class Activation_Softmax_Loss_CategoricalCrossentropy():
def backward(self, dvalues, y_true):
samples = len(dvalues)
if len(y_true.shape) == 2:
y_true = np.argmax(y_true, axis=1)
# Copy
self.dinputs = dvalues.copy()
# Calculate gradient
self.dinputs[range(samples), y_true] -= 1
# Normalize gradient
self.dinputs = self.dinputs / samples
class Accuracy:
def calculate(self, predictions, y):
comparisons = self.compare(predictions, y)
accuracy = np.mean(comparisons)
self.accumulated_sum += np.sum(comparisons)
self.accumulated_count += len(comparisons)
return accuracy
def calculate_accumulated(self):
accuracy = self.accumulated_sum / self.accumulated_count
return accuracy
def new_pass(self):
self.accumulated_sum = 0
self.accumulated_count = 0
class Accuracy_Regression(Accuracy):
def __init__(self):
self.precision = None
def init(self, y, reinit=False):
if self.precision is None or reinit:
self.precision = np.std(y) / 250
def compare(self, predictions, y):
return np.absolute(predictions - y) < self.precision
class Accuracy_Categorical(Accuracy):
def __init__(self, *, binary=False):
self.binary = binary
def init(self, y):
pass
def compare(self, predictions, y):
if not self.binary and len(y.shape) == 2:
y = np.argmax(y, axis=1)
return predictions == y
class Model:
def __init__(self):
self.layers = []
self.softmax_classifier_output = None
def add(self, layer):
self.layers.append(layer)
def set(self, *, loss=None, optimizer=None, accuracy=None):
if loss is not None:
self.loss = loss
if optimizer is not None:
self.optimizer = optimizer
if accuracy is not None:
self.accuracy = accuracy
def finalize(self):
# Create and set the input layer
self.input_layer = Layer_Input()
# Count all the objects
layer_count = len(self.layers)
self.trainable_layers = []
for i in range(layer_count):
if i == 0:
self.layers[i].prev = self.input_layer
self.layers[i].next = self.layers[i+1]
elif i < layer_count - 1:
self.layers[i].prev = self.layers[i-1]
self.layers[i].next = self.layers[i+1]
else:
self.layers[i].prev = self.layers[i-1]
self.layers[i].next = self.loss
self.output_layer_activation = self.layers[i]
if hasattr(self.layers[i], 'weights'):
self.trainable_layers.append(self.layers[i])
# Update loss object with trainable layers
if self.loss is not None:
self.loss.remember_trainable_layers(self.trainable_layers)
# Refactor Softmax/CategoricalCrossEntropy
if isinstance(self.layers[-1], Activation_Softmax) and \
isinstance(self.loss, Loss_CategoricalCrossentropy):
self.softmax_classifier_output = Activation_Softmax_Loss_CategoricalCrossentropy()
def train(self, X, y, *, epochs=1, batch_size=None, print_every=1, validation_data=None):
# Initialize accuracy object
self.accuracy.init(y)
# Default to 1 if not set
train_steps = 1
if validation_data is not None:
validation_steps = 1
X_val, y_val = validation_data
if batch_size is not None:
train_steps = len(X) // batch_size
if train_steps * batch_size < len(X):
train_steps += 1
if validation_data is not None:
validation_steps = len(X_val) // batch_size
if validation_steps * batch_size < len(X_val):
validation_steps += 1
# Main training loop
for epoch in range(1, epochs + 1):
# Print epoch number
print(f'epoch: {epoch}')
# Reset accumulated values in loss and accuracy objects
self.loss.new_pass()
self.accuracy.new_pass()
for step in range(train_steps):
if batch_size is None:
batch_X = X
batch_y = y
else:
batch_X = X[step * batch_size:(step+1) * batch_size]
batch_y = y[step * batch_size:(step+1) * batch_size]
output = self.forward(batch_X, training=True)
# Calculate loss
data_loss, regularization_loss = \
self.loss.calculate(output, batch_y, include_regularization=True)
loss = data_loss + regularization_loss
predictions = self.output_layer_activation.predictions(output)
accuracy = self.accuracy.calculate(predictions, batch_y)
# Backward pass
self.backward(output, batch_y)
# Optimize
self.optimizer.pre_update_params()
for layer in self.trainable_layers:
self.optimizer.update_params(layer)
self.optimizer.post_update_params()
if not step % print_every or step == train_steps - 1:
print(f'step: {step}, ' +
f'acc: {accuracy:.3f}, ' +
f'loss: {loss:.3f} (' +
f'data_loss: {data_loss:.3f}, ' +
f'reg_loss: {regularization_loss:.3f}), ' +
f'lr: {self.optimizer.current_learning_rate}')
epoch_data_loss, epoch_regularization_loss = \
self.loss.calculate_accumulated(include_regularization=True)
epoch_loss = epoch_data_loss + epoch_regularization_loss
epoch_accuracy = self.accuracy.calculate_accumulated()
print(f'training, ' +
f'acc: {epoch_accuracy:.3f}, ' +
f'loss: {epoch_loss:.3f} (' +
f'data_loss: {epoch_data_loss:.3f}, ' +
f'reg_loss: {epoch_regularization_loss:.3f}), ' +
f'lr: {self.optimizer.current_learning_rate}')
if validation_data is not None:
self.evaluate(*validation_data, batch_size=batch_size)
def forward(self, X, training):
# Call forward method on the input layer
# this will set the output property that
# the first layer in "prev" object is expecting
self.input_layer.forward(X, training)
# Call forward method of every object in a chain
# Pass output of the previous object as a parameter
for layer in self.layers:
layer.forward(layer.prev.output, training)
# "layer" is now the last object from the list,
# return its output
return layer.output
def backward(self, output, y):
if self.softmax_classifier_output is not None:
self.softmax_classifier_output.backward(output, y)
self.layers[-1].dinputs = self.softmax_classifier_output.dinputs
for layer in reversed(self.layers[:-1]):
layer.backward(layer.next.dinputs)
return
self.loss.backward(output, y)
for layer in reversed(self.layers):
layer.backward(layer.next.dinputs)
def evaluate(self, X_val, y_val, *, batch_size=None):
validation_steps = 1
if batch_size is not None:
validation_steps = len(X_val) // batch_size
if validation_steps * batch_size < len(X_val):
validation_steps += 1
self.loss.new_pass()
self.accuracy.new_pass()
for step in range(validation_steps):
if batch_size is None:
batch_X = X_val
batch_y = y_val
else:
batch_X = X_val[step * batch_size:(step + 1) * batch_size]
batch_y = y_val[step * batch_size:(step + 1) * batch_size]
output = self.forward(batch_X, training=False)
self.loss.calculate(output, batch_y)
predictions = self.output_layer_activation.predictions(output)
self.accuracy.calculate(predictions, batch_y)
validation_loss = self.loss.calculate_accumulated()
validation_accuracy = self.accuracy.calculate_accumulated()
print(f'validation, ' +
f'acc: {validation_accuracy:.3f}, ' +
f'loss: {validation_loss:.3f}')
def get_parameters(self):
parameters = []
for layer in self.trainable_layers:
parameters.append(layer.get_parameters())
return parameters
def set_parameters(self, parameters):
for parameter_set, layer in zip(parameters, self.trainable_layers):
layer.set_parameters(*parameter_set)
def save_parameters(self, path):
with open(path, 'wb') as f:
pickle.dump(self.get_parameters(), f)
def load_parameters(self, path):
with open(path, 'rb') as f:
self.set_parameters(pickle.load(f))
def save(self, path):
model = copy.deepcopy(self)
# Reset accumulated values
model.loss.new_pass()
model.accuracy.new_pass()
# Remove data from input layer and gradients from the loss object
model.input_layer.__dict__.pop('output', None)
model.loss.__dict__.pop('dinputs', None)
# For each layer, remove inputs, output and dinputs
for layer in model.layers:
for property in ['inputs', 'output', 'dinputs', 'dweights', 'dbiases']:
layer.__dict__.pop(property, None)
# Save the model
with open(path, 'wb') as f:
pickle.dump(model, f)
@staticmethod
def load(path):
with open(path, 'rb') as f:
model = pickle.load(f)
return model
def predict(self, X, *, batch_size=None):
# Default value if batch size is not being set
prediction_steps = 1
if batch_size is not None:
prediction_steps = len(X) // batch_size
if prediction_steps * batch_size < len(X):
prediction_steps += 1
output = []
for step in range(prediction_steps):
if batch_size is None:
batch_X = X
else:
batch_X = X[step * batch_size:(step + 1) * batch_size]
batch_output = self.forward(batch_X, training=False)
output.append(batch_output)
return np.vstack(output)
def load_mnist_dataset(dataset, path):
labels = os.listdir(os.path.join(path, dataset))
X = []
y = []
for label in labels:
for file in os.listdir(os.path.join(path, dataset, label)):
image = cv2.imread(
os.path.join(path, dataset, label, file),
cv2.IMREAD_UNCHANGED)
X.append(image)
y.append(label)
return np.array(X), np.array(y).astype('uint8')
def create_data_mnist(path):
X, y = load_mnist_dataset('train', path)
X_test, y_test = load_mnist_dataset('test', path)
return X, y, X_test, y_test