-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmnist.py
93 lines (73 loc) · 2.8 KB
/
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
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
import numpy as np
import torch
import torchvision
from torch.utils.data import DataLoader
import layers
import loss
import optimizers
from model import Model
def get_dataset(batch_size):
train_loader = DataLoader(
torchvision.datasets.MNIST('./data/', train=True, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
])), shuffle=True, batch_size=batch_size)
test_loader = DataLoader(
torchvision.datasets.MNIST('./data/', train=False, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
])), shuffle=True, batch_size=batch_size)
return train_loader, test_loader
if __name__ == '__main__':
torch.random.manual_seed(1234)
np.random.seed(1234)
epochs = 10
lr = 0.01
batch_size = 32
optimizer = optimizers.SGD(learning_rate=lr)
criterion = loss.CrossEntropy()
layers = [
layers.LinearLayer(784, 512),
layers.ReLU(),
layers.Dropout(keep_rate=0.8),
layers.LinearLayer(512, 512),
layers.ReLU(),
layers.Dropout(keep_rate=0.8),
layers.LinearLayer(512, 10)
]
model = Model(layers, optimizer, criterion)
train_loader, test_loader = get_dataset(batch_size)
for epoch_id in range(epochs):
model.train()
total = 0
correct = 0
for i, (x, y) in enumerate(train_loader):
x = x.numpy().reshape(y.shape[0], -1, 1)
y = y.numpy()
model.optimizer.zero_grad()
loss, pred, logits = model.forward(x, y)
model.backward(y, logits)
correct += np.sum(y == pred.flatten())
total += y.shape[0]
if i % 100 == 0:
print("Loss:", loss.mean())
print("Accuracy (train) epoch {}: {} %".format(epoch_id + 1, correct / total * 100.0))
model.eval()
total = 0
correct = 0
for i, (x, y) in enumerate(test_loader):
x = x.numpy().reshape(y.shape[0], -1, 1)
y = y.numpy()
_, pred, _ = model.forward(x, y)
correct += np.sum(y == pred.flatten())
total += y.shape[0]
print("Accuracy (test) epoch {}: {} %".format(epoch_id + 1, correct / total * 100.0))
total = 0
correct = 0
for i, (x, y) in enumerate(train_loader):
x = x.numpy().reshape(y.shape[0], -1, 1)
y = y.numpy()
_, pred, _ = model.forward(x, y)
correct += np.sum(y == pred.flatten())
total += y.shape[0]
print("Accuracy final (train) epoch {}: {} %".format(epochs, correct / total * 100.0))