-
Notifications
You must be signed in to change notification settings - Fork 1
/
mnist_classifier.py
90 lines (82 loc) · 2.61 KB
/
mnist_classifier.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
import torch
import time
from torch import nn
import torchvision.datasets
from torchvision import transforms
import matplotlib
matplotlib.use('Agg')
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(500, 50)
# self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
# x = x.view(-1, 320)
x = x.view(-1, 500)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
def train(model, num_epochs, lr, train_loader):
solver = torch.optim.Adam(model.parameters(), lr=lr)
loss_op = nn.CrossEntropyLoss()
for i in range(num_epochs):
for x,y in train_loader:
if torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
solver.zero_grad()
a = model(x)
cost = F.nll_loss(a, y)
cost.backward()
solver.step()
print("Epoch:", i, "Cost:", cost)
return model
def test(model, test_loader):
model.eval()
avg_correct = []
costs = []
with torch.no_grad():
for x, y in test_loader:
if torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
a = model(x)
preds = a.max(1, keepdim=True)[1]
correct = preds.eq(y.view_as(preds)).float().mean()
cost = F.nll_loss(a, y)
avg_correct += [correct]
costs += [cost]
avg_correct = float(sum(avg_correct)/len(avg_correct))
avg_cost = float(sum(costs)/len(costs))
return avg_cost, avg_correct
def main():
filepath = "./saved_models/"
filename = "mnist_classifer"
batch_size = 128
img_size = 32
transform = transforms.Compose([
transforms.Resize(img_size),
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
mnist_train = torchvision.datasets.MNIST('./MNIST_data', train=True, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True)
mnist_test = torchvision.datasets.MNIST('./MNIST_data', train=False, download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=True)
net = Net()
if torch.cuda.is_available():
net = net.cuda()
torch.backends.cudnn.benchmark = True
net = train(net, 20, .002, train_loader)
cost, correct = test(net, test_loader)
print("cost", cost, "correct", correct)
torch.save(net.state_dict(), filepath + filename + ".pt")
if __name__ == "__main__":
main()