/
cnn.py
129 lines (84 loc) · 2.59 KB
/
cnn.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
epochs = 1
batch_size = 1
learning_rate = 0.0001
train_dataset = dsets.MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = dsets.MNIST(root='./data/',
train=False,
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 10, kernel_size=3,padding=1),
nn.BatchNorm2d(10),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.layer2 = nn.Sequential(
nn.Conv2d(10, 10, kernel_size=3,padding=1),
nn.BatchNorm2d(20),
nn.ReLU(),
)
self.layer3 = nn.Sequential(
nn.Conv2d(10, 20, kernel_size=3,padding=1),
nn.BatchNorm2d(20),
nn.ReLU(),
)
self.layer4 = nn.Sequential(
nn.Conv2d(20, 20, kernel_size=3,padding=1),
nn.BatchNorm2d(20),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.layer5 = nn.Linear(7*7*20, 10) #28-14-7 (2 maxpools)
self.layer6 = nn.Softmax2d()
def forward(self, x):
output1 = self.layer1(x)
output2 = self.layer2(output1)
output3 = self.layer3(output2)
output4 = self.layer4(output3)
output4 = output4.view(output3.size(0), -1)
output5 = self.layer5(output4)
output6 - self.layer6(output5)
return output6
model = CNN()
model.eval()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)
for j in range(epochs):
for i, (images,labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
pred = model(images)
optimizer.zero_grad()
loss = criterion(pred,labels)
loss.backward()
optimizer.step()
if (i % 1000 == 0):
print("ITERATION: " + str(i))
print("LOSS: " + str(loss))
print("TESTING")
c = 0
total = 0
for images, labels in test_loader:
images = Variable(images)
OUT = model(images)
_, pred = torch.max(OUT.data, 1)
total += labels.size(0)
c += (pred == labels).sum()
print(100 * c / total)