-
Notifications
You must be signed in to change notification settings - Fork 22
/
model.py
792 lines (606 loc) · 30.2 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import sys
import numpy as np
from torch.nn import BatchNorm2d, Conv1d, Conv2d, ModuleList, Parameter,LayerNorm,InstanceNorm2d
from utils import ST_BLOCK_0 #ASTGCN
from utils import ST_BLOCK_1 #DGCN_Mask/DGCN_Res
from utils import ST_BLOCK_2_r #DGCN_recent
from utils import ST_BLOCK_4 #Gated-STGCN
from utils import ST_BLOCK_5 #GRCN
from utils import ST_BLOCK_6 #OTSGGCN
from utils import multi_gcn #gwnet
from utils import GCNPool #H_GCN
from utils import Transmit
from utils import gate
from utils import GCNPool_dynamic
from utils import GCNPool_h
from utils import T_cheby_conv_ds_1
from utils import dynamic_adj
from utils import SATT_h_gcn
from sparse_activations import Sparsemax
"""
the parameters:
x-> [batch_num,in_channels,num_nodes,tem_size],
"""
class ASTGCN_Recent(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(ASTGCN_Recent,self).__init__()
self.block1=ST_BLOCK_0(in_dim,dilation_channels,num_nodes,length,K,Kt)
self.block2=ST_BLOCK_0(dilation_channels,dilation_channels,num_nodes,length,K,Kt)
self.final_conv=Conv2d(length,12,kernel_size=(1, dilation_channels),padding=(0,0),
stride=(1,1), bias=True)
self.supports=supports
self.bn=BatchNorm2d(in_dim,affine=False)
def forward(self,input):
x=self.bn(input)
adj=self.supports[0]
x,_,_ = self.block1(x,adj)
x,d_adj,t_adj = self.block2(x,adj)
x = x.permute(0,3,2,1)
x = self.final_conv(x)#b,12,n,1
return x,d_adj,t_adj
class DGCN_recent(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(DGCN_recent,self).__init__()
tem_size=length
self.block1=ST_BLOCK_2_r(in_dim,dilation_channels,num_nodes,length,K,Kt)
self.block2=ST_BLOCK_2_r(dilation_channels,dilation_channels,num_nodes,length,K,Kt)
self.conv1=Conv2d(dilation_channels,1,kernel_size=(1, 1),padding=(0,0),
stride=(1,1), bias=True)
self.supports=supports
self.h=Parameter(torch.zeros(num_nodes,num_nodes), requires_grad=True)
nn.init.uniform_(self.h, a=0, b=0.0001)
def forward(self,input):
x=input
A=self.h+self.supports[0]
d=1/(torch.sum(A,-1)+0.0001)
D=torch.diag_embed(d)
A=torch.matmul(D,A)
A1=F.dropout(A,0.5,self.training)
x,_,_=self.block1(x,A1)
x,d_adj,t_adj=self.block2(x,A1)
x=self.conv1(x).permute(0,3,2,1).contiguous()#b,c,n,l
return x,d_adj,A
class LSTM(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(LSTM,self).__init__()
self.lstm=nn.LSTM(in_dim,dilation_channels,batch_first=True)#b*n,l,c
self.c_out=dilation_channels
tem_size=length
self.tem_size=tem_size
self.conv1=Conv2d(dilation_channels,12,kernel_size=(1, tem_size),padding=(0,0),
stride=(1,1), bias=True)
def forward(self,input):
x=input
shape = x.shape
h = Variable(torch.zeros((1,shape[0]*shape[2],self.c_out))).cuda()
c = Variable(torch.zeros((1,shape[0]*shape[2],self.c_out))).cuda()
hidden=(h,c)
x=x.permute(0,2,3,1).contiguous().view(shape[0]*shape[2],shape[3],shape[1])
x,hidden=self.lstm(x,hidden)
x=x.view(shape[0],shape[2],shape[3],self.c_out).permute(0,3,1,2).contiguous()
x=self.conv1(x)#b,c,n,l
return x,hidden[0],hidden[0]
class GRU(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(GRU,self).__init__()
self.gru=nn.GRU(in_dim,dilation_channels,batch_first=True)#b*n,l,c
self.c_out=dilation_channels
tem_size=length
self.tem_size=tem_size
self.conv1=Conv2d(dilation_channels,12,kernel_size=(1,tem_size),
stride=(1,1), bias=True)
def forward(self,input):
x=input
shape = x.shape
h =Variable(torch.zeros((1,shape[0]*shape[2],self.c_out))).cuda()
hidden=h
x=x.permute(0,2,3,1).contiguous().view(shape[0]*shape[2],shape[3],shape[1])
x,hidden=self.gru(x,hidden)
x=x.view(shape[0],shape[2],shape[3],self.c_out).permute(0,3,1,2).contiguous()
x=self.conv1(x)#b,c,n,l
return x,hidden[0],hidden[0]
class Gated_STGCN(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(Gated_STGCN,self).__init__()
tem_size=length
self.block1=ST_BLOCK_4(in_dim,dilation_channels,num_nodes,tem_size,K,Kt)
self.block2=ST_BLOCK_4(dilation_channels,dilation_channels,num_nodes,tem_size,K,Kt)
self.block3=ST_BLOCK_4(dilation_channels,dilation_channels,num_nodes,tem_size,K,Kt)
self.conv1=Conv2d(dilation_channels,12,kernel_size=(1, tem_size),padding=(0,0),
stride=(1,1), bias=True)
self.supports=supports
self.bn=BatchNorm2d(in_dim,affine=False)
def forward(self,input):
x=self.bn(input)
adj=self.supports[0]
x=self.block1(x,adj)
x=self.block2(x,adj)
x=self.block3(x,adj)
x=self.conv1(x)#b,12,n,1
return x,adj,adj
class GRCN(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(GRCN,self).__init__()
self.block1=ST_BLOCK_5(in_dim,dilation_channels,num_nodes,length,K,Kt)
self.block2=ST_BLOCK_5(dilation_channels,dilation_channels,num_nodes,length,K,Kt)
self.tem_size=length
self.conv1=Conv2d(dilation_channels,out_dim,kernel_size=(1,length),
stride=(1,1), bias=True)
self.supports=supports
self.bn=BatchNorm2d(in_dim,affine=False)
def forward(self,input):
x=self.bn(input)
adj=self.supports[0]
x=self.block1(x,adj)
x=self.block2(x,adj)
x=self.conv1(x)
return x,adj,adj
class OGCRNN(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(OGCRNN,self).__init__()
self.block1=ST_BLOCK_5(in_dim,dilation_channels,num_nodes,length,K,Kt)
self.tem_size=length
self.conv1=Conv2d(dilation_channels,out_dim,kernel_size=(1,length),
stride=(1,1), bias=True)
self.supports=supports
self.bn=BatchNorm2d(in_dim,affine=False)
self.h=Parameter(torch.zeros(num_nodes,num_nodes), requires_grad=True)
nn.init.uniform_(self.h, a=0, b=0.0001)
def forward(self,input):
x=self.bn(input)
A=self.h+self.supports[0]
d=1/(torch.sum(A,-1)+0.0001)
D=torch.diag_embed(d)
A=torch.matmul(D,A)
A=F.dropout(A,0.5)
x=self.block1(x,A)
x=self.conv1(x)
return x,A,A
#OTSGGCN
class OTSGGCN(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(OTSGGCN,self).__init__()
tem_size=length
self.num_nodes=num_nodes
self.block1=ST_BLOCK_6(in_dim,dilation_channels,num_nodes,tem_size,K,Kt)
self.block2=ST_BLOCK_6(dilation_channels,dilation_channels,num_nodes,tem_size,K,Kt)
self.block3=ST_BLOCK_6(dilation_channels,dilation_channels,num_nodes,tem_size,K,Kt)
self.conv1=Conv2d(dilation_channels,12,kernel_size=(1, tem_size),padding=(0,0),
stride=(1,1), bias=True)
self.supports=supports
self.bn=BatchNorm2d(in_dim,affine=False)
self.h=Parameter(torch.ones(num_nodes,num_nodes), requires_grad=True)
#nn.init.uniform_(self.h, a=0, b=0.0001)
def forward(self,input):
x=input#self.bn(input)
mask=(self.supports[0]!=0).float()
A=self.h*mask
d=1/(torch.sum(A,-1)+0.0001)
D=torch.diag_embed(d)
A=torch.matmul(D,A)
A1=torch.eye(self.num_nodes).cuda()-A
# A1=F.dropout(A1,0.5)
x=self.block1(x,A1)
x=self.block2(x,A1)
x=self.block3(x,A1)
x=self.conv1(x)#b,12,n,1
return x,A1,A1
#gwnet
class gwnet(nn.Module):
def __init__(self, device, num_nodes, dropout=0.3, supports=None, length=12,
in_dim=1,out_dim=12,residual_channels=32,
dilation_channels=32,skip_channels=256,
end_channels=512,kernel_size=2,blocks=4,layers=2):
super(gwnet, self).__init__()
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.bn = nn.ModuleList()
self.gconv = nn.ModuleList()
self.start_conv = nn.Conv2d(in_channels=in_dim,
out_channels=residual_channels,
kernel_size=(1,1))
self.supports = supports
receptive_field = 1
self.supports_len = 0
if supports is not None:
self.supports_len += len(supports)
if supports is None:
self.supports = []
self.nodevec1 = nn.Parameter(torch.randn(num_nodes, 10).to(device), requires_grad=True).to(device)
self.nodevec2 = nn.Parameter(torch.randn(10, num_nodes).to(device), requires_grad=True).to(device)
self.supports_len +=1
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))
# 1x1 convolution for residual connection
self.residual_convs.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=residual_channels,
kernel_size=(1, 1)))
# 1x1 convolution for skip connection
self.skip_convs.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=skip_channels,
kernel_size=(1, 1)))
self.bn.append(nn.BatchNorm2d(residual_channels))
new_dilation *=2
receptive_field += additional_scope
additional_scope *= 2
self.gconv.append(multi_gcn(dilation_channels,residual_channels,dropout,support_len=self.supports_len))
self.end_conv_1 = nn.Conv2d(in_channels=skip_channels,
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
self.bn_1=BatchNorm2d(in_dim,affine=False)
def forward(self, input):
input=self.bn_1(input)
in_len = input.size(3)
if in_len<self.receptive_field:
x = nn.functional.pad(input,(self.receptive_field-in_len,0,0,0))
else:
x = input
x = self.start_conv(x)
skip = 0
# calculate the current adaptive adj matrix once per iteration
new_supports = None
if self.supports is not None:
adp = F.softmax(F.relu(torch.mm(self.nodevec1, self.nodevec2)), dim=1)
new_supports = self.supports + [adp]
# WaveNet layers
for i in range(self.blocks * self.layers):
# |----------------------------------------| *residual*
# | |
# | |-- conv -- tanh --| |
# -> dilate -|----| * ----|-- 1x1 -- + --> *input*
# |-- conv -- sigm --| |
# 1x1
# |
# ---------------------------------------> + -------------> *skip*
#(dilation, init_dilation) = self.dilations[i]
#residual = dilation_func(x, dilation, init_dilation, i)
residual = x
# dilated convolution
filter = self.filter_convs[i](residual)
filter = torch.tanh(filter)
gate = self.gate_convs[i](residual)
gate = torch.sigmoid(gate)
x = filter * gate
# parametrized skip connection
s = x
s = self.skip_convs[i](s)
try:
skip = skip[:, :, :, -s.size(3):]
except:
skip = 0
skip = s + skip
x = self.gconv[i](x, new_supports)
x = x + residual[:, :, :, -x.size(3):]
x = self.bn[i](x)
x = F.relu(skip)
x = F.relu(self.end_conv_1(x))
x = self.end_conv_2(x)
return x,adp,adp
class H_GCN_wh(nn.Module):
def __init__(self,device, num_nodes, dropout=0.3, supports=None,length=12,
in_dim=1,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(H_GCN_wh, self).__init__()
self.dropout = dropout
self.num_nodes=num_nodes
self.start_conv = nn.Conv2d(in_channels=in_dim,
out_channels=residual_channels,
kernel_size=(1,1))
self.supports = supports
self.supports_len = 0
if supports is not None:
self.supports_len += len(supports)
if supports is None:
self.supports = []
self.nodevec1 = nn.Parameter(torch.randn(num_nodes, 10).to(device), requires_grad=True).to(device)
self.nodevec2 = nn.Parameter(torch.randn(10, num_nodes).to(device), requires_grad=True).to(device)
self.h=Parameter(torch.zeros(num_nodes,num_nodes), requires_grad=True)
nn.init.uniform_(self.h, a=0, b=0.0001)
self.supports_len +=1
Kt1=2
self.block1=GCNPool(dilation_channels,dilation_channels,num_nodes,length-6,3,dropout,num_nodes,
self.supports_len)
self.block2=GCNPool(dilation_channels,dilation_channels,num_nodes,length-9,2,dropout,num_nodes,
self.supports_len)
self.skip_conv1=Conv2d(dilation_channels,skip_channels,kernel_size=(1,1),
stride=(1,1), bias=True)
self.skip_conv2=Conv2d(dilation_channels,skip_channels,kernel_size=(1,1),
stride=(1,1), bias=True)
self.end_conv_1 = nn.Conv2d(in_channels=skip_channels,
out_channels=end_channels,
kernel_size=(1,3),
bias=True)
self.end_conv_2 = nn.Conv2d(in_channels=end_channels,
out_channels=out_dim,
kernel_size=(1,1),
bias=True)
self.bn=BatchNorm2d(in_dim,affine=False)
def forward(self, input):
x=self.bn(input)
shape=x.shape
if self.supports is not None:
#nodes
#A=A+self.supports[0]
A=F.relu(torch.mm(self.nodevec1, self.nodevec2))
d=1/(torch.sum(A,-1))
D=torch.diag_embed(d)
A=torch.matmul(D,A)
new_supports = self.supports + [A]
skip=0
x = self.start_conv(x)
#1
x=self.block1(x,new_supports)
s1=self.skip_conv1(x)
skip=s1+skip
#2
x=self.block2(x,new_supports)
s2=self.skip_conv2(x)
skip = skip[:, :, :, -s2.size(3):]
skip = s2 + skip
#output
x = F.relu(skip)
x = F.relu(self.end_conv_1(x))
x = self.end_conv_2(x)
return x,x,A
class H_GCN_wdf(nn.Module):
def __init__(self,device, num_nodes, cluster_nodes,dropout=0.3, supports=None,supports_cluster=None,transmit=None,length=12,
in_dim=1,in_dim_cluster=3,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(H_GCN_wdf, self).__init__()
self.dropout = dropout
self.num_nodes=num_nodes
self.transmit=transmit
self.cluster_nodes=cluster_nodes
self.start_conv = nn.Conv2d(in_channels=in_dim,
out_channels=residual_channels,
kernel_size=(1,1))
self.start_conv_cluster = nn.Conv2d(in_channels=in_dim_cluster,
out_channels=residual_channels,
kernel_size=(1,1))
self.supports = supports
self.supports_cluster = supports_cluster
self.supports_len = 0
self.supports_len_cluster = 0
if supports is not None:
self.supports_len += len(supports)
self.supports_len_cluster+=len(supports_cluster)
if supports is None:
self.supports = []
self.supports_cluster = []
self.h=Parameter(torch.zeros(num_nodes,num_nodes), requires_grad=True)
nn.init.uniform_(self.h, a=0, b=0.0001)
self.h_cluster=Parameter(torch.zeros(cluster_nodes,cluster_nodes), requires_grad=True)
nn.init.uniform_(self.h_cluster, a=0, b=0.0001)
self.supports_len +=1
self.supports_len_cluster +=1
self.nodevec1 = nn.Parameter(torch.randn(num_nodes, 10).to(device), requires_grad=True).to(device)
self.nodevec2 = nn.Parameter(torch.randn(10, num_nodes).to(device), requires_grad=True).to(device)
self.nodevec1_c = nn.Parameter(torch.randn(cluster_nodes, 10).to(device), requires_grad=True).to(device)
self.nodevec2_c = nn.Parameter(torch.randn(10,cluster_nodes).to(device), requires_grad=True).to(device)
self.block1=GCNPool(2*dilation_channels,dilation_channels,num_nodes,length-6,3,dropout,num_nodes,
self.supports_len)
self.block2=GCNPool(2*dilation_channels,dilation_channels,num_nodes,length-9,2,dropout,num_nodes,
self.supports_len)
self.block_cluster1=GCNPool(dilation_channels,dilation_channels,cluster_nodes,length-6,3,dropout,cluster_nodes,
self.supports_len)
self.block_cluster2=GCNPool(dilation_channels,dilation_channels,cluster_nodes,length-9,2,dropout,cluster_nodes,
self.supports_len)
self.skip_conv1=Conv2d(2*dilation_channels,skip_channels,kernel_size=(1,1),
stride=(1,1), bias=True)
self.skip_conv2=Conv2d(2*dilation_channels,skip_channels,kernel_size=(1,1),
stride=(1,1), bias=True)
self.end_conv_1 = nn.Conv2d(in_channels=skip_channels,
out_channels=end_channels,
kernel_size=(1,3),
bias=True)
self.end_conv_2 = nn.Conv2d(in_channels=end_channels,
out_channels=out_dim,
kernel_size=(1,1),
bias=True)
self.bn=BatchNorm2d(in_dim,affine=False)
self.conv_cluster1=Conv2d(dilation_channels,out_dim,kernel_size=(1,3),
stride=(1,1), bias=True)
self.bn_cluster=BatchNorm2d(in_dim_cluster,affine=False)
self.gate1=gate(2*dilation_channels)
self.gate2=gate(2*dilation_channels)
self.gate3=gate(2*dilation_channels)
def forward(self, input, input_cluster):
x=self.bn(input)
shape=x.shape
input_c=input_cluster
x_cluster=self.bn_cluster(input_c)
if self.supports is not None:
#nodes
A=F.relu(torch.mm(self.nodevec1, self.nodevec2))
d=1/(torch.sum(A,-1))
D=torch.diag_embed(d)
A=torch.matmul(D,A)
new_supports = self.supports + [A]
#region
A_cluster=F.relu(torch.mm(self.nodevec1_c, self.nodevec2_c))
d_c=1/(torch.sum(A_cluster,-1))
D_c=torch.diag_embed(d_c)
A_cluster=torch.matmul(D_c,A_cluster)
new_supports_cluster = self.supports_cluster + [A_cluster]
#network
transmit=self.transmit
x = self.start_conv(x)
x_cluster = self.start_conv_cluster(x_cluster)
x_1=(torch.einsum('mn,bcnl->bcml',transmit,x_cluster))
x=self.gate1(x,x_1)
skip=0
skip_c=0
#1
x_cluster=self.block_cluster1(x_cluster,new_supports_cluster)
x=self.block1(x,new_supports)
x_2=(torch.einsum('mn,bcnl->bcml',transmit,x_cluster))
x=self.gate2(x,x_2)
s1=self.skip_conv1(x)
skip=s1+skip
#2
x_cluster=self.block_cluster2(x_cluster,new_supports_cluster)
x=self.block2(x,new_supports)
x_3=(torch.einsum('mn,bcnl->bcml',transmit,x_cluster))
x=self.gate3(x,x_3)
s2=self.skip_conv2(x)
skip = skip[:, :, :, -s2.size(3):]
skip = s2 + skip
#output
x = F.relu(skip)
x = F.relu(self.end_conv_1(x))
x = self.end_conv_2(x)
return x,A,A
class H_GCN(nn.Module):
def __init__(self,device, num_nodes, cluster_nodes,dropout=0.3, supports=None,supports_cluster=None,transmit=None,length=12,
in_dim=1,in_dim_cluster=3,out_dim=12,residual_channels=32,dilation_channels=32,
skip_channels=256,end_channels=512,kernel_size=2,K=3,Kt=3):
super(H_GCN, self).__init__()
self.dropout = dropout
self.num_nodes=num_nodes
self.transmit=transmit
self.cluster_nodes=cluster_nodes
self.start_conv = nn.Conv2d(in_channels=in_dim,
out_channels=residual_channels,
kernel_size=(1,1))
self.start_conv_cluster = nn.Conv2d(in_channels=in_dim_cluster,
out_channels=residual_channels,
kernel_size=(1,1))
self.supports = supports
self.supports_cluster = supports_cluster
self.supports_len = 0
self.supports_len_cluster = 0
if supports is not None:
self.supports_len += len(supports)
self.supports_len_cluster+=len(supports_cluster)
if supports is None:
self.supports = []
self.supports_cluster = []
self.h=Parameter(torch.zeros(num_nodes,num_nodes), requires_grad=True)
nn.init.uniform_(self.h, a=0, b=0.0001)
self.h_cluster=Parameter(torch.zeros(cluster_nodes,cluster_nodes), requires_grad=True)
nn.init.uniform_(self.h_cluster, a=0, b=0.0001)
self.supports_len +=1
self.supports_len_cluster +=1
self.nodevec1 = nn.Parameter(torch.randn(num_nodes, 10).to(device), requires_grad=True).to(device)
self.nodevec2 = nn.Parameter(torch.randn(10, num_nodes).to(device), requires_grad=True).to(device)
self.nodevec1_c = nn.Parameter(torch.randn(cluster_nodes, 10).to(device), requires_grad=True).to(device)
self.nodevec2_c = nn.Parameter(torch.randn(10,cluster_nodes).to(device), requires_grad=True).to(device)
self.block1=GCNPool(2*dilation_channels,dilation_channels,num_nodes,length-6,3,dropout,num_nodes,
self.supports_len)
self.block2=GCNPool(2*dilation_channels,dilation_channels,num_nodes,length-9,2,dropout,num_nodes,
self.supports_len)
self.block_cluster1=GCNPool(dilation_channels,dilation_channels,cluster_nodes,length-6,3,dropout,cluster_nodes,
self.supports_len)
self.block_cluster2=GCNPool(dilation_channels,dilation_channels,cluster_nodes,length-9,2,dropout,cluster_nodes,
self.supports_len)
self.skip_conv1=Conv2d(2*dilation_channels,skip_channels,kernel_size=(1,1),
stride=(1,1), bias=True)
self.skip_conv2=Conv2d(2*dilation_channels,skip_channels,kernel_size=(1,1),
stride=(1,1), bias=True)
self.end_conv_1 = nn.Conv2d(in_channels=skip_channels,
out_channels=end_channels,
kernel_size=(1,3),
bias=True)
self.end_conv_2 = nn.Conv2d(in_channels=end_channels,
out_channels=out_dim,
kernel_size=(1,1),
bias=True)
self.bn=BatchNorm2d(in_dim,affine=False)
self.conv_cluster1=Conv2d(dilation_channels,out_dim,kernel_size=(1,3),
stride=(1,1), bias=True)
self.bn_cluster=BatchNorm2d(in_dim_cluster,affine=False)
self.gate1=gate(2*dilation_channels)
self.gate2=gate(2*dilation_channels)
self.gate3=gate(2*dilation_channels)
self.transmit1=Transmit(dilation_channels,length,transmit,num_nodes,cluster_nodes)
self.transmit2=Transmit(dilation_channels,length-6,transmit,num_nodes,cluster_nodes)
self.transmit3=Transmit(dilation_channels,length-9,transmit,num_nodes,cluster_nodes)
def forward(self, input, input_cluster):
x=self.bn(input)
shape=x.shape
input_c=input_cluster
x_cluster=self.bn_cluster(input_c)
if self.supports is not None:
#nodes
A=F.relu(torch.mm(self.nodevec1, self.nodevec2))
d=1/(torch.sum(A,-1))
D=torch.diag_embed(d)
A=torch.matmul(D,A)
new_supports = self.supports + [A]
#region
A_cluster=F.relu(torch.mm(self.nodevec1_c, self.nodevec2_c))
d_c=1/(torch.sum(A_cluster,-1))
D_c=torch.diag_embed(d_c)
A_cluster=torch.matmul(D_c,A_cluster)
new_supports_cluster = self.supports_cluster + [A_cluster]
#network
transmit=self.transmit
x = self.start_conv(x)
x_cluster = self.start_conv_cluster(x_cluster)
transmit1 = self.transmit1(x,x_cluster)
x_1=(torch.einsum('bmn,bcnl->bcml',transmit1,x_cluster))
x=self.gate1(x,x_1)
skip=0
skip_c=0
#1
x_cluster=self.block_cluster1(x_cluster,new_supports_cluster)
x=self.block1(x,new_supports)
transmit2 = self.transmit2(x,x_cluster)
x_2=(torch.einsum('bmn,bcnl->bcml',transmit2,x_cluster))
x=self.gate2(x,x_2)
s1=self.skip_conv1(x)
skip=s1+skip
#2
x_cluster=self.block_cluster2(x_cluster,new_supports_cluster)
x=self.block2(x,new_supports)
transmit3 = self.transmit3(x,x_cluster)
x_3=(torch.einsum('bmn,bcnl->bcml',transmit3,x_cluster))
x=self.gate3(x,x_3)
s2=self.skip_conv2(x)
skip = skip[:, :, :, -s2.size(3):]
skip = s2 + skip
#output
x = F.relu(skip)
x = F.relu(self.end_conv_1(x))
x = self.end_conv_2(x)
return x,transmit3,A