-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_evaluate.py
More file actions
113 lines (89 loc) · 3.38 KB
/
Copy pathtrain_evaluate.py
File metadata and controls
113 lines (89 loc) · 3.38 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
import torch
import numpy as np
import pickle
import random
import copy
from IPython.core.debugger import set_trace
from torch.autograd import Variable
import torch.nn as nn
use_cuda = False
device = torch.device("cuda" if use_cuda else "cpu")
def train(model, training_data, epochs, onepole, four):
data = training_data[:,(0,1,2,3,4)].to(device)
label = training_data[:,5].to(device)
parameters = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters)
for i in range(epochs):
# Train Model
model.train()
output = model(data, onepole, four)
# Loss
criterion = nn.CrossEntropyLoss()
# set_trace()
loss = criterion(output, label)
# Backprop
optimizer.zero_grad()
loss.backward()
optimizer.step()
def evaluate(model, test_data, reverse_test_data, reverse, onepole, four):
label = test_data[:,5].to(device)
input = test_data[:,(0,1,2,3,4)].to(device)
reverse_input = reverse_test_data[:,(0,1,2,3,4)].to(device)
input.requires_grad = False
reverse_input.requires_grad = False
model.eval()
output = model(input, onepole, four)
# Average output with reverse output
if reverse == "true":
reverse_output = model(reverse_input, onepole, four)
output = (output.data + torch.cat([reverse_output.data[:,(2,1,0)], reverse_output.data[:,3:]], 1))/2
# Prediction and Accuracy
_ , prediction = torch.max(output ,1)
return float((label.data == prediction).sum()) / len(label)
def find_LC_query(model, data, onepole, four):
input = data[:,(0,1,2,3,4)].to(device)
input.requires_grad = False
model.eval()
output = torch.zeros((data.shape[0],4))
for i in range(0,20):
output = output + model(input, onepole, four)
max_prediction , prediction = torch.max(output, 1)
least_confident = torch.argmin(max_prediction)
# print(torch.min(max_prediction))
# print("Data Size", data.shape)
return least_confident.item()
def find_EMC_query(model, data, onepole, four):
label = data[:,5].to(device)
input = data[:,(0,1,2,3,4)].to(device)
input.requires_grad = False
# set_trace()
parameters = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters)
model.train()
change = torch.zeros(len(data))
for i in range(0,len(data)):
# set_trace()
output = model(input[i,:].unsqueeze(0), onepole, four)
criterion = nn.CrossEntropyLoss()
# set_trace()
loss = criterion(output, label[i].unsqueeze(0))
optimizer.zero_grad()
loss.backward()
# set_trace()
change[i] = torch.sum(abs(model.linear.weight.grad)) + torch.sum(abs(model.linear0.weight.grad))
# change = change + ((torch.sum(abs(model.linear.weight.grad),0))[0:300] + (torch.sum(abs(model.linear.weight.grad),0))[300:600] + torch.sum(abs(model.linear0.weight.grad),0))
max_change = torch.argmax(change)
# print("max_change: ", torch.max(change))
return max_change.item()
def find_uncertain_synthesis_query(model, data, onepole, four):
input = data.to(device)
input.requires_grad = False
model.eval()
output = torch.zeros((data.shape[0],4))
for i in range(0, 20):
output = output + model(input, onepole, four)
max_prediction , prediction = torch.max(output, 1)
least_confident = torch.argmin(max_prediction)
print(torch.min(max_prediction))
# print("Data Size", data.shape)
return data[least_confident.item()]