-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment2.py
143 lines (95 loc) · 4.34 KB
/
Assignment2.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from tqdm import tqdm
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
input_size = 784
hidden_size = 500
num_classes = 10
batch_size = 1024
learning_rate = 0.001
train_dataset = torchvision.datasets.MNIST(root='data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = torchvision.datasets.MNIST(root='data',
train=False, download=False,
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=False)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
class NeuralNet(nn.Module):
def __init__(self, aa, num_classes):
super(NeuralNet,self).__init__()
self.conv1 = nn.Conv2d(in_channels=aa, out_channels=32, kernel_size=(3,3), stride=1, padding=1)
self.max1 = nn.MaxPool2d((2,2), stride=(2,2))
self.max2 = nn.MaxPool2d((2,2),stride=(2,2))
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3,3), stride=1, padding=1)
self.act = nn.ReLU()
self.fc1 = nn.Linear(in_features=64*7*7, out_features=512)
self.fc2 = nn.Linear(in_features=512, out_features=256)
self.fc3 = nn.Linear(in_features=256, out_features=128)
self.fc4 = nn.Linear(in_features=128, out_features=num_classes)
def forward(self, x):
out1 = self.conv1(x)
out2 = self.act(out1)
out3 = self.max1(out2)
out4 = self.conv2(out3)
out5 = self.act(out4)
out6 = self.max2(out5)
out7 = torch.flatten(out6, start_dim=1)
out8 = self.fc1(out7)
out9 = self.act(out8)
out10 = self.fc2(out9)
out11 = self.act(out10)
out12 = self.fc3(out11)
out13 = self.act(out12)
out14 = self.fc4(out13)
return out14
# print(out6.shape)
model = NeuralNet(1,10).to(device) # feed-forward---> input, hidden, output.
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)
num_epochs = 20
total_step = len(train_loader)
epoch_accuracy = []
train_accuracy = 0
for epoch in range(num_epochs):
correct = 0
total = 0
for i, (images, labels) in (enumerate(tqdm(train_loader))):
#images = torch.reshape(images, (10,784)).to(device)
labels = labels.to(device)
#print('images: ', images.shape)# to check the input shape and the number of channels
images = images.to(device) #????
outputs = model(images)
loss = criterion(outputs, labels)
optimizer.zero_grad() # initialize the optimizer
loss.backward() # weight calculation
optimizer.step() # weight updates
### train accuracies
out_probs = torch.softmax(outputs, dim=1)
__, predicted = torch.max(out_probs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
if (i+1) % 10 == 0: # ?????
print ('\nEpoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
epoch_accuracy.append(100 * correct / total)
for epoch in range(num_epochs):
print('Accuracy of the network on the train images: {:.2f} %, on epoch {}'.format(epoch_accuracy[epoch], epoch+1))
correct = 0
total = 0
for i, (images, labels) in (enumerate(tqdm(test_loader))):
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
out_probs = torch.softmax(outputs, dim=1) ### convert into range of [0 1]
__, predicted = torch.max(out_probs.data, 1) ## only take the maximum class probability
total += labels.size(0) #total no. of samples in all iterations
correct += (predicted == labels).sum().item() ### total no. of correct predictions in all iterations
print("Test accuracy {:.2f}".format(correct/total * 100))