-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·251 lines (205 loc) · 10.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import argparse
import time
import numpy as np
from Layers import *
from tqdm import tqdm
import torch.optim as optim
def cal_performance(a , b1,b2,b3):
# loss_15min = torch.sqrt(nn.MSELoss()(a[0],b1.unsqueeze(1)))
# loss_60min = torch.sqrt(nn.MSELoss()(a[1],b2.unsqueeze(1)))
# loss_6hour = torch.sqrt(nn.MSELoss()(a[2],b3.unsqueeze(1)))
loss_15min = nn.MSELoss()(a[0],b1.unsqueeze(1))
loss_60min = nn.MSELoss()(a[1],b2.unsqueeze(1))
loss_6hour = nn.MSELoss()(a[2],b3.unsqueeze(1))
loss = loss_15min + loss_60min + loss_6hour
acc_num_15min = get_acc_num(a[0],b1.unsqueeze(1),0.25) #get number of samples under 25% error.
acc_num_60min = get_acc_num(a[1],b2.unsqueeze(1),0.25)
acc_num_6hour = get_acc_num(a[2],b3.unsqueeze(1),0.25)
return loss, loss_15min, loss_60min, loss_6hour, acc_num_15min, acc_num_60min, acc_num_6hour
def get_acc_num(pred, target, percent):
p=pred.cpu().data.numpy()
t=target.cpu().data.numpy()
return (np.absolute(p-t)/t).mean()
def train_epoch(model, training_data, optimizer, device, batch_size):
''' Epoch operation in training phase'''
model.train()
total_loss=total_loss_15min=total_loss_60min=total_loss_6hour = 0
total_acc_15min=total_acc_60min=total_acc_6hour = 0
count = 0
for batch in tqdm(
training_data, mininterval=2,
desc=' - (Training) ', leave=False):
# prepare data
src_seq,mask, t1,t2,t3 = map(lambda x: x.float().to(device), batch)
b_size=src_seq.shape[0]
# forward
optimizer.zero_grad()
pred = model(src_seq,mask=mask.byte())
# backward
loss, loss_15min, loss_60min, loss_6hour, acc_num_15min, \
acc_num_60min, acc_num_6hour = cal_performance(pred, t1,t2,t3)
loss_15min.backward(retain_graph=True)
loss_60min.backward(retain_graph=True)
loss_6hour.backward()
# update parameters
optimizer.step()
# note keeping
total_loss += loss.item()
total_loss_15min += loss_15min.item()
total_loss_60min += loss_60min.item()
total_loss_6hour += loss_6hour.item()
total_acc_15min += acc_num_15min
total_acc_60min += acc_num_60min
total_acc_6hour += acc_num_6hour
count += 1
loss, loss_15min, loss_60min, loss_6hour = np.sqrt(total_loss/count), \
np.sqrt(total_loss_15min/count), np.sqrt(total_loss_60min/count), np.sqrt(total_loss_6hour/count)
acc_15min, acc_60min, acc_6hour = 100*total_acc_15min/(count), \
100*total_acc_60min/(count), 100*total_acc_6hour/(count)
return loss, loss_15min, loss_60min, loss_6hour, acc_15min, acc_60min, acc_6hour
def eval_epoch(model,val_data, optimizer, device, batch_size):
''' Epoch operation in val phase'''
model.eval()
total_loss=total_loss_15min=total_loss_60min=total_loss_6hour = 0
total_acc_15min=total_acc_60min=total_acc_6hour = 0
count = 0
for batch in tqdm(
val_data, mininterval=2,
desc=' - (Validation) ', leave=False):
# prepare data
src_seq,mask, t1,t2,t3 = map(lambda x: x.float().to(device), batch)
# forward
optimizer.zero_grad()
pred = model(src_seq,mask=mask.byte())
# backward
loss, loss_15min, loss_60min, loss_6hour, acc_num_15min, \
acc_num_60min, acc_num_6hour = cal_performance(pred, t1,t2,t3)
# note keeping
total_loss += loss.item()
total_loss_15min += loss_15min.item()
total_loss_60min += loss_60min.item()
total_loss_6hour += loss_6hour.item()
total_acc_15min += acc_num_15min
total_acc_60min += acc_num_60min
total_acc_6hour += acc_num_6hour
count += 1
loss, loss_15min, loss_60min, loss_6hour = np.sqrt(total_loss/count), \
np.sqrt(total_loss_15min/count), np.sqrt(total_loss_60min/count), np.sqrt(total_loss_6hour/count)
acc_15min, acc_60min, acc_6hour = 100*total_acc_15min/(count), \
100*total_acc_60min/(count), 100*total_acc_6hour/(count)
return loss, loss_15min, loss_60min, loss_6hour, acc_15min, acc_60min, acc_6hour
def train(model, training_data, validation_data, optimizer, device, opt):
log_train_file = opt.logdir + '/train.log'
log_valid_file = opt.logdir + '/valid.log'
valid_accus = []
rmses_15min = []
for epoch_i in range(opt.epoch):
print('[ Epoch', epoch_i, ']')
start = time.time()
loss, loss_15min, loss_60min, loss_6hour, acc_15min, acc_60min, acc_6hour =\
train_epoch(model, training_data, optimizer, device, opt.batch_size)
print(' - (Training) loss: {loss: 8.5f}, rmse_15min:{loss_15min:8.5f}, '
'rmse_60min:{loss_60min:8.5f}, rmse_6hour:{loss_6hour:8.5f}, \n\t\t elapse: {elapse:3.1f} min,'\
' acc_15min: {acc_15min:3.3f} %, acc_60min: {acc_60min:3.3f} %, acc_6hour: {acc_6hour:3.3f} %,'.format(
loss=loss, loss_15min=loss_15min, loss_60min=loss_60min, loss_6hour=loss_6hour,
acc_15min=acc_15min, acc_60min=acc_60min, acc_6hour=acc_6hour,
elapse=(time.time()-start)/60))
start = time.time()
vloss, vloss_15min, vloss_60min, vloss_6hour, vacc_15min, vacc_60min, vacc_6hour =\
eval_epoch(model, validation_data,optimizer, device, opt.batch_size)
print(' - (Validation) loss: {loss: 8.5f}, rmse_15min:{loss_15min:8.5f}, '
'rmse_60min:{loss_60min:8.5f}, rmse_6hour:{loss_6hour:8.5f}, \n\t\t elapse: {elapse:3.1f} min,'\
' acc_15min: {acc_15min:3.3f} %, acc_60min: {acc_60min:3.3f} %, acc_6hour: {acc_6hour:3.3f} %,'.format(
loss=vloss, loss_15min=vloss_15min, loss_60min=vloss_60min, loss_6hour=vloss_6hour,
acc_15min=vacc_15min, acc_60min=vacc_60min, acc_6hour=vacc_6hour,
elapse=(time.time()-start)/60))
valid_accus += [vloss]
rmses_15min += [vloss_15min]
if vloss_15min<3.59:
with open(opt.logdir + '/newrecord', 'a') as f:
f.write('{loss_15min:8.5f}\n'.format(loss_15min=vloss_15min))
model_state_dict = model.state_dict()
checkpoint = {
'model': model_state_dict,
'settings': opt,
'epoch': epoch_i}
if opt.save_model:
if opt.save_mode == 'all':
model_name = opt.save_model + '_accu_{accu:3.3f}.chkpt'.format(accu=100*valid_accu)
torch.save(checkpoint, model_name)
elif opt.save_mode == 'best':
model_name = opt.logdir+'/'+opt.save_model + '.chkpt'
if vloss <= min(valid_accus) or vloss_15min <= min(rmses_15min):
torch.save(checkpoint, model_name)
print(' - [Info] The checkpoint file has been updated.')
with open(log_train_file, 'a') as log_tf, open(log_valid_file, 'a') as log_vf:
log_tf.write('{epoch},{loss: 8.5f},{loss_15min:8.5f},{loss_60min:8.5f},{loss_6hour:8.5f},\
{acc_15min:3.3f},{acc_60min:3.3f},{acc_6hour:3.3f}\n'.format(
epoch=epoch_i, loss=loss, loss_15min=loss_15min, loss_60min=loss_60min, loss_6hour=loss_6hour,
acc_15min=acc_15min, acc_60min=acc_60min, acc_6hour=acc_6hour))
log_vf.write('{epoch},{loss: 8.5f},{loss_15min:8.5f},{loss_60min:8.5f},{loss_6hour:8.5f},\
{acc_15min:3.3f},{acc_60min:3.3f},{acc_6hour:3.3f}\n'.format(
epoch=epoch_i, loss=vloss, loss_15min=vloss_15min, loss_60min=vloss_60min, loss_6hour=vloss_6hour,
acc_15min=vacc_15min, acc_60min=vacc_60min, acc_6hour=vacc_6hour))
def main():
''' Main function '''
parser = argparse.ArgumentParser()
parser.add_argument('-model', default="Transformer")
parser.add_argument('-dataset', default="RoadDataSet")
parser.add_argument('-epoch', type=int, default=10)
parser.add_argument('-batch_size', type=int, default=1024)
parser.add_argument('-steps', type=int, default=5000)
parser.add_argument('-n_max_seq', type=int, default=30)
parser.add_argument('-n_sample', type=int, default=24)
parser.add_argument('-n_nb_sample', type=int, default=3)
parser.add_argument('-d_model', type=int, default=64)
parser.add_argument('-d_inner', type=int, default=256)
parser.add_argument('-d_inner2', type=int, default=256)
parser.add_argument('-d_inner3', type=int, default=None)
parser.add_argument('-d_inner4', type=int, default=None)
parser.add_argument('-d_inner5', type=int, default=None)
parser.add_argument('-d_k', type=int, default=8)
parser.add_argument('-d_v', type=int, default=8)
parser.add_argument('-n_head', type=int, default=8)
parser.add_argument('-n_layers', type=int, default=6)
parser.add_argument('-n_warmup_steps', type=int, default=4000)
parser.add_argument('-dropout', type=float, default=0)
parser.add_argument('-bidirect', type=bool, default=False)
parser.add_argument('-logdir', default='./')
parser.add_argument('-save_model', default="model_saved")
parser.add_argument('-save_mode', type=str, choices=['all', 'best'], default='best')
parser.add_argument('-cuda', action='store_true')
opt = parser.parse_args()
device = torch.device('cuda' if opt.cuda else 'cpu')
# n_sample=opt.n_sample+1+44*3
# n_max_seq=4+2+opt.n_sample+1+44*3
model=ModelWrapper(opt.model,opt).model
model = model.to(device)
total_params = sum(p.numel() for p in model.parameters())
total_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("total params:\t\t\t%d"%total_params)
print("total trainable params:\t\t%d"%total_trainable_params)
print('')
print(opt)
print('')
#create folder for this training run
import datetime,os
now = datetime.datetime.now()
newDirName = now.strftime("%Y%m%d-%H%M%S")
opt.logdir=opt.logdir+newDirName
os.mkdir(opt.logdir)
print('Log folder: %s' % opt.logdir)
log_hyp_file = opt.logdir + '/hyperpara.txt'
with open(log_hyp_file, 'a') as f:
f.write('%s' % opt)
import dataloader
from torch.utils.data import DataLoader
train_data_file = "./train_data/train_data2.pkl"
val_data_file = "./train_data/val_data2.pkl"
ds=eval("dataloader."+opt.dataset)
training_data=DataLoader(ds(train_data_file,opt.n_sample,opt.steps,"train", n_nb_sample=opt.n_nb_sample),num_workers=1, batch_size=opt.batch_size)
validation_data=DataLoader(ds(train_data_file,opt.n_sample,opt.steps,"val", n_nb_sample=opt.n_nb_sample),num_workers=1, batch_size=opt.batch_size)
optimizer= optim.Adam(model.parameters())
train(model, training_data, validation_data, optimizer, device ,opt)
if __name__ == '__main__':
main()