-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtorch-convnet.py
More file actions
81 lines (63 loc) · 2.5 KB
/
torch-convnet.py
File metadata and controls
81 lines (63 loc) · 2.5 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import time
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
trainData = torchvision.datasets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
testData = torchvision.datasets.MNIST(root='./data',
train=False,
transform=transforms.ToTensor())
trainLoader = torch.utils.data.DataLoader(dataset=trainData,
batch_size=256,
shuffle=True)
testLoader = torch.utils.data.DataLoader(dataset=testData,
batch_size=256,
shuffle=False)
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(nn.Conv2d(1, 20, kernel_size=5),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(20, 50, kernel_size=5),
nn.MaxPool2d(2),
nn.ReLU())
self.layer2 = nn.Sequential(nn.Linear(800, 500),
nn.ReLU(),
nn.Linear(500, 10),
nn.LogSoftmax(dim=1))
def forward(self, x):
h = self.layer1(x)
o = self.layer2(h.reshape(-1, 800))
return o
model = ConvNet().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
def train():
for epoch in range(1, 11):
for i, (x, y) in enumerate(trainLoader):
(x, y) = x.to(device), y.to(device)
o = model(x)
loss = F.nll_loss(o, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % 100 == 0:
print("Epoch: {}\tLoss: {}".format(epoch, loss.item()))
tic = time.time()
train()
print(time.time()-tic, "s")
n, N = 0, 0
with torch.no_grad():
for (x, y) in testLoader:
(x, y) = x.to(device), y.to(device)
o = model(x)
_, ŷ = torch.max(o, 1)
N += y.size(0)
n += torch.sum(ŷ == y).item()
print("Accuracy: {}".format(n/N))