-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
303 lines (208 loc) · 10.9 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
# coding: utf-8
import sys
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import os
from time import time
import shutil
import argparse
import configparser
from model.AGNN import make_model
from lib.utils import get_adjacency_matrix, get_adjacency_matrix_2direction, compute_val_loss, predict_and_save_results, load_graphdata_normY_channel1
from tensorboardX import SummaryWriter
# read hyper-param settings
parser = argparse.ArgumentParser()
parser.add_argument("--config", default='configurations/PEMS03.conf', type=str, help="configuration file path")
parser.add_argument('--cuda', type=str, default='0')
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.cuda
USE_CUDA = torch.cuda.is_available()
DEVICE = torch.device('cuda:0')
print("CUDA:", USE_CUDA, DEVICE, flush=True)
config = configparser.ConfigParser()
print('Read configuration file: %s' % (args.config), flush=True)
config.read(args.config)
print("config Training:",config['Training'])
data_config = config['Data']
training_config = config['Training']
adj_filename = data_config['adj_filename']
graph_signal_matrix_filename = data_config['graph_signal_matrix_filename']
if config.has_option('Data', 'id_filename'):
id_filename = data_config['id_filename']
else:
id_filename = None
num_of_vertices = int(data_config['num_of_vertices'])
points_per_hour = int(data_config['points_per_hour'])
num_for_predict = int(data_config['num_for_predict'])
dataset_name = data_config['dataset_name']
model_name = training_config['model_name']
learning_rate = float(training_config['learning_rate'])
start_epoch = int(training_config['start_epoch'])
epochs = int(training_config['epochs'])
fine_tune_epochs = int(training_config['fine_tune_epochs'])
print('total training epoch, fine tune epoch:', epochs, ',' , fine_tune_epochs, flush=True)
batch_size = int(training_config['batch_size'])
print('batch_size:', batch_size, flush=True)
num_of_weeks = int(training_config['num_of_weeks'])
num_of_days = int(training_config['num_of_days'])
num_of_hours = int(training_config['num_of_hours'])
direction = int(training_config['direction'])
encoder_input_size = int(training_config['encoder_input_size'])
decoder_input_size = int(training_config['decoder_input_size'])
dropout = float(training_config['dropout'])
filename_npz = os.path.join(dataset_name + '_r' + str(num_of_hours) + '_d' + str(num_of_days) + '_w' + str(num_of_weeks)) + '.npz'
num_layers = int(training_config['num_layers'])
d_model = int(training_config['d_model'])
nb_head = int(training_config['nb_head'])
ScaledSAt = bool(int(training_config['ScaledSAt'])) # whether use spatial self attention
SE = bool(int(training_config['SE'])) # whether use spatial embedding
smooth_layer_num = int(training_config['smooth_layer_num'])
TE = bool(int(training_config['TE']))
use_LayerNorm = True
residual_connection = True
# direction = 1 means: if i connected to j, adj[i,j]=1;
# direction = 2 means: if i connected to j, then adj[i,j]=adj[j,i]=1
if direction == 2:
adj_mx, distance_mx = get_adjacency_matrix_2direction(adj_filename, num_of_vertices, id_filename)
if direction == 1:
adj_mx, distance_mx = get_adjacency_matrix(adj_filename, num_of_vertices, id_filename)
folder_dir = 'MAE_%s_h%dd%dw%d_layer%d_head%d_dm%d_channel%d_dir%d_drop%.2f_%.2e' % (model_name, num_of_hours, num_of_days, num_of_weeks, num_layers, nb_head, d_model, encoder_input_size, direction, dropout, learning_rate)
if ScaledSAt:
folder_dir = folder_dir + 'ScaledSAt'
if SE:
folder_dir = folder_dir + 'SE' + str(smooth_layer_num)
if TE:
folder_dir = folder_dir + 'TE'
print('folder_dir:', folder_dir, flush=True)
params_path = os.path.join('../experiments', dataset_name, folder_dir)
# all the input has been normalized into range [-1,1] by MaxMin normalization
train_loader, train_target_tensor, val_loader, val_target_tensor, test_loader, test_target_tensor, _max, _min = load_graphdata_normY_channel1(
graph_signal_matrix_filename, num_of_hours,
num_of_days, num_of_weeks, DEVICE, batch_size)
net = make_model(DEVICE, num_layers, encoder_input_size, decoder_input_size, d_model, adj_mx, nb_head, num_of_weeks,
num_of_days, num_of_hours, points_per_hour, num_for_predict, dropout=dropout, ScaledSAt=ScaledSAt, SE=SE, TE=TE, smooth_layer_num=smooth_layer_num, residual_connection=residual_connection, use_LayerNorm=use_LayerNorm)
print(net, flush=True)
def train_main():
if (start_epoch == 0) and (not os.path.exists(params_path)): # To start training from scratch, rebuild the folder
os.makedirs(params_path)
print('create params directory %s' % (params_path), flush=True)
elif (start_epoch == 0) and (os.path.exists(params_path)):
shutil.rmtree(params_path)
os.makedirs(params_path)
print('delete the old one and create params directory %s' % (params_path), flush=True)
elif (start_epoch > 0) and (os.path.exists(params_path)): # To start training from the middle, it is necessary to ensure that the original directory exists
print('train from params directory %s' % (params_path), flush=True)
else:
raise SystemExit('Wrong type of model!')
criterion = nn.L1Loss().to(DEVICE) #Define loss function
optimizer = optim.Adam(net.parameters(), lr=learning_rate) # Define the optimizer and pass in all network parameters
sw = SummaryWriter(logdir=params_path, flush_secs=5)
total_param = 0
print('Net\'s state_dict:', flush=True)
for param_tensor in net.state_dict():
print(param_tensor, '\t', net.state_dict()[param_tensor].size(), flush=True)
total_param += np.prod(net.state_dict()[param_tensor].size())
print('Net\'s total params:', total_param, flush=True)
print('Optimizer\'s state_dict:')
for var_name in optimizer.state_dict():
print(var_name, '\t', optimizer.state_dict()[var_name], flush=True)
global_step = 0
best_epoch = 0
best_val_loss = np.inf
# train model
if start_epoch > 0:
params_filename = os.path.join(params_path, 'epoch_%s.params' % start_epoch)
net.load_state_dict(torch.load(params_filename))
print('start epoch:', start_epoch, flush=True)
print('load weight from: ', params_filename, flush=True)
start_time = time()
for epoch in range(start_epoch, epochs):
params_filename = os.path.join(params_path, 'epoch_%s.params' % epoch)
# apply model on the validation data set
val_loss = compute_val_loss(net, val_loader, criterion, sw, epoch)
if val_loss < best_val_loss:
best_val_loss = val_loss
best_epoch = epoch
torch.save(net.state_dict(), params_filename)
print('save parameters to file: %s' % params_filename, flush=True)
net.train() # ensure dropout layers are in train mode
train_start_time = time()
for batch_index, batch_data in enumerate(train_loader):
encoder_inputs, decoder_inputs, labels = batch_data
encoder_inputs = encoder_inputs.transpose(-1, -2) # (B, N, T, F)
decoder_inputs = decoder_inputs.unsqueeze(-1) # (B, N, T, 1)
labels = labels.unsqueeze(-1)
optimizer.zero_grad()
outputs = net(encoder_inputs, decoder_inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
training_loss = loss.item()
global_step += 1
sw.add_scalar('training_loss', training_loss, global_step)
print('epoch: %s, train time every whole data:%.2fs' % (epoch, time() - train_start_time), flush=True)
print('epoch: %s, total time:%.2fs' % (epoch, time() - start_time), flush=True)
print('best epoch:', best_epoch, flush=True)
print('apply the best val model on the test data set ...', flush=True)
predict_main(best_epoch, test_loader, test_target_tensor, _max, _min, 'test')
# fine tune the model
optimizer = optim.Adam(net.parameters(), lr=learning_rate*0.1)
print('fine tune the model ... ', flush=True)
for epoch in range(epochs, epochs+fine_tune_epochs):
params_filename = os.path.join(params_path, 'epoch_%s.params' % epoch)
net.train() # ensure dropout layers are in train mode
train_start_time = time()
for batch_index, batch_data in enumerate(train_loader):
encoder_inputs, decoder_inputs, labels = batch_data
encoder_inputs = encoder_inputs.transpose(-1, -2) # (B, N, T, F)
decoder_inputs = decoder_inputs.unsqueeze(-1) # (B, N, T, 1)
labels = labels.unsqueeze(-1)
predict_length = labels.shape[2] # T
optimizer.zero_grad()
encoder_output = net.encode(encoder_inputs)
# decode
decoder_start_inputs = decoder_inputs[:, :, :1, :]
decoder_input_list = [decoder_start_inputs]
for step in range(predict_length):
decoder_inputs = torch.cat(decoder_input_list, dim=2)
predict_output = net.decode(decoder_inputs, encoder_output)
decoder_input_list = [decoder_start_inputs, predict_output]
loss = criterion(predict_output, labels)
loss.backward()
optimizer.step()
training_loss = loss.item()
global_step += 1
sw.add_scalar('training_loss', training_loss, global_step)
print('epoch: %s, train time every whole data:%.2fs' % (epoch, time() - train_start_time), flush=True)
print('epoch: %s, total time:%.2fs' % (epoch, time() - start_time), flush=True)
# apply model on the validation data set
val_loss = compute_val_loss(net, val_loader, criterion, sw, epoch)
if val_loss < best_val_loss:
best_val_loss = val_loss
best_epoch = epoch
torch.save(net.state_dict(), params_filename)
print('save parameters to file: %s' % params_filename, flush=True)
print('best epoch:', best_epoch, flush=True)
print('apply the best val model on the test data set ...', flush=True)
predict_main(best_epoch, test_loader, test_target_tensor, _max, _min, 'test')
def predict_main(epoch, data_loader, data_target_tensor, _max, _min, type):
'''
On the test set, test the effect of the specified epoch
:param epoch: int
:param data_loader: torch.utils.data.utils.DataLoader
:param data_target_tensor: tensor
:param _max: (1, 1, 3, 1)
:param _min: (1, 1, 3, 1)
:param type: string
:return:
'''
params_filename = os.path.join(params_path, 'epoch_%s.params' % epoch)
print('load weight from:', params_filename, flush=True)
net.load_state_dict(torch.load(params_filename))
predict_and_save_results(net, data_loader, data_target_tensor, epoch, _max, _min, params_path, type)
if __name__ == "__main__":
train_main()
# predict_main(0, test_loader, test_target_tensor, _max, _min, 'test')