-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
132 lines (111 loc) · 5.34 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
import os
import zipfile
import numpy as np
import torch
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
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):
"""
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 (num_vertices, num_features,
num_timesteps)
:return:
- Node features divided into multiple samples. Shape is
(num_samples, num_vertices, num_features, num_timesteps_input).
- Node targets for the samples. Shape is
(num_samples, num_vertices, num_features, num_timesteps_output).
"""
# 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(X.shape[2] - (
num_timesteps_input + num_timesteps_output) + 1)]
# Save samples
features, target = [], []
for i, j in indices:
features.append(
X[:, :, i: i + num_timesteps_input].transpose(
(0, 2, 1)))
target.append(X[:, 0, i + num_timesteps_input: j]*stds[0]+means[0])
return torch.from_numpy(np.array(features)), \
torch.from_numpy(np.array(target))