-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_parallel.py
More file actions
148 lines (108 loc) · 4.79 KB
/
train_parallel.py
File metadata and controls
148 lines (108 loc) · 4.79 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from __future__ import print_function
import torch.nn.functional as F
from torch.autograd import Variable
import torch
def parallel_train(loader, model, optimizer, epoch, cuda, log_interval, loss_func, verbose=True):
""" Train the two models in parallel"""
model.train()
global_epoch_loss = 0
if(loss_func=='CrossEntropy'):
criterion = torch.nn.CrossEntropyLoss()
criterion2=torch.nn.CrossEntropyLoss(reduction='sum')
if(loss_func=='NLL'):
criterion = torch.nn.NLLLoss()
criterion2 = torch.nn.NLLLoss(reduction='sum')
for batch_idx, data in enumerate(loader):
data1, data2, target = data[0], data[1], data[2]
if cuda:
data1, data2, target = data1.cuda(), data2.cuda(), target.cuda()
data1, data2, target = Variable(data1), Variable(data2), Variable(target)
optimizer.zero_grad()
output = model(data1, data2)
loss=criterion(output,target)
loss.backward()
optimizer.step()
global_epoch_loss += criterion2(output, target).data
if verbose:
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data1), len(loader.dataset), 100.
* batch_idx / len(loader), loss.data))
global_epoch_loss=global_epoch_loss / len(loader.dataset)
print('\nTrain set: Average loss: {:.4f}\n'.format(
global_epoch_loss))
return global_epoch_loss
def parallel_test(loader, model, cuda,loss_func, verbose=True):
"""Test the model generated by parallel training """
model.eval()
test_loss = 0
correct = 0
prediction = []
actual = []
if(loss_func=='CrossEntropy'):
criterion = torch.nn.CrossEntropyLoss(reduction='sum')
if(loss_func=='NLL'):
criterion = torch.nn.NLLLoss(reduction='sum')
for data1, data2, target in loader:
if cuda:
data1, data2, target = data1.cuda(), data2.cuda(), target.cuda()
data1, data2, target = Variable(data1, volatile=True), Variable(data2, volatile=True), Variable(target)
output = model(data1, data2)
test_loss += criterion(output, target).data # sum up batch loss
pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
prediction.extend([element.item() for element in pred.flatten()])
actual.extend([element.item() for element in target.data.view_as(pred).flatten()])
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
test_loss /= len(loader.dataset)
precision, recall = calculate_precision_recall(prediction, actual)
print("Precision = ",precision)
print("Recall = ", recall)
if verbose:
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(loader.dataset), 100. * correct / len(loader.dataset)))
return test_loss
def parallel_val(loader, model, cuda, loss_func,verbose=True):
"""Validate the model generated by parallel training """
model.eval()
test_loss = 0
correct = 0
if(loss_func=='CrossEntropy'):
criterion = torch.nn.CrossEntropyLoss(reduction='sum')
if(loss_func=='NLL'):
criterion = torch.nn.NLLLoss(reduction='sum')
for data1, data2, target in loader:
if cuda:
data1, target = data1.cuda(), target.cuda()
data1, target = Variable(data1, volatile=True), Variable(target)
output = model(data1, data2)
test_loss += criterion(output, target).data # sum up batch loss
pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
test_loss /= len(loader.dataset)
if verbose:
print('\nValidation set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(loader.dataset), 100. * correct / len(loader.dataset)))
return test_loss
def calculate_precision_recall(prediction, actual):
"""Calculate the precision/recall for the trained model """
# Number of classes = 30
tp = [0] * 30 # True positives
fp = [0] * 30 # True negatives
actual_count = [0] * 30
precision = [0] * 30
recall = [0] * 30
for p,a in zip(prediction,actual):
if p == a:
tp[p] += 1
else:
fp[p]+= 1
for a in actual:
actual_count[a] += 1
print("tp = ", tp)
print("fp = ", fp)
print("actual_count = ", actual_count)
for i in range(30):
if tp[i] != 0 or fp[i] != 0:
precision[i] = tp[i]/(tp[i] + fp[i])
recall[i] = tp[i]/actual_count[i]
return precision, recall