-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMNIST_DATA_Model.py
More file actions
75 lines (63 loc) · 2.3 KB
/
Copy pathMNIST_DATA_Model.py
File metadata and controls
75 lines (63 loc) · 2.3 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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from tqdm import tqdm
# 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)
data_train_iter = iter(train_loader)
#images, labels = data_train_iter.next()
images, labels = next(data_train_iter)
print("Shape of the minibatch of images: {}".format(images.shape))
print("Shape of the minibatch of labels: {}".format(labels.shape))
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv2 = nn.Sequential(
nn.Conv2d(1,5, kernel_size=5,stride=3,padding=2),
nn.ReLU(),
#nn.Dropout2d(0.25)
)
self.lin = nn.Sequential(
nn.Linear(500, 10),
)
def forward(self, x):
x = self.conv2(x)
# flatten the output of conv2
x = x.view(x.size(0), -1)
output = self.lin(x)
# Apply softmax to x
#output = F.log_softmax(x, dim=1)
return output
model = CNN()
print(model)
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
# Iterate through train set minibatchs
for images, labels in tqdm(train_loader):
# Zero out the gradients
optimizer.zero_grad()
# Forward pass
#print(images.shape)
#x = images.view(-1,28*28)
y = model(images)
loss = criterion(y, labels)
# Backward pass
loss.backward()
optimizer.step()
correct = 0
total = len(mnist_test)
with torch.no_grad():
# Iterate through test set minibatchs
for images, labels in tqdm(test_loader):
# Forward pass
#x = images.view(-1, 28*28)
y = model(images)
predictions = torch.argmax(y, dim=1)
correct += torch.sum((predictions == labels).float())
print('Test accuracy: {}'.format(correct/total))