-
Notifications
You must be signed in to change notification settings - Fork 0
/
step53.py
36 lines (28 loc) · 908 Bytes
/
step53.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
if '__file__' in globals():
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import dezero
import dezero.functions as F
from dezero import optimizers
from dezero import DataLoader
from dezero.models import MLP
max_epoch = 3
batch_size = 100
train_set = dezero.datasets.MNIST(train=True)
train_loader = DataLoader(train_set, batch_size)
model = MLP((1000, 10))
optimizer = optimizers.SGD().setup(model)
if os.path.exists('my_mlp.npz'):
model.load_weights('my_mlp.npz')
for epoch in range(max_epoch):
sum_loss = 0
for x, t in train_loader:
y = model(x)
loss = F.softmax_cross_entropy(y, t)
model.cleargrads()
loss.backward()
optimizer.update()
sum_loss += float(loss.data) * len(t)
print('epoch: {}, loss: {:.4f}'.format(
epoch + 1, sum_loss / len(train_set)))
model.save_weights('my_mlp.npz')