forked from XDZhelheim/Torch-MTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMAN.py
396 lines (356 loc) · 14.6 KB
/
GMAN.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchinfo import summary
class conv2d_(nn.Module):
def __init__(self, input_dims, output_dims, kernel_size, stride=(1, 1),
padding='SAME', use_bias=True, activation=F.relu,
bn_decay=None):
super(conv2d_, self).__init__()
self.activation = activation
if padding == 'SAME':
self.padding_size = math.ceil(kernel_size)
else:
self.padding_size = [0, 0]
self.conv = nn.Conv2d(input_dims, output_dims, kernel_size, stride=stride,
padding=0, bias=use_bias)
self.batch_norm = nn.BatchNorm2d(output_dims, momentum=bn_decay)
torch.nn.init.xavier_uniform_(self.conv.weight)
if use_bias:
torch.nn.init.zeros_(self.conv.bias)
def forward(self, x):
x = x.permute(0, 3, 2, 1)
x = F.pad(x, ([self.padding_size[1], self.padding_size[1], self.padding_size[0], self.padding_size[0]]))
x = self.conv(x)
x = self.batch_norm(x)
if self.activation is not None:
x = F.relu_(x)
return x.permute(0, 3, 2, 1)
class FC(nn.Module):
def __init__(self, input_dims, units, activations, bn_decay, use_bias=True):
super(FC, self).__init__()
if isinstance(units, int):
units = [units]
input_dims = [input_dims]
activations = [activations]
elif isinstance(units, tuple):
units = list(units)
input_dims = list(input_dims)
activations = list(activations)
assert type(units) == list
self.convs = nn.ModuleList([conv2d_(
input_dims=input_dim, output_dims=num_unit, kernel_size=[1, 1], stride=[1, 1],
padding='VALID', use_bias=use_bias, activation=activation,
bn_decay=bn_decay) for input_dim, num_unit, activation in
zip(input_dims, units, activations)])
def forward(self, x):
for conv in self.convs:
x = conv(x)
return x
class STEmbedding(nn.Module):
"""
spatio-temporal embedding
SE: [num_vertex, D]
TE: [batch_size, num_hist + num_pred, 2] (dayofweek, timeofday)
T: num of time steps in one day
D: output dims
return: [batch_size, num_his + num_pred, num_vertex, D]
"""
def __init__(self, D, bn_decay, device):
super(STEmbedding, self).__init__()
self.FC_se = FC(
input_dims=[D, D], units=[D, D], activations=[F.relu, None],
bn_decay=bn_decay
)
self.FC_te = FC(
input_dims=[295, D], units=[D, D], activations=[F.relu, None],
bn_decay=bn_decay
)
self.device = device
def forward(self, SE, TE, T=288):
# print("*"*50, TE.shape, SE.shape)
# spatial embedding
SE = SE.unsqueeze(0).unsqueeze(0)
SE = self.FC_se(SE)
# temporal embedding
dayofweek = torch.empty(TE.shape[0], TE.shape[1], 7)
timeofday = torch.empty(TE.shape[0], TE.shape[1], T)
for i in range(TE.shape[0]):
dayofweek[i] = F.one_hot(TE[..., 1][i].to(torch.int64), 7)
for j in range(TE.shape[0]):
timeofday[j] = F.one_hot((288*TE[..., 0][j]).to(torch.int64), T)
TE = torch.cat((dayofweek, timeofday), dim=-1)
TE = TE.unsqueeze(dim=2).to(device=self.device)
TE = self.FC_te(TE)
# print("*"*50, TE.shape, SE.shape)
del dayofweek, timeofday
return SE + TE
class spatialAttention(nn.Module):
"""
spatial attention mechanism
X: [batch_size, num_step, num_vertex, D]
STE: [batch_size, num_step, num_vertex, D]
K: number of attention heads
d: dimension of each attention outputs
return: [batch_size, num_step, num_vertex, D]
"""
def __init__(self, K, d, bn_decay):
super(spatialAttention, self).__init__()
D = K * d
self.d = d
self.K = K
self.FC_q = FC(input_dims=2 * D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC_k = FC(input_dims=2 * D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC_v = FC(input_dims=2 * D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC = FC(input_dims=D, units=D, activations=F.relu,
bn_decay=bn_decay)
def forward(self, X, STE):
batch_size = X.shape[0]
X = torch.cat((X, STE), dim=-1)
# [batch_size, num_step, num_vertex, K * d]
query = self.FC_q(X)
key = self.FC_k(X)
value = self.FC_v(X)
# [K * batch_size, num_step, num_vertex, d]
query = torch.cat(torch.split(query, self.d, dim=-1), dim=0)
key = torch.cat(torch.split(key, self.d, dim=-1), dim=0)
value = torch.cat(torch.split(value, self.d, dim=-1), dim=0)
# [K * batch_size, num_step, num_vertex, num_vertex]
attention = torch.matmul(query, key.transpose(2, 3))
attention /= (self.d ** 0.5)
attention = F.softmax(attention, dim=-1)
# [batch_size, num_step, num_vertex, D]
X = torch.matmul(attention, value)
X = torch.cat(torch.split(X, batch_size, dim=0), dim=-1)
X = self.FC(X)
del query, key, value, attention
return X
class temporalAttention(nn.Module):
"""
temporal attention mechanism
X: [batch_size, num_step, num_vertex, D]
STE: [batch_size, num_step, num_vertex, D]
K: number of attention heads
d: dimension of each attention outputs
return: [batch_size, num_step, num_vertex, D]
"""
def __init__(self, K, d, bn_decay, mask=True):
super(temporalAttention, self).__init__()
D = K * d
self.d = d
self.K = K
self.mask = mask
self.FC_q = FC(input_dims=2 * D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC_k = FC(input_dims=2 * D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC_v = FC(input_dims=2 * D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC = FC(input_dims=D, units=D, activations=F.relu,
bn_decay=bn_decay)
def forward(self, X, STE):
batch_size_ = X.shape[0]
X = torch.cat((X, STE), dim=-1)
# [batch_size, num_step, num_vertex, K * d]
query = self.FC_q(X)
key = self.FC_k(X)
value = self.FC_v(X)
# [K * batch_size, num_step, num_vertex, d]
query = torch.cat(torch.split(query, self.d, dim=-1), dim=0)
key = torch.cat(torch.split(key, self.d, dim=-1), dim=0)
value = torch.cat(torch.split(value, self.d, dim=-1), dim=0)
# query: [K * batch_size, num_vertex, num_step, d]
# key: [K * batch_size, num_vertex, d, num_step]
# value: [K * batch_size, num_vertex, num_step, d]
query = query.permute(0, 2, 1, 3)
key = key.permute(0, 2, 3, 1)
value = value.permute(0, 2, 1, 3)
# [K * batch_size, num_vertex, num_step, num_step]
attention = torch.matmul(query, key)
attention /= (self.d ** 0.5)
# mask attention score
if self.mask:
batch_size = X.shape[0]
num_step = X.shape[1]
num_vertex = X.shape[2]
mask = torch.ones(num_step, num_step)
mask = torch.tril(mask)
mask = torch.unsqueeze(torch.unsqueeze(mask, dim=0), dim=0)
mask = mask.repeat(self.K * batch_size, num_vertex, 1, 1)
mask = mask.to(torch.bool)
attention = torch.where(mask, attention, -1 ** 15 + 1)
# softmax
attention = F.softmax(attention, dim=-1)
# [batch_size, num_step, num_vertex, D]
X = torch.matmul(attention, value)
X = X.permute(0, 2, 1, 3)
X = torch.cat(torch.split(X, batch_size_, dim=0), dim=-1)
X = self.FC(X)
del query, key, value, attention
return X
class gatedFusion(nn.Module):
"""
gated fusion
HS: [batch_size, num_step, num_vertex, D]
HT: [batch_size, num_step, num_vertex, D]
D: output dims
return: [batch_size, num_step, num_vertex, D]
"""
def __init__(self, D, bn_decay):
super(gatedFusion, self).__init__()
self.FC_xs = FC(input_dims=D, units=D, activations=None,
bn_decay=bn_decay, use_bias=False)
self.FC_xt = FC(input_dims=D, units=D, activations=None,
bn_decay=bn_decay, use_bias=True)
self.FC_h = FC(input_dims=[D, D], units=[D, D], activations=[F.relu, None],
bn_decay=bn_decay)
def forward(self, HS, HT):
XS = self.FC_xs(HS)
XT = self.FC_xt(HT)
z = torch.sigmoid(torch.add(XS, XT))
H = torch.add(torch.mul(z, HS), torch.mul(1 - z, HT))
H = self.FC_h(H)
del XS, XT, z
return H
class STAttBlock(nn.Module):
def __init__(self, K, d, bn_decay, mask=False):
super(STAttBlock, self).__init__()
self.spatialAttention = spatialAttention(K, d, bn_decay)
self.temporalAttention = temporalAttention(K, d, bn_decay, mask=mask)
self.gatedFusion = gatedFusion(K * d, bn_decay)
def forward(self, X, STE):
HS = self.spatialAttention(X, STE)
HT = self.temporalAttention(X, STE)
H = self.gatedFusion(HS, HT)
del HS, HT
return torch.add(X, H)
class transformAttention(nn.Module):
"""
transform attention mechanism
X: [batch_size, num_his, num_vertex, D]
STE_his: [batch_size, num_his, num_vertex, D]
STE_pred: [batch_size, num_pred, num_vertex, D]
K: number of attention heads
d: dimension of each attention outputs
return: [batch_size, num_pred, num_vertex, D]
"""
def __init__(self, K, d, bn_decay):
super(transformAttention, self).__init__()
D = K * d
self.K = K
self.d = d
self.FC_q = FC(input_dims=D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC_k = FC(input_dims=D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC_v = FC(input_dims=D, units=D, activations=F.relu,
bn_decay=bn_decay)
self.FC = FC(input_dims=D, units=D, activations=F.relu,
bn_decay=bn_decay)
def forward(self, X, STE_his, STE_pred):
batch_size = X.shape[0]
# [batch_size, num_step, num_vertex, K * d]
query = self.FC_q(STE_pred)
key = self.FC_k(STE_his)
value = self.FC_v(X)
# [K * batch_size, num_step, num_vertex, d]
query = torch.cat(torch.split(query, self.d, dim=-1), dim=0)
key = torch.cat(torch.split(key, self.d, dim=-1), dim=0)
value = torch.cat(torch.split(value, self.d, dim=-1), dim=0)
# query: [K * batch_size, num_vertex, num_pred, d]
# key: [K * batch_size, num_vertex, d, num_his]
# value: [K * batch_size, num_vertex, num_his, d]
query = query.permute(0, 2, 1, 3)
key = key.permute(0, 2, 3, 1)
value = value.permute(0, 2, 1, 3)
# [K * batch_size, num_vertex, num_pred, num_his]
attention = torch.matmul(query, key)
attention /= (self.d ** 0.5)
attention = F.softmax(attention, dim=-1)
# [batch_size, num_pred, num_vertex, D]
X = torch.matmul(attention, value)
X = X.permute(0, 2, 1, 3)
X = torch.cat(torch.split(X, batch_size, dim=0), dim=-1)
X = self.FC(X)
del query, key, value, attention
return X
class GMAN(nn.Module):
"""
GMAN
X: [batch_size, num_his, num_vertex]
TE: [batch_size, num_his + num_pred, 2] (time-of-day, day-of-week)
SE: [num_vertex, K * d]
num_his: number of history steps
num_pred: number of prediction steps
T: one day is divided into T steps
L: number of STAtt blocks in the encoder/decoder
K: number of attention heads
d: dimension of each attention head outputs
return: [batch_size, num_pred, num_vertex]
"""
def __init__(self,
SE_file_path,
device,
timestep_in=12,
statt_layers=5,
att_heads=8,
att_dims=8,
bn_decay=0.1
):
super(GMAN, self).__init__()
L = statt_layers
K = att_heads
d = att_dims
D = K * d
self.num_his = timestep_in
self.SE = self.getSE(SE_file_path).to(device)
self.STEmbedding = STEmbedding(D, bn_decay, device)
self.STAttBlock_1 = nn.ModuleList([STAttBlock(K, d, bn_decay) for _ in range(L)])
self.STAttBlock_2 = nn.ModuleList([STAttBlock(K, d, bn_decay) for _ in range(L)])
self.transformAttention = transformAttention(K, d, bn_decay)
self.FC_1 = FC(input_dims=[1, D], units=[D, D], activations=[F.relu, None],
bn_decay=bn_decay)
self.FC_2 = FC(input_dims=[D, D], units=[D, 1], activations=[F.relu, None],
bn_decay=bn_decay)
def getSE(self, SE_file):
with open(SE_file, mode='r') as f:
lines = f.readlines()
temp = lines[0].split(' ')
num_vertex, dims = int(temp[0]), int(temp[1])
SE = torch.zeros((num_vertex, dims), dtype=torch.float32)
for line in lines[1:]:
temp = line.split(' ')
index = int(temp[0])
SE[index] = torch.tensor([float(ch) for ch in temp[1:]])
return SE
def forward(self, x, y_tod_dow):
# x: (B, T, N, 3=input+tod+dow)
TE = x[..., 0, 1:]
y_tod_dow = y_tod_dow[:, :, 0, :]
TE = torch.concat([TE, y_tod_dow], dim=1) # (B, 24, 2)
X = x[..., :1]
# input
X = self.FC_1(X)
# STE
STE = self.STEmbedding(self.SE, TE)
STE_his = STE[:, :self.num_his]
STE_pred = STE[:, self.num_his:]
# encoder
for net in self.STAttBlock_1:
X = net(X, STE_his)
# transAtt
X = self.transformAttention(X, STE_his, STE_pred)
# decoder
for net in self.STAttBlock_2:
X = net(X, STE_pred)
# output
X = self.FC_2(X)
del STE, STE_his, STE_pred
return X
if __name__ == '__main__':
model = GMAN(SE_file_path="../data/METRLA/SE_metrla.txt", device="cpu")
summary(model, [[64, 12, 207, 3], [64, 12, 207, 2]], device="cpu")