-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_gru_test.py
96 lines (75 loc) · 3 KB
/
lstm_gru_test.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
import time
from utils import *
from opts import TestOptions
from torch.utils.data import DataLoader
from PreprocessData import datasets
# Parameter option
opt = TestOptions().parse()
# Intinial datasets class
OpenDataset = datasets(opt.dataset)
# Important Parameter
node_length = 170 if "pems08" in opt.dataset else 307
predict_length = int(opt.hdwps.split(',')[-2])
periodic_shift = int(opt.hdwps.split(',')[-1])
process_method = opt.process_method
features, labels, max_data = load_data(OpenDataset, opt.dataset, process_method, None, node_length, opt.Multidataset, opt.hdwps, is_training = False)
print(features.size(), labels.size(), max_data)
use_gpu = torch.cuda.is_available()
print("\n[STEP 2] : Obtain (adjacency, feature, label) matrix")
print("| Feature matrix : {}".format(features.shape))
print("| Label matrix : {}".format(labels.shape))
# get best model
load_model = torch.load(os.path.join(opt.save_path, '%s.t7' %(opt.model + '_' + str(predict_length))))
model = load_model['model']
epoch = load_model['epoch']
acc_val = load_model['acc']
MAE_val = load_model['MAE']
RMSE_val = load_model['RMSE']
if use_gpu:
_, features, labels = list(map(lambda x: x.cuda(), [model, features , labels]))
# get test dataset
test_Dataset = PEMS_dataset(features, labels)
test_loader = DataLoader(dataset = test_Dataset,
batch_size = 32,
shuffle = False)
def test(inputs, labels):
with torch.no_grad():
output = model(inputs)
output = output * max_data
labels = labels * max_data
# Set to 0 if the predicted result is less than 0
output = torch.where(output > 0, output, torch.full_like(output, 0))
MSE = torch.mean(torch.pow((output - labels),2))
RMSE = torch.sqrt(torch.mean(torch.pow((output - labels), 2)))
MAE = torch.mean(torch.abs(output - labels))
return MSE, RMSE, MAE
if __name__ == "__main__":
print("\n[STEP 4] : Testing")
print(opt.model + '_' + process_method)
print(load_model)
MSE_loss = 0.0
MAE_loss= 0.0
RMSE_loss = 0.0
start_time = time.time()
for index, data in enumerate(test_loader):
inputs, labels = data
# batch loss
MSE, RMSE, MAE = test(inputs, labels)
MSE_loss += MSE
MAE_loss += MAE
RMSE_loss += RMSE
# average loss
mse_loss = MSE_loss / (index + 1)
mae_loss = MAE_loss / (index + 1)
rmse_loss = RMSE_loss / (index + 1)
end_time = time.time()
print(
"|Validation acc : {:.2f}".format(acc_val.data.cpu().numpy()),
"|MAE_loss: {:.2f}".format(MAE_val.data.cpu().numpy()),
"|RMSE_loss: {:.2f}".format(RMSE_val.data.cpu().numpy()),
"|epoch {} ".format(epoch),
"|time:{}".format(end_time - start_time),
)
print("| Test acc : {:.2f}".format(mse_loss.data.cpu().numpy()),
"|MAE_loss: {:.2f}".format(mae_loss.data.cpu().numpy()),
"|RMSE_loss: {:.2f}".format(rmse_loss.data.cpu().numpy()),)