-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
234 lines (193 loc) · 10.3 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class nconv(nn.Module):
def __init__(self):
super(nconv, self).__init__()
def forward(self, x, A):
x = torch.einsum('ncvl,nwv->ncwl', (x, A))
return x.contiguous()
class linear(nn.Module):
def __init__(self, c_in, c_out):
super(linear, self).__init__()
self.mlp = torch.nn.Conv2d(c_in, c_out, kernel_size=(1, 1), padding=(0, 0), stride=(1, 1), bias=True)
def forward(self, x):
return self.mlp(x)
class gcn(nn.Module):
def __init__(self, c_in, c_out, dropout, support_len=3, order=2):
super(gcn, self).__init__()
self.nconv = nconv()
c_in = (order * support_len + 1) * c_in
self.mlp = linear(c_in, c_out)
self.dropout = dropout
self.order = order
def forward(self, x, support):
out = [x]
for a in support:
x1 = self.nconv(x, a)
out.append(x1)
for k in range(2, self.order + 1):
x2 = self.nconv(x1, a)
out.append(x2)
x1 = x2
h = torch.cat(out, dim=1)
h = self.mlp(h)
h = F.dropout(h, self.dropout, training=self.training)
return h
class DMSTGCN(nn.Module):
def __init__(self, device, num_nodes, dropout=0.3,
out_dim=12, residual_channels=16, dilation_channels=16, end_channels=512,
kernel_size=2, blocks=4, layers=2, days=288, dims=40, order=2, in_dim=9, normalization="batch"):
super(DMSTGCN, self).__init__()
skip_channels = 8
self.dropout = dropout
self.blocks = blocks
self.layers = layers
self.filter_convs = nn.ModuleList()
self.gate_convs = nn.ModuleList()
self.residual_convs = nn.ModuleList()
self.skip_convs = nn.ModuleList()
self.normal = nn.ModuleList()
self.gconv = nn.ModuleList()
self.filter_convs_a = nn.ModuleList()
self.gate_convs_a = nn.ModuleList()
self.residual_convs_a = nn.ModuleList()
self.skip_convs_a = nn.ModuleList()
self.normal_a = nn.ModuleList()
self.gconv_a = nn.ModuleList()
self.gconv_a2p = nn.ModuleList()
self.start_conv_a = nn.Conv2d(in_channels=in_dim,
out_channels=residual_channels,
kernel_size=(1, 1))
self.start_conv = nn.Conv2d(in_channels=in_dim,
out_channels=residual_channels,
kernel_size=(1, 1))
receptive_field = 1
self.supports_len = 1
self.nodevec_p1 = nn.Parameter(torch.randn(days, dims).to(device), requires_grad=True).to(device)
self.nodevec_p2 = nn.Parameter(torch.randn(num_nodes, dims).to(device), requires_grad=True).to(device)
self.nodevec_p3 = nn.Parameter(torch.randn(num_nodes, dims).to(device), requires_grad=True).to(device)
self.nodevec_pk = nn.Parameter(torch.randn(dims, dims, dims).to(device), requires_grad=True).to(device)
self.nodevec_a1 = nn.Parameter(torch.randn(days, dims).to(device), requires_grad=True).to(device)
self.nodevec_a2 = nn.Parameter(torch.randn(num_nodes, dims).to(device), requires_grad=True).to(device)
self.nodevec_a3 = nn.Parameter(torch.randn(num_nodes, dims).to(device), requires_grad=True).to(device)
self.nodevec_ak = nn.Parameter(torch.randn(dims, dims, dims).to(device), requires_grad=True).to(device)
self.nodevec_a2p1 = nn.Parameter(torch.randn(days, dims).to(device), requires_grad=True).to(device)
self.nodevec_a2p2 = nn.Parameter(torch.randn(num_nodes, dims).to(device), requires_grad=True).to(device)
self.nodevec_a2p3 = nn.Parameter(torch.randn(num_nodes, dims).to(device), requires_grad=True).to(device)
self.nodevec_a2pk = nn.Parameter(torch.randn(dims, dims, dims).to(device), requires_grad=True).to(device)
for b in range(blocks):
additional_scope = kernel_size - 1
new_dilation = 1
for i in range(layers):
# dilated convolutions
self.filter_convs.append(nn.Conv2d(in_channels=residual_channels,
out_channels=dilation_channels,
kernel_size=(1, kernel_size), dilation=new_dilation))
self.gate_convs.append(nn.Conv1d(in_channels=residual_channels,
out_channels=dilation_channels,
kernel_size=(1, kernel_size), dilation=new_dilation))
self.residual_convs.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=residual_channels,
kernel_size=(1, 1)))
self.skip_convs.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=skip_channels,
kernel_size=(1, 1)))
self.filter_convs_a.append(nn.Conv2d(in_channels=residual_channels,
out_channels=dilation_channels,
kernel_size=(1, kernel_size), dilation=new_dilation))
self.gate_convs_a.append(nn.Conv1d(in_channels=residual_channels,
out_channels=dilation_channels,
kernel_size=(1, kernel_size), dilation=new_dilation))
# 1x1 convolution for residual connection
self.residual_convs_a.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=residual_channels,
kernel_size=(1, 1)))
if normalization == "batch":
self.normal.append(nn.BatchNorm2d(residual_channels))
self.normal_a.append(nn.BatchNorm2d(residual_channels))
elif normalization == "layer":
self.normal.append(nn.LayerNorm([residual_channels, num_nodes, 13 - receptive_field - new_dilation + 1]))
self.normal_a.append(nn.LayerNorm([residual_channels, num_nodes, 13 - receptive_field - new_dilation + 1]))
new_dilation *= 2
receptive_field += additional_scope
additional_scope *= 2
self.gconv.append(
gcn(dilation_channels, residual_channels, dropout, support_len=self.supports_len, order=order))
self.gconv_a.append(
gcn(dilation_channels, residual_channels, dropout, support_len=self.supports_len, order=order))
self.gconv_a2p.append(
gcn(dilation_channels, residual_channels, dropout, support_len=self.supports_len, order=order))
self.relu = nn.ReLU(inplace=True)
self.end_conv_1 = nn.Conv2d(in_channels=skip_channels * (12 + 10 + 9 + 7 + 6 + 4 + 3 + 1),
out_channels=end_channels,
kernel_size=(1, 1),
bias=True)
self.end_conv_2 = nn.Conv2d(in_channels=end_channels,
out_channels=out_dim,
kernel_size=(1, 1),
bias=True)
self.receptive_field = receptive_field
def dgconstruct(self, time_embedding, source_embedding, target_embedding, core_embedding):
adp = torch.einsum('ai, ijk->ajk', time_embedding, core_embedding)
adp = torch.einsum('bj, ajk->abk', source_embedding, adp)
adp = torch.einsum('ck, abk->abc', target_embedding, adp)
adp = F.softmax(F.relu(adp), dim=2)
return adp
def forward(self, inputs, ind):
"""
input: (B, F, N, T)
"""
in_len = inputs.size(3)
if in_len < self.receptive_field:
xo = nn.functional.pad(inputs, (self.receptive_field - in_len, 0, 0, 0))
else:
xo = inputs
x = self.start_conv(xo[:, [0]])
x_a = self.start_conv_a(xo[:, [1]])
skip = 0
# dynamic graph construction
adp = self.dgconstruct(self.nodevec_p1[ind], self.nodevec_p2, self.nodevec_p3, self.nodevec_pk)
adp_a = self.dgconstruct(self.nodevec_a1[ind], self.nodevec_a2, self.nodevec_a3, self.nodevec_ak)
adp_a2p = self.dgconstruct(self.nodevec_a2p1[ind], self.nodevec_a2p2, self.nodevec_a2p3, self.nodevec_a2pk)
new_supports = [adp]
new_supports_a = [adp_a]
new_supports_a2p = [adp_a2p]
for i in range(self.blocks * self.layers):
# tcn for primary part
residual = x
filter = self.filter_convs[i](residual)
filter = torch.tanh(filter)
gate = self.gate_convs[i](residual)
gate = torch.sigmoid(gate)
x = filter * gate
# tcn for auxiliary part
residual_a = x_a
filter_a = self.filter_convs_a[i](residual_a)
filter_a = torch.tanh(filter_a)
gate_a = self.gate_convs_a[i](residual_a)
gate_a = torch.sigmoid(gate_a)
x_a = filter_a * gate_a
# skip connection
s = x
s = self.skip_convs[i](s)
if isinstance(skip, int): # B F N T
skip = s.transpose(2, 3).reshape([s.shape[0], -1, s.shape[2], 1]).contiguous()
else:
skip = torch.cat([s.transpose(2, 3).reshape([s.shape[0], -1, s.shape[2], 1]), skip], dim=1).contiguous()
# dynamic graph convolutions
x = self.gconv[i](x, new_supports)
x_a = self.gconv_a[i](x_a, new_supports_a)
# multi-faceted fusion module
x_a2p = self.gconv_a2p[i](x_a, new_supports_a2p)
x = x_a2p + x
# residual and normalization
x_a = x_a + residual_a[:, :, :, -x_a.size(3):]
x = x + residual[:, :, :, -x.size(3):]
x = self.normal[i](x)
x_a = self.normal_a[i](x_a)
# output layer
x = F.relu(skip)
x = F.relu(self.end_conv_1(x))
x = self.end_conv_2(x)
return x