Skip to content
Snippets Groups Projects
Commit 26f52bcc authored by Amy's avatar Amy
Browse files

improved accuracy

parent 672d83e8
No related branches found
No related tags found
No related merge requests found
No preview for this file type
File added
File added
File added
File added
import random import random, sys
evaluating = True
if (len(sys.argv) >= 2 and sys.argv[1].lower() == "train"):
evaluating = False
raw_data = open("TrainingDataBinary.csv").read().split("\n") raw_data = open("TrainingDataBinary.csv").read().split("\n")
...@@ -19,7 +24,11 @@ for line in raw_data: ...@@ -19,7 +24,11 @@ for line in raw_data:
inp.append(elem) inp.append(elem)
inputs.append(inp[:128]) inputs.append(inp[:128])
if (len(inp) >= 129):
labels.append(inp[128]) labels.append(inp[128])
else:
labels.append(inp[0])
print(f"loaded data: {len(inputs)}, {len(labels)}: {inputs[0]}; {labels[0]}") print(f"loaded data: {len(inputs)}, {len(labels)}: {inputs[0]}; {labels[0]}")
...@@ -34,129 +43,72 @@ class Net(nn.Module): ...@@ -34,129 +43,72 @@ class Net(nn.Module):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
# each PMU goes through this (29 data + 3 logs) # each PMU goes through this (29 data + 3 logs)
# self.pmu_proc1 = nn.Linear(32, 48)
# self.pmu_proc2 = nn.Linear(48, 16)
self.pmu_proc1 = nn.Linear(32, 16) self.pmu_proc1 = nn.Linear(32, 16)
self.pmu_proc2 = nn.Linear(16, 8)
# all PMUs together to form an abstract repr. # all PMUs together to form an abstract repr.
self.pmu_pooler1 = nn.Linear(8 * 4, 8) self.pmu_pooler1 = nn.Linear(16 * 4, 16)
self.pmu_pooler2 = nn.Linear(8, 1) self.pmu_pooler2 = nn.Linear(16, 8)
self.pmu_fin = nn.Linear(8, 1)
self.pmu_norm = nn.BatchNorm1d(32, track_running_stats = False) self.pmu_norm = nn.BatchNorm1d(32, track_running_stats = False)
def forward(self, x): def forward(self, x):
global ranges global ranges
# x = self.pool(F.relu(self.conv1(x)))
# x = self.pool(F.relu(self.conv2(x)))
# x = torch.flatten(x, 1) # flatten all dimensions except batch
# x = F.relu(self.fc1(x))
# x = F.relu(self.fc2(x))
# x = self.fc3(x)
pmu_batch = torch.empty(0).cuda() pmu_batch = torch.empty(0).cuda()
pmu_outputs = torch.empty(0).cuda()
# reorder input
for i in range(4): for i in range(4):
# print(x[i * 29: i * 29 + 29], x[116 + i * 3:116 + i * 3 + 3])
pmu_input = torch.cat((x[i * 29: i * 29 + 29], x[116 + i * 3:116 + i * 3 + 3])) pmu_input = torch.cat((x[i * 29: i * 29 + 29], x[116 + i * 3:116 + i * 3 + 3]))
# print(pmu_input, ",")
# for k in range(32):
# pi = pmu_input # shorthand
# if k not in ranges:
# ranges[k] = [pi[k], pi[k]]
# else:
# if (pi[k] < ranges[k][0]):
# ranges[k][0] = pi[k]
# if (pi[k] > ranges[k][1]):
# ranges[k][1] = pi[k]
# for k in [1, 3, 5, 13]:
# pmu_input[k] /= 100000
# for k in [7, 9, 11, 19]:
# pmu_input[k] /= 395
# for k in [0, 6, 12, 18, 20, 22]:
# pmu_input[k] /= 179
# for k in [2, 8]:
# pmu_input[k] /= 30
# for k in [4, 10]:
# pmu_input[k] /= 80
# pmu_input[24] /= 60
# pmu_input[26] /= 100
# for k in [21, 23]:
# pmu_input[k] /= 400
# for k in [14, 16]:
# pmu_input[k] /= 80
# for k in [15, 17]:
# pmu_input[k] /= 10000
# for k in [28]:
# pmu_input[k] /= 2000
# for p in pmu_input:
# if (abs(p) >= 5):
# print(f"pmu {i} input_original:", x[i * 29: i * 29 + 29] + x[116 + i * 3:116 + i * 3 + 3])
# print(f"pmu {i} input {len(pmu_input)}:", pmu_input)
# pmu_batch = torch.cat((pmu_batch, torch.tensor([pmu_input])))
pmu_batch = torch.cat((pmu_batch, pmu_input.unsqueeze(0))) pmu_batch = torch.cat((pmu_batch, pmu_input.unsqueeze(0)))
#print(f"pmu_batch before: {pmu_batch[0][:4]}") # normalize per-channel
pmu_batch = self.pmu_norm(pmu_batch) pmu_batch = self.pmu_norm(pmu_batch) #.flatten()
#print(f"pmu_batch after: {pmu_batch[0][:4]}") # print(pmu_batch)
# print("pmu_batch", pmu_batch)
# print("pmu_batchnorm", pmu_batch) # pmu_outputs = F.relu(self.pmu_proc1(pmu_batch))
pmu_outputs = torch.empty(0).cuda() # pmu_outputs = F.relu(self.pmu_proc2(pmu_outputs))
# pmu_outputs = F.relu(self.pmu_proc3(pmu_outputs))
for i in range(4):
pmu_input = pmu_batch[i]
pmu_out = F.relu(self.pmu_proc1(pmu_input))
#print(f"pmu {i} pmu_out1 {len(pmu_out)}:", pmu_out)
pmu_out = F.relu(self.pmu_proc2(pmu_out))
#print(f"pmu {i} pmu_out2 {len(pmu_out)}:", pmu_out)
pmu_outputs = torch.cat((pmu_outputs, pmu_out)) pmu_outputs = F.relu(self.pmu_proc1(pmu_batch)).flatten()
# for i in range(4):
# pmu_input = pmu_batch[i]
# pmu_out = F.relu(self.pmu_proc1(pmu_input))
# print(f"pmu_outputs {len(pmu_outputs)}:", pmu_outputs) # pmu_outputs = torch.cat((pmu_outputs, pmu_out))
pmu_pool_out1 = F.relu(self.pmu_pooler1(pmu_outputs))
pmu_pool_out2 = self.pmu_pooler2(pmu_pool_out1)
#print(f"data flow:\npmu_outputs: {pmu_outputs}\npmu_pool_out1: {pmu_pool_out1}\npmu_pool_out2: {pmu_pool_out2}") pmu_outputs = F.relu(self.pmu_pooler1(pmu_outputs))
# <0.5 is 0, >=0.5 is 1 pmu_outputs = F.relu(self.pmu_pooler2(pmu_outputs))
pmu_outputs = self.pmu_fin(pmu_outputs)
return F.sigmoid(pmu_pool_out2) return F.sigmoid(pmu_outputs)
net = Net() net = Net()
net.cuda() net.cuda()
print(net) print(net)
print("total params:", sum(p.numel() for p in net.parameters()))
evaluating = True
if (evaluating): if (evaluating):
net.load_state_dict(torch.load("model.pth")) net.load_state_dict(torch.load("model5.pth"))
net.eval() net.eval()
else: else:
net.load_state_dict(torch.load("model5.pth"))
net.train() net.train()
#print(net.state_dict()) #print(net.state_dict())
criterion = nn.BCELoss() criterion = nn.BCELoss()
optimizer = optim.Adam(net.parameters()) optimizer = optim.Adam(net.parameters(), lr=5e-4)
epochs = 10 # 1e-3 196, 2.5e-4 64
epochs = 64
if (evaluating): if (evaluating):
epochs = 1 epochs = 1
...@@ -165,6 +117,8 @@ for epoch in range(epochs): # loop over the dataset multiple times ...@@ -165,6 +117,8 @@ for epoch in range(epochs): # loop over the dataset multiple times
running_loss = 0.0 running_loss = 0.0
indexes = [i for i in range(len(inputs))] indexes = [i for i in range(len(inputs))]
if (not evaluating):
random.shuffle(indexes) random.shuffle(indexes)
correct = 0 correct = 0
...@@ -179,8 +133,12 @@ for epoch in range(epochs): # loop over the dataset multiple times ...@@ -179,8 +133,12 @@ for epoch in range(epochs): # loop over the dataset multiple times
# forward + backward + optimize # forward + backward + optimize
out = net(inp) out = net(inp)
#if (i % 64 == 0):
# print(f"out: {out}, label: {label}") # print(out)
# if (out >= 0.5):
# print("1", end = ",\n")
# else:
# print("0", end = ",\n")
if (out >= 0.5 and label == 1): if (out >= 0.5 and label == 1):
correct += 1 correct += 1
...@@ -210,7 +168,7 @@ for epoch in range(epochs): # loop over the dataset multiple times ...@@ -210,7 +168,7 @@ for epoch in range(epochs): # loop over the dataset multiple times
print(f'correct: {correct}/{totals} ({correct*100/totals:.3f}%)') print(f'correct: {correct}/{totals} ({correct*100/totals:.3f}%)')
if (not evaluating): if (not evaluating):
torch.save(net.state_dict(), "model.pth") torch.save(net.state_dict(), "model6.pth")
# print(ranges) # print(ranges)
......
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment