-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyTorch_NeuralNetwork.py
More file actions
101 lines (74 loc) · 2.9 KB
/
PyTorch_NeuralNetwork.py
File metadata and controls
101 lines (74 loc) · 2.9 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
94
95
96
97
98
99
100
from __future__ import print_function
import math
import torch
import numpy as np
from torch.autograd import Variable as V
import pandas as pd
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim # Imports the optimization function such as adam , rmsprop ,SGD, Nesterov-SGD
import torch.utils.data
random_seed = 3.14
torch.backends.cudnn.enabled = False
torch.manual_seed(random_seed)
class Net(nn.Module):
def __init__(self):
super(Net , self).__init__()
self.conv1 = nn.Conv2d(1 , 8 , 3)
self.conv2 = nn.Conv2d(8 , 16 , 3)
self.fc1 = nn.Linear(16 * 5 * 5 , 64 )
self.fc2 = nn.Linear(64 , 32)
self.fc3 = nn.Linear(32 , 10)
def forward(self , x):
x = F.max_pool2d(F.relu(self.conv1(x)) , 2)
x = F.max_pool2d(F.relu(self.conv2(x)) , 2)
x = x.view(-1 , self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.log_softmax(self.fc3(x))
return x
def num_flat_features(self , x):
num_features = np.prod(np.array(x.detach().numpy().shape[1:]))
return num_features
net = Net()
net
params = list(net.parameters())
len(params)
list(map(len , params))
# Random Input
input = torch.randn( 1 , 1 , 28 , 28)
out = net(input)
print(out)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters() , lr = .01 , momentum = .5)
# Loading MNIST data
w = pd.read_csv("/home/manpreet/D/Python_Codes/MNIST_Train/train.csv") # Downloaded file location should be changed here
# View some random images
plt.imshow(x_train[ 0 , :].reshape(28 , 28) , cmap = "gray")
plt.imshow(x_train[ 1 , :].reshape(28 , 28) , cmap = "gray")
plt.imshow(x_train[ 8 , :].reshape(28 , 28) , cmap = "gray")
# Random input in batches
batch_size = 256
train = w.values
train_loader = torch.utils.data.DataLoader(train ,
batch_size = batch_size ,
shuffle = True ,
num_workers = 2)
example = enumerate(train_loader)
batch_id , example_data = next(example)
for epoch in range(100):
net.train()
sum_acc = 0
for idx , data in enumerate(train_loader):
x_train = V(data[ : , 1:].reshape(data.size(0) , 1 , 28 , 28)).float()
y_train = V(torch.FloatTensor(data[ : , 0].float())).long()
optimizer.zero_grad()
outputs = net(x_train)
loss = criterion(outputs.exp() , y_train)
acc = sum(np.reshape(np.array(list(map(lambda x : np.where(x == max(x)) , outputs))) ,
data.size(0)) == np.array(y_train))
sum_acc += acc
loss.backward()
optimizer.step()
print(epoch , loss.item() , (sum_acc / w.values.shape[0]))