-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
330 lines (245 loc) · 13 KB
/
models.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
# -*- coding:utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from layers import GCN_layer, Spatial_Attention, Temporal_Attention, AutoEncoder
class MCSTGCN(nn.Module):
def __init__(self, time_step, gcn1_in_feature, gcn1_out_feature, gcn2_out_feature, nb_time_filter,
pre_len, time_strides):
'''
Parameter of Multi-component stgcn.
:param time_step: int, length of input sequence
:param gcn1_in_feature: int, in_feature for GCN layer 1
:param gcn1_out_feature: int, out_feature for GCN layer 1
:param gcn2_out_feature: int, out_feature for GCN layer 2
:param nb_time_filter: int, out_feature for time CNN
:param pre_len: int, length of prediction
:param time_strides: int, length of time CNN kernel stride
'''
super(MCSTGCN, self).__init__()
self.spatial_gcn = GCN_layer(gcn1_in_feature, gcn1_out_feature, gcn2_out_feature)
self.time_conv = nn.Conv2d(gcn2_out_feature, nb_time_filter, kernel_size=(1, 3), stride=(1, time_strides),
padding=(0, 1))
self.residual_conv = nn.Conv2d(gcn1_in_feature, nb_time_filter, kernel_size=(1, 1), stride=(1, time_strides))
self.ln = nn.LayerNorm(nb_time_filter)
self.final_conv = nn.Conv2d(int(time_step / time_strides), pre_len, kernel_size=(1, nb_time_filter))
def forward(self, x, adj):
'''
:param x: (batch_size, node, timestep, in_feature)——(B, N, T, in_F)
:param adj: (N, N)
:return: (B, N, pre_T, in_F)
'''
x = x.permute(0, 1, 3, 2) # (B, N, in_F, T)
# GCN for spatial feature
spatial_gcn = self.spatial_gcn(x, adj) # (B, N, gcn_F, T)
# convolution along time axis
time_conv_output = self.time_conv(spatial_gcn.permute(0, 2, 1, 3)) # (B, gcn_F, N, T) - (B, cnn_F, N, stride_T)
residual_conv = self.residual_conv(x.permute(0, 2, 1, 3)) # (B, in_F, N, T) - (B, cnn_F, N, stride_T)
# (B, cnn_F, N, stride_T) - (B, stride_T, N, cnn_F) - (B, N, cnn_F, stride_T)
x_residual = self.ln(F.relu(residual_conv + time_conv_output).permute(0, 3, 2, 1)).permute(0, 2, 3, 1)
# (B, N, cnn_F, stride_T) - (B, stride_T, N, cnn_F) - (B, pre_T, N, in_F) - (B, N, pre_T, in_F)
pred = self.final_conv(x_residual.permute(0, 3, 1, 2)).permute(0, 2, 1, 3)
return pred
class AMRGCN(nn.Module):
def __init__(self, node_length, time_step, gcn1_in_feature, gcn1_out_feature, gcn2_out_feature, nb_time_filter,
pre_len, dropout, device):
'''
Parameter of Augmented Multi-component Recurrent Gcn.
:param node_length: int, num of nodes
:param time_step: int, length of input sequence
:param gcn1_in_feature: int, in_feature for GCN layer 1
:param gcn1_out_feature: int, out_feature for GCN layer 1
:param gcn2_out_feature: int, out_feature for GCN layer 2
:param nb_time_filter: int, out_feature for time CNN
:param pre_len: int, length of prediction
:param dropout: double, dropout for preventing overfitting
:param device: cuda:0 or cpu
'''
super(AMRGCN, self).__init__()
self.autoencoder = AutoEncoder(gcn1_in_feature, gcn1_out_feature, gcn2_out_feature, dropout, node_length,
nb_time_filter, time_step, pre_len, device)
self.residual_conv = nn.Conv2d(gcn1_in_feature, nb_time_filter, kernel_size=(1, 1), stride=(1, 1))
self.ln = nn.LayerNorm(nb_time_filter)
self.final_conv = nn.Conv2d(time_step, pre_len, kernel_size=(1, nb_time_filter))
def forward(self, x, adj):
'''
:param x: (batch_size, node, timestep, in_feature)—(B, N, T, in_F)
:param adj: (N, N)
:return: (B, N, pre_T, in_F)
'''
x = x.permute(0, 1, 3, 2) # (B, N, in_F, T)
# Encoder-Predictor architecture: (B, N, in_F, T) - (B, T, N, convlstm_F) - (B, convlstm_F, N, T)
time_conv_output = self.autoencoder(x, adj).permute(0, 3, 2, 1)
# (B, in_F, N, T) - (B, convlstm_F, N, T)
residual_conv = self.residual_conv(x.permute(0, 2, 1, 3))
# residual connection
x_residual = self.ln(F.relu(residual_conv + time_conv_output).permute(0, 3, 2, 1)).permute(0, 2, 3, 1)
# (B, convlstm_F, N, T) - (B, T, convlstm_F, N) - (B, pre_T, N, in_F) - (B, N, pre_T, in_F)
pred = self.final_conv(x_residual.permute(0, 3, 1, 2)).permute(0, 2, 1, 3)
return pred
class ASTGCN(nn.Module):
def __init__(self, node_length, time_step, gcn1_in_feature, gcn1_out_feature, gcn2_out_feature, nb_time_filter,
pre_len, time_strides, DEVICE):
'''
Parameter of Attention Multi-component stgcn.
:param node_length: int, num of nodes
:param time_step: int, length of input sequence
:param gcn1_in_feature: int, in_feature for GCN layer 1
:param gcn1_out_feature: int, out_feature for GCN layer 1
:param gcn2_out_feature: int, out_feature for GCN layer 2
:param nb_time_filter: int, out_feature for time CNN
:param pre_len: int, length of prediction
:param time_strides: int, length of time CNN kernel stride
:param DEVICE: cuda:0 or cpu
'''
super(ASTGCN, self).__init__()
self.TAt = Temporal_Attention(DEVICE, gcn1_in_feature, node_length, time_step)
self.SAt = Spatial_Attention(DEVICE, gcn1_in_feature, node_length, time_step)
self.spatial_gcn = GCN_layer(gcn1_in_feature, gcn1_out_feature, gcn2_out_feature)
# ((time_step - kernel_size) + padding * 2) / stride + 1)
self.time_conv = nn.Conv2d(gcn2_out_feature, nb_time_filter, kernel_size=(1, 3), stride=(1, time_strides),
padding=(0, 1))
self.residual_conv = nn.Conv2d(gcn1_in_feature, nb_time_filter, kernel_size=(1, 1), stride=(1, time_strides))
self.ln = nn.LayerNorm(nb_time_filter)
self.final_conv = nn.Conv2d(int(time_step / time_strides), pre_len, kernel_size=(1, nb_time_filter))
def forward(self, x, adj):
'''
:param x: (batch_size, node, timestep, in_feature)—(B, N, T, in_F)
:param adj: (N, N)
:return: (B, N, pre_T, in_F)
'''
x = x.permute(0, 1, 3, 2) # (B, N, in_F, T)
batch_size, node_length, num_of_features, time_step = x.shape
# Temporal Attention
temporal_At = self.TAt(x) # (B, T, T)
# Get temporal score
x_TAt = torch.matmul(x.reshape(batch_size, -1, time_step), temporal_At).reshape(batch_size, node_length,
num_of_features,
time_step)
# Spatial Attention
spatial_At = self.SAt(x_TAt) # (B, N, N)
# Get spatial score
x_SAT = torch.matmul(x.reshape(batch_size, -1, node_length), spatial_At).reshape(batch_size, node_length,
num_of_features,
time_step)
# GCN for spatial feature
spatial_gcn = self.spatial_gcn(x_SAT, adj) # (B, N, in_F, T) - (B, N, gcn_F, T)
# convolution along time axis
time_conv_output = self.time_conv(spatial_gcn.permute(0, 2, 1, 3)) # (B, gcn_F, N, T) - (B, cnn_F, N, stride_T)
residual_conv = self.residual_conv(x.permute(0, 2, 1, 3)) # (B, in_F, N, T) - (B, cnn_F, N, stride_T)
# (B, cnn_F, N, stride_T) - (B, stride_T, N, cnn_F) - (B, N, cnn_F, stride_T)
x_residual = self.ln(F.relu(residual_conv + time_conv_output).permute(0, 3, 2, 1)).permute(0, 2, 3, 1)
# (B, N, cnn_F, stride_T) - (B, stride_T, N, cnn_F) - (B, pre_T, N, in_F) - (B, N, pre_T, in_F)
pred = self.final_conv(x_residual.permute(0, 3, 1, 2)).permute(0, 2, 1, 3)
return pred
class Baseline_LSTM(nn.Module):
def __init__(self, node_length, input_size, hidden_size, pre_len):
'''
Parameter of LSTM
:param node_length: int, num of nodes
:param input_size: int, in_feature for LSTM
:param hidden_size: int
:param pre_len: int, length of prediction
'''
super(Baseline_LSTM, self).__init__()
self.pre_len = pre_len
self.lstm = nn.LSTM(batch_first=True, input_size=input_size, hidden_size=hidden_size)
self.fc1 = nn.Sequential(
nn.Linear(hidden_size, node_length)
)
self.fc2 = nn.Sequential(
nn.Linear(1, pre_len)
)
def forward(self, x):
'''
LSTM Baseline
:param x: (batch_size, node, timestep, in_feature)—(B, N, T, in_F)
:return: (B, N, pre_T, in_F)
'''
x = x.squeeze() # (B, N, T)
batch, node_length, timestep = x.shape
x = x.permute(0, 2, 1) # (B, T, N)
x, _ = self.lstm(x) # x: (B, T, hidden)
x = x[:, -1, :].view(batch, -1) # (B, hidden)
x = self.fc1(x).view(batch, node_length, 1) # (B, N, 1)
x = self.fc2(x) # (B, N, pre_T)
x = x.view(batch, node_length, -1, 1) # (B, N, pre_T, in_F)
return x
class Baseline_GRU(nn.Module):
def __init__(self, node_length, input_size, hidden_size, pre_len):
'''
Parameter of GRU
:param node_length: int, num of nodes
:param input_size: int, in_feature for GRU
:param hidden_size: int
:param pre_len: int, length of prediction
'''
super(Baseline_GRU, self).__init__()
self.pre_len = pre_len
self.gru = nn.GRU(batch_first=True, input_size=input_size, hidden_size=hidden_size)
self.fc1 = nn.Sequential(
nn.Linear(hidden_size, node_length)
)
self.fc2 = nn.Sequential(
nn.Linear(1, pre_len)
)
def forward(self, x):
'''
GRU Baseline
:param x: (batch_size, node, timestep, in_feature)—(B, N, T, in_F)
:return: (B, N, pre_T, in_F)
'''
x = x.squeeze() # (B, N, T)
batch, node_length, timestep = x.shape
x = x.permute(0, 2, 1) # (B, T, N)
x, _ = self.gru(x) # x: (B, T, hidden)
x = x[:, -1, :].view(batch, -1) # (B, hidden)
x = self.fc1(x).view(batch, node_length, 1) # (B, N, 1)
x = self.fc2(x) # (B, N, pre_T)
x = x.view(batch, node_length, -1, 1) # (B, N, pre_T, in_F)
return x
class AM_LSTM_GCN(nn.Module):
def __init__(self, time_step, gcn1_in_feature, gcn1_out_feature, gcn2_out_feature, nb_time_filter, pre_len):
'''
Comparison Model of replacing ConvLSTM-1D with LSTM
:param time_step:int, length of input sequence
:param gcn1_in_feature: int, in_feature for GCN layer 1
:param gcn1_out_feature: int, out_feature for GCN layer 1
:param gcn2_out_feature: int, out_feature for GCN layer 2
:param nb_time_filter: int, out_feature for time CNN
:param pre_len: int, length of prediction
'''
super(AM_LSTM_GCN, self).__init__()
self.spatial_gcn = GCN_layer(gcn1_in_feature, gcn1_out_feature, gcn2_out_feature)
self.gru = nn.LSTM(gcn2_out_feature, nb_time_filter, batch_first=True)
self.residual_conv = nn.Conv2d(gcn1_in_feature, gcn2_out_feature, kernel_size=(1, 1),
stride=(1, time_step / pre_len))
self.ln = nn.LayerNorm(gcn2_out_feature)
self.final_conv = nn.Conv2d(pre_len, pre_len, kernel_size=(1, nb_time_filter))
self.pre_len = pre_len
self.fc = nn.Linear(1, pre_len)
def forward(self, x, adj):
'''
:param x: (batch_size, node, timestep, in_feature)——(B, N, T, in_F)
:param adj: (N, N)
:return: (B, N, pre_T, in_F)
'''
x = x.permute(0, 1, 3, 2) # (B, N, in_F, T)
batch_size, node_length, num_of_features, time_step = x.shape
# GCN for spatial feature
spatial_gcn = self.spatial_gcn(x, adj) # (B, N, gcn_F, T)
spatial_gcn = spatial_gcn.permute(0, 3, 2, 1) # (B, T, gcn_F, N)
gru_cat = []
for i in range(node_length):
conv_gcn = spatial_gcn[:, :, :, i] # (B, T, gcn_F)
gru_out, _ = self.gru(conv_gcn) # gru_out: (B, T, hidden)
gru_cat.append(gru_out[:, -1, :].unsqueeze(-1)) # (B, hidden, 1)
gru_output = torch.cat(gru_cat, dim=-1).unsqueeze(-1) # (B, hidden, N, 1)
time_output = self.fc(gru_output) # (B, hidden, N, pre_T)
# (B, N, in_F, T) - (B, in_F, N, T) - (B, hidden, N, pre_T)
residual_conv = self.residual_conv(x.permute(0, 2, 1, 3))
# (B, hidden, N, pre_T) - (B, N, pre_T, hidden) - (B, pre_T, N, hidden)
x_residual = self.ln(F.relu(residual_conv + time_output).permute(0, 2, 3, 1)).permute(0, 2, 1, 3)
# (B, pre_T, N, hidden) - (B, pre_T, N, in_F) - (B, N, pre_T, in_F)
pred = self.final_conv(x_residual).permute(0, 2, 1, 3)
return pred