-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN MNIST.py
106 lines (83 loc) · 2.94 KB
/
CNN 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
94
95
96
97
98
99
100
101
102
103
104
105
106
#COMPLETED
# time taken on CUDA = 34.012 seconds
# time taken on CPU = 387.68 seconds
# CUDA was 11.39 times faster than CPU
import time
start = time.time()
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from tqdm.notebook import tqdm, trange
class MNIST_CNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(32, 32, kernel_size=3, padding=1)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.fc1 = nn.Linear(7 * 7 * 64, 256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
# conv layer 1
x = self.conv1(x)
x = F.relu(x)
# conv layer 2
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, kernel_size=2)
# conv layer 3
x = self.conv3(x)
x = F.relu(x)
# conv layer 4
x = self.conv4(x)
x = F.relu(x)
x = F.max_pool2d(x, kernel_size=2)
# fc layer 1
x = x.view(-1, 7 * 7 * 64)
x = self.fc1(x)
x = F.relu(x)
# fc layer 2
x = self.fc2(x)
x = F.softmax(x, dim = 1)
return x
# Load the data
mnist_train = datasets.MNIST(root="./datasets", train=True, transform=transforms.ToTensor(), download=True)
mnist_test = datasets.MNIST(root="./datasets", train=False, transform=transforms.ToTensor(), download=True)
train_loader = torch.utils.data.DataLoader(mnist_train, batch_size=100, shuffle=True)
test_loader = torch.utils.data.DataLoader(mnist_test, batch_size=100, shuffle=False)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
## Training
# Instantiate model
model = MNIST_CNN().to(device) # <---- change here
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # <---- change here
# Iterate through train set minibatchs
for epoch in trange(3): # <---- change here
for images, labels in tqdm(train_loader):
images, labels = images.to(device) , labels.to(device)
# Zero out the gradients
optimizer.zero_grad()
# Forward pass
x = images # <---- change here
y = model(x)
loss = criterion(y, labels)
# Backward pass
loss.backward()
optimizer.step()
## Testing
correct = 0
total = len(mnist_test)
with torch.no_grad():
# Iterate through test set minibatchs
for images, labels in tqdm(test_loader):
images, labels = images.to(device), labels.to(device)
# Forward pass
x = images # <---- change here
y = model(x)
predictions = torch.argmax(y, dim=1)
correct += torch.sum((predictions == labels).float())
print('Test accuracy: {}'.format(correct/total))
end = time.time()
print("total time taken: {}".format(end - start))