-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_mnist.py
49 lines (37 loc) · 1.36 KB
/
train_mnist.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
import numpy as np
import mnist_utils
import mnist_model
TRAIN_SIZE = 32
EPOCHS = 1000000
def train():
model = mnist_model.build_model()
model.summary()
for e in range(EPOCHS):
X, y = mnist_utils.get_batch(TRAIN_SIZE, 'train')
pred = model.forward(X)
loss, entropy_loss, l2_loss = mnist_utils.cal_loss(y, pred, model)
print('loss {:.8f} = entropy {:.8f} + l2 {:.8f} | {} samples'
.format(np.mean(loss), np.mean(entropy_loss), np.mean(l2_loss), (e+1) * TRAIN_SIZE))
grad = y - pred
model.backward(grad)
if (e+1) % 20 == 0:
validate(model)
min_loss = 999999
lr_diminished = False
def validate(model):
global min_loss, lr_diminished
loss, accuracy = mnist_model.evaluate_model(model)
print('-'*50)
print('Validate Dataset, loss {:.8f}, acc {:.4f}'.format(loss, accuracy))
if loss < 1.0 and not lr_diminished:
print('\n\n******* Decrease LR to {} *******\n\n'.format(model.get_lr() / 20))
model.set_lr(model.get_lr() / 20)
lr_diminished = True
if min_loss > loss:
weight_file = './weights/weights-{}-{:.6f}-{:.4f}'.format(model.trained_samples, loss, accuracy)
model.save_weights(weight_file)
print('weight saved as {}'.format(weight_file))
min_loss = loss
print('-'*50)
if __name__ == '__main__':
train()