-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_gclstm.py
207 lines (159 loc) · 6.92 KB
/
train_gclstm.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
import torch
import numpy as np
from torch.autograd import Variable
import time
from GCLSTM import *
def TrainGCLSTM(train_dataloader, valid_dataloader, A, K, num_epochs=1):
inputs, labels = next(iter(train_dataloader))
[batch_size, step_size, fea_size] = inputs.size()
input_dim = fea_size
hidden_dim = fea_size
output_dim = fea_size
gclstm = GCLSTM(K, torch.Tensor(A), A.shape[0])
gclstm.cuda()
loss_MSE = torch.nn.MSELoss()
loss_L1 = torch.nn.L1Loss()
learning_rate = 1e-4
optimizer = torch.optim.RMSprop(gclstm.parameters(), lr=learning_rate)
use_gpu = torch.cuda.is_available()
interval = 100
losses_train = []
losses_interval_train = []
losses_valid = []
losses_interval_valid = []
losses_epoch = []
cur_time = time.time()
pre_time = time.time()
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
trained_number = 0
# validation data loader iterator init
valid_dataloader_iter = iter(valid_dataloader)
for data in train_dataloader:
inputs, labels = data
if inputs.shape[0] != batch_size:
continue
if use_gpu:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
gclstm.zero_grad()
labels = torch.squeeze(labels)
Hidden_State, Cell_State = gclstm.loop(inputs)
loss_train = loss_MSE(Hidden_State, labels)
optimizer.zero_grad()
loss_train.backward()
optimizer.step()
losses_train.append(loss_train.data)
# validation
try:
inputs_val, labels_val = next(valid_dataloader_iter)
except StopIteration:
valid_dataloader_iter = iter(valid_dataloader)
inputs_val, labels_val = next(valid_dataloader_iter)
if use_gpu:
inputs_val, labels_val = Variable(inputs_val.cuda()), Variable(labels_val.cuda())
else:
inputs_val, labels_val = Variable(inputs_val), Variable(labels_val)
labels_val = torch.squeeze(labels_val)
Hidden_State, Cell_State = gclstm.loop(inputs_val)
loss_valid = loss_MSE(Hidden_State, labels_val)
losses_valid.append(loss_valid.data)
# output
trained_number += 1
if trained_number % interval == 0:
cur_time = time.time()
loss_interval_train = np.around(sum(losses_train[-interval:]).cpu().numpy() / interval, decimals=8)
losses_interval_train.append(loss_interval_train)
loss_interval_valid = np.around(sum(losses_valid[-interval:]).cpu().numpy() / interval, decimals=8)
losses_interval_valid.append(loss_interval_valid)
print('Iteration #: {}, train_loss: {}, valid_loss: {}, time: {}'.format( \
trained_number * batch_size, \
loss_interval_train, \
loss_interval_valid, \
np.around([cur_time - pre_time], decimals=8)))
pre_time = cur_time
loss_epoch = loss_valid.cpu().data.numpy()
losses_epoch.append(loss_epoch)
return gclstm, [losses_train, losses_interval_train, losses_valid, losses_interval_valid]
def TestGCLSTM(gclstm, test_dataloader, max_speed):
inputs, labels = next(iter(test_dataloader))
[batch_size, step_size, fea_size] = inputs.size()
cur_time = time.time()
pre_time = time.time()
use_gpu = torch.cuda.is_available()
loss_MSE = torch.nn.MSELoss()
loss_L1 = torch.nn.L1Loss()
tested_batch = 0
losses_mse = []
losses_l1 = []
MAEs = []
MAPEs = []
MSEs = []
MSPEs = []
RMSEs = []
R2s = []
VARs = []
for data in test_dataloader:
inputs, labels = data
if inputs.shape[0] != batch_size:
continue
if use_gpu:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
Hidden_State, Cell_State = gclstm.loop(inputs)
labels = torch.squeeze(labels)
loss_MSE = torch.nn.MSELoss()
loss_L1 = torch.nn.L1Loss()
loss_mse = loss_MSE(Hidden_State, labels)
loss_l1 = loss_L1(Hidden_State, labels)
MAE = torch.mean(torch.abs(Hidden_State - torch.squeeze(labels)))
MAPE = torch.mean(torch.abs(Hidden_State - torch.squeeze(labels)) / torch.squeeze(labels))
MSE = torch.mean((torch.squeeze(labels) - Hidden_State)**2)
MSPE = torch.mean(((Hidden_State - torch.squeeze(labels)) / torch.squeeze(labels))**2)
RMSE = math.sqrt(torch.mean((torch.squeeze(labels) - Hidden_State)**2))
R2 = 1-((torch.squeeze(labels)-Hidden_State)**2).sum()/(((torch.squeeze(labels))-(torch.squeeze(labels)).mean())**2).sum()
VAR = 1-(torch.var(torch.squeeze(labels)-Hidden_State))/torch.var(torch.squeeze(labels))
losses_mse.append(loss_mse.data)
losses_l1.append(loss_l1.data)
MAEs.append(MAE.data)
MAPEs.append(MAPE.data)
MSEs.append(MSE.item())
MSPEs.append(MSPE.item())
RMSEs.append(RMSE)
R2s.append(R2.item())
VARs.append(VAR.item())
tested_batch += 1
if tested_batch % 1000 == 0:
cur_time = time.time()
print('Tested #: {}, loss_l1: {}, loss_mse: {}, time: {}'.format( \
tested_batch * batch_size, \
np.around([loss_l1.data[0]], decimals=8), \
np.around([loss_mse.data[0]], decimals=8), \
np.around([cur_time - pre_time], decimals=8)))
pre_time = cur_time
losses_l1 = np.array(losses_l1)
losses_mse = np.array(losses_mse)
MAEs = np.array(MAEs)
MAPEs = np.array(MAPEs)
MSEs = np.array(MSEs)
MSPEs = np.array(MSPEs)
RMSEs = np.array(RMSEs)
R2s = np.array(R2s)
VARs = np.array(VARs)
mean_l1 = np.mean(losses_l1) * max_speed
std_l1 = np.std(losses_l1) * max_speed
mean_mse = np.mean(losses_mse) * max_speed
MAE_ = np.mean(MAEs) * max_speed
std_MAE_ = np.std(MAEs) * max_speed
MAPE_ = np.mean(MAPEs) * 100
MSE_ = np.mean(MSEs) * (max_speed ** 2)
MSPE_ = np.mean(MSPEs) * 100
RMSE_ = np.mean(RMSEs) * max_speed
R2_ = np.mean(R2s)
VAR_ = np.mean(VARs)
results = [MAE_, std_MAE_, MAPE_, MSE_, MSPE_, RMSE_, R2_, VAR_]
print('Tested: MAE: {}, std_MAE: {}, MAPE: {}, MSE: {}, MSPE: {}, RMSE: {}, R2: {}, VAR: {}'.format(MAE_, std_MAE_, MAPE_, MSE_, MSPE_, RMSE_, R2_, VAR_))
return results