-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLP_MNIST SGD.py
76 lines (56 loc) · 2.03 KB
/
MLP_MNIST SGD.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
# COMPLETED
# SGD accuracy = 89.99%
# Saturated
import torch
from torchvision import datasets, transforms
import time
begin = time.time()
import torch.nn as nn
from torch.nn import functional as F
from tqdm.notebook import tqdm
from torch.utils.data import DataLoader as dataloader
# Loading Up the data from the dataset
Train_Data = datasets.MNIST(root="./datasets", train=True, transform=transforms.ToTensor(), download=True)
Test_Data = datasets.MNIST(root="./datasets", train=False, transform=transforms.ToTensor(), download=True)
Train_Loader = dataloader(Train_Data, batch_size=200, shuffle=True)
Test_Loader = dataloader(Test_Data, batch_size=200, shuffle=False)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Building the model class
class Net(nn.Module):
def __init__(self):
super().__init__()
self.Layer1 = nn.Linear(784, 500)
self.Layer2 = nn.Linear(500, 10)
def forward(self, x):
x = x.view(-1, 784)
x = F.relu(self.Layer1(x))
x = self.Layer2(x)
return x
#Creating Instance
model = Net().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
loss_fn = nn.CrossEntropyLoss().to(device)
Classifier = nn.LogSoftmax(dim=1)
# TRAINING
for images, labels in tqdm(Train_Loader):
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad() #resetting the gradients for the next batch
y = model(images)
loss = loss_fn(y, labels) #Calculating Loss
loss.backward() #Backpropagation
optimizer.step() #Optimizing
# TESTING
correct = 0
total = len(Test_Data)
with torch.no_grad():
for images, labels in tqdm(Test_Loader):
images = images.to(device)
labels = labels.to(device)
X = images.view(-1, 784)
Y = model.forward(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-begin))