-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_networks.py
488 lines (358 loc) · 18.8 KB
/
neural_networks.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
from utils import softmax_cross_entropy, add_momentum, data_loader_mnist, predict_label, DataSplit
import sys
import os
import argparse
import numpy as np
import json
###################################
# Only modify the TODO blocks #
###################################
# 1. One linear Neural Network layer with forward and backward steps
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.gradient = dict()
###############################################################################################
# TODO: Use np.random.normal() with mean 0 and standard deviation 0.1 to initialize
# - self.params['W']
# - self.params['b']
###############################################################################################
mu = 0 #Mean
sigma = 0.1 #Standard Deviation
self.params['W'] = np.random.normal(mu, sigma, size = [input_D,output_D])
self.params['b'] = np.random.normal(mu, sigma, size = [1,output_D])
###############################################################################################
# TODO: Initialize the following two (gradients) with zeros
# - self.gradient['W']
# - self.gradient['b']
###############################################################################################
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 (N is the batch size)
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.add(np.matmul(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 derivative 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'].
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.matmul(X.transpose(), grad)
self.gradient['b'] = np.array([np.sum(grad, axis = 0)])
backward_output = np.matmul(grad, self.params['W'].transpose())
return backward_output
# 2. ReLU Activation
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.
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)
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.
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
# You can use the mask created in the forward step.
####################################################################################################
backward_output = np.zeros(np.shape(X))
backward_output[X>0] = 1
backward_output = np.multiply(grad, backward_output)
return backward_output
# 3. tanh Activation
class tanh:
def forward(self, X):
"""
Input:
- X: A numpy array of arbitrary shape.
Return:
- forward_output: A numpy array of the same shape of X
"""
################################################################################
# TODO: Implement the tanh forward pass. Store the result in forward_output
# You can use np.tanh()
################################################################################
forward_output = np.tanh(X)
return forward_output
def backward(self, X, grad):
"""
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.
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
# Derivative of tanh(z) is (1 - tanh(z)^2)
####################################################################################################
backward_output = np.multiply((1- (np.tanh(X)**2)), grad)
return backward_output
# 4. Dropout
class dropout:
"""
It is built up with one argument:
- 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):
"""
Input:
- X: A numpy array of arbitrary shape.
- is_train: A boolean value. If False, no dropout should be performed.
Operation:
- Suppose p is uniformly randomly generated from [0,1]. If p >= self.r, output that element multiplied by (1.0 / (1 - self.r)); otherwise, output 0 for that element
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):
"""
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.
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
# You can use the mask created in the forward step
####################################################################################################
backward_output = np.multiply(grad, self.mask)
return backward_output
# 5. Mini-batch Gradient Descent Optimization
def miniBatchGradientDescent(model, momentum, _alpha, _learning_rate):
for module_name, module in model.items():
# check if a module has learnable parameters
if hasattr(module, 'params'):
for key, _ in module.params.items():
# This is the gradient for the parameter named "key" in this module
g = module.gradient[key]
if _alpha <= 0.0:
####################################################################################
# TODO: update the model parameter module.params[key] by a step of gradient descent.
# Note again that the gradient is stored in g already.
####################################################################################
module.params[key] = module.params[key] -(_learning_rate*g)
else:
###################################################################################################
# TODO: Update the model parameter module.params[key] by a step of gradient descent with momentum.
# Access the previous momentum by momentum[module_name + '_' + key], and then update it directly.
###################################################################################################
momentum[module_name+'_'+ key] = _alpha*momentum[module_name+'_'+ key] - (_learning_rate*g)
module.params[key] = module.params[key] + momentum[module_name+'_'+ key]
return model
def main(main_params):
### set the random seed. DO NOT MODIFY. ###
np.random.seed(int(main_params['random_seed']))
### data processing ###
Xtrain, Ytrain, Xval, Yval , _, _ = data_loader_mnist(dataset = main_params['input_file'])
N_train, d = Xtrain.shape
N_val, _ = Xval.shape
index = np.arange(10)
unique, counts = np.unique(Ytrain, return_counts=True)
counts = dict(zip(unique, counts)).values()
trainSet = DataSplit(Xtrain, Ytrain)
valSet = DataSplit(Xval, Yval)
### building/defining MLP ###
"""
In this script, we are going to build a MLP for a 10-class classification problem on MNIST.
The network structure is input --> linear --> relu --> dropout --> linear --> softmax_cross_entropy loss
the hidden_layer size (num_L1) is 1000
the output_layer size (num_L2) is 10
"""
model = dict()
num_L1 = 1000
num_L2 = 10
# experimental setup
num_epoch = int(main_params['num_epoch'])
minibatch_size = int(main_params['minibatch_size'])
# optimization setting
_learning_rate = float(main_params['learning_rate'])
_step = 10
_alpha = float(main_params['alpha'])
_dropout_rate = float(main_params['dropout_rate'])
_activation = main_params['activation']
if _activation == 'relu':
act = relu
else:
act = tanh
# create objects (modules) from the module classes
model['L1'] = linear_layer(input_D = d, output_D = num_L1)
model['nonlinear1'] = act()
model['drop1'] = dropout(r = _dropout_rate)
model['L2'] = linear_layer(input_D = num_L1, output_D = num_L2)
model['loss'] = softmax_cross_entropy()
# Momentum
if _alpha > 0.0:
momentum = add_momentum(model)
else:
momentum = None
train_acc_record = []
val_acc_record = []
train_loss_record = []
val_loss_record = []
### run training and validation ###
for t in range(num_epoch):
print('At epoch ' + str(t + 1))
if (t % _step == 0) and (t != 0):
_learning_rate = _learning_rate * 0.1
idx_order = np.random.permutation(N_train)
train_acc = 0.0
train_loss = 0.0
train_count = 0
val_acc = 0.0
val_count = 0
val_loss = 0.0
for i in range(int(np.floor(N_train / minibatch_size))):
# get a mini-batch of data
x, y = trainSet.get_example(idx_order[i * minibatch_size : (i + 1) * minibatch_size])
### forward pass ###
a1 = model['L1'].forward(x)
h1 = model['nonlinear1'].forward(a1)
d1 = model['drop1'].forward(h1, is_train = True)
a2 = model['L2'].forward(d1)
loss = model['loss'].forward(a2, y)
### backward pass ###
grad_a2 = model['loss'].backward(a2, y)
grad_d1 = model['L2'].backward(d1, grad_a2)
grad_h1 = model['drop1'].backward(h1, grad_d1)
grad_a1 = model['nonlinear1'].backward(a1, grad_h1)
######################################################################################
# TODO: Call the backward methods of every layer in the model in reverse order.
# We have given the first and last backward calls (above and below this TODO block).
######################################################################################
grad_x = model['L1'].backward(x, grad_a1)
### gradient_update ###
model = miniBatchGradientDescent(model, momentum, _alpha, _learning_rate)
### Computing training accuracy and obj ###
for i in range(int(np.floor(N_train / minibatch_size))):
x, y = trainSet.get_example(np.arange(i * minibatch_size, (i + 1) * minibatch_size))
### forward pass ###
a1 = model['L1'].forward(x)
h1 = model['nonlinear1'].forward(a1)
d1 = model['drop1'].forward(h1, is_train = False)
a2 = model['L2'].forward(d1)
loss = model['loss'].forward(a2, y)
train_loss += loss
train_acc += np.sum(predict_label(a2) == y)
train_count += len(y)
train_acc = train_acc / train_count
train_acc_record.append(train_acc)
train_loss_record.append(train_loss)
print('Training loss at epoch ' + str(t + 1) + ' is ' + str(train_loss))
print('Training accuracy at epoch ' + str(t + 1) + ' is ' + str(train_acc))
### Computing validation accuracy ###
for i in range(int(np.floor(N_val / minibatch_size))):
x, y = valSet.get_example(np.arange(i * minibatch_size, (i + 1) * minibatch_size))
### forward pass ###
a1 = model['L1'].forward(x)
h1 = model['nonlinear1'].forward(a1)
d1 = model['drop1'].forward(h1, is_train = False)
a2 = model['L2'].forward(d1)
loss = model['loss'].forward(a2, y)
val_loss += loss
val_acc += np.sum(predict_label(a2) == y)
val_count += len(y)
val_loss_record.append(val_loss)
val_acc = val_acc / val_count
val_acc_record.append(val_acc)
print('Validation accuracy at epoch ' + str(t + 1) + ' is ' + str(val_acc))
# save file
json.dump({'train': train_acc_record, 'val': val_acc_record},
open('MLP_lr' + str(main_params['learning_rate']) +
'_m' + str(main_params['alpha']) +
'_d' + str(main_params['dropout_rate']) +
'_a' + str(main_params['activation']) +
'.json', 'w'))
print('Finish running!')
return train_loss_record, val_loss_record
if __name__ == "__main__":
######################################################################################
# These are the default arguments used to run your code.
# These parameters will be changed while grading.
# You can modify them to test your code (this does not affect the grading as long as
# you remember to run runme.py before submitting).
######################################################################################
parser = argparse.ArgumentParser()
parser.add_argument('--random_seed', default=42)
parser.add_argument('--learning_rate', default=0.01)
parser.add_argument('--alpha', default=0.0)
parser.add_argument('--dropout_rate', default=0.5)
parser.add_argument('--num_epoch', default=10)
parser.add_argument('--minibatch_size', default=5)
parser.add_argument('--activation', default='relu')
parser.add_argument('--input_file', default='mnist_subset.json')
args = parser.parse_args()
main_params = vars(args)
main(main_params)