-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnn_misc.py
395 lines (296 loc) · 17.4 KB
/
dnn_misc.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
"""
Do not change the input and output format.
If our script cannot run your code or the format is improper, your code will not be graded.
The only classes/functions you need to implement in this template is linear_layer, relu, and dropout
"""
import numpy as np
import dnn_im2col
### Modules ###
########################################################################################
# The following three modules (class) are what you need to complete (check TODO) #
########################################################################################
class linear_layer:
"""
The linear (affine/fully-connected) module.
It is built up with two arguments:
- input_D: the dimensionality of the input example/instance of the forward pass
- output_D: the dimensionality of the output example/instance of the forward pass
It has two learnable parameters:
- self.params['W']: the W matrix (numpy array) of shape input_D-by-output_D
- self.params['b']: the b vector (numpy array) of shape 1-by-output_D
It will record the partial derivatives of loss w.r.t. self.params['W'] and self.params['b'] in:
- self.gradient['W']: input_D-by-output_D numpy array
- self.gradient['b']: 1-by-output_D numpy array
"""
def __init__(self, input_D, output_D):
self.params = dict()
self.params['W'] = np.random.normal(0, 0.1, (input_D, output_D))
self.params['b'] = np.random.normal(0, 0.1, (1, output_D))
self.gradient = dict()
self.gradient['W'] = np.zeros((input_D, output_D))
self.gradient['b'] = np.zeros((1, output_D))
def forward(self, X):
"""
The forward pass of the linear (affine/fully-connected) module.
Input:
- X: A N-by-input_D numpy array, where each 'row' is an input example/instance (i.e., X[i], where i = 1,...,N).
The mini-batch size is N.
Operation:
- You are going to generate a N-by-output_D numpy array named forward_output.
- For each row x of X (say X[i]), perform X[i] self.params['W'] + self.params['b'], and store the output in forward_output[i].
- Please use np.XX to call a numpy function XX.
- You are encouraged to use matrix/element-wise operations to avoid using FOR loop.
Return:
- forward_output: A N-by-output_D numpy array, where each 'row' is an output example/instance.
"""
################################################################################
# TODO: Implement the linear forward pass. Store the result in forward_output #
################################################################################
forward_output = np.einsum('ij,jk->ik', X, self.params['W']) + self.params['b']
return forward_output
def backward(self, X, grad):
"""
The backward pass of the linear (affine/fully-connected) module.
Input:
- X: A N-by-input_D numpy array, the input to the forward pass.
- grad: A N-by-output_D numpy array, where each 'row' (say row i) is the partial derivatives of the mini-batch loss
w.r.t. forward_output[i].
Operation:
- Compute the partial derivatives (gradients) of the mini-batch loss w.r.t. self.params['W'], self.params['b'], and X.
- You are going to generate a N-by-input_D numpy array named backward_output.
- Store the partial derivatives (gradients) of the mini-batch loss w.r.t. X in backward_output.
- Store the partial derivatives (gradients) of the mini-batch loss w.r.t. self.params['W'] in self.gradient['W'].
- Store the partial derivatives (gradients) of the mini-batch loss w.r.t. self.params['b'] in self.gradient['b'].
- You are encouraged to use matrix/element-wise operations to avoid using FOR loop.
Return:
- backward_output: A N-by-input_D numpy array, where each 'row' (say row i) is the partial derivatives of the mini-batch loss
w.r.t. X[i].
"""
##########################################################################################################################
# TODO: Implement the backward pass (i.e., compute the following three terms) #
# self.gradient['W'] = ? (input_D-by-output_D numpy array, the gradient of the mini-batch loss w.r.t. self.params['W']) #
# self.gradient['b'] = ? (1-by-output_D numpy array, the gradient of the mini-batch loss w.r.t. self.params['b']) #
# backward_output = ? (N-by-input_D numpy array, the gradient of the mini-batch loss w.r.t. X) #
# only return backward_output, but need to compute self.gradient['W'] and self.gradient['b'] #
##########################################################################################################################
self.gradient['W'] = np.einsum('ij,ik->jk', X, grad) # input_D-by-output_D
self.gradient['b'] = np.sum(grad, axis=0) # 1-by-output_D
backward_output = np.einsum('ij,aj->ia', grad, self.params['W']) # N-by-input_D
return backward_output
class relu:
"""
The relu (rectified linear unit) module.
It is built up with NO arguments.
It has no parameters to learn.
self.mask is an attribute of relu. You can use it to store things (computed in the forward pass) for the use in the backward pass.
"""
def __init__(self):
self.mask = None
def forward(self, X):
"""
The forward pass of the relu (rectified linear unit) module.
Input:
- X: A numpy array of arbitrary shape.
Operation:
- You are to generate a numpy array named forward_output of the same shape of X.
- For each element x of X, perform max{0, x}, and store it in the corresponding element of forward_output.
- Please use np.XX to call a numpy function XX if necessary.
- You are encouraged to use matrix/element-wise operations to avoid using FOR loop.
- You can use self.mask to store what you may need (except X) for the use in the backward pass.
Return:
- forward_output: A numpy array of the same shape of X
"""
################################################################################
# TODO: Implement the relu forward pass. Store the result in forward_output #
################################################################################
forward_output = np.maximum(X, 0)
self.mask = X.clip(0)
self.mask[self.mask > 0] = 1
return forward_output
def backward(self, X, grad):
"""
The backward pass of the relu (rectified linear unit) module.
Input:
- X: A numpy array of arbitrary shape, the input to the forward pass.
- grad: A numpy array of the same shape of X, where each element is the partial derivative of the mini-batch loss
w.r.t. the corresponding element in forward_output.
Operation:
- You are to generate a numpy array named backward_output of the same shape of X.
- Compute the partial derivatives (gradients) of the mini-batch loss w.r.t. X, and store it in backward_output.
- You are encouraged to use matrix/element-wise operations to avoid using FOR loop.
- You can use self.mask.
- PLEASE follow the Heaviside step function defined in CSCI567_HW2.pdf
Return:
- backward_output: A numpy array of the same shape as X, where each element is the partial derivative of the mini-batch loss
w.r.t. the corresponding element in X.
"""
##########################################################################################################################
# TODO: Implement the backward pass (i.e., compute the following term) #
# backward_output = ? (A numpy array of the shape of X, the gradient of the mini-batch loss w.r.t. X) #
# PLEASE follow the Heaviside step function defined in CSCI567_HW2.pdf #
##########################################################################################################################
backward_output = grad * self.mask
return backward_output
class dropout:
"""
The dropout module.
It is built up with one arguments:
- r: the dropout rate
It has no parameters to learn.
self.mask is an attribute of dropout. You can use it to store things (computed in the forward pass) for the use in the backward pass.
"""
def __init__(self, r):
self.r = r
self.mask = None
def forward(self, X, is_train):
"""
The forward pass of the dropout module.
Input:
- X: A numpy array of arbitrary shape.
- is_train: A boolean value. If False, no dropout is performed.
Operation:
- Sample uniformly a value p in [0.0, 1.0) for each element of X
- If p >= self.r, output that element multiplied by (1.0 / (1 - self.r)); otherwise, output 0 for that element
- Please use np.XX to call a numpy function XX if necessary.
- You are encouraged to use matrix/element-wise operations to avoid using FOR loop.
- You can use self.mask to store what you may need (except X) for the use in the backward pass.
Return:
- forward_output: A numpy array of the same shape of X (the output of dropout)
"""
################################################################################
# TODO: We provide the forward pass to you. You only need to understand it. #
################################################################################
if is_train:
self.mask = (np.random.uniform(0.0, 1.0, X.shape) >= self.r).astype(float) * (1.0 / (1.0 - self.r))
else:
self.mask = np.ones(X.shape)
forward_output = np.multiply(X, self.mask)
return forward_output
def backward(self, X, grad):
"""
The backward pass of the dropout module.
Input:
- X: A numpy array of arbitrary shape, the input to the forward pass.
- grad: A numpy array of the same shape of X, where each element is the partial derivative of the mini-batch loss
w.r.t. the corresponding element in forward_output.
Operation:
- You are to generate a numpy array named backward_output of the same shape of X.
- Compute the partial derivatives (gradients) of the mini-batch loss w.r.t. X, and store it in backward_output.
- You are encouraged to use matrix/element-wise operations to avoid using FOR loop.
- You should use self.mask. You should NOT re-sample p.
- Check CSCI567_HW2.pdf for the backward pass of dropout.
Return:
- backward_output: A numpy array of the same shape as X, where each element is the partial derivative of the mini-batch loss
w.r.t. the corresponding element in X.
"""
##########################################################################################################################
# TODO: Implement the backward pass (i.e., compute the following term) #
# backward_output = ? (A numpy array of the shape of X, the gradient of the mini-batch loss w.r.t. X) #
# PLEASE follow the formula shown in the homework pdf #
##########################################################################################################################
backward_output = grad * self.mask
return backward_output
###########################################################################
# Please DO NOT change the following parts of the script #
###########################################################################
class conv_layer:
def __init__(self, num_input, num_output, filter_len, stride):
self.params = dict()
self.params['W'] = np.random.normal(0, 0.1, (num_output, num_input, filter_len, filter_len))
self.params['b'] = np.random.normal(0, 0.1, (num_output, 1))
self.gradient = dict()
self.gradient['W'] = np.zeros((num_output, num_input, filter_len, filter_len))
self.gradient['b'] = np.zeros((num_output, 1))
self.stride = stride
self.padding = int((filter_len - 1) / 2)
self.X_col = None
def forward(self, X):
n_filters, d_filter, h_filter, w_filter = self.params['W'].shape
n_x, d_x, h_x, w_x = X.shape
h_out = int((h_x - h_filter + 2 * self.padding) / self.stride + 1)
w_out = int((w_x - w_filter + 2 * self.padding) / self.stride + 1)
self.X_col = dnn_im2col.im2col_indices(X, h_filter, w_filter, self.padding, self.stride)
W_col = self.params['W'].reshape(n_filters, -1)
out = np.matmul(W_col, self.X_col) + self.params['b']
out = out.reshape(n_filters, h_out, w_out, n_x)
out_forward = out.transpose(3, 0, 1, 2)
return out_forward
def backward(self, X, grad):
n_filters, d_filter, h_filter, w_filter = self.params['W'].shape
self.gradient['b'] = np.sum(grad, axis=(0, 2, 3)).reshape(n_filters, -1)
grad_reshaped = grad.transpose(1, 2, 3, 0).reshape(n_filters, -1)
self.gradient['W'] = np.matmul(grad_reshaped, self.X_col.T).reshape(self.params['W'].shape)
W_reshape = self.params['W'].reshape(n_filters, -1)
out = np.matmul(W_reshape.T, grad_reshaped)
out_backward = dnn_im2col.col2im_indices(out, X.shape, h_filter, w_filter, self.padding, self.stride)
return out_backward
class max_pool:
def __init__(self, max_len, stride):
self.max_len = max_len
self.stride = stride
self.padding = 0 # int((max_len - 1) / 2)
self.argmax_cols = None
def forward(self, X):
n_x, d_x, h_x, w_x = X.shape
h_out = int((h_x - self.max_len + 2 * self.padding) / self.stride + 1)
w_out = int((w_x - self.max_len + 2 * self.padding) / self.stride + 1)
max_cols, self.argmax_cols = dnn_im2col.maxpool_im2col_indices(X, self.max_len, self.max_len, self.padding, self.stride)
out_forward = max_cols.reshape(n_x, d_x, h_out, w_out)
return out_forward
def backward(self, X, grad):
out_backward = dnn_im2col.maxpool_col2im_indices(grad, self.argmax_cols, X.shape, self.max_len, self.max_len, self.padding, self.stride)
return out_backward
class flatten_layer:
def __init__(self):
self.size = None
def forward(self, X):
self.size = X.shape
out_forward = X.reshape(X.shape[0], -1)
return out_forward
def backward(self, X, grad):
out_backward = grad.reshape(self.size)
return out_backward
### Loss functions ###
class softmax_cross_entropy:
def __init__(self):
self.expand_Y = None
self.calib_logit = None
self.sum_exp_calib_logit = None
self.prob = None
def forward(self, X, Y):
self.expand_Y = np.zeros(X.shape).reshape(-1)
self.expand_Y[Y.astype(int).reshape(-1) + np.arange(X.shape[0]) * X.shape[1]] = 1.0
self.expand_Y = self.expand_Y.reshape(X.shape)
self.calib_logit = X - np.amax(X, axis=1, keepdims=True)
self.sum_exp_calib_logit = np.sum(np.exp(self.calib_logit), axis=1, keepdims=True)
self.prob = np.exp(self.calib_logit) / self.sum_exp_calib_logit
forward_output = - np.sum(np.multiply(self.expand_Y, self.calib_logit - np.log(self.sum_exp_calib_logit))) / X.shape[0]
return forward_output
def backward(self, X, Y):
backward_output = - (self.expand_Y - self.prob) / X.shape[0]
return backward_output
class sigmoid_cross_entropy:
def __init__(self):
self.expand_Y = None
self.calib_logit = None
self.sum_exp_calib_logit = None
self.prob = None
def forward(self, X, Y):
self.expand_Y = np.concatenate((Y, 1 - Y), axis=1)
X_cat = np.concatenate((X, np.zeros((X.shape[0], 1))), axis=1)
self.calib_logit = X_cat - np.amax(X_cat, axis=1, keepdims=True)
self.sum_exp_calib_logit = np.sum(np.exp(self.calib_logit), axis=1, keepdims=True)
self.prob = np.exp(self.calib_logit[:, 0].reshape(X.shape[0], -1)) / self.sum_exp_calib_logit
forward_output = - np.sum(np.multiply(self.expand_Y, self.calib_logit - np.log(self.sum_exp_calib_logit))) / X.shape[0]
return forward_output
def backward(self, X, Y):
backward_output = - (self.expand_Y[:, 0].reshape(X.shape[0], -1) - self.prob) / X.shape[0]
return backward_output
### Momentum ###
def add_momentum(model):
momentum = dict()
for module_name, module in model.items():
if hasattr(module, 'params'):
for key, _ in module.params.items():
momentum[module_name + '_' + key] = np.zeros(module.gradient[key].shape)
return momentum