-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
311 lines (267 loc) · 11.3 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -- coding: utf-8 --
import numpy as np
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
from scipy.sparse.linalg.eigen.arpack import eigsh
import sys
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from baseline.gman import tf_utils
def FC(x, units, activations, bn, bn_decay, is_training, use_bias=True):
if isinstance(units, int):
units = [units]
activations = [activations]
elif isinstance(units, tuple):
units = list(units)
activations = list(activations)
assert type(units) == list
for num_unit, activation in zip(units, activations):
x = tf_utils.conv2d(
x, output_dims=num_unit, kernel_size=[1, 1], stride=[1, 1],
padding='VALID', use_bias=use_bias, activation=activation,
bn=bn, bn_decay=bn_decay, is_training=is_training)
return x
def gatedFusion(HS, HT, D, bn, bn_decay, is_training):
'''
gated fusion
HS: [batch_size, num_step, N, D]
HT: [batch_size, num_step, N, D]
D: output dims
return: [batch_size, num_step, N, D]
'''
XS = FC(
HS, units=D, activations=None,
bn=bn, bn_decay=bn_decay,
is_training=is_training, use_bias=False)
XT = FC(
HT, units=D, activations=None,
bn=bn, bn_decay=bn_decay,
is_training=is_training, use_bias=True)
z = tf.nn.sigmoid(tf.add(XS, XT))
H = tf.add(tf.multiply(z, HS), tf.multiply(1 - z, HT))
H = FC(
H, units=[D, D], activations=[tf.nn.relu, None],
bn=bn, bn_decay=bn_decay, is_training=is_training)
return H
def STEmbedding(SE, TE, T, D, bn, bn_decay, is_training):
'''
spatio-temporal embedding
SE: [N, D]
TE: [batch_size, P + Q, 2] (dayofweek, timeofday)
T: num of time steps in one day
D: output dims
retrun: [batch_size, P + Q, N, D]
'''
# spatial embedding
SE = FC(
SE, units=[D, D], activations=[tf.nn.relu, None],
bn=bn, bn_decay=bn_decay, is_training=is_training)
# temporal embedding
TE = tf.add_n(TE)
# TE = tf.concat((TE), axis=-1)
TE = FC(
TE, units=[D, D], activations=[tf.nn.relu, None],
bn=bn, bn_decay=bn_decay, is_training=is_training)
return tf.add(SE, TE)
# return tf.concat([SE, TE],axis=-1)
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
def sample_mask(idx, l):
"""Create mask."""
mask = np.zeros(l)
mask[idx] = 1
return np.array(mask, dtype=np.bool)
def load_data(dataset_str):
"""
Loads input data from gcn/data directory
ind.dataset_str.x => the feature vectors of the training instances as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.tx => the feature vectors of the test instances as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.allx => the feature vectors of both labeled and unlabeled training instances
(a superset of ind.dataset_str.x) as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.y => the one-hot labels of the labeled training instances as numpy.ndarray object;
ind.dataset_str.ty => the one-hot labels of the test instances as numpy.ndarray object;
ind.dataset_str.ally => the labels for instances in ind.dataset_str.allx as numpy.ndarray object;
ind.dataset_str.graph => a dict in the format {index: [index_of_neighbor_nodes]} as collections.defaultdict
object;
ind.dataset_str.test.index => the indices of test instances in graph, for the inductive setting as list object.
All objects above must be saved using python pickle module.
:param dataset_str: Dataset name
:return: All data input files loaded (as well the training/test data).
"""
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
with open("data/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
if sys.version_info > (3, 0):
objects.append(pkl.load(f, encoding='latin1'))
else:
objects.append(pkl.load(f))
x, y, tx, ty, allx, ally, graph = tuple(objects)
print(x.shape)
print(y.shape)
print(tx.shape)
print(allx.shape)
test_idx_reorder = parse_index_file("data/ind.{}.test.index".format(dataset_str))
test_idx_range = np.sort(test_idx_reorder)
if dataset_str == 'citeseer':
# Fix citeseer dataset (there are some isolated nodes in the graph)
# Find isolated nodes, add them as zero-vecs into the right position
test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
tx_extended[test_idx_range-min(test_idx_range), :] = tx
tx = tx_extended
ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
ty_extended[test_idx_range-min(test_idx_range), :] = ty
ty = ty_extended
features = sp.vstack((allx, tx)).tolil()
features[test_idx_reorder, :] = features[test_idx_range, :]
adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]
idx_test = test_idx_range.tolist()
idx_train = range(len(y))
idx_val = range(len(y), len(y)+500)
train_mask = sample_mask(idx_train, labels.shape[0])
val_mask = sample_mask(idx_val, labels.shape[0])
test_mask = sample_mask(idx_test, labels.shape[0])
y_train = np.zeros(labels.shape)
y_val = np.zeros(labels.shape)
y_test = np.zeros(labels.shape)
y_train[train_mask, :] = labels[train_mask, :]
y_val[val_mask, :] = labels[val_mask, :]
y_test[test_mask, :] = labels[test_mask, :]
return adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
def preprocess_features(features):
"""Row-normalize feature matrix and convert to tuple representation"""
rowsum = np.array(features.sum(1))
print(rowsum.shape)
r_inv = np.power(rowsum, -1).flatten()
r_inv[np.isinf(r_inv)] = 0.
print(r_inv.shape)
r_mat_inv = sp.diags(r_inv)
print(r_mat_inv.shape)
features = r_mat_inv.dot(features)
print('features shape is : ',features.shape)
return sparse_to_tuple(features)
def normalize_adj(adj):
'''
:param adj: Symmetrically normalize adjacency matrix
:return:
'''
adj = sp.coo_matrix(adj) # 转化为稀疏矩阵表示的形式
rowsum = np.array(adj.sum(1)) # 原连接矩阵每一行的元素和
d_inv_sqrt = np.power(rowsum, -0.5).flatten() #先根号,再求倒数,然后flatten返回一个折叠成一维的数组
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. #
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
def preprocess_adj(adj):
'''
:param adj: A=A+E, and then to normalize the the adj matrix,
preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation.
:return:
'''
# 邻接矩阵 加上 单位矩阵
'''
[[1,0,0],[0,1,0],[0,0,1]]
'''
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]))
print('adj_normalized shape is : ', adj_normalized.shape)
return sparse_to_tuple(adj_normalized)
def construct_feed_dict(x_s, adj, label_s, day, hour, minute, x_p, label_p, placeholders):
"""Construct feed dictionary."""
feed_dict = dict()
feed_dict.update({placeholders['position']: np.array([[i for i in range(108)]],dtype=np.int32)})
feed_dict.update({placeholders['labels_s']: label_s})
feed_dict.update({placeholders['day']: day})
feed_dict.update({placeholders['hour']: hour})
feed_dict.update({placeholders['minute']: minute})
feed_dict.update({placeholders['features_s']: x_s})
feed_dict.update({placeholders['indices_i']: adj[0]})
feed_dict.update({placeholders['values_i']: adj[1]})
feed_dict.update({placeholders['dense_shape_i']: adj[2]})
feed_dict.update({placeholders['features_p']: x_p})
feed_dict.update({placeholders['labels_p']: label_p})
# feed_dict.update({placeholders['support'][i]: support[i] for i in range(len(support))})
feed_dict.update({placeholders['num_features_nonzero']: x_s[0].shape})
return feed_dict
def chebyshev_polynomials(adj, k):
"""Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
print("Calculating Chebyshev polynomials up to order {}...".format(k))
adj_normalized = normalize_adj(adj)
laplacian = sp.eye(adj.shape[0]) - adj_normalized
largest_eigval, _ = eigsh(laplacian, 1, which='LM')
scaled_laplacian = (2. / largest_eigval[0]) * laplacian - sp.eye(adj.shape[0])
t_k = list()
t_k.append(sp.eye(adj.shape[0]))
t_k.append(scaled_laplacian)
def chebyshev_recurrence(t_k_minus_one, t_k_minus_two, scaled_lap):
s_lap = sp.csr_matrix(scaled_lap, copy=True)
return 2 * s_lap.dot(t_k_minus_one) - t_k_minus_two
for i in range(2, k+1):
t_k.append(chebyshev_recurrence(t_k[-1], t_k[-2], scaled_laplacian))
return sparse_to_tuple(t_k)
import matplotlib.pyplot as plt
def describe(label, predict):
'''
:param label:
:param predict:
:param prediction_size:
:return:
'''
plt.figure()
# Label is observed value,Blue
plt.plot(label[0:], 'b', label=u'actual value')
# Predict is predicted value,Red
plt.plot(predict[0:], 'r', label=u'predicted value')
# use the legend
plt.legend()
# plt.xlabel("time(hours)", fontsize=17)
# plt.ylabel("pm$_{2.5}$ (ug/m$^3$)", fontsize=17)
# plt.title("the prediction of pm$_{2.5}", fontsize=17)
plt.show()
def metric(pred, label):
with np.errstate(divide='ignore', invalid='ignore'):
mask = np.not_equal(label, 0)
mask = mask.astype(np.float32)
mask /= np.mean(mask)
mae = np.abs(np.subtract(pred, label)).astype(np.float32)
rmse = np.square(mae)
mape = np.divide(mae, label)
# mae = np.nan_to_num(mae * mask)
# wape = np.divide(np.sum(mae), np.sum(label))
mae = np.mean(mae)
# rmse = np.nan_to_num(rmse * mask)
rmse = np.sqrt(np.mean(rmse))
mape = np.nan_to_num(mape * mask)
mape = np.mean(mape)
cor = np.mean(np.multiply((label - np.mean(label)),
(pred - np.mean(pred)))) / (np.std(pred) * np.std(label))
sse = np.sum((label - pred) ** 2)
sst = np.sum((label - np.mean(label)) ** 2)
r2 = 1 - sse / sst # r2_score(y_actual, y_predicted, multioutput='raw_values')
print('mae is : %.6f'%mae)
print('rmse is : %.6f'%rmse)
print('mape is : %.6f'%mape)
print('r is : %.6f'%cor)
print('r$^2$ is : %.6f'%r2)
return mae, rmse, mape, cor, r2