-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewWeightsModel.py
138 lines (110 loc) · 4.79 KB
/
newWeightsModel.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
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import constants as C
from statistics import mean
class TestModel(nn.Module):
def __init__(self, input_dims, output_dims, lr, num_layers, num_nodes, trainloader, testloader):
super(TestModel, self).__init__()
self.input_dims = input_dims
self.lr = lr
self.num_layers = num_layers
self.output_dims = output_dims
self.num_nodes = num_nodes
self.fcs = None
self.output = None
self.initialise(num_layers, num_nodes)
self.criterion = nn.CrossEntropyLoss()
self.optimizer = optim.Adam(self.parameters(), lr=self.lr)
self.trainloader = trainloader
self.testloader = testloader
def initialise(self, layers, neurons):
nodes = [neurons]*layers
hidden_layers = zip(nodes[:-1], nodes[1:])
self.fcs = nn.ModuleList([nn.Linear(*self.input_dims, neurons)])
self.fcs.extend([nn.Linear(h1, h2) for h1, h2 in hidden_layers])
self.output = nn.Linear(neurons, self.output_dims)
self.num_layers = layers
self.num_nodes = neurons
self.optimizer = optim.Adam(self.parameters(), lr=self.lr)
def reset_optimizer(self):
pass
def forward(self, x):
for layer in self.fcs:
x = layer(x)
x = F.relu(x)
x = self.output(x)
return x
def add_neurons(self, num):
# Getting the older weights of all layers
self.num_nodes += num
self.initialise(self.num_layers, self.num_nodes)
return [self.num_layers, self.num_nodes]
def remove_neurons(self, num):
# Getting the older weights of all layers
fin_neurons = max(self.num_nodes - num,1)
self.num_nodes = fin_neurons
self.initialise(self.num_layers, self.num_nodes)
return [self.num_layers, self.num_nodes]
def add_layers(self, num):
self.num_layers += num
self.initialise(self.num_layers, self.num_nodes)
return [self.num_layers, self.num_nodes]
def remove_layers(self, num):
self.num_layers = max(self.num_layers-num, 1)
return [self.num_layers, self.num_nodes]
def print_param(self):
x = next(self.parameters()).data
print(x)
def train(self):
loss_list, acc_list = [], []
for epochs in range(C.EPOCHS):
correct = 0
total = 0
train_loss = 0
loader = self.trainloader
for data, target in loader: # print("Target = ",target[0].item())
# clear the gradients of all optimized variables
self.optimizer.zero_grad()
# forward pass: compute predicted outputs by passing inputs to the model
output = self.forward(data.float())
#target = target.type(T.FloatTensor)
loss = self.criterion(output, target.long().squeeze())
train_loss += loss.item()*data.size(0)
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# perform a single optimization step (parameter update)
self.optimizer.step()
# update running training loss
total += target.size(0)
# accuracy
_, predicted = T.max(output.data, 1)
correct += (predicted == target.squeeze()).sum().item()
acc_list.append(100*correct/total)
loss_list.append(train_loss/total)
#print("Epoch {} / {}: Accuracy is {}, loss is {}".format(epochs,C.EPOCHS,100*correct/total,train_loss/total))
return acc_list[-1], loss_list[-1]
#return mean(acc_list[-4:]), mean(loss_list[-4:])
def test(self):
correct = 0
total = 0
val_loss = 0
with T.no_grad():
for data, target in self.testloader:
# Predict Output
output = self.forward(data.float())
# Calculate Loss
#target = target.view(-1)
loss = self.criterion(output, target.squeeze())
val_loss += loss.item()*data.size(0)
# Get predictions from the maximum value
_, predicted = T.max(output.data, 1)
# Total number of labels
total += target.size(0)
# Total correct predictions
correct += (predicted == target.squeeze()).sum().item()
# calculate average training loss and accuracy over an epoch
val_loss = val_loss/len(self.testloader.dataset)
accuracy = 100 * correct/float(total)
return accuracy, val_loss