-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathVGRNN_prediction.py
1059 lines (807 loc) · 37.3 KB
/
VGRNN_prediction.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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#importing libraries
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import numpy as np
import torch
import torch.nn as nn
import torch.utils
import torch.utils.data
from torchvision import datasets, transforms
from torch.autograd import Variable
import matplotlib.pyplot as plt
from scipy.ndimage import rotate
from torch.distributions.uniform import Uniform
from torch.distributions.normal import Normal
from sklearn.datasets import fetch_mldata
# from torch_geometric import nn as tgnn
from input_data import load_data
from preprocessing import preprocess_graph, construct_feed_dict, sparse_to_tuple, mask_test_edges
import scipy.sparse as sp
from scipy.linalg import block_diag
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
import tarfile
import torch.nn.functional as F
import copy
import time
from torch_scatter import scatter_mean, scatter_max, scatter_add
from torch_geometric.utils import remove_self_loops, add_self_loops
from torch_geometric.datasets import Planetoid
import networkx as nx
import scipy.io as sio
import torch_scatter
import inspect
from sklearn.metrics import roc_auc_score
from sklearn.metrics import average_precision_score
import copy
import pickle
# In[2]:
seed = 123
np.random.seed(seed)
# In[3]:
# utility functions
def uniform(size, tensor):
stdv = 1.0 / math.sqrt(size)
if tensor is not None:
tensor.data.uniform_(-stdv, stdv)
def glorot(tensor):
stdv = math.sqrt(6.0 / (tensor.size(0) + tensor.size(1)))
if tensor is not None:
tensor.data.uniform_(-stdv, stdv)
def zeros(tensor):
if tensor is not None:
tensor.data.fill_(0)
def ones(tensor):
if tensor is not None:
tensor.data.fill_(1)
def reset(nn):
def _reset(item):
if hasattr(item, 'reset_parameters'):
item.reset_parameters()
if nn is not None:
if hasattr(nn, 'children') and len(list(nn.children())) > 0:
for item in nn.children():
_reset(item)
else:
_reset(nn)
def scatter_(name, src, index, dim_size=None):
r"""Aggregates all values from the :attr:`src` tensor at the indices
specified in the :attr:`index` tensor along the first dimension.
If multiple indices reference the same location, their contributions
are aggregated according to :attr:`name` (either :obj:`"add"`,
:obj:`"mean"` or :obj:`"max"`).
Args:
name (string): The aggregation to use (:obj:`"add"`, :obj:`"mean"`,
:obj:`"max"`).
src (Tensor): The source tensor.
index (LongTensor): The indices of elements to scatter.
dim_size (int, optional): Automatically create output tensor with size
:attr:`dim_size` in the first dimension. If set to :attr:`None`, a
minimal sized output tensor is returned. (default: :obj:`None`)
:rtype: :class:`Tensor`
"""
assert name in ['add', 'mean', 'max']
op = getattr(torch_scatter, 'scatter_{}'.format(name))
fill_value = -1e38 if name is 'max' else 0
out = op(src, index, 0, None, dim_size, fill_value)
if isinstance(out, tuple):
out = out[0]
if name is 'max':
out[out == fill_value] = 0
return out
class MessagePassing(torch.nn.Module):
r"""Base class for creating message passing layers
.. math::
\mathbf{x}_i^{\prime} = \gamma_{\mathbf{\Theta}} \left( \mathbf{x}_i,
\square_{j \in \mathcal{N}(i)} \, \phi_{\mathbf{\Theta}}
\left(\mathbf{x}_i, \mathbf{x}_j,\mathbf{e}_{i,j}\right) \right),
where :math:`\square` denotes a differentiable, permutation invariant
function, *e.g.*, sum, mean or max, and :math:`\gamma_{\mathbf{\Theta}}`
and :math:`\phi_{\mathbf{\Theta}}` denote differentiable functions such as
MLPs.
See `here <https://rusty1s.github.io/pytorch_geometric/build/html/notes/
create_gnn.html>`__ for the accompanying tutorial.
"""
def __init__(self, aggr='add'):
super(MessagePassing, self).__init__()
self.message_args = inspect.getargspec(self.message)[0][1:]
self.update_args = inspect.getargspec(self.update)[0][2:]
def propagate(self, aggr, edge_index, **kwargs):
r"""The initial call to start propagating messages.
Takes in an aggregation scheme (:obj:`"add"`, :obj:`"mean"` or
:obj:`"max"`), the edge indices, and all additional data which is
needed to construct messages and to update node embeddings."""
assert aggr in ['add', 'mean', 'max']
kwargs['edge_index'] = edge_index
size = None
message_args = []
for arg in self.message_args:
if arg[-2:] == '_i':
tmp = kwargs[arg[:-2]]
size = tmp.size(0)
message_args.append(tmp[edge_index[0]])
elif arg[-2:] == '_j':
tmp = kwargs[arg[:-2]]
size = tmp.size(0)
message_args.append(tmp[edge_index[1]])
else:
message_args.append(kwargs[arg])
update_args = [kwargs[arg] for arg in self.update_args]
out = self.message(*message_args)
out = scatter_(aggr, out, edge_index[0], dim_size=size)
out = self.update(out, *update_args)
return out
def message(self, x_j): # pragma: no cover
r"""Constructs messages in analogy to :math:`\phi_{\mathbf{\Theta}}`
for each edge in :math:`(i,j) \in \mathcal{E}`.
Can take any argument which was initially passed to :meth:`propagate`.
In addition, features can be lifted to the source node :math:`i` and
target node :math:`j` by appending :obj:`_i` or :obj:`_j` to the
variable name, *.e.g.* :obj:`x_i` and :obj:`x_j`."""
return x_j
def update(self, aggr_out): # pragma: no cover
r"""Updates node embeddings in analogy to
:math:`\gamma_{\mathbf{\Theta}}` for each node
:math:`i \in \mathcal{V}`.
Takes in the output of aggregation as first argument and any argument
which was initially passed to :meth:`propagate`."""
return aggr_out
def tuple_to_array(lot):
out = np.array(list(lot[0]))
for i in range(1, len(lot)):
out = np.vstack((out, np.array(list(lot[i]))))
return out
# In[4]:
# masking functions
def mask_edges_det(adjs_list):
adj_train_l, train_edges_l, val_edges_l = [], [], []
val_edges_false_l, test_edges_l, test_edges_false_l = [], [], []
edges_list = []
for i in range(0, len(adjs_list)):
# Function to build test set with 10% positive links
# NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper.
adj = adjs_list[i]
# Remove diagonal elements
adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
adj.eliminate_zeros()
# Check that diag is zero:
assert np.diag(adj.todense()).sum() == 0
adj_triu = sp.triu(adj)
adj_tuple = sparse_to_tuple(adj_triu)
edges = adj_tuple[0]
edges_all = sparse_to_tuple(adj)[0]
num_test = int(np.floor(edges.shape[0] / 10.))
num_val = int(np.floor(edges.shape[0] / 20.))
all_edge_idx = range(edges.shape[0])
np.random.shuffle(all_edge_idx)
val_edge_idx = all_edge_idx[:num_val]
test_edge_idx = all_edge_idx[num_val:(num_val + num_test)]
test_edges = edges[test_edge_idx]
val_edges = edges[val_edge_idx]
train_edges = np.delete(edges, np.hstack([test_edge_idx, val_edge_idx]), axis=0)
edges_list.append(edges)
def ismember(a, b, tol=5):
rows_close = np.all(np.round(a - b[:, None], tol) == 0, axis=-1)
return np.any(rows_close)
test_edges_false = []
while len(test_edges_false) < len(test_edges):
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], edges_all):
continue
if test_edges_false:
if ismember([idx_j, idx_i], np.array(test_edges_false)):
continue
if ismember([idx_i, idx_j], np.array(test_edges_false)):
continue
test_edges_false.append([idx_i, idx_j])
val_edges_false = []
while len(val_edges_false) < len(val_edges):
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], train_edges):
continue
if ismember([idx_j, idx_i], train_edges):
continue
if ismember([idx_i, idx_j], val_edges):
continue
if ismember([idx_j, idx_i], val_edges):
continue
if val_edges_false:
if ismember([idx_j, idx_i], np.array(val_edges_false)):
continue
if ismember([idx_i, idx_j], np.array(val_edges_false)):
continue
val_edges_false.append([idx_i, idx_j])
assert ~ismember(test_edges_false, edges_all)
assert ~ismember(val_edges_false, edges_all)
assert ~ismember(val_edges, train_edges)
assert ~ismember(test_edges, train_edges)
assert ~ismember(val_edges, test_edges)
data = np.ones(train_edges.shape[0])
# Re-build adj matrix
adj_train = sp.csr_matrix((data, (train_edges[:, 0], train_edges[:, 1])), shape=adj.shape)
adj_train = adj_train + adj_train.T
adj_train_l.append(adj_train)
train_edges_l.append(train_edges)
val_edges_l.append(val_edges)
val_edges_false_l.append(val_edges_false)
test_edges_l.append(test_edges)
test_edges_false_l.append(test_edges_false)
# NOTE: these edge lists only contain single direction of edge!
return adj_train_l, train_edges_l, val_edges_l, val_edges_false_l, test_edges_l, test_edges_false_l
def mask_edges_prd(adjs_list):
pos_edges_l , false_edges_l = [], []
edges_list = []
for i in range(0, len(adjs_list)):
# Function to build test set with 10% positive links
# NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper.
adj = adjs_list[i]
# Remove diagonal elements
adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
adj.eliminate_zeros()
# Check that diag is zero:
assert np.diag(adj.todense()).sum() == 0
adj_triu = sp.triu(adj)
adj_tuple = sparse_to_tuple(adj_triu)
edges = adj_tuple[0]
edges_all = sparse_to_tuple(adj)[0]
num_false = int(edges.shape[0])
pos_edges_l.append(edges)
def ismember(a, b, tol=5):
rows_close = np.all(np.round(a - b[:, None], tol) == 0, axis=-1)
return np.any(rows_close)
edges_false = []
while len(edges_false) < num_false:
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], edges_all):
continue
if edges_false:
if ismember([idx_j, idx_i], np.array(edges_false)):
continue
if ismember([idx_i, idx_j], np.array(edges_false)):
continue
edges_false.append([idx_i, idx_j])
assert ~ismember(edges_false, edges_all)
false_edges_l.append(edges_false)
# NOTE: these edge lists only contain single direction of edge!
return pos_edges_l, false_edges_l
def mask_edges_prd_new(adjs_list, adj_orig_dense_list):
pos_edges_l , false_edges_l = [], []
edges_list = []
# Function to build test set with 10% positive links
# NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper.
adj = adjs_list[0]
# Remove diagonal elements
adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
adj.eliminate_zeros()
# Check that diag is zero:
assert np.diag(adj.todense()).sum() == 0
adj_triu = sp.triu(adj)
adj_tuple = sparse_to_tuple(adj_triu)
edges = adj_tuple[0]
edges_all = sparse_to_tuple(adj)[0]
num_false = int(edges.shape[0])
pos_edges_l.append(edges)
def ismember(a, b, tol=5):
rows_close = np.all(np.round(a - b[:, None], tol) == 0, axis=-1)
return np.any(rows_close)
edges_false = []
while len(edges_false) < num_false:
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], edges_all):
continue
if edges_false:
if ismember([idx_j, idx_i], np.array(edges_false)):
continue
if ismember([idx_i, idx_j], np.array(edges_false)):
continue
edges_false.append([idx_i, idx_j])
assert ~ismember(edges_false, edges_all)
false_edges_l.append(np.asarray(edges_false))
for i in range(1, len(adjs_list)):
edges_pos = np.transpose(np.asarray(np.where((adj_orig_dense_list[i] - adj_orig_dense_list[i-1])>0)))
num_false = int(edges_pos.shape[0])
adj = adjs_list[i]
# Remove diagonal elements
adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
adj.eliminate_zeros()
# Check that diag is zero:
assert np.diag(adj.todense()).sum() == 0
adj_triu = sp.triu(adj)
adj_tuple = sparse_to_tuple(adj_triu)
edges = adj_tuple[0]
edges_all = sparse_to_tuple(adj)[0]
edges_false = []
while len(edges_false) < num_false:
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
if ismember([idx_i, idx_j], edges_all):
continue
if edges_false:
if ismember([idx_j, idx_i], np.array(edges_false)):
continue
if ismember([idx_i, idx_j], np.array(edges_false)):
continue
edges_false.append([idx_i, idx_j])
assert ~ismember(edges_false, edges_all)
false_edges_l.append(np.asarray(edges_false))
pos_edges_l.append(edges_pos)
# NOTE: these edge lists only contain single direction of edge!
return pos_edges_l, false_edges_l
# In[5]:
# loading data
# # Enron dataset
# with open('data/enron10/adj_time_list.pickle', 'rb') as handle:
# adj_time_list = pickle.load(handle)
# with open('data/enron10/adj_orig_dense_list.pickle', 'rb') as handle:
# adj_orig_dense_list = pickle.load(handle)
# # COLAB dataset
# with open('data/dblp/adj_time_list.pickle', 'rb') as handle:
# adj_time_list = pickle.load(handle)
# with open('data/dblp/adj_orig_dense_list.pickle', 'rb') as handle:
# adj_orig_dense_list = pickle.load(handle)
# Facebook dataset
with open('data/fb/adj_time_list.pickle', 'rb') as handle:
adj_time_list = pickle.load(handle)
with open('data/fb/adj_orig_dense_list.pickle', 'rb') as handle:
adj_orig_dense_list = pickle.load(handle)
# In[6]:
# masking edges
outs = mask_edges_det(adj_time_list)
train_edges_l = outs[1]
pos_edges_l, false_edges_l = mask_edges_prd(adj_time_list)
pos_edges_l_n, false_edges_l_n = mask_edges_prd_new(adj_time_list, adj_orig_dense_list)
# In[7]:
# creating edge list
edge_idx_list = []
for i in range(len(train_edges_l)):
edge_idx_list.append(torch.tensor(np.transpose(train_edges_l[i]), dtype=torch.long))
# In[8]:
# layers
class GCNConv(MessagePassing):
def __init__(self, in_channels, out_channels, act=F.relu, improved=True, bias=False):
super(GCNConv, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.improved = improved
self.act = act
self.weight = Parameter(torch.Tensor(in_channels, out_channels))
if bias:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
glorot(self.weight)
zeros(self.bias)
def forward(self, x, edge_index, edge_weight=None):
if edge_weight is None:
edge_weight = torch.ones(
(edge_index.size(1), ), dtype=x.dtype, device=x.device)
edge_weight = edge_weight.view(-1)
assert edge_weight.size(0) == edge_index.size(1)
edge_index = add_self_loops(edge_index, num_nodes=x.size(0))
loop_weight = torch.full(
(x.size(0), ),
1 if not self.improved else 2,
dtype=x.dtype,
device=x.device)
edge_weight = torch.cat([edge_weight, loop_weight], dim=0)
row, col = edge_index
deg = scatter_add(edge_weight, row, dim=0, dim_size=x.size(0))
deg_inv = deg.pow(-0.5)
deg_inv[deg_inv == float('inf')] = 0
norm = deg_inv[row] * edge_weight * deg_inv[col]
x = torch.matmul(x, self.weight)
out = self.propagate('add', edge_index, x=x, norm=norm)
return self.act(out)
def message(self, x_j, norm):
return norm.view(-1, 1) * x_j
def update(self, aggr_out):
if self.bias is not None:
aggr_out = aggr_out + self.bias
return aggr_out
def __repr__(self):
return '{}({}, {})'.format(self.__class__.__name__, self.in_channels,
self.out_channels)
class SAGEConv(torch.nn.Module):
def __init__(self, in_channels, out_channels, pool='mean', act=F.relu, normalize=False, bias=False):
super(SAGEConv, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.normalize = normalize
self.weight = Parameter(torch.Tensor(self.in_channels, out_channels))
self.act = act
self.pool = pool
if bias:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
size = self.weight.size(0)
uniform(size, self.weight)
uniform(size, self.bias)
def forward(self, x, edge_index):
edge_index, _ = remove_self_loops(edge_index)
edge_index = add_self_loops(edge_index, num_nodes=x.size(0))
x = x.unsqueeze(-1) if x.dim() == 1 else x
row, col = edge_index
if self.pool == 'mean':
out = torch.matmul(x, self.weight)
if self.bias is not None:
out = out + self.bias
out = self.act(out)
out = scatter_mean(out[col], row, dim=0, dim_size=out.size(0))
elif self.pool == 'max':
out = torch.matmul(x, self.weight)
if self.bias is not None:
out = out + self.bias
out = self.act(out)
out, _ = scatter_max(out[col], row, dim=0, dim_size=out.size(0))
elif self.pool == 'add':
x = torch.matmul(x, self.weight)
if self.bias is not None:
out = out + self.bias
out = self.act(out)
out = scatter_add(x[col], row, dim=0, dim_size=x.size(0))
else:
print('pooling not defined!')
if self.normalize:
out = F.normalize(out, p=2, dim=-1)
return out
def __repr__(self):
return '{}({}, {})'.format(self.__class__.__name__, self.in_channels, self.out_channels)
class GINConv(torch.nn.Module):
def __init__(self, nn, eps=0, train_eps=False):
super(GINConv, self).__init__()
self.nn = nn
self.initial_eps = eps
if train_eps:
self.eps = torch.nn.Parameter(torch.Tensor([eps]))
else:
self.register_buffer('eps', torch.Tensor([eps]))
self.reset_parameters()
def reset_parameters(self):
reset(self.nn)
self.eps.data.fill_(self.initial_eps)
def forward(self, x, edge_index):
x = x.unsqueeze(-1) if x.dim() == 1 else x
edge_index, _ = remove_self_loops(edge_index)
row, col = edge_index
out = scatter_add(x[col], row, dim=0, dim_size=x.size(0))
out = (1 + self.eps) * x + out
out = self.nn(out)
return out
def __repr__(self):
return '{}(nn={})'.format(self.__class__.__name__, self.nn)
class graph_gru_sage(nn.Module):
def __init__(self, input_size, hidden_size, n_layer, bias=True):
super(graph_gru_sage, self).__init__()
self.hidden_size = hidden_size
self.n_layer = n_layer
# gru weights
self.weight_xz = []
self.weight_hz = []
self.weight_xr = []
self.weight_hr = []
self.weight_xh = []
self.weight_hh = []
for i in range(self.n_layer):
if i==0:
self.weight_xz.append(SAGEConv(input_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hz.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xr.append(SAGEConv(input_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hr.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xh.append(SAGEConv(input_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hh.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
else:
self.weight_xz.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hz.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xr.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hr.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xh.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hh.append(SAGEConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
def forward(self, inp, edgidx, h):
h_out = torch.zeros(h.size())
for i in range(self.n_layer):
if i==0:
z_g = torch.sigmoid(self.weight_xz[i](inp, edgidx) + self.weight_hz[i](h[i], edgidx))
r_g = torch.sigmoid(self.weight_xr[i](inp, edgidx) + self.weight_hr[i](h[i], edgidx))
h_tilde_g = torch.tanh(self.weight_xh[i](inp, edgidx) + self.weight_hh[i](r_g * h[i], edgidx))
h_out[i] = z_g * h[i] + (1 - z_g) * h_tilde_g
# out = self.decoder(h_t.view(1,-1))
else:
z_g = torch.sigmoid(self.weight_xz[i](h_out[i-1], edgidx) + self.weight_hz[i](h[i], edgidx))
r_g = torch.sigmoid(self.weight_xr[i](h_out[i-1], edgidx) + self.weight_hr[i](h[i], edgidx))
h_tilde_g = torch.tanh(self.weight_xh[i](h_out[i-1], edgidx) + self.weight_hh[i](r_g * h[i], edgidx))
h_out[i] = z_g * h[i] + (1 - z_g) * h_tilde_g
# out = self.decoder(h_t.view(1,-1))
out = h_out
return out, h_out
class graph_gru_gcn(nn.Module):
def __init__(self, input_size, hidden_size, n_layer, bias=True):
super(graph_gru_gcn, self).__init__()
self.hidden_size = hidden_size
self.n_layer = n_layer
# gru weights
self.weight_xz = []
self.weight_hz = []
self.weight_xr = []
self.weight_hr = []
self.weight_xh = []
self.weight_hh = []
for i in range(self.n_layer):
if i==0:
self.weight_xz.append(GCNConv(input_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hz.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xr.append(GCNConv(input_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hr.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xh.append(GCNConv(input_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hh.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
else:
self.weight_xz.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hz.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xr.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hr.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_xh.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
self.weight_hh.append(GCNConv(hidden_size, hidden_size, act=lambda x:x, bias=bias))
def forward(self, inp, edgidx, h):
h_out = torch.zeros(h.size())
for i in range(self.n_layer):
if i==0:
z_g = torch.sigmoid(self.weight_xz[i](inp, edgidx) + self.weight_hz[i](h[i], edgidx))
r_g = torch.sigmoid(self.weight_xr[i](inp, edgidx) + self.weight_hr[i](h[i], edgidx))
h_tilde_g = torch.tanh(self.weight_xh[i](inp, edgidx) + self.weight_hh[i](r_g * h[i], edgidx))
h_out[i] = z_g * h[i] + (1 - z_g) * h_tilde_g
# out = self.decoder(h_t.view(1,-1))
else:
z_g = torch.sigmoid(self.weight_xz[i](h_out[i-1], edgidx) + self.weight_hz[i](h[i], edgidx))
r_g = torch.sigmoid(self.weight_xr[i](h_out[i-1], edgidx) + self.weight_hr[i](h[i], edgidx))
h_tilde_g = torch.tanh(self.weight_xh[i](h_out[i-1], edgidx) + self.weight_hh[i](r_g * h[i], edgidx))
h_out[i] = z_g * h[i] + (1 - z_g) * h_tilde_g
# out = self.decoder(h_t.view(1,-1))
out = h_out
return out, h_out
class InnerProductDecoder(nn.Module):
def __init__(self, act=torch.sigmoid, dropout=0.):
super(InnerProductDecoder, self).__init__()
self.act = act
self.dropout = dropout
def forward(self, inp):
inp = F.dropout(inp, self.dropout, training=self.training)
x = torch.transpose(inp, dim0=0, dim1=1)
x = torch.mm(inp, x)
return self.act(x)
# In[9]:
# evaluation function
def get_roc_scores(edges_pos, edges_neg, adj_orig_dense_list, embs):
def sigmoid(x):
return 1 / (1 + np.exp(-x))
auc_scores = []
ap_scores = []
for i in range(len(edges_pos)):
# Predict on test set of edges
emb = embs[i].detach().numpy()
adj_rec = np.dot(emb, emb.T)
adj_orig_t = adj_orig_dense_list[i]
preds = []
pos = []
for e in edges_pos[i]:
preds.append(sigmoid(adj_rec[e[0], e[1]]))
pos.append(adj_orig_t[e[0], e[1]])
preds_neg = []
neg = []
for e in edges_neg[i]:
preds_neg.append(sigmoid(adj_rec[e[0], e[1]]))
neg.append(adj_orig_t[e[0], e[1]])
preds_all = np.hstack([preds, preds_neg])
labels_all = np.hstack([np.ones(len(preds)), np.zeros(len(preds_neg))])
auc_scores.append(roc_auc_score(labels_all, preds_all))
ap_scores.append(average_precision_score(labels_all, preds_all))
return auc_scores, ap_scores
# In[10]:
# VGRNN model
class VGRNN(nn.Module):
def __init__(self, x_dim, h_dim, z_dim, n_layers, eps, conv='GCN', bias=False):
super(VGRNN, self).__init__()
self.x_dim = x_dim
self.eps = eps
self.h_dim = h_dim
self.z_dim = z_dim
self.n_layers = n_layers
if conv == 'GCN':
self.phi_x = nn.Sequential(nn.Linear(x_dim, h_dim), nn.ReLU())
self.phi_z = nn.Sequential(nn.Linear(z_dim, h_dim), nn.ReLU())
self.enc = GCNConv(h_dim + h_dim, h_dim)
self.enc_mean = GCNConv(h_dim, z_dim, act=lambda x:x)
self.enc_std = GCNConv(h_dim, z_dim, act=F.softplus)
self.prior = nn.Sequential(nn.Linear(h_dim, h_dim), nn.ReLU())
self.prior_mean = nn.Sequential(nn.Linear(h_dim, z_dim))
self.prior_std = nn.Sequential(nn.Linear(h_dim, z_dim), nn.Softplus())
self.rnn = graph_gru_gcn(h_dim + h_dim, h_dim, n_layers, bias)
elif conv == 'SAGE':
self.phi_x = nn.Sequential(nn.Linear(x_dim, h_dim), nn.ReLU())
self.phi_z = nn.Sequential(nn.Linear(z_dim, h_dim), nn.ReLU())
self.enc = SAGEConv(h_dim + h_dim, h_dim)
self.enc_mean = SAGEConv(h_dim, z_dim, act=lambda x:x)
self.enc_std = SAGEConv(h_dim, z_dim, act=F.softplus)
self.prior = nn.Sequential(nn.Linear(h_dim, h_dim), nn.ReLU())
self.prior_mean = nn.Sequential(nn.Linear(h_dim, z_dim))
self.prior_std = nn.Sequential(nn.Linear(h_dim, z_dim), nn.Softplus())
self.rnn = graph_gru_sage(h_dim + h_dim, h_dim, n_layers, bias)
elif conv == 'GIN':
self.phi_x = nn.Sequential(nn.Linear(x_dim, h_dim), nn.ReLU())
self.phi_z = nn.Sequential(nn.Linear(z_dim, h_dim), nn.ReLU())
self.enc = GINConv(nn.Sequential(nn.Linear(h_dim + h_dim, h_dim), nn.ReLU()))
self.enc_mean = GINConv(nn.Sequential(nn.Linear(h_dim, z_dim)))
self.enc_std = GINConv(nn.Sequential(nn.Linear(h_dim, z_dim), nn.Softplus()))
self.prior = nn.Sequential(nn.Linear(h_dim, h_dim), nn.ReLU())
self.prior_mean = nn.Sequential(nn.Linear(h_dim, z_dim))
self.prior_std = nn.Sequential(nn.Linear(h_dim, z_dim), nn.Softplus())
self.rnn = graph_gru_gcn(h_dim + h_dim, h_dim, n_layers, bias)
def forward(self, x, edge_idx_list, adj_orig_dense_list, hidden_in=None):
assert len(adj_orig_dense_list) == len(edge_idx_list)
kld_loss = 0
nll_loss = 0
all_enc_mean, all_enc_std = [], []
all_prior_mean, all_prior_std = [], []
all_dec_t, all_z_t = [], []
if hidden_in is None:
h = Variable(torch.zeros(self.n_layers, x.size(1), self.h_dim))
else:
h = Variable(hidden_in)
for t in range(x.size(0)):
phi_x_t = self.phi_x(x[t])
#encoder
enc_t = self.enc(torch.cat([phi_x_t, h[-1]], 1), edge_idx_list[t])
enc_mean_t = self.enc_mean(enc_t, edge_idx_list[t])
enc_std_t = self.enc_std(enc_t, edge_idx_list[t])
#prior
prior_t = self.prior(h[-1])
prior_mean_t = self.prior_mean(prior_t)
prior_std_t = self.prior_std(prior_t)
#sampling and reparameterization
z_t = self._reparameterized_sample(enc_mean_t, enc_std_t)
phi_z_t = self.phi_z(z_t)
#decoder
dec_t = self.dec(z_t)
#recurrence
_, h = self.rnn(torch.cat([phi_x_t, phi_z_t], 1), edge_idx_list[t], h)
nnodes = adj_orig_dense_list[t].size()[0]
enc_mean_t_sl = enc_mean_t[0:nnodes, :]
enc_std_t_sl = enc_std_t[0:nnodes, :]
prior_mean_t_sl = prior_mean_t[0:nnodes, :]
prior_std_t_sl = prior_std_t[0:nnodes, :]
dec_t_sl = dec_t[0:nnodes, 0:nnodes]
#computing losses
# kld_loss += self._kld_gauss_zu(enc_mean_t, enc_std_t)
kld_loss += self._kld_gauss(enc_mean_t_sl, enc_std_t_sl, prior_mean_t_sl, prior_std_t_sl)
nll_loss += self._nll_bernoulli(dec_t_sl, adj_orig_dense_list[t])
all_enc_std.append(enc_std_t_sl)
all_enc_mean.append(enc_mean_t_sl)
all_prior_mean.append(prior_mean_t_sl)
all_prior_std.append(prior_std_t_sl)
all_dec_t.append(dec_t_sl)
all_z_t.append(z_t)
return kld_loss, nll_loss, all_enc_mean, all_prior_mean, h
def dec(self, z):
outputs = InnerProductDecoder(act=lambda x:x)(z)
return outputs
def reset_parameters(self, stdv=1e-1):
for weight in self.parameters():
weight.data.normal_(0, stdv)
def _init_weights(self, stdv):
pass
def _reparameterized_sample(self, mean, std):
eps1 = torch.FloatTensor(std.size()).normal_()
eps1 = Variable(eps1)
return eps1.mul(std).add_(mean)
def _kld_gauss(self, mean_1, std_1, mean_2, std_2):
num_nodes = mean_1.size()[0]
kld_element = (2 * torch.log(std_2 + self.eps) - 2 * torch.log(std_1 + self.eps) +
(torch.pow(std_1 + self.eps ,2) + torch.pow(mean_1 - mean_2, 2)) /
torch.pow(std_2 + self.eps ,2) - 1)
return (0.5 / num_nodes) * torch.mean(torch.sum(kld_element, dim=1), dim=0)
def _kld_gauss_zu(self, mean_in, std_in):
num_nodes = mean_in.size()[0]
std_log = torch.log(std_in + self.eps)
kld_element = torch.mean(torch.sum(1 + 2 * std_log - mean_in.pow(2) -
torch.pow(torch.exp(std_log), 2), 1))
return (-0.5 / num_nodes) * kld_element
def _nll_bernoulli(self, logits, target_adj_dense):
temp_size = target_adj_dense.size()[0]
temp_sum = target_adj_dense.sum()
posw = float(temp_size * temp_size - temp_sum) / temp_sum
norm = temp_size * temp_size / float((temp_size * temp_size - temp_sum) * 2)
nll_loss_mat = F.binary_cross_entropy_with_logits(input=logits
, target=target_adj_dense
, pos_weight=posw
, reduction='none')
nll_loss = -1 * norm * torch.mean(nll_loss_mat, dim=[0,1])
return - nll_loss
# In[11]:
# hyperparameters
h_dim = 32
z_dim = 16
n_layers = 1
clip = 10
learning_rate = 1e-2
seq_len = len(train_edges_l)
num_nodes = adj_orig_dense_list[seq_len-1].shape[0]
x_dim = num_nodes
eps = 1e-10
conv_type='GCN'
# In[12]:
# creating input tensors
x_in_list = []
for i in range(0, seq_len):
x_temp = torch.tensor(np.eye(num_nodes).astype(np.float32))
x_in_list.append(torch.tensor(x_temp))
x_in = Variable(torch.stack(x_in_list))
# In[13]:
# building model
model = VGRNN(x_dim, h_dim, z_dim, n_layers, eps, conv=conv_type, bias=True)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# In[14]: