-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcifar10.py
1316 lines (1052 loc) · 49.2 KB
/
cifar10.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Builds the CIFAR-10 network."""
import numpy as np
import sys
import os
import urllib
import tarfile
from datetime import datetime
import time
import cifar10_input
DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz' # Data Download URL
data_dir = '/Users/apple/desktop/cifar-10-batches-py' # Data directory
batch_size = 128 # Batch size
def conv_forward_naive(x, w, b, conv_param):
"""A naive implementation of the forward propagation for convolutional neural network
Args:
x: Input data of shape(M, channels, height, width)
w: Filter weights of shape(filter_num, channels, filter_height, filter_width)
b: Biases of shape(filter_num, 1)
conv_param: A dictionary with the following keys:
'stride': The number of pixels between adjacent receptive fields in the
horizontal and vertical directions
'pad': The number of pixels that will be used to zero-pad the input
Returns:
out: Output data, of shape(N, F, H', W') where H' and W' are given by
H' = 1 + (H + 2 * pad - HH) / stride
W' = 1 + (W + 2 * pad - WW) / stride
cache: (x, w, b, conv_param) Store the variables to compute the derivatives
"""
# Compute the dimensions
M, channels, height, width = x.shape
filter_num, channels, filter_height, filter_width = w.shape
stride = conv_param['stride']
pad = conv_param['pad']
# Compute the dimensions of output
new_H = 1 + int((height + 2 * pad - filter_height) / stride)
new_W = 1 + int((width + 2 * pad - filter_width) / stride)
out = np.zeros((M, filter_num, new_H, new_W))
# Forward prop
for m in range(M):
for f in range(filter_num):
conv_newH_newW = np.ones((new_H, new_W)) * b[f]
for c in range(channels):
padded_x = np.lib.pad(x[m, c], pad_width=pad, mode='constant',
constant_values=0)
for i in range(new_H):
for j in range(new_W):
conv_newH_newW[i, j] += np.sum(padded_x[i*stride: \
i*stride+filter_height, j*stride: j*stride+filter_width]\
* w[f,c,:,:])
out[m, f] = conv_newH_newW
cache = (x, w, b, conv_param)
return out, cache
def conv_backward_naive(dout, cache):
"""A naive implementation of the backward propagation for convolutional neural network
Args:
dout: Upstream derivatives
cache: A tuple of (x, w, b, conv_param) as in forward prop
Returns:
dx: Gradients with respect to x
dw: Gradients with respect to w
db: Gradients with respect to b
"""
x, w, b, conv_param = cache
pad = conv_param['pad']
stride = conv_param['stride']
M, channels, height, width = x.shape
filter_num, channels, filter_height, filter_width = w.shape
M, filter_num, new_H, new_W = dout.shape
padded_x = np.lib.pad(x,
((0, 0), (0, 0), (pad, pad), (pad, pad)),
mode='constant',
constant_values=0)
padded_dx = np.zeros_like(padded_x)
dw = np.zeros_like(w)
db = np.zeros_like(b)
# Backward prop
for m in range(M):
for f in range(filter_num):
for i in range(new_H):
for j in range(new_W):
db[f] += dout[m, f, i, j]
dw[f] += padded_x[m, :, i*stride: i*stride+filter_height,
j*stride: j*stride+filter_width] * dout[m, f, i, j]
padded_dx[m, :, i*stride: i*stride+filter_height,
j*stride: j*stride+filter_width] += w[f] * dout[m, f, i, j]
dx = padded_dx[:, :, pad:pad+height, pad:pad+width]
return dx, dw, db
def max_pool_forward_naive(x, pool_param):
"""A naive implementation of the forward propagation for max pooling layer
Args:
x: Input data, of shape(M, channels, height, width)
pool_param: dictionary with the following keys:
'pool_height': The height of each pooling region
'pool_width': The width of each pooling region
'stride': The distance between adjacent pooling regions
Returns:
out: Output data
cache: (x, pool_param)
"""
M, channels, height, weight = x.shape
pool_height = pool_param['pool_height']
pool_width = pool_param['pool_width']
pool_stride = pool_param['stride']
new_H = 1 + int((height - pool_height) / pool_stride)
new_W = 1 + int((weight - pool_width) / pool_stride)
out = np.zeros((M, channels, new_H, new_W))
# Forward prop
for m in range(M):
for c in range(channels):
for i in range(new_H):
for j in range(new_W):
out[m, c, i, j] = np.max(x[m, c, i*pool_stride: i*pool_stride \
+pool_height, j*pool_stride: \
j*pool_stride+pool_width])
cache = (x, pool_param)
return out, cache
def max_pool_backward_naive(dout, cache):
"""A naive implementation of the backward propagation for max pooling layer
Args:
dout: Upstream derivatives
cache: A tuple of (x, pool_param) as in the forward pass
Returns:
dx: Gradients with respect to x
"""
x, pool_param = cache
M, channels, height, width = x.shape
pool_height = pool_param['pool_height']
pool_width = pool_param['pool_width']
pool_stride = pool_param['stride']
new_H = 1 + int((height - pool_height) / pool_stride)
new_W = 1 + int((width - pool_width) / pool_stride)
dx = np.zeros_like(x)
# Backward prop
for m in range(M):
for c in range(channels):
for i in range(new_H):
for j in range(new_W):
window = x[m, c, i*pool_stride: i*pool_stride+pool_height,
j*pool_stride: j*pool_stride+pool_width]
dx[m, c, i*pool_stride: i*pool_stride+pool_height, j*pool_stride\
: j*pool_stride+pool_width] = (window == np.max(window)) * \
dout[m, c, i, j]
return dx
def linear_forward(A, W, b):
"""Implement the linear part of a layer's forward propagation.
Args:
A: activations from previous layer (or input data): (size of previous layer, number of examples)
W: weights matrix: numpy array of shape (size of current layer, size of previous layer)
b: bias vector, numpy array of shape (size of the current layer, 1)
Returns:
Z: The input of the activation function, also called pre-activation parameter
cache : A python dictionary containing "A", "W" and "b" ; stored for computing the backward pass efficiently
"""
Z = np.dot(W, A) + b
assert (Z.shape == (W.shape[0], A.shape[1]))
cache = (A, W, b)
return Z, cache
def linear_activation_forward(A_prev, W, b, activation, gamma, beta, bn_param):
"""Implement the forward propagation for the LINEAR->ACTIVATION layer
Args:
A_prev: activations from previous layer (or input data): (size of previous layer, number of examples)
W: weights matrix: numpy array of shape (size of current layer, size of previous layer)
b: bias vector, numpy array of shape (size of the current layer, 1)
activation: the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"
Returns:
A: The output of the activation function, also called the post-activation value
cache: A python dictionary containing "linear_cache" and "activation_cache";
stored for computing the backward pass efficiently
"""
# Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
if activation == 'relu':
Z, linear_cache = linear_forward(A_prev, W, b)
Z, batchnorm_cache = batchnorm_forward(Z, gamma, beta, bn_param)
A, activation_cache = relu(Z)
elif activation == 'softmax':
Z, linear_cache = linear_forward(A_prev, W, b)
Z, batchnorm_cache = batchnorm_forward(Z, gamma, beta, bn_param)
A, activation_cache = softmax(Z)
# assert (A.shape == (W.shape[0], A_prev.shape[1]))
cache = (linear_cache, activation_cache, batchnorm_cache)
return A, cache
def linear_backward(dZ, cache):
"""Implement the linear portion of backward propagation for a single layer (layer l)
Args:
dZ: Gradient of the cost with respect to the linear output (of current layer l)
cache: tuple of values (A_prev, W, b) coming from the forward propagation in the current layer
Returns:
dA_prev: Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
dW: Gradient of the cost with respect to W (current layer l), same shape as W
db: Gradient of the cost with respect to b (current layer l), same shape as b
"""
A_prev, W, b = cache
m = A_prev.shape[1]
dW = 1. / m * np.dot(dZ, A_prev.T)
db = 1. / m * np.sum(dZ, axis=1, keepdims=True)
dA_prev = np.dot(W.T, dZ)
assert (dA_prev.shape == A_prev.shape)
assert (dW.shape == W.shape)
assert (db.shape == b.shape)
return dA_prev, dW, db
def linear_activation_backward(dA, cache):
"""Implement the backward propagation for the LINEAR->ACTIVATION layer.
Args:
dA: post-activation gradient for current layer l
cache: tuple of values (linear_cache, activation_cache) we store for computing backward propagation efficiently
activation: the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"
Returns:
dA_prev: Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev
dW: Gradient of the cost with respect to W (current layer l), same shape as W
db: Gradient of the cost with respect to b (current layer l), same shape as b
"""
linear_cache, activation_cache, batchnorm_cache = cache
dZ = relu_backward(dA, activation_cache)
dZ, dgamma, dbeta = batchnorm_backward(dZ, batchnorm_cache)
dA_prev, dW, db = linear_backward(dZ, linear_cache)
return dA_prev, dW, db, dgamma, dbeta
def batchnorm_forward(x, gamma, beta, bn_param):
"""Batch normalization of activation outputs.
Args:
x: activation outputs.
gamma: parameter of formula (x_norm = gamma * x_norm + beta).
beta: parameter of formula (x_norm = gamma * x_norm + beta).
bn_param: A python dictionary of {mode, epsilon, momentum, running_mean, running_var}.
Returns:
out: Normalization of activation outputs.
cache: Tuple of values (x, sample_mean, sample_var, x_hat, eps, gamma, beta) we stored.
"""
mode = bn_param['mode']
eps = bn_param['eps']
momentum = bn_param['momentum']
M, D = x.shape
running_mean = bn_param.get('running_mean', np.zeros(D, dtype=x.dtype))
running_var = bn_param.get('running_var', np.zeros(D, dtype=x.dtype))
out, cache = None, None
if mode == 'train':
sample_mean = np.mean(x, axis=0)
sample_var = np.var(x, axis=0)
x_hat = (x - sample_mean) / np.sqrt(sample_var + eps)
out = gamma * x_hat + beta
cache = (x, sample_mean, sample_var, x_hat, eps, gamma, beta)
running_mean = momentum * running_mean + (1 - momentum) * sample_mean
running_var = momentum * running_var + (1 - momentum) * sample_var
elif mode == 'test':
out = (x - running_mean) * gamma / np.sqrt(running_var + eps) + beta
bn_param['running_mean'] = running_mean
bn_param['running_var'] = running_var
return out, cache
def batchnorm_backward(dout, cache):
"""Backward propagation of batch normalization.
Args:
dout: The derivative with respect to last variable.
cache: Tuple of values (x, sample_mean, sample_var, x_hat, eps, gamma, beta)
we stored in forward propagation.
Returns:
dx: The derivative with respect to activation output.
dgamma: The derivative with respect to gamma.
dbeta: The derivative with respect to beta.
"""
x, mean, var, x_hat, eps, gamma, beta = cache
M = x.shape[0]
dgamma = np.sum(dout * x_hat, axis=0)
dbeta = np.sum(dout * 1.0, axis=0)
dx_hat = dout * gamma
dx_hat_numerator = dx_hat / np.sqrt(var + eps)
dx_hat_denominator = np.sum(dx_hat * (x-mean), axis=0)
dx_1 = dx_hat_numerator
dvar = -0.5 * ((var + eps) ** (-1.5)) * dx_hat_denominator
dmean = -1.0 * np.sum(dx_hat_numerator, axis=0) + \
dvar * np.mean(-2.0 * (x-mean), axis=0)
dx_var = dvar * 2.0 / M * (x-mean)
dx_mean = dmean * 1.0 / M
dx = dx_1 + dx_var + dx_mean
return dx, dgamma, dbeta
def spatial_batchnorm_forward(x, gamma, beta, bn_param):
"""Spatial batch normalization of activation outputs.
Args:
x: activation outputs.
gamma: parameter of formula (x_norm = gamma * x_norm + beta).
beta: parameter of formula (x_norm = gamma * x_norm + beta).
bn_param: A python dictionary of {mode, epsilon, momentum, running_mean, running_var}.
Returns:
out: Normalization of activation outputs.
cache: Tuple of values (x, sample_mean, sample_var, x_hat, eps, gamma, beta) we stored.
"""
out, cache = None, None
M, channels, height, width = x.shape
x_new = x.transpose(0, 2, 3, 1).reshape(M*height*width, channels)
out, cache = batchnorm_forward(x_new, gamma, beta, bn_param)
out = out.reshape(M, height, width, channels).transpose(0, 3, 1, 2)
return out, cache
def spatial_batchnorm_backward(dout, cache):
"""Spatial backward propagation of batch normalization.
Args:
dout: The derivative with respect to last variable.
cache: Tuple of values (x, sample_mean, sample_var, x_hat, eps, gamma, beta)
we stored in forward propagation.
Returns:
dx: The derivative with respect to activation output.
dgamma: The derivative with respect to gamma.
dbeta: The derivative with respect to beta.
"""
dx, dgamma, dbeta = None, None, None
M, channels, height, width = dout.shape
dout_new = dout.transpose(0, 2, 3, 1).reshape(M*height*width, channels)
dx, dgamma, dbeta = batchnorm_backward(dout_new, cache)
dx = dx.reshape(M, height, width, channels).transpose(0, 3, 1, 2)
return dx, dgamma, dbeta
def relu(x):
"""Relu activation function.
Args:
x: Inputs.
Returns:
Activation outputs.
activation_cache: The cache of activation input.
"""
activation_cache = x
return np.maximum(x, 0), activation_cache
def relu_backward(dA, activation_cache):
"""Relu backward propagation.
Args:
dA: The derivative of last variable.
activation_cache: The cache of activation input we stored in relu function.
Returns:
The derivative of activation input.
"""
return np.multiply((activation_cache >= 0), 1) * dA
def softmax(x):
"""Softmax activation function.
Args:
x: Inputs.
Returns:
Activation outputs.
activation_cache: The cache of activation input.
"""
assert x.shape == (10, 128)
activation_cache = x
return np.exp(x) / np.sum(np.exp(x), axis=0, keepdims=True), activation_cache
def softmax_backward(y, yhat):
"""Softmax backward propagation.
Args:
y: The real labels.
yhat: The predictions we got.
Returns:
The derivative of activation input.
"""
return yhat - y
def initialize_parameters():
"""Initialize all parameters.
Returns:
parameters: A python dictionary of all parameters.
"""
parameters = {}
parameters['w_conv1'] = np.random.randn(5, 3, 3, 3) * 0.01
parameters['b_conv1'] = np.zeros((5, 1))
parameters['w_conv2'] = np.random.randn(5, 5, 3, 3) * 0.01
parameters['b_conv2'] = np.zeros((5, 1))
parameters['w1'] = np.random.randn(384, 320) * 0.01
parameters['b1'] = np.zeros((384, 1))
parameters['w2'] = np.random.randn(192, 384) * 0.01
parameters['b2'] = np.zeros((192, 1))
parameters['w3'] = np.random.randn(10, 192) * 0.01
parameters['b3'] = np.zeros((10, 1))
parameters['gamma_conv1'] = 1
parameters['gamma_conv2'] = 1
parameters['gamma1'] = 1
parameters['gamma2'] = 1
parameters['gamma3'] = 1
parameters['beta_conv1'] = 0
parameters['beta_conv2'] = 0
parameters['beta1'] = 0
parameters['beta2'] = 0
parameters['beta3'] = 0
return parameters
def update_parameters(parameters, grads, learning_rate):
"""Update parameters using gradient descent.
Args:
parameters: python dictionary containing your parameters.
grads: python dictionary containing your gradients, output of L_model_backward.
Returns:
parameters: python dictionary containing your updated parameters.
parameters["W" + str(l)] = ...
parameters["b" + str(l)] = ...
"""
# Update rule for each parameter.
parameters['w_conv2'] = parameters['w_conv2'] - learning_rate * grads['dw_conv2']
parameters['b_conv2'] = parameters['b_conv2'] - learning_rate * grads['db_conv2']
parameters['w_conv1'] = parameters['w_conv1'] - learning_rate * grads['dw_conv1']
parameters['b_conv1'] = parameters['b_conv1'] - learning_rate * grads['db_conv1']
parameters['w3'] = parameters['w3'] - learning_rate * grads['dw3']
parameters['b3'] = parameters['b3'] - learning_rate * grads['db3']
parameters['w2'] = parameters['w2'] - learning_rate * grads['dw2']
parameters['b2'] = parameters['b2'] - learning_rate * grads['db2']
parameters['w1'] = parameters['w1'] - learning_rate * grads['dw1']
parameters['b1'] = parameters['b1'] - learning_rate * grads['db1']
parameters['gamma_conv2'] = parameters['gamma_conv2'] - learning_rate * \
grads['dgamma_conv2']
parameters['beta_conv2'] = parameters['beta_conv2'] - learning_rate * \
grads['dbeta_conv2']
parameters['gamma_conv1'] = parameters['gamma_conv1'] - learning_rate * \
grads['dgamma_conv1']
parameters['beta_conv1'] = parameters['beta_conv1'] - learning_rate * \
grads['dbeta_conv1']
parameters['gamma3'] = parameters['gamma3'] - learning_rate * grads['dgamma3']
parameters['beta3'] = parameters['beta3'] - learning_rate * grads['dbeta3']
parameters['gamma2'] = parameters['gamma2'] - learning_rate * grads['dgamma2']
parameters['beta2'] = parameters['beta2'] - learning_rate * grads['dbeta2']
parameters['gamma1'] = parameters['gamma1'] - learning_rate * grads['dgamma1']
parameters['beta1'] = parameters['beta1'] - learning_rate * grads['dbeta1']
return parameters
def preprocessing_inputs():
"""Construct input for CIFAR training.
Returns:
mini_batches: The whole training data which is split up to n mini_batches.
Raises:
ValueError: If no data_dir
"""
if not data_dir:
raise ValueError('Please supply a data_dir')
return cifar10_input.preprocessing_inputs(data_dir=data_dir, batch_size=batch_size)
def preprocessing_inputs_test():
"""Construct input for CIFAR testing.
Returns:
test_images: A (10000, 3, 32, 32) shape testing images.
test_labels: A (1, 10000) shape testing labels.
Raises:
ValueError: If no data_dir
"""
if not data_dir:
raise ValueError('Please supply a data_dir')
return cifar10_input.preprocessing_inputs_test(data_dir=data_dir, batch_size=batch_size)
def gradient_check(images, labels, epsilon):
"""Gradient checking for parameters.
Args:
images: Training images.
labels: Training labels.
epsilon: A little real number closed to 0.
Returns:
difference: The difference between two derivatives computing ways.
"""
parameters = initialize_parameters()
w_conv1 = parameters['w_conv1']
b_conv1 = parameters['b_conv1']
w_conv2 = parameters['w_conv2']
b_conv2 = parameters['b_conv2']
w1 = parameters['w1']
b1 = parameters['b1']
w2 = parameters['w2']
b2 = parameters['b2']
w3 = parameters['w3']
b3 = parameters['b3']
gamma_conv1 = parameters['gamma_conv1']
gamma_conv2 = parameters['gamma_conv2']
gamma1 = parameters['gamma1']
gamma2 = parameters['gamma2']
gamma3 = parameters['gamma3']
beta_conv1 = parameters['beta_conv1']
beta_conv2 = parameters['beta_conv2']
beta1 = parameters['beta1']
beta2 = parameters['beta2']
beta3 = parameters['beta3']
# Compute backward gradients
# conv_1
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
conv_1, cache_conv_1 = conv_forward_naive(images, w_conv1,
b_conv1, conv_param)
conv_1, cache_batchnorm_conv_1 = spatial_batchnorm_forward(conv_1,
gamma_conv1,
beta_conv1,
bn_param)
conv_1, cache_activation_conv_1 = relu(conv_1)
# pool_1
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_1, cache_pool_1 = max_pool_forward_naive(conv_1, pool_param)
# conv_2
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
conv_2, cache_conv_2 = conv_forward_naive(pool_1, w_conv2, b_conv2,
conv_param)
conv_2, cache_batchnorm_conv_2 = spatial_batchnorm_forward(conv_2,
gamma_conv2,
beta_conv2,
bn_param)
conv_2, cache_activation_conv_2 = relu(conv_2)
# pool_2
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_2, cache_pool_2 = max_pool_forward_naive(conv_2, pool_param)
# local_3_relu
reshape = pool_2.reshape(128, -1).T
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
local_3, cache_local_3 = linear_activation_forward(reshape, w1, b1,
activation='relu',
gamma=gamma1,
beta=beta1,
bn_param=bn_param)
# local_4_relu
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
local_4, cache_local_4 = linear_activation_forward(local_3, w2, b2,
activation='relu',
gamma=gamma2,
beta=beta2,
bn_param=bn_param)
# local_5_softmax
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
logits, cache_local_5 = linear_activation_forward(local_4, w3, b3,
activation='softmax',
gamma=gamma3,
beta=beta3,
bn_param=bn_param)
# Compute cost
cost, new_labels = compute_cost(logits, labels)
# Backward prop local
dZ = softmax_backward(new_labels, logits)
dZ, dgamma3, dbeta3 = batchnorm_backward(dZ, cache_local_5[2])
dA2, dw3, db3 = linear_backward(dZ, cache_local_5[0])
dA1, dw2, db2, dgamma2, dbeta2 = linear_activation_backward(dA2,
cache_local_4)
dA0, dw1, db1, dgamma1, dbeta1 = linear_activation_backward(dA1,
cache_local_3)
# Backward prop conv
dout_pool_2 = dA0.T.reshape(128, 5, 8, 8)
# pool_2 back prop
d_pool_2 = max_pool_backward_naive(dout_pool_2, cache_pool_2)
# conv_2 back prop
dout_conv_2 = relu_backward(d_pool_2, cache_activation_conv_2)
dout_conv_2, dgamma_conv2, dbeta_conv2 = spatial_batchnorm_backward(dout_conv_2,
cache_batchnorm_conv_2)
dout_pool_1, dw_conv2, db_conv2 = conv_backward_naive(dout_conv_2,
cache_conv_2)
# pool_1 back prop
d_pool_1 = max_pool_backward_naive(dout_pool_1, cache_pool_1)
# conv_1 back prop
dout_conv_1 = relu_backward(d_pool_1, cache_activation_conv_1)
dout_conv_1, dgamma_conv1, dbeta_conv1 = spatial_batchnorm_backward(dout_conv_1,
cache_batchnorm_conv_1)
dAA, dw_conv1, db_conv1 = conv_backward_naive(dout_conv_1, cache_conv_1)
# Reshape gradients
dw_conv2 = dw_conv2.reshape(1, 5*5*3*3)
db_conv2 = db_conv2.reshape(1, 5*1)
dw_conv1 = dw_conv1.reshape(1, 5*3*3*3)
db_conv1 = db_conv1.reshape(1, 5*1)
dw1 = dw1.reshape(1, 384*320)
db1 = db1.reshape(1, 384*1)
dw2 = dw2.reshape(1, 192*384)
db2 = db2.reshape(1, 192*1)
dw3 = dw3.reshape(1, 10*192)
db3 = db3.reshape(1, 10*1)
dgamma_conv2 = dgamma_conv2.reshape(1, 5)
dbeta_conv2 = dbeta_conv2.reshape(1, 5)
dgamma_conv1 = dgamma_conv1.reshape(1, 5)
dbeta_conv1 = dbeta_conv1.reshape(1, 5)
dgamma1 = dgamma1.reshape(1, 128)
dbeta1 = dbeta1.reshape(1, 128)
dgamma2 = dgamma2.reshape(1, 128)
dbeta2 = dbeta2.reshape(1, 128)
dgamma3 = dgamma3.reshape(1, 128)
dbeta3 = dbeta3.reshape(1, 128)
grads = np.concatenate((dw_conv2,db_conv2,dw_conv1,db_conv1,dw1,db1,dw2,db2,
dw3,db3,dgamma_conv2,dbeta_conv2,dgamma_conv1,dbeta_conv1,
dgamma1,dbeta1,dgamma2,dbeta2,dgamma3,dbeta3),axis=1)
# Compute approximate gradient
gradapprox = np.zeros((1, grads.shape[1]))
# Reshape parameters
w_conv2 = w_conv2.reshape(1, 5 * 5 * 3 * 3)
b_conv2 = b_conv2.reshape(1, 5 * 1)
w_conv1 = w_conv1.reshape(1, 5 * 3 * 3 * 3)
b_conv1 = b_conv1.reshape(1, 5 * 1)
w1 = w1.reshape(1, 384 * 320)
b1 = b1.reshape(1, 384 * 1)
w2 = w2.reshape(1, 192 * 384)
b2 = b2.reshape(1, 192 * 1)
w3 = w3.reshape(1, 10 * 192)
b3 = b3.reshape(1, 10 * 1)
gamma_conv2 = np.tile(np.array(gamma_conv2), 5).reshape(1, 5)
beta_conv2 = np.tile(np.array(beta_conv2), 5).reshape(1, 5)
gamma_conv1 = np.tile(np.array(gamma_conv1), 5).reshape(1, 5)
beta_conv1 = np.tile(np.array(beta_conv1), 5).reshape(1, 5)
gamma1 = np.tile(np.array(gamma1), 128).reshape(1, 128)
beta1 = np.tile(np.array(beta1), 128).reshape(1, 128)
gamma2 = np.tile(np.array(gamma2), 128).reshape(1, 128)
beta2 = np.tile(np.array(beta2), 128).reshape(1, 128)
gamma3 = np.tile(np.array(gamma3), 128).reshape(1, 128)
beta3 = np.tile(np.array(beta3), 128).reshape(1, 128)
collection = np.concatenate((w_conv2, b_conv2, w_conv1, b_conv1, w1, b1, w2, b2,
w3, b3, gamma_conv2, beta_conv2, gamma_conv1, beta_conv1,
gamma1, beta1, gamma2, beta2, gamma3, beta3), axis=1)
collection_plus = collection
collection_minus = collection
for i in range(collection.shape[1]):
collection_plus[:, i] = collection_plus[:, i] + epsilon
collection_minus[:, i] = collection_minus[:, i] - epsilon
# Plus parameters forward prop
w_conv2 = collection_plus[:, 0: 225].reshape(5, 5, 3, 3)
b_conv2 = collection_plus[:, 225: 230].reshape(5, 1)
w_conv1 = collection_plus[:, 230: 365].reshape(5, 3, 3, 3)
b_conv1 = collection_plus[:, 365: 370].reshape(5, 1)
w1 = collection_plus[:, 370: 123250].reshape(384, 320)
b1 = collection_plus[:, 123250: 123634].reshape(384, 1)
w2 = collection_plus[:, 123634: 197362].reshape(192, 384)
b2 = collection_plus[:, 197362: 197554].reshape(192, 1)
w3 = collection_plus[:, 197554: 199474].reshape(10, 192)
b3 = collection_plus[:, 199474: 199484].reshape(10, 1)
gamma_conv2 = collection_plus[:, 199484: 199489].reshape(5,)
beta_conv2 = collection_plus[:, 199489: 199494].reshape(5,)
gamma_conv1 = collection_plus[:, 199494: 199499].reshape(5,)
beta_conv1 = collection_plus[:, 199499: 199504].reshape(5,)
gamma1 = collection_plus[:, 199504: 199632].reshape(128,)
beta1 = collection_plus[:, 199632: 199760].reshape(128,)
gamma2 = collection_plus[:, 199760: 199888].reshape(128,)
beta2 = collection_plus[:, 199888: 200016].reshape(128,)
gamma3 = collection_plus[:, 200016: 200144].reshape(128,)
beta3 = collection_plus[:, 200144: 200272].reshape(128,)
# conv_1
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
conv_1, cache_conv_1 = conv_forward_naive(images, w_conv1,
b_conv1, conv_param)
conv_1, cache_batchnorm_conv_1 = spatial_batchnorm_forward(conv_1,
gamma_conv1,
beta_conv1,
bn_param)
conv_1, cache_activation_conv_1 = relu(conv_1)
# pool_1
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_1, cache_pool_1 = max_pool_forward_naive(conv_1, pool_param)
# conv_2
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
conv_2, cache_conv_2 = conv_forward_naive(pool_1, w_conv2, b_conv2,
conv_param)
conv_2, cache_batchnorm_conv_2 = spatial_batchnorm_forward(conv_2,
gamma_conv2,
beta_conv2,
bn_param)
conv_2, cache_activation_conv_2 = relu(conv_2)
# pool_2
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_2, cache_pool_2 = max_pool_forward_naive(conv_2, pool_param)
# local_3_relu
reshape = pool_2.reshape(128, -1).T
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
local_3, cache_local_3 = linear_activation_forward(reshape, w1, b1,
activation='relu',
gamma=gamma1,
beta=beta1,
bn_param=bn_param)
# local_4_relu
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
local_4, cache_local_4 = linear_activation_forward(local_3, w2, b2,
activation='relu',
gamma=gamma2,
beta=beta2,
bn_param=bn_param)
# local_5_softmax
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
logits, cache_local_5 = linear_activation_forward(local_4, w3, b3,
activation='softmax',
gamma=gamma3,
beta=beta3,
bn_param=bn_param)
# Compute cost
cost_plus, new_labels = compute_cost(logits, labels)
# Minus parameters forward prop
w_conv2 = collection_minus[:, 0: 225].reshape(5, 5, 3, 3)
b_conv2 = collection_minus[:, 225: 230].reshape(5, 1)
w_conv1 = collection_minus[:, 230: 365].reshape(5, 3, 3, 3)
b_conv1 = collection_minus[:, 365: 370].reshape(5, 1)
w1 = collection_minus[:, 370: 123250].reshape(384, 320)
b1 = collection_minus[:, 123250: 123634].reshape(384, 1)
w2 = collection_minus[:, 123634: 197362].reshape(192, 384)
b2 = collection_minus[:, 197362: 197554].reshape(192, 1)
w3 = collection_minus[:, 197554: 199474].reshape(10, 192)
b3 = collection_minus[:, 199474: 199484].reshape(10, 1)
gamma_conv2 = collection_minus[:, 199484: 199489].reshape(5, )
beta_conv2 = collection_minus[:, 199489: 199494].reshape(5, )
gamma_conv1 = collection_minus[:, 199494: 199499].reshape(5, )
beta_conv1 = collection_minus[:, 199499: 199504].reshape(5, )
gamma1 = collection_minus[:, 199504: 199632].reshape(128, )
beta1 = collection_minus[:, 199632: 199760].reshape(128, )
gamma2 = collection_minus[:, 199760: 199888].reshape(128, )
beta2 = collection_minus[:, 199888: 200016].reshape(128, )
gamma3 = collection_minus[:, 200016: 200144].reshape(128, )
beta3 = collection_minus[:, 200144: 200272].reshape(128, )
# conv_1
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
conv_1, cache_conv_1 = conv_forward_naive(images, w_conv1,
b_conv1, conv_param)
conv_1, cache_batchnorm_conv_1 = spatial_batchnorm_forward(conv_1,
gamma_conv1,
beta_conv1,
bn_param)
conv_1, cache_activation_conv_1 = relu(conv_1)
# pool_1
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_1, cache_pool_1 = max_pool_forward_naive(conv_1, pool_param)
# conv_2
conv_param = {}
conv_param['stride'] = 1
conv_param['pad'] = 1
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
conv_2, cache_conv_2 = conv_forward_naive(pool_1, w_conv2, b_conv2,
conv_param)
conv_2, cache_batchnorm_conv_2 = spatial_batchnorm_forward(conv_2,
gamma_conv2,
beta_conv2,
bn_param)
conv_2, cache_activation_conv_2 = relu(conv_2)
# pool_2
pool_param = {}
pool_param['pool_height'] = 2
pool_param['pool_width'] = 2
pool_param['stride'] = 2
pool_2, cache_pool_2 = max_pool_forward_naive(conv_2, pool_param)
# local_3_relu
reshape = pool_2.reshape(128, -1).T
bn_param = {}
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
local_3, cache_local_3 = linear_activation_forward(reshape, w1, b1,
activation='relu',
gamma=gamma1,
beta=beta1,
bn_param=bn_param)
# local_4_relu
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
local_4, cache_local_4 = linear_activation_forward(local_3, w2, b2,
activation='relu',
gamma=gamma2,
beta=beta2,
bn_param=bn_param)
# local_5_softmax
bn_param['mode'] = 'train'
bn_param['eps'] = 1e-5
bn_param['momentum'] = 0.9
logits, cache_local_5 = linear_activation_forward(local_4, w3, b3,
activation='softmax',
gamma=gamma3,
beta=beta3,
bn_param=bn_param)
# Compute cost
cost_minus, new_labels = compute_cost(logits, labels)
collection_plus = collection
collection_minus = collection