-
Notifications
You must be signed in to change notification settings - Fork 124
/
layers.py
522 lines (432 loc) · 18.2 KB
/
layers.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
import numpy as np
from nn_components.initializers import he_normal, xavier_normal, standard_normal, he_uniform, xavier_uniform
from nn_components.activations import relu, sigmoid, tanh, softmax, relu_grad, sigmoid_grad, tanh_grad
import copy
initialization_mapping = {"he_normal": he_normal, "xavier_normal": xavier_normal, "std": standard_normal,
"he_uniform": he_uniform, "xavier_uniform": xavier_uniform}
class Layer:
def forward(self, X):
raise NotImplementedError("Child class must implement forward() function")
def backward(self):
raise NotImplementedError("Child class must implement backward() function")
class LearnableLayer:
def forward(self, X):
raise NotImplementedError("Child class must implement forward() function")
def backward_layer(self):
pass
def backward(self):
raise NotImplementedError("Child class must implement backward() function")
def update_params(self, grad):
self.W = self.W - grad
def _split_X(X, filter_size, stride):
"""
Preprocess input X to avoid for-loop.
"""
m, iH, iW, iC = X.shape
fH, fW = filter_size
oH = int((iH - fH)/stride + 1)
oW = int((iW - fW)/stride + 1)
batch_strides, width_strides, height_strides, channel_strides = X.strides
view_shape = (m, oH, oW, fH, fW, iC)
X = np.lib.stride_tricks.as_strided(X, shape=view_shape, strides=(batch_strides, stride*width_strides,
stride*height_strides, width_strides,
height_strides, channel_strides), writeable=False)
return X
class InputLayer(Layer):
def __init__(self, return_dX=False):
self.return_dX = return_dX
self.output = None
def forward(self, X):
self.output = X
return self.output
def backward(self, d_prev, weights_prev):
"""
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
weights_prev: the weights of previous layer according backward direction.
"""
if self.return_dX:
return d_prev.dot(weights_prev.T)
return None
class FCLayer(LearnableLayer):
def __init__(self, num_neurons, weight_init="std"):
"""
The fully connected layer.
Parameters
----------
num_neurons: (integer) number of neurons in the layer.
weight_init: (string) either `he_normal`, `xavier_normal`, `he_uniform`, `xavier_uniform` or standard normal distribution.
"""
assert weight_init in ["std", "he_normal", "xavier_normal", "he_uniform", "xavier_uniform"],\
"Unknow weight initialization type."
self.num_neurons = num_neurons
self.weight_init = weight_init
self.output = None
self.W = None
def forward(self, inputs):
"""
Layer forward level.
Parameters
----------
inputs: inputs of the current layer. This is equivalent to the output of the previous layer.
Returns
-------
output: Output value LINEAR of the current layer.
"""
if self.W is None:
self.W = initialization_mapping[self.weight_init](weight_shape=(inputs.shape[1], self.num_neurons))
self.output = inputs.dot(self.W)
return self.output
def backward_layer(self, d_prev, _):
"""
Compute gradient w.r.t X only.
"""
d_prev = d_prev.dot(self.W.T)
return d_prev
def backward(self, d_prev, prev_layer):
"""
Layer backward level. Compute gradient respect to W and update it.
Also compute gradient respect to X for computing gradient of previous
layers as the forward direction [l-1].
Parameters
----------
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
prev_layer: previous layer according forward direction.
Returns
-------
d_prev: gradient of J respect to A[l] at the current layer.
"""
dW = prev_layer.output.T.dot(d_prev)
d_prev = self.backward_layer(d_prev, None)
return d_prev, dW
class ConvLayer(LearnableLayer):
def __init__(self, filter_size, filters, padding='SAME', stride=1, weight_init="std"):
"""
The convolutional layer.
Parameters
----------
filter_size: a 2-elements tuple (width `fH`, height `fW`) of the filter.
filters: an integer specifies number of filter in the layer.
padding: use padding to keep output dimension = input dimension .
either 'SAME' or 'VALID'.
stride: stride of the filters.
weight_init: (string) either `he_normal`, `xavier_normal`, `he_uniform`, `xavier_uniform` or standard normal distribution.
"""
assert len(filter_size) == 2, "Filter size must be a 2-elements tuple (width, height)."
assert weight_init in ["std", "he_normal", "xavier_normal", "he_uniform", "xavier_uniform"],\
"Unknow weight initialization type."
self.filter_size = filter_size
self.filters = filters
self.padding = padding
self.stride = stride
self.weight_init = weight_init
self.W = None
self.output = None
def _conv_op(self, input_, kernel):
"""
Convolutional operation of 2 slices.
Parameters
----------
input_: Input, shape = (m, oH, oW, fH, fW, in_filters)
kernel: Kernel shape = (fH, fW, in_filters, out_filters)
Returns
-------
Output shape = (m, oH, oW, out_filters)
"""
return np.einsum("bwhijk,ijkl->bwhl", input_, kernel)
def _conv_op_backward(self, input_, d_prev, update_params=True):
"""
Convolutional backward operation.
Parameters
----------
if update_params is true:
input_: Input, shape = (m, oH, oW, fH, fW, in_filters)
else:
input_: Kernel, shape = (fH, fW, in_filters, out_filters)
d_prev: Derivative of previous layer. shape = (m, oH, oW, out_filters)
Returns
-------
if update_params is true:
Derivative with respect to W, shape = (fH, fW, in_filters, out_filters)
else:
Derivative with respect to X, shape = (m, oH, oW, fH, fW, in_filters)
"""
operation = "bwhijk,bwhl->ijkl" if update_params else "ijkl,bwhl->bwhijk"
return np.einsum(operation, input_, d_prev)
def _pad_input(self, inp):
"""
Pad the input when using padding mode 'SAME'.
"""
m, iH, iW, iC = inp.shape
fH, fW = self.filter_size
oH, oW = iH, iW
pH = int(((oH - 1)*self.stride + fH - iH)/2)
pW = int(((oW - 1)*self.stride + fW - iW)/2)
X = np.pad(inp, ((0, 0), (pH, pW), (pH, pW), (0, 0)), 'constant')
return X
def forward(self, X):
"""
Forward propagation of the convolutional layer.
If padding is 'SAME', we must solve this equation to find appropriate number p:
oH = (iH - fH + 2p)/s + 1
oW = (iW - fW + 2p)/s + 1
Parameters
----------
X: the input to this layer. shape = (m, iH, iW, iC)
Returns
-------
Output value of the layer. shape = (m, oH, oW, filters)
"""
assert len(X.shape) == 4, "The shape of input image must be a 4-elements tuple (batch_size, height, width, channel)."
if self.W is None:
self.W = initialization_mapping[self.weight_init](weight_shape=self.filter_size + (X.shape[-1], self.filters))
if self.padding == "SAME":
X = self._pad_input(X)
X = _split_X(X, self.filter_size, self.stride)
self.output = self._conv_op(X, self.W)
return self.output
def backward_layer(self, d_prev, X):
_, iH, iW, _ = X.shape
m, oH, oW, oC = d_prev.shape
fH, fW = self.filter_size
dA = np.zeros(shape=(X.shape))
dA_temp = self._conv_op_backward(self.W, d_prev, update_params=False)
for h in range(oH):
for w in range(oW):
h_step = h*self.stride
w_step = w*self.stride
dA[:, h_step:h_step+fH, w_step:w_step+fW, :] += dA_temp[:, h, w, :, :, :]
if self.padding == "SAME":
offset_h = (iH - oH)//2
offset_w = (iW - oW)//2
dA = dA[:, offset_h:-offset_h, offset_w:-offset_w, :]
return dA
def backward(self, d_prev, prev_layer):
"""
Backward propagation of the convolutional layer.
Parameters
----------
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
prev_layer: previous layer according forward direction.
"""
X = prev_layer.output
if self.padding == "SAME":
X = self._pad_input(X)
dA = self.backward_layer(d_prev, X)
X = _split_X(X, self.filter_size, self.stride)
dW = self._conv_op_backward(X, d_prev, update_params=True)
return dA, dW
class PoolingLayer(Layer):
def __init__(self, filter_size=(2, 2), stride=2, mode="max"):
"""
The pooling layer.
Parameters
----------
filter_size: a 2-elements tuple (width `fH`, height `fW`) of the filter.
stride: strides of the filter.
mode: either average pooling or max pooling.
"""
assert len(filter_size) == 2, "Pooling filter size must be a 2-elements tuple (width, height)."
assert mode in ["max", "avg"], "Mode of pooling is either max pooling or average pooling."
self.filter_size = filter_size
self.stride = stride
self.mode = mode
def _pool_op(self, input_):
"""
Pooling operation, either max pooling or average pooling.
Parameters
----------
input_: tensor to be pooling, shape = (m, oH, oW, fH, fW, iC).
Returns
-------
Output of the pooling layer, shape = (m, oH, oW, iC).
"""
if self.mode == "max":
return np.max(input_, axis=(3, 4))
else:
return np.average(input_, axis=(3, 4))
def _pool_op_backward(self, X, output, d_prev):
"""
Pooling backpropagation operation. We expect to distribute `d_prev` to appropriate place in the `input_`.
Parameters
----------
X: input of the pooling layer. shape = (m, oH, oW, fH, fW, out_filters).
output: output of the pooling layer. shape = (m, oH, oW, out_filters).
d_prev: derivative of the previous layer according backward direction `l+1`. shape = (m, oH, oW, out_filters).
Returns
-------
Derivative of J respect to this pooling layer `l`. The shape out this gradient will equal the shape of prev_layer output
with corresponding pooling type (max or avg).
"""
m, iH, iW, iC = X.shape
X = _split_X(X, self.filter_size, self.stride)
m, oH, oW, _ = output.shape
output = np.reshape(output, newshape=(m, oH, oW, 1, 1, iC))
d_prev = np.reshape(d_prev, newshape=(m, oH, oW, 1, 1, iC))
dA = d_prev*(X == output)
m, oH, oW, fH, fW, _ = dA.shape
dA = dA.transpose(0, 1, 3, 2, 4, 5).reshape((m, oH*fH, oW*fW, iC))
if iH - oH*fH > 0 or iW - oW*fW > 0:
dA = np.pad(dA, ((0, 0), (0, iH - oH*fH), (0, iW - oW*fW), (0, 0)), 'constant')
return dA
def forward(self, X):
"""
Pooling layer forward propagation. Through this layer, the input dimension will reduce:
oH = floor((iH - fH)/stride + 1)
oW = floor((iW - fW)/stride + 1)
Paramters
---------
X: input tensor to this pooling layer. shape=(m, iH, iW, iC)
Returns
-------
Output tensor that has shape = (m, oH, oW, iC)
"""
X = _split_X(X, self.filter_size, self.stride)
self.output = self._pool_op(X)
return self.output
def backward(self, d_prev, prev_layer):
"""
Pooling layer backward propagation.
Parameters
----------
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
prev_layer: previous layer according forward direction `l-1`.
Returns
-------
Gradient of J respect to this pooling layer `l`. The shape out this gradient will equal the shape of prev_layer output
with corresponding pooling type (max or avg).
E.g:
prev_layer output: [[1, 2], then max: [[0, 0], or avg: [[1/4, 2/4],
[3, 4]] [0, 4]] [3/4, 4/4]]
"""
X = prev_layer.output
d_prev = self._pool_op_backward(X, self.output, d_prev)
return d_prev
class FlattenLayer(Layer):
def __init__(self):
pass
def forward(self, X):
"""
Flatten tensor `X` to a vector.
"""
m, iH, iW, iC = X.shape
self.output = np.reshape(X, (m, iH*iW*iC))
return self.output
def backward(self, d_prev, prev_layer):
"""
Reshape d_prev shape to prev_layer output shape in the backpropagation.
"""
m, iH, iW, iC = prev_layer.output.shape
d_prev = np.reshape(d_prev, (m, iH, iW, iC))
return d_prev
class ActivationLayer(Layer):
def __init__(self, activation):
"""
activation: (string) available activation functions. Must be in [sigmoid, tanh,
relu, softmax]. Softmax activation must be at the last layer.
"""
assert activation in ["sigmoid", "tanh", "relu", "softmax"], "Unknown activation function: " + str(activation)
self.activation = activation
def forward(self, X):
"""
Activation layer forward propgation.
"""
self.output = eval(self.activation)(X)
return self.output
def backward(self, d_prev, _):
"""
Activation layer backward propagation.
Parameters
----------
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
Returns
-------
Gradient of J respect to type of activations (sigmoid, tanh, relu) in this layer `l`.
"""
d_prev = d_prev * eval(self.activation + "_grad")(self.output)
return d_prev
class DropoutLayer(Layer):
"""
Refer to the paper:
http://jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf
"""
def __init__(self, keep_prob):
"""
keep_prob: (float) probability to keep neurons in network, use for dropout technique.
"""
assert 0.0 < keep_prob < 1.0, "keep_prob must be in range [0, 1]."
self.keep_prob = keep_prob
def forward(self, X, prediction=False):
"""
Drop neurons random uniformly.
"""
self.mask = np.random.uniform(size=X.shape) < self.keep_prob
self.output = X * self.mask
return self.output
def backward(self, d_prev, _):
"""
Flow gradient of previous layer [l+1] according backward direction through dropout layer.
"""
return d_prev * self.mask
class BatchNormLayer(LearnableLayer):
def __init__(self, momentum=0.99, epsilon=1e-9):
self.momentum = momentum
self.epsilon = epsilon
self.W = None
def forward(self, X, prediction=False):
"""
Compute batch norm forward.
LINEAR -> BATCH NORM -> ACTIVATION.
Returns
-------
Output values of batch normalization.
"""
if self.W is None:
gamma = np.ones(((1,) + X.shape[1:]))
beta = np.zeros(((1,) + X.shape[1:]))
self.W = np.vstack((gamma, beta))
self.moving_average = np.zeros_like(self.W)
if not prediction:
self.mu = np.mean(X, axis=0, keepdims=True)
self.sigma = np.std(X, axis=0, keepdims=True)
self.moving_average[0] = self.momentum*(self.moving_average[0]) + (1-self.momentum)*self.mu
self.moving_average[1] = self.momentum*(self.moving_average[1]) + (1-self.momentum)*self.sigma
else:
self.mu = self.moving_average[0]
self.sigma = self.moving_average[1]
self.Xnorm = (X - self.mu)/np.sqrt(self.sigma + self.epsilon)
self.output = self.W[0]*self.Xnorm + self.W[1]
return self.output
def backward_layer(self, d_prev, prev_layer):
"""
Compute gradient w.r.t X only.
Parameters
----------
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
prev_layer: previous layer according forward direction.
"""
m = prev_layer.output.shape[0]
dXnorm = d_prev * self.W[0]
dSigma = np.sum(dXnorm * (-((prev_layer.output - self.mu)*(self.sigma+self.epsilon)**(-3/2))/2),
axis=0, keepdims=True)
dMu = np.sum(dXnorm*(-1/np.sqrt(self.sigma+self.epsilon)), axis=0, keepdims=True) +\
dSigma*((-2/m)*np.sum(prev_layer.output - self.mu, axis=0, keepdims=True))
d_prev = dXnorm*(1/np.sqrt(self.sigma+self.epsilon)) + dMu/m +\
dSigma*((2/m)*np.sum(prev_layer.output - self.mu, axis=0, keepdims=True))
return d_prev
def backward(self, d_prev, prev_layer):
"""
Compute batch norm backward.
LINEAR <- BATCH NORM <- ACTIVATION.
https://giangtranml.github.io/ml/machine-learning/batch-normalization
Parameters
----------
d_prev: gradient of J respect to A[l+1] of the previous layer according backward direction.
prev_layer: previous layer according forward direction.
Returns
-------
dZ: Gradient w.r.t LINEAR function Z.
"""
gamma_grad = np.sum(d_prev * self.Xnorm, axis=0, keepdims=True)
beta_grad = np.sum(d_prev, axis=0, keepdims=True)
d_prev = self.backward_layer(d_prev, prev_layer)
return d_prev, np.vstack((gamma_grad, beta_grad))