-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
186 lines (157 loc) · 6.85 KB
/
utils.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
import os
import zipfile
import numpy as np
import torch
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import random
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
np.random.seed(seed) # Numpy module.
random.seed(seed) # Python random module.
torch.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.set_default_dtype(torch.float32)
return None
def get_data_list(data_list):
dlist = []
split_data_list = list(data_list.split('_'))
if('chengdu' in split_data_list):
dlist.append('chengdu_m')
if('metr' in split_data_list):
dlist.append('metr-la')
if('pems' in split_data_list):
dlist.append('pems-bay')
if('shenzhen' in split_data_list):
dlist.append('shenzhen')
return dlist
def unnorm(x ,means, stds):
return x * stds[0] + means[0]
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def calc_metric(pred, y, stage = "train"):
if(stage == "train"):
MSE = torch.mean((pred - y)**2)
RMSE = torch.sqrt(MSE)
MAE = torch.mean(torch.abs(pred - y))
MAPE = torch.mean(torch.abs(pred- y) / y)
else:
B, N, L = pred.shape
pred = pred.reshape(-1, L)
y = y.reshape(-1, L)
MSE = torch.mean((pred - y)**2, dim = 0)
RMSE = torch.sqrt(MSE)
MAE = torch.mean(torch.abs(pred - y), dim = 0)
MAPE = torch.mean(torch.abs(pred - y) / y, dim = 0)
return MSE, RMSE, MAE, MAPE
def metric_func(pred, y, times):
result = {}
result['MSE'], result['RMSE'], result['MAE'], result['MAPE'] = np.zeros(times), np.zeros(times), np.zeros(times), np.zeros(times)
# print("metric | pred shape:", pred.shape, " y shape:", y.shape)
def cal_MAPE(pred, y):
diff = np.abs(np.array(y) - np.array(pred))
return np.mean(diff / y)
for i in range(times):
y_i = y[:,i,:]
pred_i = pred[:,i,:]
MSE = mean_squared_error(pred_i, y_i)
RMSE = mean_squared_error(pred_i, y_i) ** 0.5
MAE = mean_absolute_error(pred_i, y_i)
MAPE = cal_MAPE(pred_i, y_i)
result['MSE'][i] += MSE
result['RMSE'][i] += RMSE
result['MAE'][i] += MAE
result['MAPE'][i] += MAPE
return result
def result_print(result, info_name='Evaluate'):
total_MSE, total_RMSE, total_MAE, total_MAPE = result['MSE'], result['RMSE'], result['MAE'], result['MAPE']
print("========== {} results ==========".format(info_name))
print(" MAE: %.3f/ %.3f/ %.3f/ %.3f/ %.3f/ %.3f"%(total_MAE[0], total_MAE[1], total_MAE[2], total_MAE[3], total_MAE[4], total_MAE[5]))
print("MAPE: %.3f/ %.3f/ %.3f/ %.3f/ %.3f/ %.3f"%(total_MAPE[0] * 100, total_MAPE[1] * 100, total_MAPE[2] * 100, total_MAPE[3] * 100, total_MAPE[4] * 100, total_MAPE[5] * 100))
print("RMSE: %.3f/ %.3f/ %.3f/ %.3f/ %.3f/ %.3f"%(total_RMSE[0], total_RMSE[1], total_RMSE[2], total_RMSE[3], total_RMSE[4], total_RMSE[5]))
print("---------------------------------------")
if info_name == 'Best':
print("========== Best results ==========")
print(" MAE: %.3f/ %.3f/ %.3f/ %.3f/ %.3f/ %.3f"%(total_MAE[0], total_MAE[1], total_MAE[2], total_MAE[3], total_MAE[4], total_MAE[5]))
print("MAPE: %.3f/ %.3f/ %.3f/ %.3f/ %.3f/ %.3f"%(total_MAPE[0] * 100, total_MAPE[1] * 100, total_MAPE[2] * 100, total_MAPE[3] * 100, total_MAPE[4] * 100, total_MAPE[5] * 100))
print("RMSE: %.3f/ %.3f/ %.3f/ %.3f/ %.3f/ %.3f"%(total_RMSE[0], total_RMSE[1], total_RMSE[2], total_RMSE[3], total_RMSE[4], total_RMSE[5]))
print("---------------------------------------")
def load_data(dataset_name, stage):
print("INFO: load {} data @ {} stage".format(dataset_name, stage))
A = np.load("data/" + dataset_name + "/matrix.npy")
A = get_normalized_adj(A)
A = torch.from_numpy(A)
X = np.load("data/" + dataset_name + "/dataset.npy")
X = X.transpose((1, 2, 0))
X = X.astype(np.float32)
# Normalization using Z-score method
means = np.mean(X, axis=(0, 2))
X = X - means.reshape(1, -1, 1)
stds = np.std(X, axis=(0, 2))
X = X / stds.reshape(1, -1, 1)
# train: 70%, validation: 10%, test: 20%
# source: 100%, target_1day: 288, target_3day: 288*3, target_1week: 288*7
if stage == 'train':
X = X[:, :, :int(X.shape[2]*0.7)]
elif stage == 'validation':
X = X[:, :, int(X.shape[2]*0.7):int(X.shape[2]*0.8)]
elif stage == 'test':
X = X[:, :, int(X.shape[2]*0.8):]
elif stage == 'source':
X = X
elif stage == 'target_1day':
X = X[:, :, :288]
elif stage == 'target_3day':
X = X[:, :, :288*3]
elif stage == 'target_1week':
X = X[:, :, :288*7]
else:
print("Error: unsupported data stage")
print("INFO: A shape is {}, X shape is {}, means = {}, stds = {}".format(A.shape, X.shape, means, stds))
return A, X, means, stds
def get_normalized_adj(A):
"""
Returns the degree normalized adjacency matrix.
"""
A = A + np.diag(np.ones(A.shape[0], dtype=np.float32))
D = np.array(np.sum(A, axis=1)).reshape((-1,))
D[D <= 10e-5] = 10e-5 # Prevent infs
diag = np.reciprocal(np.sqrt(D))
A_wave = np.multiply(np.multiply(diag.reshape((-1, 1)), A),
diag.reshape((1, -1)))
return A_wave
def generate_dataset(X, num_timesteps_input, num_timesteps_output, means, stds, inter_step):
"""
Takes node features for the graph and divides them into multiple samples
along the time-axis by sliding a window of size (num_timesteps_input+
num_timesteps_output) across it in steps of 1.
:param X: Node features of shape (N, 2, L)
:return:
- Node features divided into multiple samples. Shape is
(B, N, L, 2).
- Node targets for the samples. Shape is
(B, N, L).
"""
# Generate the beginning index and the ending index of a sample, which
# contains (num_points_for_training + num_points_for_predicting) points
indices = [(i, i + (num_timesteps_input + num_timesteps_output)) for i
in range(0, X.shape[2] - (
num_timesteps_input + num_timesteps_output) + 1, inter_step)]
# Save samples
# features are normalized and target are not normalized
features, target = [], []
for i, j in indices:
features.append(
# [N, 4, L] -> [N, L, 4]
X[:, :, i: i + num_timesteps_input].transpose(
(0, 2, 1)))
# target only add true value
target.append(X[:, 0, i + num_timesteps_input: j]*stds[0]+means[0])
x = torch.from_numpy(np.array(features)).float()
y = torch.from_numpy(np.array(target)).float()
# x : [B, N, L, 2]
# y : [B, N, L]
return x,y