-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.py
More file actions
93 lines (69 loc) · 2.65 KB
/
NeuralNetwork.py
File metadata and controls
93 lines (69 loc) · 2.65 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Neural Network for MNIST dataset
# This Python file will contain
# DataLoader, Transformations
# MultiLayer Neural Network, Activation Functions
# Loss and Optimizer
# Training Loop (Batch Training)
# Model Evaluation
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
# Device Config
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Hyper parameters
input_size = 28*28 # Flatten the image to 784 X 1
hidden_size = 100 # Hidden Layer Nodes
num_classes = 10 # Number of digits
num_epochs = 2
batch_size = 100
learning_rate = 0.001
# MNIST
train_dataset = torchvision.datasets.MNIST(root='data/MNIST', train=True,
transform=transforms.ToTensor(), download=True)
test_dataset = torchvision.datasets.MNIST(root='data/MNIST', train=False,
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
class NeuralNet(nn.Module):
def __init__(self,input_dim,hidden_dim,num_classes):
super(NeuralNet, self).__init__()
self.l1 = nn.Linear(input_dim,hidden_dim)
self.relu = nn.ReLU()
self.l2 = nn.Linear(hidden_dim,num_classes)
def forward(self,x):
out = self.l1(x)
out = self.relu(out)
out = self.l2(out)
return out
model = NeuralNet(input_size,hidden_size,num_classes)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)
for epoch in range(num_epochs):
model.train()
for i,(img,labels) in enumerate(train_loader):
img = img.reshape(-1,28*28).to(device)
labels = labels.to(device)
# Forward Pass
output = model(img)
loss = criterion(output,labels)
# Backward Pass
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1)%100 == 0:
print(f'epoch {epoch+1}/{num_epochs}, step {i+1}: Loss={loss.item():.4f}')
# test
model.eval()
with torch.no_grad():
n_correct = 0
n_samples = 0
for i,(input,labels) in enumerate(test_loader):
input = input.reshape(-1,28*28).to(device)
labels = labels.to(device)
output = model(input)
_,preds = torch.max(output,1)
n_samples+=labels.shape[0]
n_correct += (preds == labels).sum().item()
print(f'Test Accuracy: {n_correct/n_samples}')