-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_prediction_1.py
264 lines (233 loc) · 10.4 KB
/
mnist_prediction_1.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 6 11:08:03 2019
@author: abishekvaithylingam
"""
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
# Training hyperparameters
epochs = 4
batch_size = 64
#https://machinelearningmastery.com/learning-rate-for-deep-learning-neural-networks/
'''
A neural network learns or approximates a function to best map inputs to outputs from examples in the training dataset.
The learning rate hyperparameter controls the rate or speed at which the model learns. Specifically, it controls the
amount of apportioned error that the weights of the model are updated with each time they are updated, such as at the
end of each batch of training examples. Given a perfectly configured learning rate, the model will learn to best
approximate the function given available resources (the number of layers and the number of nodes per layer) in a given
number of training epochs (passes through the training data).
Generally, a large learning rate allows the model to learn faster, at the cost of arriving on a sub-optimal final set
of weights. A smaller learning rate may allow the model to learn a more optimal or even globally optimal set of
weights but may take significantly longer to train.
At extremes, a learning rate that is too large will result in weight updates that will be too large and the performance
of the model (such as its loss on the training dataset) will oscillate over training epochs. Oscillating performance is
said to be caused by weights that diverge (are divergent). A learning rate that is too small may never converge or may
get stuck on a suboptimal solution.
'''
learning_rate = 0.03 # TODO
momentum = 0.9
#https://d2l.ai/chapter_multilayer-perceptrons/weight-decay.html
'''
'''
weight_decay = 1e-4 #0.001 # TODO
log_interval = 20
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
#kernel_size = 5 defines a convolution filer of 5x5
#out_channels = 16 defines the number of output channels
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16,
kernel_size=5, stride=1, padding=0)
self.maxpool = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32,
kernel_size=3, stride=1, padding=0)
self.fc1 = nn.Linear(in_features=800, out_features=128)
self.fc2 = nn.Linear(in_features=128, out_features=10)
nn.init.kaiming_normal_(self.conv1.weight, nonlinearity='relu')
nn.init.kaiming_normal_(self.conv2.weight, nonlinearity='relu')
nn.init.kaiming_normal_(self.fc1.weight, nonlinearity='relu')
nn.init.kaiming_normal_(self.fc2.weight, nonlinearity='linear')
def forward(self, x):
x = self.conv1(x) #1st convolution Layer
x = F.relu(x) #ReLu
x = self.maxpool(x) #Maxpool 2x2
x = self.conv2(x) #2nd convolution Layer
x = F.relu(x) #ReLu
x = self.maxpool(x) #Maxpool 2x2
x = x.view(-1, 800)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
class CNN2(nn.Module):
def __init__(self):
super(CNN2, self).__init__()
#TO DO : Check Input feature size
self.conv1 = nn.Conv2d(in_channels=1, out_channels=64,
kernel_size=5, stride=1, padding=2)
self.maxpool = nn.MaxPool2d(2)
#TO DO : Check Input feature size
self.conv2 = nn.Conv2d(in_channels=64, out_channels=64,
kernel_size=5, stride=1, padding=2)
self.fc1 = nn.Linear(in_features=1600, out_features=256) #Initial value of out_features=196
self.fc2 = nn.Linear(in_features=256, out_features=10)
nn.init.kaiming_normal_(self.conv1.weight, nonlinearity='relu')
nn.init.kaiming_normal_(self.conv2.weight, nonlinearity='relu')
nn.init.kaiming_normal_(self.fc1.weight, nonlinearity='relu')
nn.init.kaiming_normal_(self.fc2.weight, nonlinearity='linear')
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.maxpool(x)
x = self.conv2(x)
x = F.relu(x)
x = self.maxpool(x)
x = x.view(-1, 3136)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
class CNN3(nn.Module):
def __init__(self):
super(CNN3, self).__init__()
# TODO
def forward(self, x):
# TODO
return F.log_softmax(x, dim=1)
def plot_data(data, label, text):
fig = plt.figure()
for i in range(6):
plt.subplot(2,3,i+1)
plt.tight_layout()
plt.imshow(data[i][0], cmap='gray', interpolation='none')
plt.title(text + ": {}".format(label[i]))
plt.xticks([])
plt.yticks([])
plt.show()
def predict_batch(model, device, test_loader):
examples = enumerate(test_loader)
model.eval()
with torch.no_grad():
batch_idx, (data, target) = next(examples)
data, target = data.to(device), target.to(device)
output = model(data)
pred = output.cpu().data.max(1, keepdim=True)[1] # get the index of the max log-probability
pred = pred.numpy()
return data.cpu().data.numpy(), target.cpu().data.numpy(), pred
def plot_graph(train_x, train_y, test_x, test_y, ylabel=''):
fig = plt.figure()
plt.plot(train_x, train_y, color='blue')
plt.plot(test_x, test_y, color='red')
plt.legend(['Train', 'Test'], loc='upper right')
plt.xlabel('number of training examples seen')
plt.ylabel(ylabel)
plt.grid()
plt.show()
return
def train(model, device, train_loader, optimizer, epoch, losses=[], counter=[], errors=[]):
model.train()
correct=0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
losses.append(loss.item())
counter.append((batch_idx*batch_size) + ((epoch-1)*len(train_loader.dataset)))
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
errors.append(100. * (1 - correct / len(train_loader.dataset)))
def test(model, device, test_loader, losses=[], errors=[]):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
losses.append(test_loss)
errors.append(100. * (1 - correct / len(test_loader.dataset)))
def main():
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
# data transformation
train_data = datasets.MNIST('../data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
test_data = datasets.MNIST('../data', train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
#Data loaders
print("Initialising data loaders")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False, **kwargs)
#extract and plot random samples of data
#examples = enumerate(test_loader)
#batch_idx, (data, target) = next(examples)
#plot_data(data, target, 'Ground truth')
# model creation
print("Creating the model")
model = CNN2().to(device)
# optimizer creation
print("Creating the optimizer")
optimizer = optim.SGD(model.parameters(), lr=0.03, momentum=momentum, weight_decay=1e-4)
# lists for saving history
train_losses = []
train_counter = []
test_losses = []
test_counter = [i*len(train_loader.dataset) for i in range(epochs + 1)]
train_errors = []
test_errors = []
error_counter = [i*len(train_loader.dataset) for i in range(epochs)]
# test of randomly initialized model
print("Testing randomly initialized model")
test(model, device, test_loader, losses=test_losses)
# global training and testing loop
print("Training and testing loops")
for epoch in range(1, epochs + 1):
train(model, device, train_loader, optimizer, epoch, losses=train_losses, counter=train_counter, errors=train_errors)
test(model, device, test_loader, losses=test_losses, errors=test_errors)
# plotting training history
print("Plotting training history")
plot_graph(train_counter, train_losses, test_counter, test_losses, ylabel='negative log likelihood loss')
'''
plt.figure()
plt.plot(train_counter, train_losses, color='blue')
plt.plot(test_counter, test_losses, color='red')
plt.legend(['Train', 'Test'], loc='upper right')
plt.xlabel('Number of training examples seen')
plt.ylabel('Negative log likelihood loss')
plt.grid()
plt.show()
'''
print("Plotting training history")
plot_graph(error_counter, train_errors, error_counter, test_errors, ylabel='error (%)')
# extract and plot random samples of data with predicted labels
data, _, pred = predict_batch(model, device, test_loader)
plot_data(data, pred, 'Predicted')
if __name__ == '__main__':
main()