-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvolutional_networks.py
More file actions
1312 lines (1152 loc) · 58.3 KB
/
convolutional_networks.py
File metadata and controls
1312 lines (1152 loc) · 58.3 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
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
"""
Implements convolutional networks in PyTorch.
WARNING: you SHOULD NOT use ".to()" or ".cuda()" in each implementation block.
"""
import torch
from p3_helper import softmax_loss
from fully_connected_networks import Linear_ReLU, Linear, Solver, adam, ReLU
def hello_convolutional_networks():
"""
This is a sample function that we will try to import and run to ensure that
our environment is correctly set up on Google Colab.
"""
print('Hello from convolutional_networks.py!')
class Conv(object):
@staticmethod
def forward(x, w, b, conv_param):
"""
A naive implementation of the forward pass for a convolutional layer.
The input consists of N data points, each with C channels, height H and
width W. We convolve each input with F different filters, where each
filter spans all C channels and has height HH and width WW.
Input:
- x: Input data of shape (N, C, H, W)
- w: Filter weights of shape (F, C, HH, WW)
- b: Biases, of shape (F,)
- 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 is used to zero-pad the input.
During padding, 'pad' zeros should be placed symmetrically (i.e equally
on both sides) along the height and width axes of the input. Be careful
not to modfiy the original input x directly.
Returns a tuple of:
- 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)
"""
out = None
####################################################################
# TODO: Implement the convolutional forward pass. #
# Hint: you can use function torch.nn.functional.pad for padding. #
# You are NOT allowed to use anything in torch.nn in other places. #
####################################################################
# Replace "pass" statement with your code
padding = torch.nn.functional.pad
pad = conv_param['pad']
stride = conv_param['stride']
N, C, H, W = x.shape
F, _, HH, WW = w.shape
H_out = 1 + (H + 2 * pad - HH) // stride
W_out = 1 + (W + 2 * pad - WW) // stride
out = torch.zeros((N, F, H_out, W_out), device = x.device, dtype= x.dtype)
x_padded = padding(x, (pad, pad, pad, pad), mode='constant', value=0)
for image in range(N):
for filter in range(F):
for i in range(H_out):
for j in range(W_out):
out[image,filter,i,j] = \
torch.sum(x_padded[image,:,i*stride:i*stride+HH, j*stride:j*stride+WW] * w[filter,:,:,:]) + b[filter]
#####################################################################
# END OF YOUR CODE #
#####################################################################
cache = (x, w, b, conv_param)
return out, cache
@staticmethod
def backward(dout, cache):
"""
A naive implementation of the backward pass for a convolutional layer.
Inputs:
- dout: Upstream derivatives.
- cache: A tuple of (x, w, b, conv_param) as in conv_forward_naive
Returns a tuple of:
- dx: Gradient with respect to x
- dw: Gradient with respect to w
- db: Gradient with respect to b
"""
dx, dw, db = None, None, None
###############################################################
# TODO: Implement the convolutional backward pass. #
###############################################################
# Replace "pass" statement with your code
x, w, b, conv_param = cache
N, C, H, W = x.shape
F, _, HH, WW = w.shape
_, _, H_out, W_out = dout.shape
pad, stride = conv_param['pad'], conv_param['stride']
padding = torch.nn.functional.pad
x_pad = padding(x, (pad, pad, pad, pad), mode='constant', value=0).to(x.dtype).to(x.device)
dx = torch.zeros_like(x_pad) # dx = dout * w
dw = torch.zeros_like(w) # dw = dout * x
db = torch.zeros_like(b)
for img in range(N):
for fil in range(F):
db[fil] += torch.sum(dout[img, fil])
for i in range(H_out):
for j in range(W_out):
dw[fil, :, :, :] += dout[img, fil, i, j] * \
x_pad[img, : , i*stride:i*stride+HH, j*stride:j*stride+WW]
# image patch
dx[img, :, i*stride:i*stride+HH,j*stride:j*stride+WW]\
+= dout[img, fil, i, j] * w[fil, :, :, :]
dx = dx[:, :, pad: H+pad, pad: W+pad] # only remain non-padding part
###############################################################
# END OF YOUR CODE #
###############################################################
return dx, dw, db
class MaxPool(object):
@staticmethod
def forward(x, pool_param):
"""
A naive implementation of the forward pass for a max-pooling layer.
Inputs:
- x: Input data, of shape (N, C, H, W)
- 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
No padding is necessary here.
Returns a tuple of:
- out: Output of shape (N, C, H', W') where H' and W' are given by
H' = 1 + (H - pool_height) / stride
W' = 1 + (W - pool_width) / stride
- cache: (x, pool_param)
"""
out = None
####################################################################
# TODO: Implement the max-pooling forward pass #
####################################################################
# Replace "pass" statement with your code
N, C, H, W = x.shape
HH, WW = pool_param['pool_height'], pool_param['pool_width']
stride = pool_param['stride']
H_out = 1 + (H - HH) // stride
W_out = 1 + (W - WW) // stride
out = torch.zeros((N, C, H_out, W_out), device=x.device, dtype=x.dtype)
for img in range(N):
for channel in range(C):
for i in range(H_out):
for j in range(W_out):
out[img, channel, i, j] = \
torch.max(x[img, channel, i*stride:i*stride+HH, j*stride:j*stride+WW])
####################################################################
# END OF YOUR CODE #
####################################################################
cache = (x, pool_param)
return out, cache
@staticmethod
def backward(dout, cache):
"""
A naive implementation of the backward pass for a max-pooling layer.
Inputs:
- dout: Upstream derivatives
- cache: A tuple of (x, pool_param) as in the forward pass.
Returns:
- dx: Gradient with respect to x
"""
dx = None
#####################################################################
# TODO: Implement the max-pooling backward pass #
#####################################################################
# Replace "pass" statement with your code
x, pool_param = cache
N, C, H, W = x.shape
HH, WW = pool_param['pool_height'], pool_param['pool_width']
stride = pool_param['stride']
_, _, H_out, W_out = dout.shape
dx = torch.zeros_like(x)
for img in range(N):
for c in range(C):
for i in range(H_out):
for j in range(W_out):
img_window = x[img, c, i*stride:i*stride+HH, j*stride:j*stride+WW]
max_idx = torch.argmax(img_window)
row = max_idx // img_window.shape[0]
column = max_idx % img_window.shape[1]
dx[img, c, i*stride:i*stride+HH, j*stride:j*stride+WW][row, column] = \
dout[img, c, i, j]
####################################################################
# END OF YOUR CODE #
####################################################################
return dx
class ThreeLayerConvNet(object):
"""
A three-layer convolutional network with the following architecture:
conv - relu - 2x2 max pool - linear - relu - linear - softmax
The network operates on minibatches of data that have shape (N, C, H, W)
consisting of N images, each with height H and width W and with C input
channels.
"""
def __init__(self,
input_dims=(3, 32, 32),
num_filters=32,
filter_size=7,
hidden_dim=100,
num_classes=10,
weight_scale=1e-3,
reg=0.0,
dtype=torch.float,
device='cpu'):
"""
Initialize a new network.
Inputs:
- input_dims: Tuple (C, H, W) giving size of input data
- num_filters: Number of filters to use in the convolutional layer
- filter_size: Width/height of filters to use in convolutional layer
- hidden_dim: Number of units to use in fully-connected hidden layer
- num_classes: Number of scores to produce from the final linear layer.
- weight_scale: Scalar giving standard deviation for random
initialization of weights.
- reg: Scalar giving L2 regularization strength
- dtype: A torch data type object; all computations will be performed
using this datatype. float is faster but less accurate, so you
should use double for numeric gradient checking.
- device: device to use for computation. 'cpu' or 'cuda'
"""
self.params = {}
self.reg = reg
self.dtype = dtype
######################################################################
# TODO: Initialize weights,biases for the three-layer convolutional #
# network. Weights should be initialized from a Gaussian #
# centered at 0.0 with standard deviation equal to weight_scale; #
# biases should be initialized to zero. All weights and biases #
# should be stored in thedictionary self.params. Store weights and #
# biases for the convolutional layer using the keys 'W1' and 'b1'; #
# use keys 'W2' and 'b2' for the weights and biases of the hidden #
# linear layer, and key 'W3' and 'b3' for the weights and biases of #
# the output linear layer #
# #
# IMPORTANT: For this assignment, you can assume that the padding #
# and stride of the first convolutional layer are chosen so that #
# **the width and height of the input are preserved**. Take a #
# look at the start of the loss() function to see how that happens. #
######################################################################
# Replace "pass" statement with your code
C, H, W = input_dims
self.params['W1'] = weight_scale * \
torch.randn(num_filters, C, filter_size, filter_size, dtype=dtype, device=device)
self.params['b1'] = torch.zeros(num_filters, dtype=dtype, device=device)
# linear layer
# 2 * 2 max pooling reduce the dimension by 2
in_dim = num_filters * (H//2) * (W//2)
# print(num_filters)
# print(in_dim)
# print(H//2)
self.params["W2"] = weight_scale * torch.randn(in_dim, hidden_dim, dtype=dtype, device=device)
self.params["b2"] = torch.zeros(hidden_dim, dtype=dtype, device=device)
# linear layer
self.params["W3"] = weight_scale * torch.randn(hidden_dim, num_classes, dtype=dtype, device=device)
self.params["b3"] = torch.zeros(num_classes, dtype=dtype, device=device)
######################################################################
# END OF YOUR CODE #
######################################################################
def save(self, path):
checkpoint = {
'reg': self.reg,
'dtype': self.dtype,
'params': self.params,
}
torch.save(checkpoint, path)
print("Saved in {}".format(path))
def load(self, path):
checkpoint = torch.load(path, map_location='cpu')
self.params = checkpoint['params']
self.dtype = checkpoint['dtype']
self.reg = checkpoint['reg']
print("load checkpoint file: {}".format(path))
def loss(self, X, y=None):
"""
Evaluate loss and gradient for the three-layer convolutional network.
Input / output: Same API as TwoLayerNet.
"""
X = X.to(self.dtype)
W1, b1 = self.params['W1'], self.params['b1']
W2, b2 = self.params['W2'], self.params['b2']
W3, b3 = self.params['W3'], self.params['b3']
# pass conv_param to the forward pass for the convolutional layer
# Padding and stride chosen to preserve the input spatial size
filter_size = W1.shape[2]
conv_param = {'stride': 1, 'pad': (filter_size - 1) // 2}
# pass pool_param to the forward pass for the max-pooling layer
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}
scores = None
######################################################################
# TODO: Implement the forward pass for three-layer convolutional #
# net, computing the class scores for X and storing them in the #
# scores variable. #
# #
# Remember you can use functions defined in your implementation #
# above #
######################################################################
# Replace "pass" statement with your code
conv_Relu_pool, cache_c_R_p = Conv_ReLU_Pool.forward(X, W1, b1, conv_param, pool_param)
linear_Relu, cache_L_R = Linear_ReLU.forward(conv_Relu_pool, W2, b2)
scores, cache_l_c = Linear.forward(linear_Relu, W3, b3)
######################################################################
# END OF YOUR CODE #
######################################################################
if y is None:
return scores
loss, grads = 0.0, {}
####################################################################
# TODO: Implement backward pass for three-layer convolutional net, #
# storing the loss and gradients in the loss and grads variables. #
# Compute data loss using softmax, and make sure that grads[k] #
# holds the gradients for self.params[k]. Don't forget to add #
# L2 regularization! #
# #
# NOTE: To ensure that your implementation matches ours and you #
# pass the automated tests, make sure that your L2 regularization #
# does not include a factor of 0.5 #
####################################################################
# Replace "pass" statement with your code
loss, dout = softmax_loss(scores, y)
loss += self.reg * (torch.sum(W1*W1) + torch.sum(W2*W2) + torch.sum(W3*W3))
d_l_c, dW3, grads['b3'] = Linear.backward(dout, cache_l_c)
d_L_R, dW2, grads['b2'] = Linear_ReLU.backward(d_l_c, cache_L_R)
d_c_R_p, dW1, grads['b1'] = Conv_ReLU_Pool.backward(d_L_R, cache_c_R_p)
grads['W1'] = dW1 + 2 * self.reg * W1
grads['W2'] = dW2 + 2 * self.reg * W2
grads['W3'] = dW3 + 2 * self.reg * W3
###################################################################
# END OF YOUR CODE #
###################################################################
return loss, grads
class DeepConvNet(object):
"""
A convolutional neural network with an arbitrary number of convolutional
layers in VGG-Net style. All convolution layers will use kernel size 3 and
padding 1 to preserve the feature map size, and all pooling layers will be
max pooling layers with 2x2 receptive fields and a stride of 2 to halve the
size of the feature map.
The network will have the following architecture:
{conv - [batchnorm?] - relu - [pool?]} x (L - 1) - linear
Each {...} structure is a "macro layer" consisting of a convolution layer,
an optional batch normalization layer, a ReLU nonlinearity, and an optional
pooling layer. After L-1 such macro layers, a single fully-connected layer
is used to predict the class scores.
The network operates on minibatches of data that have shape (N, C, H, W)
consisting of N images, each with height H and width W and with C input
channels.
"""
def __init__(self,
input_dims=(3, 32, 32),
num_filters=[8, 8, 8, 8, 8],
max_pools=[0, 1, 2, 3, 4],
batchnorm=False,
num_classes=10,
weight_scale=1e-3,
reg=0.0,
weight_initializer=None,
dtype=torch.float,
device='cpu'):
"""
Initialize a new network.
Inputs:
- input_dims: Tuple (C, H, W) giving size of input data
- num_filters: List of length (L - 1) giving the number of
convolutional filters to use in each macro layer.
- max_pools: List of integers giving the indices of the macro
layers that should have max pooling (zero-indexed).
- batchnorm: Whether to include batch normalization in each macro layer
- num_classes: Number of scores to produce from the final linear layer.
- weight_scale: Scalar giving standard deviation for random
initialization of weights, or the string "kaiming" to use Kaiming
initialization instead
- reg: Scalar giving L2 regularization strength. L2 regularization
should only be applied to convolutional and fully-connected weight
matrices; it should not be applied to biases or to batchnorm scale
and shifts.
- dtype: A torch data type object; all computations will be performed
using this datatype. float is faster but less accurate, so you should
use double for numeric gradient checking.
- device: device to use for computation. 'cpu' or 'cuda'
"""
self.params = {}
self.num_layers = len(num_filters)+1
self.max_pools = max_pools
self.batchnorm = batchnorm
self.reg = reg
self.dtype = dtype
if device == 'cuda':
device = 'cuda:0'
#####################################################################
# TODO: Initialize the parameters for the DeepConvNet. All weights, #
# biases, and batchnorm scale and shift parameters should be #
# stored in the dictionary self.params. #
# #
# Weights for conv and fully-connected layers should be initialized #
# according to weight_scale. Biases should be initialized to zero. #
# Batchnorm scale (gamma) and shift (beta) parameters should be #
# initilized to ones and zeros respectively. #
#####################################################################
# Replace "pass" statement with your code
C,H,W = input_dims
cat_filter = [C]
cat_filter += num_filters
for layer in range(self.num_layers-1):
num_f = num_filters[layer]
if weight_scale == 'kaiming':
self.params['W' + str(layer+1)] = kaiming_initializer(cat_filter[layer],num_f,K=3,relu=True,device=device,dtype=dtype)
else:
self.params['W'+ str(layer+1)] = weight_scale * torch.randn(num_f,cat_filter[layer],3,3,dtype=dtype,device=device)
self.params['b'+str(layer+1)] = torch.zeros(num_f,dtype=dtype ,device = device)
if self.batchnorm:
self.params['gamma'+str(layer+1)] = torch.ones(num_f,dtype = dtype,device = device)
self.params['beta'+str(layer+1)] = torch.zeros(num_f,dtype=dtype,device=device)
num_max_pool = len(max_pools)
final = num_filters[-1] * (H//(2**num_max_pool)) * (W//(2**num_max_pool))
if weight_scale == 'kaiming':
self.params["W"+str(self.num_layers)] = kaiming_initializer(final,num_classes,relu=False,device=device,dtype = dtype)
else:
self.params["W"+str(self.num_layers)] = weight_scale * torch.randn(final,num_classes,dtype=dtype,device=device)
self.params["b"+str(self.num_layers)] = torch.zeros(num_classes,dtype=dtype,device=device)
################################################################
# END OF YOUR CODE #
################################################################
# With batch normalization we need to keep track of running
# means and variances, so we need to pass a special bn_param
# object to each batch normalization layer. You should pass
# self.bn_params[0] to the forward pass of the first batch
# normalization layer, self.bn_params[1] to the forward
# pass of the second batch normalization layer, etc.
self.bn_params = []
if self.batchnorm:
self.bn_params = [{'mode': 'train'}
for _ in range(len(num_filters))]
# Check that we got the right number of parameters
if not self.batchnorm:
params_per_macro_layer = 2 # weight and bias
else:
params_per_macro_layer = 4 # weight, bias, scale, shift
num_params = params_per_macro_layer * len(num_filters) + 2
msg = 'self.params has the wrong number of ' \
'elements. Got %d; expected %d'
msg = msg % (len(self.params), num_params)
assert len(self.params) == num_params, msg
# Check that all parameters have the correct device and dtype:
for k, param in self.params.items():
msg = 'param "%s" has device %r; should be %r' \
% (k, param.device, device)
assert param.device == torch.device(device), msg
msg = 'param "%s" has dtype %r; should be %r' \
% (k, param.dtype, dtype)
assert param.dtype == dtype, msg
def save(self, path):
checkpoint = {
'reg': self.reg,
'dtype': self.dtype,
'params': self.params,
'num_layers': self.num_layers,
'max_pools': self.max_pools,
'batchnorm': self.batchnorm,
'bn_params': self.bn_params,
}
torch.save(checkpoint, path)
print("Saved in {}".format(path))
def load(self, path, dtype, device):
checkpoint = torch.load(path, map_location='cpu')
self.params = checkpoint['params']
self.dtype = dtype
self.reg = checkpoint['reg']
self.num_layers = checkpoint['num_layers']
self.max_pools = checkpoint['max_pools']
self.batchnorm = checkpoint['batchnorm']
self.bn_params = checkpoint['bn_params']
for p in self.params:
self.params[p] = \
self.params[p].type(dtype).to(device)
for i in range(len(self.bn_params)):
for p in ["running_mean", "running_var"]:
self.bn_params[i][p] = \
self.bn_params[i][p].type(dtype).to(device)
print("load checkpoint file: {}".format(path))
def loss(self, X, y=None):
"""
Evaluate loss and gradient for the deep convolutional
network.
Input / output: Same API as ThreeLayerConvNet.
"""
X = X.to(self.dtype)
mode = 'test' if y is None else 'train'
# Set train/test mode for batchnorm params since they
# behave differently during training and testing.
if self.batchnorm:
for bn_param in self.bn_params:
bn_param['mode'] = mode
scores = None
# pass conv_param to the forward pass for the
# convolutional layer
# Padding and stride chosen to preserve the input
# spatial size
filter_size = 3
conv_param = {'stride': 1, 'pad': (filter_size - 1) // 2}
# pass pool_param to the forward pass for the max-pooling layer
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}
scores = None
#########################################################
# TODO: Implement the forward pass for the DeepConvNet, #
# computing the class scores for X and storing them in #
# the scores variable. #
# #
# You should use the fast versions of convolution and #
# max pooling layers, or the convolutional sandwich #
# layers, to simplify your implementation. #
#########################################################
# Replace "pass" statement with your code
h_out = X
caches = []
max_pool_set = set()
for p in self.max_pools:
max_pool_set.add(p)
# print("initially, out has size", h_out.shape)
for l in range(self.num_layers - 1):
w = self.params['W' + str(l + 1)]
b = self.params['b' + str(l + 1)]
# print('now, h_out has shape', h_out.shape)
if l in max_pool_set:
# it is maxpooling mode, conv-relu-pool
if self.batchnorm:
gamma = self.params['gamma' + str(l + 1)]
beta = self.params['beta' + str(l + 1)]
bn_param = self.bn_params[l]
h_out, cache = Conv_BatchNorm_ReLU_Pool.forward(h_out, w, b, gamma, \
beta, conv_param, bn_param, pool_param)
caches.append(cache)
else:
h_out, cache = Conv_ReLU_Pool.forward(h_out, w, b, conv_param, pool_param)
caches.append(cache)
else:
# no maxpool mode, conv-relu
if self.batchnorm:
gamma = self.params['gamma' + str(l + 1)]
beta = self.params['beta' + str(l + 1)]
bn_param = self.bn_params[l]
h_out, cache = Conv_BatchNorm_ReLU.forward(h_out, w, b, gamma, beta, conv_param, bn_param)
caches.append(cache)
else:
h_out, cache = Conv_ReLU.forward(h_out, w, b, conv_param)
caches.append(cache)
# then it is the last layer
w = self.params['W' + str(self.num_layers)]
b = self.params['b' + str(self.num_layers)]
# print("after operation, out has size", h_out.shape)
# print("number of max pooling", len(self.max_pools))
scores, cache = Linear.forward(h_out, w, b)
caches.append(cache)
#####################################################
# END OF YOUR CODE #
#####################################################
if y is None:
return scores
loss, grads = 0, {}
###################################################################
# TODO: Implement the backward pass for the DeepConvNet, #
# storing the loss and gradients in the loss and grads variables. #
# Compute data loss using softmax, and make sure that grads[k] #
# holds the gradients for self.params[k]. Don't forget to add #
# L2 regularization! #
# #
# NOTE: To ensure that your implementation matches ours and you #
# pass the automated tests, make sure that your L2 regularization #
# does not include a factor of 0.5 #
###################################################################
# Replace "pass" statement with your code
loss,d_out = softmax_loss(scores,y)
for l in range(self.num_layers):
w = self.params["W"+str(l+1)]
loss += self.reg * torch.sum(w**2)
d_out, dw, db = Linear.backward(d_out, caches.pop())
grads['W' + str(self.num_layers)] = dw + 2 * self.reg * self.params['W' + str(self.num_layers)]
grads['b' + str(self.num_layers)] = db
for l in range(0, self.num_layers-1):
# go to index 0
if self.num_layers- 2 - l in max_pool_set:
# it is maxpooling mode, conv-relu-pool
if self.batchnorm:
d_out, dw, db, dgamma, dbeta = Conv_BatchNorm_ReLU_Pool.backward(\
d_out, caches.pop())
grads['W' + str(self.num_layers -1 - l)] = dw + 2 * self.reg * self.params['W' + str(self.num_layers -1 - l)]
grads['b' + str(self.num_layers -1 - l)] = db
grads['gamma' + str(self.num_layers -1 - l)] = dgamma
grads['beta' + str(self.num_layers -1 - l)] = dbeta
else:
d_out, dw, db = Conv_ReLU_Pool.backward(d_out, caches.pop())
grads['W' + str(self.num_layers -1 - l)] = dw + 2 * self.reg * self.params['W' + str(self.num_layers -1 - l)]
grads['b' + str(self.num_layers -1 - l)] = db
else:
# no maxpool mode, conv-relu
if self.batchnorm:
d_out, dw, db, dgamma, dbeta = Conv_BatchNorm_ReLU.backward(\
d_out, caches.pop())
grads['W' + str(self.num_layers -1 - l)] = dw + 2 * self.reg * self.params['W' + str(self.num_layers -1 - l)]
grads['b' + str(self.num_layers -1 - l)] = db
grads['gamma' + str(self.num_layers -1 - l)] = dgamma
grads['beta' + str(self.num_layers -1 - l)] = dbeta
else:
d_out, dw, db = Conv_ReLU.backward(d_out, caches.pop())
grads['W' + str(self.num_layers -1 - l)] = dw + 2 * self.reg * self.params['W' + str(self.num_layers -1 - l)]
grads['b' + str(self.num_layers -1 - l)] = db
#############################################################
# END OF YOUR CODE #
#############################################################
return loss, grads
def find_overfit_parameters():
weight_scale = 2e-3 # Experiment with this!
learning_rate = 1e-5 # Experiment with this!
###########################################################
# TODO: Change weight_scale and learning_rate so your #
# model achieves 100% training accuracy within 30 epochs. #
###########################################################
# Replace "pass" statement with your code
weight_scale = 1e-1
learning_rate = 5e-3
###########################################################
# END OF YOUR CODE #
###########################################################
return weight_scale, learning_rate
def create_convolutional_solver_instance(data_dict, dtype, device):
model = None
solver = None
#########################################################
# TODO: Train the best DeepConvNet that you can on #
# PROPS within 60 seconds. #
#########################################################
# Replace "pass" statement with your code
lr = 2e-3
device = data_dict["X_train"].device
dtype = data_dict["X_train"].dtype
input_dims = data_dict["X_train"].shape[1:]
model = DeepConvNet(input_dims = input_dims,num_classes = 10,num_filters = [8,32,64,128],max_pools=[0,1,2],weight_scale='kaiming',reg=3e-3,dtype=dtype,device=device)
solver = Solver(model,data_dict,print_every=50,num_epochs=5,batch_size=128,update_rule=adam,optim_config={'learning_rate':lr,},device='cuda')
#########################################################
# END OF YOUR CODE #
#########################################################
return solver
def kaiming_initializer(Din, Dout, K=None, relu=True, device='cpu',
dtype=torch.float32):
"""
Implement Kaiming initialization for linear and convolution layers.
Inputs:
- Din, Dout: Integers giving the number of input and output dimensions
for this layer
- K: If K is None, then initialize weights for a linear layer with
Din input dimensions and Dout output dimensions. Otherwise if K is
a nonnegative integer then initialize the weights for a convolution
layer with Din input channels, Dout output channels, and a kernel size
of KxK.
- relu: If ReLU=True, then initialize weights with a gain of 2 to
account for a ReLU nonlinearity (Kaiming initializaiton); otherwise
initialize weights with a gain of 1 (Xavier initialization).
- device, dtype: The device and datatype for the output tensor.
Returns:
- weight: A torch Tensor giving initialized weights for this layer.
For a linear layer it should have shape (Din, Dout); for a
convolution layer it should have shape (Dout, Din, K, K).
"""
gain = 2. if relu else 1.
weight = None
if K is None:
###################################################################
# TODO: Implement Kaiming initialization for linear layer. #
# The weight scale is sqrt(gain / fan_in), #
# where gain is 2 if ReLU is followed by the layer, or 1 if not, #
# and fan_in = num_in_channels (= Din). #
# The output should be a tensor in the designated size, dtype, #
# and device. #
###################################################################
# Replace "pass" statement with your code
weight = torch.randn(Din,Dout,dtype=dtype,device=device)*((gain/Din)**0.5)
###################################################################
# END OF YOUR CODE #
###################################################################
else:
###################################################################
# TODO: Implement Kaiming initialization for convolutional layer. #
# The weight scale is sqrt(gain / fan_in), #
# where gain is 2 if ReLU is followed by the layer, or 1 if not, #
# and fan_in = num_in_channels (= Din) * K * K #
# The output should be a tensor in the designated size, dtype, #
# and device. #
###################################################################
# Replace "pass" statement with your code
weight = torch.randn(Dout,Din,K,K,dtype=dtype,device=device)*((gain/(Din*K*K))**0.5)
###################################################################
# END OF YOUR CODE #
###################################################################
return weight
class BatchNorm(object):
@staticmethod
def forward(x, gamma, beta, bn_param):
"""
Forward pass for batch normalization.
During training the sample mean and (uncorrected) sample variance
are computed from minibatch statistics and used to normalize the
incoming data. During training we also keep an exponentially decaying
running mean of the mean and variance of each feature, and these
averages are used to normalize data at test-time.
At each timestep we update the running averages for mean and
variance using an exponential decay based on the momentum parameter:
running_mean = momentum * running_mean + (1 - momentum) * sample_mean
running_var = momentum * running_var + (1 - momentum) * sample_var
Note that the batch normalization paper suggests a different
test-time behavior: they compute sample mean and variance for
each feature using a large number of training images rather than
using a running average. For this implementation we have chosen to use
running averages instead since they do not require an additional
estimation step; the PyTorch implementation of batch normalization
also uses running averages.
Input:
- x: Data of shape (N, D)
- gamma: Scale parameter of shape (D,)
- beta: Shift paremeter of shape (D,)
- bn_param: Dictionary with the following keys:
- mode: 'train' or 'test'; required
- eps: Constant for numeric stability
- momentum: Constant for running mean / variance.
- running_mean: Array of shape (D,) giving running mean
of features
- running_var Array of shape (D,) giving running variance
of features
Returns a tuple of:
- out: of shape (N, D)
- cache: A tuple of values needed in the backward pass
"""
mode = bn_param['mode']
eps = bn_param.get('eps', 1e-5)
momentum = bn_param.get('momentum', 0.9)
N, D = x.shape
running_mean = bn_param.get('running_mean',
torch.zeros(D,
dtype=x.dtype,
device=x.device))
running_var = bn_param.get('running_var',
torch.zeros(D,
dtype=x.dtype,
device=x.device))
out, cache = None, None
if mode == 'train':
##################################################################
# TODO: Implement the training-time forward pass for batch norm. #
# Use minibatch statistics to compute the mean and variance, use #
# these statistics to normalize the incoming data, and scale and #
# shift the normalized data using gamma and beta. #
# #
# You should store the output in the variable out. #
# Any intermediates that you need for the backward pass should #
# be stored in the cache variable. #
# #
# You should also use your computed sample mean and variance #
# together with the momentum variable to update the running mean #
# and running variance, storing your result in the running_mean #
# and running_var variables. #
# #
# Note that though you should be keeping track of the running #
# variance, you should normalize the data based on the standard #
# deviation (square root of variance) instead! #
# Referencing the original paper #
# (https://arxiv.org/abs/1502.03167) might prove to be helpful. #
##################################################################
# Replace "pass" statement with your code
cache = {"gamma":gamma,"eps":eps,"data":x,"beta":beta,"mode":"train"}
mean = x.mean(dim=0)
var = x.var(dim=0,unbiased=False)
sd = (var + eps)**0.5
x_norm = (x - mean)/sd
out = gamma * x_norm + beta
running_mean = momentum*running_mean + (1-momentum)*mean
running_var = momentum*running_var + (1-momentum)*var
cache["mean"] = mean
cache["var"] = var
cache["x_norm"] = x_norm
cache["std"] = sd
################################################################
# END OF YOUR CODE #
################################################################
elif mode == 'test':
################################################################
# TODO: Implement the test-time forward pass for #
# batch normalization. Use the running mean and variance to #
# normalize the incoming data, then scale and shift the #
# normalized data using gamma and beta. Store the result #
# in the out variable. #
################################################################
# Replace "pass" statement with your code
cache = {"eps":eps, "beta":beta, "gamma":gamma, "data":x, "mode":"test", "running_mean":running_mean, "running_var":running_var}
out = gamma * (x-running_mean) / ((running_var+eps)**0.5) + beta
################################################################
# END OF YOUR CODE #
################################################################
else:
raise ValueError('Invalid forward batchnorm mode "%s"' % mode)
# Store the updated running means back into bn_param
bn_param['running_mean'] = running_mean.detach()
bn_param['running_var'] = running_var.detach()
return out, cache
@staticmethod
def backward(dout, cache):
"""
Backward pass for batch normalization.
For this implementation, you should write out a
computation graph for batch normalization on paper and
propagate gradients backward through intermediate nodes.
Inputs:
- dout: Upstream derivatives, of shape (N, D)
- cache: Variable of intermediates from batchnorm_forward.
Returns a tuple of:
- dx: Gradient with respect to inputs x, of shape (N, D)
- dgamma: Gradient with respect to scale parameter gamma,
of shape (D,)
- dbeta: Gradient with respect to shift parameter beta,
of shape (D,)
"""
dx, dgamma, dbeta = None, None, None
#####################################################################
# TODO: Implement the backward pass for batch normalization. #
# Store the results in the dx, dgamma, and dbeta variables. #
# Referencing the original paper (https://arxiv.org/abs/1502.03167) #
# might prove to be helpful. #
# Don't forget to implement train and test mode separately. #
#####################################################################
# Replace "pass" statement with your code
mode = cache["mode"]
gamma,beta,data,eps = cache["gamma"],cache["beta"],cache["data"],cache["eps"]
N,D = dout.shape
if mode == "train":
total = dout.shape[0]*1.0
var,mean,sd = cache["var"],cache["mean"],cache["std"]
x_norm = cache["x_norm"]
dbeta = 1. * dout.sum(dim=0)
dgamma = torch.sum(dout*x_norm,dim=0)
dx_hat = dout * gamma #N*D
ivar = 1/sd
dxhat_dx_minus_m = ivar * dx_hat
divar = torch.sum(dx_hat*(data-mean),dim=0)
dsqrtvar = divar * (-1/(sd**2))
dvar = 0.5*((var+eps)**(-0.5))*dsqrtvar
dsq = 1 / total * dvar
dx_minus_mean2 = 2 * (data-mean)*dsq
dx_minus_mean = dxhat_dx_minus_m + dx_minus_mean2
dx1 = dx_minus_mean * 1.
d_mean = (-1. * dx_minus_mean).sum(dim=0)
dx2 = 1/total * d_mean
dx = dx1 + dx2
elif mode == "test":
r_mean = cache["running_mean"]
r_var = cache["running_var"]
eps = cache['eps']
dbeta = 1.* dout.sum(dim=0)
dgamma = torch.sum((x-r_mean)/((r_var + eps)**0.5)*dout,dim=0)
print("dgamma",dgamma.shape)
dx = gamma/ ((r_var + eps)**0.5)
else:
raise ValueError('Invalid mode "%s"' % mode)
#################################################################
# END OF YOUR CODE #
#################################################################
return dx, dgamma, dbeta
@staticmethod
def backward_alt(dout, cache):
"""
Alternative backward pass for batch normalization.
For this implementation you should work out the derivatives
for the batch normalizaton backward pass on paper and simplify
as much as possible. You should be able to derive a simple expression
for the backward pass. See the jupyter notebook for more hints.
Note: This implementation should expect to receive the same
cache variable as batchnorm_backward, but might not use all of
the values in the cache.
Inputs / outputs: Same as batchnorm_backward
"""
dx, dgamma, dbeta = None, None, None
###################################################################
# TODO: Implement the backward pass for batch normalization. #
# Store the results in the dx, dgamma, and dbeta variables. #
# #
# After computing the gradient with respect to the centered #
# inputs, you should be able to compute gradients with respect to #
# the inputs in a single statement; our implementation fits on a #
# single 80-character line. #
###################################################################
# Replace "pass" statement with your code
beta,gamma,data,eps = cache["beta"],cache["gamma"],cache["data"],cache["eps"]