-
Notifications
You must be signed in to change notification settings - Fork 5
/
pan_benchmark.py
918 lines (692 loc) · 33.5 KB
/
pan_benchmark.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
#import sys
#import inspect
import torch
import torch.nn.functional as F
from torch.nn import Parameter
from torch_scatter import scatter_add, scatter_max, scatter_mean
from torch_geometric.utils import softmax, degree
from torch_geometric.nn import MessagePassing
from torch_geometric.data import DataLoader, Data
from torch_geometric.datasets import TUDataset
from torch_geometric.utils.num_nodes import maybe_num_nodes
#from torch_geometric.nn.pool import TopKPooling, SAGPooling
from torch.utils.data import random_split
from torch_sparse import spspmm
from torch_sparse import coalesce
from torch_sparse import eye
#from collections import OrderedDict
import os
import scipy.io as sio
import numpy as np
from optparse import OptionParser
import time
#import gdown
#import zipfile
#CUDA_visible_devices = 1
#seed = 11
#np.random.seed(seed)
#torch.manual_seed(seed)
#torch.cuda.manual_seed(seed)
#torch.cuda.manual_seed_all(seed)
##torch.cuda.seed_all(seed)
#torch.backends.cudnn.deterministic = True
#torch.backends.cudnn.benchmark = False
### define convolution
class PANConv(MessagePassing):
def __init__(self, in_channels, out_channels, filter_size=4, panconv_filter_weight=None):
super(PANConv, self).__init__(aggr='add') # "Add" aggregation.
self.lin = torch.nn.Linear(in_channels, out_channels)
self.m = None
self.filter_size = filter_size
if panconv_filter_weight is None:
self.panconv_filter_weight = torch.nn.Parameter(0.5 * torch.ones(filter_size), requires_grad=True)
def forward(self, x, edge_index, num_nodes=None, edge_mask_list=None):
# x has shape [N, in_channels]
if edge_mask_list is None:
AFTERDROP = False
else:
AFTERDROP = True
# edge_index has shape [2, E]
num_nodes = maybe_num_nodes(edge_index, num_nodes)
# Step 1: Path integral
edge_index, edge_weight = self.panentropy_sparse(edge_index, num_nodes, AFTERDROP, edge_mask_list)
# Step 2: Linearly transform node feature matrix.
x = self.lin(x)
x_size0 = x.size(0)
# Step 3: Compute normalization
row, col = edge_index
deg = degree(row, x_size0, dtype=x.dtype)
deg_inv_sqrt = deg.pow(-0.5)
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
norm = norm.mul(edge_weight)
# save M
m_list = norm.mul(edge_weight).view(-1, 1).squeeze()
m_adj = torch.zeros(x_size0, x_size0, device=edge_index.device)
m_adj[row, col] = m_list
self.m = m_adj
# Step 4-6: Start propagating messages.
return self.propagate(edge_index, size=(x_size0, x_size0), x=x, norm=norm)
def message(self, x_j, norm):
# x_j has shape [E, out_channels]
return norm.view(-1, 1) * x_j
def update(self, aggr_out):
# aggr_out has shape [N, out_channels]
# Step 5: Return new node embeddings.
return aggr_out
def panentropy(self, edge_index, num_nodes):
# sparse to dense
# adj = to_dense_adj(edge_index)
adj = torch.zeros(num_nodes, num_nodes, device=edge_index.device)
adj[edge_index[0, :], edge_index[1, :]] = 1
# iteratively add weighted matrix power
adjtmp = torch.eye(num_nodes, device=edge_index.device)
pan_adj = self.panconv_filter_weight[0] * torch.eye(num_nodes, device=edge_index.device)
for i in range(self.filter_size - 1):
adjtmp = torch.mm(adjtmp, adj)
pan_adj = pan_adj + self.panconv_filter_weight[i+1] * adjtmp
# dense to sparse
edge_index_new = torch.nonzero(pan_adj).t()
edge_weight_new = pan_adj[edge_index_new[0], edge_index_new[1]]
return edge_index_new, edge_weight_new
def panentropy_sparse(self, edge_index, num_nodes, AFTERDROP, edge_mask_list):
edge_value = torch.ones(edge_index.size(1), device=edge_index.device)
edge_index, edge_value = coalesce(edge_index, edge_value, num_nodes, num_nodes)
# iteratively add weighted matrix power
pan_index, pan_value = eye(num_nodes, device=edge_index.device)
indextmp = pan_index.clone().to(edge_index.device)
valuetmp = pan_value.clone().to(edge_index.device)
pan_value = self.panconv_filter_weight[0] * pan_value
for i in range(self.filter_size - 1):
if AFTERDROP:
indextmp, valuetmp = spspmm(indextmp, valuetmp, edge_index, edge_value * edge_mask_list[i], num_nodes, num_nodes, num_nodes)
else:
indextmp, valuetmp = spspmm(indextmp, valuetmp, edge_index, edge_value, num_nodes, num_nodes, num_nodes)
valuetmp = valuetmp * self.panconv_filter_weight[i+1]
indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
pan_index = torch.cat((pan_index, indextmp), 1)
pan_value = torch.cat((pan_value, valuetmp))
return coalesce(pan_index, pan_value, num_nodes, num_nodes, op='add')
### define pooling
class PANPooling(torch.nn.Module):
r""" General Graph pooling layer based on PAN, which can work with all layers.
"""
def __init__(self, in_channels, ratio=0.5, pan_pool_weight=None, min_score=None, multiplier=1,
nonlinearity=torch.tanh, filter_size=3, panpool_filter_weight=None):
super(PANPooling, self).__init__()
self.in_channels = in_channels
self.ratio = ratio
self.min_score = min_score
self.multiplier = multiplier
self.nonlinearity = nonlinearity
self.filter_size = filter_size
if panpool_filter_weight is None:
self.panpool_filter_weight = torch.nn.Parameter(0.5 * torch.ones(filter_size), requires_grad=True)
self.transform = Parameter(torch.ones(in_channels), requires_grad=True)
if pan_pool_weight is None:
#self.weight = torch.tensor([0.7, 0.3], device=self.transform.device)
self.pan_pool_weight = torch.nn.Parameter(0.5 * torch.ones(2), requires_grad=True)
else:
self.pan_pool_weight = pan_pool_weight
def forward(self, x, edge_index, M=None, batch=None, num_nodes=None):
""""""
if batch is None:
batch = edge_index.new_zeros(x.size(0))
# Path integral
num_nodes = maybe_num_nodes(edge_index, num_nodes)
edge_index, edge_weight = self.panentropy_sparse(edge_index, num_nodes)
# weighted degree
num_nodes = x.size(0)
degree = torch.zeros(num_nodes, device=edge_index.device)
degree = scatter_add(edge_weight, edge_index[0], out=degree)
# linear transform
xtransform = torch.matmul(x, self.transform)
# aggregate score
x_transform_norm = xtransform #/ xtransform.norm(p=2, dim=-1)
degree_norm = degree #/ degree.norm(p=2, dim=-1)
score = self.pan_pool_weight[0] * x_transform_norm + self.pan_pool_weight[1] * degree_norm
if self.min_score is None:
score = self.nonlinearity(score)
else:
score = softmax(score, batch)
perm = self.topk(score, self.ratio, batch, self.min_score)
x = x[perm] * score[perm].view(-1, 1)
x = self.multiplier * x if self.multiplier != 1 else x
batch = batch[perm]
edge_index, edge_weight = self.filter_adj(edge_index, edge_weight, perm, num_nodes=score.size(0))
return x, edge_index, edge_weight, batch, perm, score[perm]
def topk(self, x, ratio, batch, min_score=None, tol=1e-7):
if min_score is not None:
# Make sure that we do not drop all nodes in a graph.
scores_max = scatter_max(x, batch)[0][batch] - tol
scores_min = scores_max.clamp(max=min_score)
perm = torch.nonzero(x > scores_min).view(-1)
else:
num_nodes = scatter_add(batch.new_ones(x.size(0)), batch, dim=0)
batch_size, max_num_nodes = num_nodes.size(0), num_nodes.max().item()
cum_num_nodes = torch.cat(
[num_nodes.new_zeros(1),
num_nodes.cumsum(dim=0)[:-1]], dim=0)
index = torch.arange(batch.size(0), dtype=torch.long, device=x.device)
index = (index - cum_num_nodes[batch]) + (batch * max_num_nodes)
dense_x = x.new_full((batch_size * max_num_nodes, ), -2)
dense_x[index] = x
dense_x = dense_x.view(batch_size, max_num_nodes)
_, perm = dense_x.sort(dim=-1, descending=True)
perm = perm + cum_num_nodes.view(-1, 1)
perm = perm.view(-1)
k = (ratio * num_nodes.to(torch.float)).ceil().to(torch.long)
mask = [
torch.arange(k[i], dtype=torch.long, device=x.device) +
i * max_num_nodes for i in range(batch_size)
]
mask = torch.cat(mask, dim=0)
perm = perm[mask]
return perm
def filter_adj(self, edge_index, edge_weight, perm, num_nodes=None):
num_nodes = maybe_num_nodes(edge_index, num_nodes)
mask = perm.new_full((num_nodes, ), -1)
i = torch.arange(perm.size(0), dtype=torch.long, device=perm.device)
mask[perm] = i
row, col = edge_index
row, col = mask[row], mask[col]
mask = (row >= 0) & (col >= 0)
row, col = row[mask], col[mask]
if edge_weight is not None:
edge_weight = edge_weight[mask]
return torch.stack([row, col], dim=0), edge_weight
def panentropy_sparse(self, edge_index, num_nodes):
edge_value = torch.ones(edge_index.size(1), device=edge_index.device)
edge_index, edge_value = coalesce(edge_index, edge_value, num_nodes, num_nodes)
# iteratively add weighted matrix power
pan_index, pan_value = eye(num_nodes, device=edge_index.device)
indextmp = pan_index.clone().to(edge_index.device)
valuetmp = pan_value.clone().to(edge_index.device)
pan_value = self.panpool_filter_weight[0] * pan_value
for i in range(self.filter_size - 1):
#indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
indextmp, valuetmp = spspmm(indextmp, valuetmp, edge_index, edge_value, num_nodes, num_nodes, num_nodes)
valuetmp = valuetmp * self.panpool_filter_weight[i+1]
indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
pan_index = torch.cat((pan_index, indextmp), 1)
pan_value = torch.cat((pan_value, valuetmp))
return coalesce(pan_index, pan_value, num_nodes, num_nodes, op='add')
# equation 14
class PANUMPooling(torch.nn.Module):
r""" Specific Graph pooling layer based on unnormalized M from PAN, which can only work after PANConv.
"""
def __init__(self, in_channels, ratio=0.5, min_score=None, multiplier=1, nonlinearity=torch.tanh):
super(PANUMPooling, self).__init__()
self.in_channels = in_channels
self.ratio = ratio
self.min_score = min_score
self.multiplier = multiplier
self.nonlinearity = nonlinearity
def forward(self, x, edge_index, edge_weight=None, M=None, UM=None, batch=None, num_nodes=None):
""""""
if batch is None:
batch = edge_index.new_zeros(x.size(0))
if edge_weight is None:
edge_weight = torch.ones(edge_index.size(1), device=edge_index.device)
# compute score
diag_UM = torch.diag(UM)
score = diag_UM.squeeze()
if self.min_score is None:
score = self.nonlinearity(score)
else:
score = softmax(score, batch)
perm = self.topk(score, self.ratio, batch, self.min_score)
x = x[perm] * score[perm].view(-1, 1)
x = self.multiplier * x if self.multiplier != 1 else x
batch = batch[perm]
edge_index, edge_weight = self.filter_adj(edge_index, edge_weight, perm, num_nodes=score.size(0))
return x, edge_index, edge_weight, batch, perm, score[perm]
def topk(self, x, ratio, batch, min_score=None, tol=1e-7):
if min_score is not None:
# Make sure that we do not drop all nodes in a graph.
scores_max = scatter_max(x, batch)[0][batch] - tol
scores_min = scores_max.clamp(max=min_score)
perm = torch.nonzero(x > scores_min).view(-1)
else:
num_nodes = scatter_add(batch.new_ones(x.size(0)), batch, dim=0)
batch_size, max_num_nodes = num_nodes.size(0), num_nodes.max().item()
cum_num_nodes = torch.cat(
[num_nodes.new_zeros(1),
num_nodes.cumsum(dim=0)[:-1]], dim=0)
index = torch.arange(batch.size(0), dtype=torch.long, device=x.device)
index = (index - cum_num_nodes[batch]) + (batch * max_num_nodes)
dense_x = x.new_full((batch_size * max_num_nodes, ), -2)
dense_x[index] = x
dense_x = dense_x.view(batch_size, max_num_nodes)
_, perm = dense_x.sort(dim=-1, descending=True)
perm = perm + cum_num_nodes.view(-1, 1)
perm = perm.view(-1)
k = (ratio * num_nodes.to(torch.float)).ceil().to(torch.long)
mask = [
torch.arange(k[i], dtype=torch.long, device=x.device) +
i * max_num_nodes for i in range(batch_size)
]
mask = torch.cat(mask, dim=0)
perm = perm[mask]
return perm
def filter_adj(self, edge_index, edge_weight, perm, num_nodes=None):
num_nodes = maybe_num_nodes(edge_index, num_nodes)
mask = perm.new_full((num_nodes, ), -1)
i = torch.arange(perm.size(0), dtype=torch.long, device=perm.device)
mask[perm] = i
row, col = edge_index
row, col = mask[row], mask[col]
mask = (row >= 0) & (col >= 0)
row, col = row[mask], col[mask]
if edge_weight is not None:
edge_weight = edge_weight[mask]
return torch.stack([row, col], dim=0), edge_weight
# equation 15
class PANXUMPooling(torch.nn.Module):
r""" General Graph pooling layer based on PAN, which can work with all layers.
"""
def __init__(self, in_channels, ratio=0.5, pan_pool_weight=None, min_score=None, multiplier=1,
nonlinearity=torch.tanh, filter_size=3, panpool_filter_weight=None):
super(PANXUMPooling, self).__init__()
self.in_channels = in_channels
self.ratio = ratio
self.min_score = min_score
self.multiplier = multiplier
self.nonlinearity = nonlinearity
self.transform = Parameter(torch.ones(in_channels), requires_grad=True)
if pan_pool_weight is None:
#self.weight = torch.tensor([0.7, 0.3], device=self.transform.device)
self.pan_pool_weight = torch.nn.Parameter(0.5 * torch.ones(2), requires_grad=True)
else:
self.pan_pool_weight = pan_pool_weight
def forward(self, x, edge_index, edge_weight=None, M=None, UM=None, batch=None, num_nodes=None):
""""""
if batch is None:
batch = edge_index.new_zeros(x.size(0))
if edge_weight is None:
edge_weight = torch.ones(edge_index.size(1), device=edge_index.device)
# diag of unnormalized M
diag_UM = torch.diag(UM).squeeze()
# linear transform
xtransform = torch.matmul(x, self.transform)
# aggregate score
score = self.pan_pool_weight[0] * xtransform + self.pan_pool_weight[1] * diag_UM
if self.min_score is None:
score = self.nonlinearity(score)
else:
score = softmax(score, batch)
perm = self.topk(score, self.ratio, batch, self.min_score)
x = x[perm] * score[perm].view(-1, 1)
x = self.multiplier * x if self.multiplier != 1 else x
batch = batch[perm]
edge_index, edge_weight = self.filter_adj(edge_index, edge_weight, perm, num_nodes=score.size(0))
return x, edge_index, edge_weight, batch, perm, score[perm]
def topk(self, x, ratio, batch, min_score=None, tol=1e-7):
if min_score is not None:
# Make sure that we do not drop all nodes in a graph.
scores_max = scatter_max(x, batch)[0][batch] - tol
scores_min = scores_max.clamp(max=min_score)
perm = torch.nonzero(x > scores_min).view(-1)
else:
num_nodes = scatter_add(batch.new_ones(x.size(0)), batch, dim=0)
batch_size, max_num_nodes = num_nodes.size(0), num_nodes.max().item()
cum_num_nodes = torch.cat(
[num_nodes.new_zeros(1),
num_nodes.cumsum(dim=0)[:-1]], dim=0)
index = torch.arange(batch.size(0), dtype=torch.long, device=x.device)
index = (index - cum_num_nodes[batch]) + (batch * max_num_nodes)
dense_x = x.new_full((batch_size * max_num_nodes, ), -2)
dense_x[index] = x
dense_x = dense_x.view(batch_size, max_num_nodes)
_, perm = dense_x.sort(dim=-1, descending=True)
perm = perm + cum_num_nodes.view(-1, 1)
perm = perm.view(-1)
k = (ratio * num_nodes.to(torch.float)).ceil().to(torch.long)
mask = [
torch.arange(k[i], dtype=torch.long, device=x.device) +
i * max_num_nodes for i in range(batch_size)
]
mask = torch.cat(mask, dim=0)
perm = perm[mask]
return perm
def filter_adj(self, edge_index, edge_weight, perm, num_nodes=None):
num_nodes = maybe_num_nodes(edge_index, num_nodes)
mask = perm.new_full((num_nodes, ), -1)
i = torch.arange(perm.size(0), dtype=torch.long, device=perm.device)
mask[perm] = i
row, col = edge_index
row, col = mask[row], mask[col]
mask = (row >= 0) & (col >= 0)
row, col = row[mask], col[mask]
if edge_weight is not None:
edge_weight = edge_weight[mask]
return torch.stack([row, col], dim=0), edge_weight
def panentropy_sparse(self, edge_index, num_nodes):
edge_value = torch.ones(edge_index.size(1), device=edge_index.device)
edge_index, edge_value = coalesce(edge_index, edge_value, num_nodes, num_nodes)
# iteratively add weighted matrix power
pan_index, pan_value = eye(num_nodes, device=edge_index.device)
indextmp = pan_index.clone().to(edge_index.device)
valuetmp = pan_value.clone().to(edge_index.device)
pan_value = self.panpool_filter_weight[0] * pan_value
for i in range(self.filter_size - 1):
#indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
indextmp, valuetmp = spspmm(indextmp, valuetmp, edge_index, edge_value, num_nodes, num_nodes, num_nodes)
valuetmp = valuetmp * self.panpool_filter_weight[i+1]
indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
pan_index = torch.cat((pan_index, indextmp), 1)
pan_value = torch.cat((pan_value, valuetmp))
return coalesce(pan_index, pan_value, num_nodes, num_nodes, op='add')
# equation 16
class PANXHMPooling(torch.nn.Module):
r""" General Graph pooling layer based on PAN, which can work with all layers.
"""
def __init__(self, in_channels, ratio=0.5, pan_pool_weight=None, min_score=None, multiplier=1,
nonlinearity=torch.tanh, filter_size=3, panpool_filter_weight=None):
super(PANXHMPooling, self).__init__()
self.in_channels = in_channels
self.ratio = ratio
self.min_score = min_score
self.multiplier = multiplier
self.nonlinearity = nonlinearity
self.transform = Parameter(torch.ones(in_channels), requires_grad=True)
def forward(self, x, edge_index, edge_weight=None, M=None, UM=None, batch=None, num_nodes=None):
""""""
if batch is None:
batch = edge_index.new_zeros(x.size(0))
if edge_weight is None:
edge_weight = torch.ones(edge_index.size(1), device=edge_index.device)
# diag of unnormalized M
diag_M = torch.diag(M).squeeze()
# linear transform
xtransform = torch.matmul(x, self.transform)
# aggregate score
score = xtransform * diag_M
if self.min_score is None:
score = self.nonlinearity(score)
else:
score = softmax(score, batch)
perm = self.topk(score, self.ratio, batch, self.min_score)
x = x[perm] * score[perm].view(-1, 1)
x = self.multiplier * x if self.multiplier != 1 else x
batch = batch[perm]
edge_index, edge_weight = self.filter_adj(edge_index, edge_weight, perm, num_nodes=score.size(0))
return x, edge_index, edge_weight, batch, perm, score[perm]
def topk(self, x, ratio, batch, min_score=None, tol=1e-7):
if min_score is not None:
# Make sure that we do not drop all nodes in a graph.
scores_max = scatter_max(x, batch)[0][batch] - tol
scores_min = scores_max.clamp(max=min_score)
perm = torch.nonzero(x > scores_min).view(-1)
else:
num_nodes = scatter_add(batch.new_ones(x.size(0)), batch, dim=0)
batch_size, max_num_nodes = num_nodes.size(0), num_nodes.max().item()
cum_num_nodes = torch.cat(
[num_nodes.new_zeros(1),
num_nodes.cumsum(dim=0)[:-1]], dim=0)
index = torch.arange(batch.size(0), dtype=torch.long, device=x.device)
index = (index - cum_num_nodes[batch]) + (batch * max_num_nodes)
dense_x = x.new_full((batch_size * max_num_nodes, ), -2)
dense_x[index] = x
dense_x = dense_x.view(batch_size, max_num_nodes)
_, perm = dense_x.sort(dim=-1, descending=True)
perm = perm + cum_num_nodes.view(-1, 1)
perm = perm.view(-1)
k = (ratio * num_nodes.to(torch.float)).ceil().to(torch.long)
mask = [
torch.arange(k[i], dtype=torch.long, device=x.device) +
i * max_num_nodes for i in range(batch_size)
]
mask = torch.cat(mask, dim=0)
perm = perm[mask]
return perm
def filter_adj(self, edge_index, edge_weight, perm, num_nodes=None):
num_nodes = maybe_num_nodes(edge_index, num_nodes)
mask = perm.new_full((num_nodes, ), -1)
i = torch.arange(perm.size(0), dtype=torch.long, device=perm.device)
mask[perm] = i
row, col = edge_index
row, col = mask[row], mask[col]
mask = (row >= 0) & (col >= 0)
row, col = row[mask], col[mask]
if edge_weight is not None:
edge_weight = edge_weight[mask]
return torch.stack([row, col], dim=0), edge_weight
def panentropy_sparse(self, edge_index, num_nodes):
edge_value = torch.ones(edge_index.size(1), device=edge_index.device)
edge_index, edge_value = coalesce(edge_index, edge_value, num_nodes, num_nodes)
# iteratively add weighted matrix power
pan_index, pan_value = eye(num_nodes, device=edge_index.device)
indextmp = pan_index.clone().to(edge_index.device)
valuetmp = pan_value.clone().to(edge_index.device)
pan_value = self.panpool_filter_weight[0] * pan_value
for i in range(self.filter_size - 1):
#indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
indextmp, valuetmp = spspmm(indextmp, valuetmp, edge_index, edge_value, num_nodes, num_nodes, num_nodes)
valuetmp = valuetmp * self.panpool_filter_weight[i+1]
indextmp, valuetmp = coalesce(indextmp, valuetmp, num_nodes, num_nodes)
pan_index = torch.cat((pan_index, indextmp), 1)
pan_value = torch.cat((pan_value, valuetmp))
return coalesce(pan_index, pan_value, num_nodes, num_nodes, op='add')
### define dropout
class PANDropout(torch.nn.Module):
def __init__(self, filter_size=4):
super(PANDropout, self).__init__()
self.filter_size =filter_size
def forward(self, edge_index, p=0.5):
# p - probability of an element to be zeroed
# sava all network
#edge_mask_list = []
edge_mask_list = torch.empty(0)
edge_mask_list.to(edge_index.device)
num = edge_index.size(1)
bern = torch.distributions.bernoulli.Bernoulli(torch.tensor([p]))
for i in range(self.filter_size - 1):
edge_mask = bern.sample([num]).squeeze()
#edge_mask_list.append(edge_mask)
edge_mask_list = torch.cat([edge_mask_list, edge_mask])
return True, edge_mask_list
### build model
class PAN(torch.nn.Module):
def __init__(self, num_node_features, num_classes, nhid, ratio, filter_size):
super(PAN, self).__init__()
self.conv1 = PANConv(num_node_features, nhid, filter_size)
self.pool1 = PANPooling(nhid, filter_size=filter_size)
## self.drop1 = PANDropout()
self.conv2 = PANConv(nhid, nhid, filter_size=2)
self.pool2 = PANPooling(nhid)
## self.drop2 = PANDropout()
self.conv3 = PANConv(nhid, nhid, filter_size=2)
self.pool3 = PANPooling(nhid)
self.lin1 = torch.nn.Linear(nhid, nhid//2)
self.lin2 = torch.nn.Linear(nhid//2, nhid//4)
self.lin3 = torch.nn.Linear(nhid//4, num_classes)
self.mlp = torch.nn.Linear(nhid, num_classes)
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
perm_list = list()
edge_mask_list = None
x = self.conv1(x, edge_index)
M = self.conv1.m
x, edge_index, _, batch, perm, score_perm = self.pool1(x, edge_index, batch=batch, M=M)
perm_list.append(perm)
# AFTERDROP, edge_mask_list = self.drop1(edge_index, p=0.5)
x = self.conv2(x, edge_index, edge_mask_list=edge_mask_list)
M = self.conv2.m
x, edge_index, _, batch, perm, score_perm = self.pool2(x, edge_index, batch=batch, M=M)
perm_list.append(perm)
#
## AFTERDROP, edge_mask_list = self.drop2(edge_index, p=0.5)
x = self.conv3(x, edge_index, edge_mask_list=edge_mask_list)
M = self.conv3.m
x, edge_index, _, batch, perm, score_perm = self.pool3(x, edge_index, batch=batch, M=M)
perm_list.append(perm)
mean = scatter_mean(x, batch, dim=0)
x = mean
x = F.relu(self.lin1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = F.relu(self.lin2(x))
x = F.log_softmax(self.lin3(x), dim=-1)
# x = self.mlp(x)
# x = F.log_softmax(x, dim=-1)
return x, perm_list
def train(model,train_loader,device):
model.train()
loss_all = 0
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
output, _ = model(data)
loss = F.nll_loss(output, data.y)
loss.backward()
loss_all += data.num_graphs * loss.item()
optimizer.step()
for name, param in model.named_parameters():
# if 'pan_pool_weight' in name:
# param.data = param.data.clamp(0, 1)
if 'panconv_filter_weight' in name:
param.data = param.data.clamp(0, 1)
if 'panpool_filter_weight' in name:
param.data = param.data.clamp(0, 1)
return loss_all / len(train_loader.dataset)
def test(model,loader,device):
model.eval()
correct = 0
loss = 0.0
for data in loader:
data = data.to(device)
out, _ = model(data)
pred = out.max(dim=1)[1]
correct += pred.eq(data.y).sum().item()
loss += F.nll_loss(out, data.y).item()*data.num_graphs
return correct / len(loader.dataset), loss/len(loader.dataset)
parser = OptionParser()
parser.add_option("--dataset_name",
dest="dataset_name", default='PROTEINS',
help="the name of dataset from Pytorch Geometric, other options include PROTEINS_full, NCI1, AIDS, Mutagenicity")
parser.add_option("--phi",
dest="phi", default=0.3, type=np.float,
help="type of dataset dataset")
parser.add_option("--runs",
dest="runs", default=1, type=np.int,
help="number of runs")
parser.add_option("--batch_size", type=np.int,
dest="batch_size", default=32,
help="batch size")
parser.add_option("--L",
dest="L", default=4, type=np.int,
help="order L in MET")
parser.add_option("--learning_rate", type=np.float,
dest="learning_rate", default=0.005,
help="learning rate")
parser.add_option("--weight_decay", type=np.float,
dest="weight_decay", default=1e-3,
help="weight decay")
parser.add_option("--pool_ratio", type=np.float,
dest="pool_ratio", default=0.5,
help="proportion of nodes to be pooled")
parser.add_option("--nhid", type=np.int,
dest="nhid", default=64,
help="number of each hidden-layer neurons")
parser.add_option("--epochs", type=np.int,
dest="epochs", default=100,
help="number of epochs each run")
options, argss = parser.parse_args()
datasetname = options.dataset_name
phi = options.phi
runs = options.runs
batch_size = options.batch_size
filter_size = options.L+1
learning_rate = options.learning_rate
weight_decay = options.weight_decay
pool_ratio = options.pool_ratio
nhid = options.nhid
epochs = options.epochs
train_loss = np.zeros((runs,epochs),dtype=np.float)
val_loss = np.zeros((runs,epochs),dtype=np.float)
val_acc = np.zeros((runs,epochs),dtype=np.float)
test_acc = np.zeros(runs,dtype=np.float)
min_loss = 1e10*np.ones(runs)
# dataset
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
sv_dir = 'data/'
if not os.path.exists(sv_dir):
os.makedirs(sv_dir)
path = os.path.join(os.path.abspath(''), 'data', datasetname)
dataset = TUDataset(path, name=datasetname)
print(len(dataset))
print(dataset.num_classes)
print(dataset.num_node_features)
num_classes = dataset.num_classes
num_node_features = dataset.num_node_features
num_edge = 0
num_node = 0
num_graph = len(dataset)
dataset1 = list()
for i in range(len(dataset)):
data1 = Data(x=dataset[i].x, edge_index=dataset[i].edge_index, y=dataset[i].y)
data1.num_node = dataset[i].num_nodes
data1.num_edge = dataset[i].edge_index.size(1)
num_node = num_node + data1.num_node
num_edge = num_edge + data1.num_edge
dataset1.append(data1)
dataset = dataset1
num_edge = num_edge*1.0/num_graph
num_node = num_node*1.0/num_graph
# generate training, validation and test data sets
num_training = int(num_graph*0.8)
num_val = int(num_graph*0.1)
num_test = num_graph - (num_training+num_val)
## train model
for run in range(runs):
training_set, val_set, test_set = random_split(dataset, [num_training,num_val,num_test])
train_loader = DataLoader(training_set, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False)
print('***** PAN for {}, phi {} *****'.format(datasetname,phi))
print('#training data: {}, #test data: {}'.format(num_training,num_test))
print('Mean #nodes: {:.1f}, mean #edges: {:.1f}'.format(num_node,num_edge))
print('Network architectur: PC-PA')
print('filter_size: {:d}, pool_ratio: {:.2f}, learning rate: {:.2e}, weight decay: {:.2e}, nhid: {:d}'.format(filter_size,pool_ratio,learning_rate,weight_decay,nhid))
print('batchsize: {:d}, epochs: {:d}, runs: {:d}'.format(batch_size,epochs,runs))
print('Device: {}'.format(device))
## train model
model = PAN(num_node_features,num_classes,nhid=nhid,ratio=pool_ratio,filter_size=filter_size).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
for epoch in range(epochs):
# training
model.train()
loss_all = 0
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
output, _ = model(data)
loss = F.nll_loss(output, data.y)
loss.backward()
loss_all += data.num_graphs * loss.item()
optimizer.step()
for name, param in model.named_parameters():
# if 'pan_pool_weight' in name:
# param.data = param.data.clamp(0, 1)
if 'panconv_filter_weight' in name:
param.data = param.data.clamp(0, 1)
if 'panpool_filter_weight' in name:
param.data = param.data.clamp(0, 1)
loss = loss_all / len(train_loader.dataset)
train_loss[run,epoch] = loss
# validation
val_acc_1, val_loss_1 = test(model,val_loader,device)
val_loss[run,epoch] = val_loss_1
val_acc[run,epoch] = val_acc_1
print('Run: {:02d}, Epoch: {:03d}, Val loss: {:.4f}, Val acc: {:.4f}'.format(run+1,epoch+1,val_loss[run,epoch],val_acc[run,epoch]))
if val_loss_1 < min_loss[run]:
# save the model and reuse later in test
torch.save(model.state_dict(), 'latest.pth')
min_loss[run] = val_loss_1
# test
model.load_state_dict(torch.load('latest.pth'))
test_acc[run], _ = test(model,test_loader,device)
print('==Test Acc: {:.4f}'.format(test_acc[run]))
print('==Mean Test Acc: {:.4f}'.format(np.mean(test_acc)))
t1 = time.time()
sv = datasetname + '_pcpa_runs' + str(runs) + '_phi' + str(phi) + '_time' + str(t1) + '.mat'
sio.savemat(sv,mdict={'test_acc':test_acc,'val_loss':val_loss,'val_acc':val_acc,'train_loss':train_loss,'filter_size':filter_size,'learning_rate':learning_rate,'weight_decay':weight_decay,'nhid':nhid,'batch_size':batch_size,'epochs':epochs})