-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.py
427 lines (353 loc) · 13.2 KB
/
nn.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
from __future__ import division
from __future__ import print_function
from util import LoadData, Load, Save, DisplayPlot
import sys
import numpy as np
def InitNN(num_inputs, num_hiddens, num_outputs):
"""Initializes NN parameters.
Args:
num_inputs: Number of input units.
num_hiddens: List of two elements, hidden size for each layer.
num_outputs: Number of output units.
Returns:
model: Randomly initialized network weights.
"""
W1 = 0.1 * np.random.randn(num_inputs, num_hiddens[0])
W2 = 0.1 * np.random.randn(num_hiddens[0], num_hiddens[1])
W3 = 0.01 * np.random.randn(num_hiddens[1], num_outputs)
b1 = np.zeros((num_hiddens[0]))
b2 = np.zeros((num_hiddens[1]))
b3 = np.zeros((num_outputs))
model = {
'W1': W1,
'W2': W2,
'W3': W3,
'b1': b1,
'b2': b2,
'b3': b3,
'VW1': 0,
'VW2': 0,
'VW3': 0,
'Vb1': 0,
'Vb2': 0,
'Vb3': 0
}
return model
def Affine(x, w, b):
"""Computes the affine transformation.
Args:
x: Inputs (or hidden layers)
w: Weights
b: Bias
Returns:
y: Outputs
"""
# y = np.dot(w.T, x) + b
y = x.dot(w) + b
return y
def AffineBackward(grad_y, h, w):
"""Computes gradients of affine transformation.
hint: you may need the matrix transpose np.dot(A,B).T = np.dot(B,A) and (A.T).T = A
Args:
grad_y: gradient from last layer
h: inputs from the hidden layer
w: weights
Returns:
grad_h: Gradients wrt. the inputs/hidden layer.
grad_w: Gradients wrt. the weights.
grad_b: Gradients wrt. the biases.
"""
###########################
# Insert your code here.
# print(grad_y.shape, w.transpose().shape)
grad_h = np.matmul(grad_y, w.transpose())
grad_w = np.dot(h.transpose(), grad_y)
grad_b = np.sum(grad_y.T, axis=1)
reshape = grad_b.shape[0]
grad_b = grad_b.reshape((reshape, 1))
return grad_h, grad_w, grad_b
###########################
# raise Exception('Not implemented')
def ReLU(z):
"""Computes the ReLU activation function.
Args:
z: Inputs
Returns:
h: Activation of z
"""
return np.maximum(z, 0.0)
def ReLUBackward(grad_h, z):
"""Computes gradients of the ReLU activation function wrt. the unactivated inputs.
Returns:
grad_z: Gradients wrt. the hidden state prior to activation.
"""
###########################
# Insert your code here.
reluVals = ReLU(z)
dz_Relu = ReLUDerivative(reluVals)
grad_z = dz_Relu * grad_h
return grad_z
###########################
# raise Exception('Not implemented')
def ReLUDerivative(y):
M, N = y.shape
hold = np.zeros((M, N))
for i in range(M):
for j in range(N):
if y[i][j] > 0:
hold[i][j] = 1
return hold
def Softmax(x):
"""Computes the softmax activation function.
Args:
x: Inputs
Returns:
y: Activation
"""
return np.exp(x) / np.exp(x).sum(axis=1, keepdims=True)
def NNForward(model, x):
"""Runs the forward pass.
Args:
model: Dictionary of all the weights.
x: Input to the network.
Returns:
var: Dictionary of all intermediate variables.
"""
z1 = Affine(x, model['W1'], model['b1'])
h1 = ReLU(z1)
z2 = Affine(h1, model['W2'], model['b2'])
h2 = ReLU(z2)
y = Affine(h2, model['W3'], model['b3'])
var = {
'x': x,
'z1': z1,
'h1': h1,
'z2': z2,
'h2': h2,
'y': y
}
return var
def NNBackward(model, err, var):
"""Runs the backward pass.
Args:
model: Dictionary of all the weights.
err: Gradients to the output of the network.
var: Intermediate variables from the forward pass.
"""
dE_dh2, dE_dW3, dE_db3 = AffineBackward(err, var['h2'], model['W3'])
dE_dz2 = ReLUBackward(dE_dh2, var['z2'])
dE_dh1, dE_dW2, dE_db2 = AffineBackward(dE_dz2, var['h1'], model['W2'])
dE_dz1 = ReLUBackward(dE_dh1, var['z1'])
_, dE_dW1, dE_db1 = AffineBackward(dE_dz1, var['x'], model['W1'])
model['dE_dW1'] = dE_dW1
model['dE_dW2'] = dE_dW2
model['dE_dW3'] = dE_dW3
model['dE_db1'] = dE_db1.flatten()
model['dE_db2'] = dE_db2.flatten()
model['dE_db3'] = dE_db3.flatten()
def NNUpdate(model, eps, momentum):
"""Update NN weights.
Args:
model: Dictionary of all the weights.
eps: Learning rate.
momentum: Momentum.
"""
###########################
# Insert your code here.
# Update the weights.
model['VW1'] = momentum * model['VW1'] + (1 - momentum) * model['dE_dW1']
model['W1'] = model['W1'] - model['VW1'] * eps
model['VW2'] = momentum * model['VW2'] + (1 - momentum) * model['dE_dW2']
model['W2'] = model['W2'] - model['VW2'] * eps
model['VW3'] = momentum * model['VW3'] + (1 - momentum) * model['dE_dW3']
model['W3'] = model['W3'] - model['VW3'] * eps
model['Vb1'] = momentum * model['Vb1'] + (1 - momentum) * model['dE_db1']
model['b1'] = model['b1'] - model['Vb1'] * eps
model['Vb2'] = momentum * model['Vb2'] + (1 - momentum) * model['dE_db2']
model['b2'] = model['b2'] - model['Vb2'] * eps
model['Vb3'] = momentum * model['Vb3'] + (1 - momentum) * model['dE_db3']
model['b3'] = model['b3'] - model['Vb3'] * eps
#
# model['b1'] = model['b1'] - model['dE_db1'].flatten() * eps
# print(model['dE_db1'].flatten().shape)
# print(model['b1'].shape)
# print(model['dE_db1'].shape)
# print(model['b1'].shape)
# model['W1'] = model['W1'] - model['dE_dW1'] * eps
# model['W2'] = model['W2'] - model['dE_dW2'] * eps
# model['W3'] = model['W3'] - model['dE_dW3'] * eps
# model['b1'] = model['b1'] - model['dE_db1'] * eps
# model['b2'] = model['b2'] - model['dE_db2'] * eps
# model['b3'] = model['b3'] - model['dE_db3'] * eps
###########################
# raise Exception('Not implemented')
def Train(model, forward, backward, update, eps, momentum, num_epochs,
batch_size):
"""Trains a simple MLP.
Args:
model: Dictionary of model weights.
forward: Forward prop function.
backward: Backward prop function.
update: Update weights function.
eps: Learning rate.
momentum: Momentum.
num_epochs: Number of epochs to run training for.
batch_size: Mini-batch size, -1 for full batch.
Returns:
stats: Dictionary of training statistics.
- train_ce: Training cross entropy.
- valid_ce: Validation cross entropy.
- train_acc: Training accuracy.
- valid_acc: Validation accuracy.
"""
inputs_train, inputs_valid, inputs_test, target_train, target_valid, \
target_test = LoadData('./toronto_face.npz')
rnd_idx = np.arange(inputs_train.shape[0])
train_ce_list = []
valid_ce_list = []
train_acc_list = []
valid_acc_list = []
num_train_cases = inputs_train.shape[0]
if batch_size == -1:
batch_size = num_train_cases
num_steps = int(np.ceil(num_train_cases / batch_size))
for epoch in range(num_epochs):
np.random.shuffle(rnd_idx)
inputs_train = inputs_train[rnd_idx]
target_train = target_train[rnd_idx]
print(inputs_train.shape, target_train.shape)
for step in range(num_steps):
# Forward prop.
start = step * batch_size
end = min(num_train_cases, (step + 1) * batch_size)
x = inputs_train[start: end]
t = target_train[start: end]
var = forward(model, x)
prediction = Softmax(var['y'])
train_ce = -np.sum(t * np.log(prediction)) / x.shape[0]
train_acc = (np.argmax(prediction, axis=1) ==
np.argmax(t, axis=1)).astype('float').mean()
print('Epoch {:3d} Step {:2d} Train CE {:.5f} Train Acc {:.5f}'.format(
epoch, step, train_ce, train_acc))
# Compute error.
error = (prediction - t) / x.shape[0]
# Backward prop.
backward(model, error, var)
# Update weights.
update(model, eps, momentum)
train_ce, train_acc = Evaluate(inputs_train, target_train, model, forward, batch_size=batch_size)
valid_ce, valid_acc = Evaluate(inputs_valid, target_valid, model, forward, batch_size=batch_size)
print('Epoch {:3d} Validation CE {:.5f} Validation Acc {:.5f}\n'.format(epoch, valid_ce, valid_acc))
train_ce_list.append((epoch, train_ce))
train_acc_list.append((epoch, train_acc))
valid_ce_list.append((epoch, valid_ce))
valid_acc_list.append((epoch, valid_acc))
DisplayPlot(train_ce_list, valid_ce_list, 'Cross Entropy', number=0)
DisplayPlot(train_acc_list, valid_acc_list, 'Accuracy', number=1)
print()
train_ce, train_acc = Evaluate(
inputs_train, target_train, model, forward, batch_size=batch_size)
valid_ce, valid_acc = Evaluate(
inputs_valid, target_valid, model, forward, batch_size=batch_size)
test_ce, test_acc = Evaluate(
inputs_test, target_test, model, forward, batch_size=batch_size)
print('CE: Train %.5f Validation %.5f Test %.5f' %
(train_ce, valid_ce, test_ce))
print('Acc: Train {:.5f} Validation {:.5f} Test {:.5f}'.format(
train_acc, valid_acc, test_acc))
stats = {
'train_ce': train_ce_list,
'valid_ce': valid_ce_list,
'train_acc': train_acc_list,
'valid_acc': valid_acc_list
}
return model, stats
def Evaluate(inputs, target, model, forward, batch_size=-1):
"""Evaluates the model on inputs and target.
Args:
inputs: Inputs to the network.
target: Target of the inputs.
model: Dictionary of network weights.
"""
num_cases = inputs.shape[0]
if batch_size == -1:
batch_size = num_cases
num_steps = int(np.ceil(num_cases / batch_size))
ce = 0.0
acc = 0.0
for step in range(num_steps):
start = step * batch_size
end = min(num_cases, (step + 1) * batch_size)
x = inputs[start: end]
t = target[start: end]
prediction = Softmax(forward(model, x)['y'])
ce += -np.sum(t * np.log(prediction))
acc += (np.argmax(prediction, axis=1) == np.argmax(
t, axis=1)).astype('float').sum()
ce /= num_cases
acc /= num_cases
return ce, acc
def CheckGrad(model, forward, backward, name, x):
"""Check the gradients
Args:
model: Dictionary of network weights.
name: Weights name to check.
x: Fake input.
"""
np.random.seed(0)
var = forward(model, x)
loss = lambda y: 0.5 * (y ** 2).sum()
grad_y = var['y']
backward(model, grad_y, var)
grad_w = model['dE_d' + name].ravel()
w_ = model[name].ravel()
eps = 1e-7
grad_w_2 = np.zeros(w_.shape)
check_elem = np.arange(w_.size)
np.random.shuffle(check_elem)
# Randomly check 20 elements.
check_elem = check_elem[:20]
for ii in check_elem:
w_[ii] += eps
err_plus = loss(forward(model, x)['y'])
w_[ii] -= 2 * eps
err_minus = loss(forward(model, x)['y'])
w_[ii] += eps
grad_w_2[ii] = (err_plus - err_minus) / 2 / eps
np.testing.assert_almost_equal(grad_w[check_elem], grad_w_2[check_elem],
decimal=3)
def main():
"""Trains a NN."""
model_fname = 'nn_model.npz'
stats_fname = 'nn_stats.npz'
# Hyper-parameters. Modify them if needed.
num_hiddens = [16, 32]
eps = 0.01
momentum = 0.0
num_epochs = 1000
batch_size = 1
# Input-output dimensions.
num_inputs = 2304
num_outputs = 7
# Initialize model.
model = InitNN(num_inputs, num_hiddens, num_outputs)
# Uncomment to reload trained model here.
# model = Load(model_fname)
# Check gradient implementation.
print('Checking gradients...')
x = np.random.rand(10, 48 * 48) * 0.1
CheckGrad(model, NNForward, NNBackward, 'W3', x)
CheckGrad(model, NNForward, NNBackward, 'b3', x)
CheckGrad(model, NNForward, NNBackward, 'W2', x)
CheckGrad(model, NNForward, NNBackward, 'b2', x)
CheckGrad(model, NNForward, NNBackward, 'W1', x)
CheckGrad(model, NNForward, NNBackward, 'b1', x)
# Train model.
stats = Train(model, NNForward, NNBackward, NNUpdate, eps,
momentum, num_epochs, batch_size)
# Uncomment if you wish to save the model.
# Save(model_fname, model)
# Uncomment if you wish to save the training statistics.
# Save(stats_fname, stats)
if __name__ == '__main__':
main()