forked from miracleyoo/Poisson-Equation-Solving-with-DL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
170 lines (131 loc) · 5.73 KB
/
train.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# coding: utf-8
# Author: Zhongyang Zhang
# Email : [email protected]
import torch
import torch.nn as nn
import torch.autograd
import numpy as np
import threading
from torch.autograd import Variable
from tqdm import tqdm
lock = threading.Lock()
def save_models(opt, net, epoch, train_loss, best_loss, test_loss):
# Save a temp model
train_loss = float(train_loss)
best_loss = float(best_loss)
test_loss = float(test_loss)
if opt.SAVE_TEMP_MODEL:
net.save(epoch, train_loss / opt.NUM_TRAIN, "temp_model.dat")
# Save the best model
if test_loss / opt.NUM_TEST < best_loss:
best_loss = test_loss / opt.NUM_TEST
net.save(epoch, train_loss / opt.NUM_TRAIN, "best_model.dat")
return best_loss
class MyThread(threading.Thread):
def __init__(self, opt, net, epoch, train_loss, best_loss, test_loss):
threading.Thread.__init__(self)
self.opt = opt
self.net = net
self.epoch = epoch
self.train_loss = train_loss
self.best_loss = best_loss
self.test_loss = test_loss
def run(self):
lock.acquire()
try:
self.best_loss = save_models(self.opt, self.net, self.epoch, self.train_loss, self.best_loss,
self.test_loss)
finally:
lock.release()
def cross_entropy(pred, soft_targets):
logsoftmax = nn.LogSoftmax()
return torch.mean(torch.sum(- soft_targets * logsoftmax(pred), 1))
def vec_similarity(matrix_a, matrix_b):
return (torch.sum(torch.pow(matrix_a - matrix_b, 2))).sqrt()
def vec_dif(matrix_a, matrix_b):
print("Different between preds and labels is:", torch.mean(torch.abs(matrix_a - matrix_b)).data.tolist())
def border_loss(matrix_a, matrix_b, opt):
batch = len(matrix_a)
vec_sim = (torch.sum(torch.pow(matrix_a - matrix_b, 2))).sqrt()
std = Variable(torch.Tensor(np.zeros([batch, opt.LENGTH, opt.WIDTH])))
std[:, 0, :] = 1
std[:, -1, :] = 1
if opt.USE_CUDA:
std = std.cuda()
matrix_a = matrix_a.resize(batch, opt.LENGTH, opt.WIDTH)
matrix_b = matrix_b.resize(batch, opt.LENGTH, opt.WIDTH)
a_border = matrix_a * std
b_border = matrix_b * std
return vec_sim + 2 * (torch.sum(torch.pow(a_border - b_border, 2))).sqrt()
def training(opt, writer, train_loader, test_loader, net, pre_epoch, device, best_loss=100):
best_loss = float(best_loss)
threads = []
# WARNING: input shape: (batch, 9, 41) but output shape: (batch, 41,9)
optimizer = torch.optim.Adam(net.parameters(), lr=opt.LEARNING_RATE)#, weight_decay=opt.WEIGHT_DECAY)
for epoch in range(opt.NUM_EPOCHS):
train_loss = 0
# Start training
net.train()
print('==> Preparing Data ...')
for i, data in tqdm(enumerate(train_loader), desc="Training", total=len(train_loader), leave=False, unit='b'):
inputs, labels, *_ = data
inputs, labels = Variable(inputs.to(device)), Variable(labels.to(device))
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = border_loss(outputs, labels, opt)
# loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# Do statistics for training
train_loss += loss.data[0]
# Start testing
test_loss = testing(opt, test_loader, net, device)
# Add summary to tensorboard
writer.add_scalar("Train/loss", train_loss / opt.NUM_TRAIN, epoch + pre_epoch)
writer.add_scalar("Test/loss", test_loss / opt.NUM_TEST, epoch + pre_epoch)
# Output results
print('Epoch [%d/%d], Train Loss: %.4f Test Loss: %.4f'
% (pre_epoch + epoch + 1, opt.NUM_EPOCHS, train_loss / opt.NUM_TRAIN,
test_loss / opt.NUM_TEST))
vec_dif(outputs, labels)
# Save the model
if epoch > 0:
threads[epoch - 1].join()
best_loss_temp = threads[epoch - 1].best_loss
if best_loss_temp != best_loss:
print("==> Best Model Renewed. Best loss: {}".format(best_loss_temp))
best_loss = best_loss_temp
threads.append(MyThread(opt, net.module, pre_epoch + epoch + 1, train_loss, best_loss, test_loss))
threads[epoch].start()
print('==> Training Finished.')
return net
def testing(opt, test_loader, net, device):
net.eval()
test_loss = 0
for i, data in tqdm(enumerate(test_loader), desc="Testing", total=len(test_loader), leave=False, unit='b'):
inputs, labels, *_ = data
inputs, labels = Variable(inputs.to(device)), Variable(labels.to(device))
# Compute the outputs and judge correct
outputs = net(inputs)
loss = border_loss(outputs, labels, opt)
test_loss += loss.data[0]
return test_loss
def test_all(opt, all_loader, net, results, device):
net.eval()
test_loss = 0
for i, data in tqdm(enumerate(all_loader), desc="Testing", total=len(all_loader), leave=False, unit='b'):
inputs, labels, *_ = data
inputs, labels = Variable(inputs.to(device)), Variable(labels.to(device))
# Compute the outputs and judge correct
outputs = net(inputs)
loss = border_loss(outputs, labels, opt)
test_loss += loss.data[0]
if opt.USE_CUDA:
outputs, labels = outputs.cpu().data.tolist(), labels.cpu().data.tolist()
else:
outputs, labels = outputs.data.tolist(), labels.data.tolist()
results.extend([(label, output) for label, output in zip(labels, outputs)])
out_file = './source/val_results/' + opt.MODEL + '_' + opt.PROCESS_ID + '_results.pkl'
print('==> Testing finished. You can find the result matrix in ' + out_file)
return results