-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_pems.py
150 lines (135 loc) · 6 KB
/
test_pems.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
import torch
import time
import math
import numpy as np
from data.utils_pems import log_string, metric
from data.utils_pems import load_data
# import math
from TPGraph import TPGraph
import torch
from data.generate_adj_pems import read_adj
import pandas as pd
from args_parameter_pems import *
def test(args, model, log):
(train_loader, val_loader, test_loader,
SE, mean, std, ifo) = load_data(args)
model_path = args.model_file
model = torch.load(model_path)
log_string(log, '**** testing model ****')
log_string(log, 'loading model from %s' % model_path)
log_string(log, 'model restored!')
log_string(log, 'evaluating...')
with torch.no_grad():
'''trainset evaluate'''
trainPred = []
trainY = []
for ind, data in enumerate(train_loader):
xc, xd, xw, te, y = data # B T N -> x need: B C N T
xc, xd, xw = xc.unsqueeze(1).permute(0, 1, 3, 2), xd.unsqueeze(1).permute(0, 1, 3, 2), xw.unsqueeze(1).permute(0, 1, 3, 2)
pred_batch = model(xc, xd, xw, te) # 32 12 325 B T N
trainPred.append(pred_batch.to('cpu').detach().clone())
trainY.append(y.to('cpu').detach().clone())
if ind%100==0:
print('trainset: %.2f%% percent is calculated' % (ind * args.batch_size/36458*100))
del xc, xd, xw, pred_batch, y
trainPred = torch.from_numpy(np.concatenate(trainPred, axis=0))
trainPred = trainPred * std + mean
trainY = torch.from_numpy(np.concatenate(trainY, axis=0))
'''valset evaluate'''
valPred = []
valY = []
for ind, data in enumerate(val_loader):
xc, xd, xw, te, y = data # B T N -> x need: B C N T
xc, xd, xw = xc.unsqueeze(1).permute(0, 1, 3, 2), xd.unsqueeze(1).permute(0, 1, 3, 2), xw.unsqueeze(
1).permute(0, 1, 3, 2)
pred_batch = model(xc, xd, xw, te)
valPred.append(pred_batch.to('cpu').detach().clone())
valY.append(y.to('cpu').detach().clone())
if ind%100==0:
print('valset: %.2f%% percent is calculated' % (ind * args.batch_size/5189*100))
del xc, xd, xw, pred_batch, y
valPred = torch.from_numpy(np.concatenate(valPred, axis=0))
valPred = valPred * std + mean
valY = torch.from_numpy(np.concatenate(valY, axis=0))
'''testset evaluate'''
testPred = []
testY = []
start_test = time.time()
for ind, data in enumerate(test_loader):
xc, xd, xw, te, y = data # B T N -> x need: B C N T
xc, xd, xw = xc.unsqueeze(1).permute(0, 1, 3, 2), xd.unsqueeze(1).permute(0, 1, 3, 2), xw.unsqueeze(
1).permute(0, 1, 3, 2)
pred_batch = model(xc, xd, xw, te)
testPred.append(pred_batch.to('cpu').detach().clone())
testY.append(y.to('cpu').detach().clone())
if ind%100==0:
print('testset: %.2f%% percent is calculated' % (ind * args.batch_size/10400*100))
del xc, xd, xw, pred_batch, y
testPred = torch.from_numpy(np.concatenate(testPred, axis=0))
testPred = testPred * std + mean
testY = torch.from_numpy(np.concatenate(testY, axis=0))
end_test = time.time()
train_mae, train_rmse, train_mape = metric(trainPred, trainY)
val_mae, val_rmse, val_mape = metric(valPred, valY)
test_mae, test_rmse, test_mape = metric(testPred, testY)
log_string(log, 'testing time: %.1fs' % (end_test - start_test))
log_string(log, ' MAE\t\tRMSE\t\tMAPE')
log_string(log, 'train %.2f\t\t%.2f\t\t%.2f%%' %
(train_mae, train_rmse, train_mape * 100))
log_string(log, 'val %.2f\t\t%.2f\t\t%.2f%%' %
(val_mae, val_rmse, val_mape * 100))
log_string(log, 'test %.2f\t\t%.2f\t\t%.2f%%' %
(test_mae, test_rmse, test_mape * 100))
log_string(log, 'performance in each prediction step')
MAE, RMSE, MAPE = [], [], []
for step in range(args.num_pred):
mae, rmse, mape = metric(testPred[:, step], testY[:, step])
MAE.append(mae)
RMSE.append(rmse)
MAPE.append(mape)
log_string(log, 'step: %02d %.2f\t\t%.2f\t\t%.2f%%' %
(step + 1, mae, rmse, mape * 100))
average_mae = np.mean(MAE)
average_rmse = np.mean(RMSE)
average_mape = np.mean(MAPE)
log_string(
log, 'average: %.2f\t\t%.2f\t\t%.2f%%' %
(average_mae, average_rmse, average_mape * 100))
return trainPred, valPred, testPred, trainY, valY, testY
if __name__ =='__main__':
# model initiation
adj_w, adj_r = read_adj('data/Adj(PeMS).txt') # tensor:w是权重0-1,r是连通性0或1
adj_w, adj_r = adj_w.float(), adj_r.float()
df = pd.read_hdf('data/pems_correlation_roads.h5')
data_val = torch.from_numpy(df.values)
corr = torch.Tensor(data_val).float()
T = 24 * 60 // args.time_slot
in_channels = 1 # Channels of input
embed_size = args.embed_size # Dimension of hidden embedding features
time_num = T # 288
num_layers = args.num_layers # Number of ST Block
T_dim = [args.num_his, args.num_day, args.num_week] # Input length
output_T_dim = args.num_pred # Output Expected length
heads = args.heads # Number of Heads in MultiHeadAttention
cheb_K = 2
forward_expansion = 4 # Dimension of Feed Forward Network
dropout = 0
extra_feature = torch.Tensor(120, 2)
model = TPGraph(
adj_r,
corr,
extra_feature,
in_channels,
embed_size,
time_num,
num_layers,
T_dim,
output_T_dim,
heads,
cheb_K,
forward_expansion,
args.DEVICE,
dropout)
model.to(args.DEVICE)
log = open('data/test_log_L1loss_', 'w')
trainPred, valPred, testPred, trainY, valY, testY = test(args, model, log)