-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.py
More file actions
121 lines (102 loc) · 4.2 KB
/
Copy pathNetwork.py
File metadata and controls
121 lines (102 loc) · 4.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
import pickle
class Network(nn.Module):
def __init__(self):
super(Network,self).__init__()
self.fc1 = nn.Linear(64,30)
self.fc2 = nn.Linear(30,16)#20
self.fc3 = nn.Linear(16,10)
self.fc4 = nn.Linear(10,6)
self.fc5 = nn.Linear(6,3)
self.fc6 = nn.Linear(3,1)
self.bestModel = None
def forward(self,x):
x=F.relu(self.fc1(x))
x=F.relu(self.fc2(x))
x=F.relu(self.fc3(x))
x=F.relu(self.fc4(x))
x=F.relu(self.fc5(x))
x=torch.sigmoid(self.fc6(x))
return x
def sampleData(self, x1, x2, batchSize):
rand1 = torch.randperm(x1.shape[0])[0:batchSize]
rand2 = torch.randperm(x2.shape[0])[0:batchSize]
xbatch = torch.cat((x1[rand1,...],x2[rand2,...]),0)
return (xbatch + (torch.rand(xbatch.shape)-0.5)/1.5)
def train(self,x1,x2,x1val,x2val,batchSize,learningRate,momentum,numEpochs):
valComp = torch.cat([torch.zeros([x1val.shape[0],1]),torch.ones([x2val.shape[0],1])],0)
ybatch = torch.cat((torch.zeros([batchSize,1]),torch.ones([batchSize,1])),0)
criterion = nn.MSELoss()
optimizer = optim.Adam(self.parameters(), lr=learningRate)#, momentum=momentum)
losses = []
valLosses = []
accuracies = []
accuraciesVal = []
maxAcc = 0
for i in range(1,numEpochs+1):
avgLoss = 0
avgValLoss = 0
avgAcc = 0
avgAccVal = 0
for j in range(int((x1.shape[0]+x2.shape[0])/batchSize)):
xbatch = self.sampleData(x1,x2,batchSize)
#print(xbatch.shape)
#print(ybatch.shape)
optimizer.zero_grad()
output = self.forward(xbatch)
loss = criterion(output, ybatch)
loss.backward()
optimizer.step()
outRound = torch.round(output)
outCorrect = torch.sum(torch.abs(outRound-ybatch))
acc = 1-(outCorrect/output.shape[0]).item()
avgAcc += acc
valOut = self.forward(torch.cat([x1val,x2val],0))
valLoss = criterion(valOut, valComp)
avgLoss += loss.item()
avgValLoss += valLoss.item()
outValRound = torch.round(valOut)
outValCorrect = torch.sum(torch.abs(outValRound-valComp))
accVal = 1-(outValCorrect/valComp.shape[0]).item()
avgAccVal += accVal
avgLoss /= int((x1.shape[0]+x2.shape[0])/batchSize)
avgValLoss /= int((x1.shape[0]+x2.shape[0])/batchSize)
avgAcc /= int((x1.shape[0]+x2.shape[0])/batchSize)
avgAccVal /= int((x1.shape[0]+x2.shape[0])/batchSize)
if avgAccVal*avgAcc > maxAcc:
maxAcc = avgAccVal*avgAcc
self.bestModel = self
losses.append(avgLoss)
valLosses.append(avgValLoss)
accuracies.append(avgAcc)
accuraciesVal.append(avgAccVal)
if i%10 == 0:
print(i)
print(avgLoss)
print(avgAcc)
print(avgAccVal)
valOut = torch.round(self.bestModel.forward(torch.cat([x1val,x2val],0)))
valHealthy = valOut[:x1val.shape[0]]
valSick = valOut[x1val.shape[0]:]
valHealthyCorrect = valHealthy.shape[0]-torch.sum(valHealthy)
valHealthyIncorrect = torch.sum(valHealthy)
valSickCorrect = torch.sum(valSick)
valSickIncorrect = valSick.shape[0]-torch.sum(valSick)
print("Healthy Correct")
print(valHealthyCorrect)
print("Healthy Incorrect")
print(valHealthyIncorrect)
print("Sick Correct")
print(valSickCorrect)
print("Sick Incorrect")
print(valSickIncorrect)
plt.plot(losses,label = "Training Loss")
plt.plot(valLosses,label = "Validation Loss")
plt.plot(accuracies,label = "Training Accuracy")
plt.plot(accuraciesVal,label = "Validation Accuracy")
plt.legend()
plt.show()