-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_nets_old.py
897 lines (782 loc) · 31.4 KB
/
graph_nets_old.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
"""
Small library for creating arbitrary Graph Networks.
"""
import torch
import torch.nn.functional as F
from torch.nn import Sequential, Linear, ReLU
from torch.nn import Sigmoid, LayerNorm, Dropout
from torch.nn import BatchNorm1d
try:
from torch_geometric.data import Data
from torch_scatter import scatter_mean
from torch_scatter import scatter_add
# from torch_geometric.nn import MetaLayer
except:
from utils import Data
from scatter import scatter_add, scatter_mean
from utils import cosine_similarity
from utils import cos_sim
from utils import sim
###############################################################################
# #
# MLP function #
# #
###############################################################################
def mlp_fn(hidden_layer_sizes, normalize=False):
def mlp(f_in, f_out):
"""
This function returns a Multi-Layer Perceptron with ReLU
non-linearities with num_layers layers and h hidden nodes in each
layer, with f_in input features and f_out output features.
"""
layers = []
f1 = f_in
for i, f2 in enumerate(hidden_layer_sizes):
layers.append(Linear(f1, f2))
if (i == len(hidden_layer_sizes) - 1) and normalize:
layers.append(LayerNorm(f2))
layers.append(ReLU())
f1 = f2
layers.append(Linear(f1, f_out))
# layers.append(ReLU())
# layers.append(LayerNorm(f_out))
return Sequential(*layers)
return mlp
###############################################################################
# #
# GN Models #
# #
###############################################################################
class EdgeModelConcat(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_e_out=None):
"""
Edge model : for each edge, computes the result as a function of the
edge attribute, the sender and receiver node attribute, and the global
attribute.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(EdgeModelConcat, self).__init__()
if f_e_out is None:
f_e_out = f_e
self.phi_e = model_fn(f_e + 2*f_x + f_u, f_e_out)
def forward(self, src, dest, edge_attr, u, batch):
"""
src [E, f_x] where E is number of edges and f_x is number of vertex
features : source node tensor
dest [E, f_x] : destination node tensor
edge_attr [E, f_e] where f_e is number of edge features : edge tensor
u [B, f_u] where B is number of batches (graphs) and f_u is number of
global features : global tensor
batch [E] : edge-batch mapping
"""
out = torch.cat([src, dest, edge_attr, u[batch]], 1)
return self.phi_e(out)
class EdgeModelDiff(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_e_out=None):
"""
Edge model : for each edge, computes the result as a function of the
edge attribute, the sender and receiver node attribute, and the global
attribute.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(EdgeModelDiff, self).__init__()
if f_e_out is None:
f_e_out = f_e
self.phi_e = model_fn(f_e + f_x + f_u, f_e_out)
def forward(self, src, dest, edge_attr, u, batch):
"""
src [E, f_x] where E is number of edges and f_x is number of vertex
features : source node tensor
dest [E, f_x] : destination node tensor
edge_attr [E, f_e] where f_e is number of edge features : edge tensor
u [B, f_u] where B is number of batches (graphs) and f_u is number of
global features : global tensor
batch [E] : edge-batch mapping
"""
out = torch.cat([dest - src, edge_attr, u[batch]], 1)
return self.phi_e(out)
class EdgeModelConcatNoMem(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_e_out=None):
super(EdgeModelConcatNoMem, self).__init__()
if f_e_out is None:
f_e_out = f_e
self.phi_e = model_fn(2 * f_x + f_u, f_e_out)
def forward(self, src, dest, edge_attr, u, batch):
out = torch.cat([dest, src, u[batch]], 1)
return self.phi_e(out)
class TGNNEdge(torch.nn.Module):
"""
Edge model of the TGNN model.
It computes the query and key vectors for each node, and then uses their
dot product across node pairs as edge attributes.
No learnable parameters.
"""
def __init__(self,
f_x,
f_u,
f_int):
"""
Arguments:
- f_x : number of node features;
- f_u : number of global features;
- f_int : number of features of the intermediate vectors.
"""
super(TGNNEdge, self).__init__()
if f_e_out is None:
f_e_out = f_e
def forward(self, src, dest, edge_attr, u, batch):
K, Q = src, dest
a = torch.bmm(K.view(-1, 1, f_int), Q.view(-1, 1, f_int)).squeeze()
return a # new edge_attr
class CosineAttention():
"""
Class for computing the cosine similarity between nodes of two different
graphs. Not a torch module, this has no learnable components.
Used to Implement Graph Matching Networks.
"""
def __init__(self,
f_e,
f_x,
f_u,
f_e_out=None):
"""
This object serves for computing the cosine similarities between nodes
of two graphs. Between node i of graph 1 and node j of graph 2, the
attention is computed as the softmax of the cosine similarity between
nodes i and j against the cosine similarities of nodes i' of graph 1
and node j. Algorithmically; this is quite similar to computing edge
attributes, except there are no learnable parameters, the function is
fixed.
"""
pass
def __call__(self, x, x_src, cg_edge_index, batch_src):
"""
No edge_attr and no u on this one.
Arguments :
- x (node feature tensor, size [X, f_x]) : node features of the
current graph
- x_src (node feature tensor, size [X, f_x]) : node features of the
other graph
- cg_edge_index (edge index tensor, size [2, E]) : cross-graph edge
index tensor, mapping nodes of the other graph (source) to the
current graph (dest)
- batch (batch tensor, size [X]) : batch tensor mapping the nodes
of the other graph to their respective graph in the batch.
Returns :
- attentions (size [X], node size of the other graph.)
"""
src, dest = cg_edge_index
# exp-cosine-similarity vector
ecs = torch.exp(sim(x_src[src], x[dest]))
# attentions
a = ecs / (scatter_add(ecs, batch_src[src])[batch_src[src]])
vec = x[dest] - x_src[src] # to be multiplied by attentions
mu = (a * vec.T)
return scatter_add(mu, dest).T # see if we don't need mean instead here
class LearnedCrossGraphAttention(torch.nn.Module):
"""
Class for computing scalar attentions between the nodes of two different
graphs.
"""
def __init__(self,
f_x,
model_fn):
"""
This cross-graph attention function, to be used with a model similar
to the GraphMatching model, uses the node features of the source
(other) graph and the destination (current) graph to compute scalar
attentions that will be multiplied with the destination node features.
The computation of the attentions is done with a mlp.
The output of the model is a vector of size [X_dest], the number of
destination graph nodes (on all batches).
"""
super(LearnedCrossGraphAttention, self).__init__()
self.mlp = model_fn(2 * f_x , 1)
def forward(self, x, x_src, cg_edge_index, batch, batch_src):
src, dest = cg_edge_index
# attentions
a = self.mlp(torch.cat([x[dest], x_src[src]], 1))
a = scatter_add(a, dest, dim=0)
# get the attentions to the [0, 1] range after summing them over
# all destination nodes
a = torch.sigmoid(a)
return a
class CosineSimNodeModel(torch.nn.Module):
"""
Node model with cosine similarity attentions between nodes of 2 different
graphs. Used to implement Graph Matching Networks.
"""
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_x_out=None):
"""
Cosine Similarity Node model : this model performs the node updates
in a similar fashion to the vanilla NodeModel, except that it takes
as additional input the cross-graph attentions, coming from the other
graph. These cross-graph attentions are computed by the
CosineAttention function above.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
if f_x_out is None:
f_x_out = f_x
super(CosineSimNodeModel, self).__init__()
self.phi_x = model_fn(f_e + 2 * f_x + f_u, f_x_out)
def forward(self, x, a, edge_index, edge_attr, u, batch):
"""
"""
src, dest = edge_index
# aggregate all edges which have the same destination
e_agg_node = scatter_mean(edge_attr, dest, dim=0)
out = torch.cat([x, a, e_agg_node, u[batch]], 1)
return self.phi_x(out)
class NodeModel(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_x_out=None):
"""
Node model : for each node, first computes the mean of every incoming
edge attibute tensor, then uses this, in addition to the node features
and the global features to compute the updated node attributes
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
if f_x_out is None:
f_x_out = f_x
super(NodeModel, self).__init__()
self.phi_x = model_fn(f_e + f_x + f_u, f_x_out)
def forward(self, x, edge_index, edge_attr, u, batch):
"""
"""
if not len(edge_attr):
return
src, dest = edge_index
# aggregate all edges which have the same destination
e_agg_node = scatter_mean(edge_attr, dest, dim=0)
# print(src.shape)
# print()
out = torch.cat([x, e_agg_node, u[batch]], 1)
return self.phi_x(out)
class NodeModelAdd(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_x_out=None):
"""
Node model : for each node, first computes the mean of every incoming
edge attibute tensor, then uses this, in addition to the node features
and the global features to compute the updated node attributes
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
if f_x_out is None:
f_x_out = f_x
super(NodeModelAdd, self).__init__()
self.phi_x = model_fn(f_e + f_x + f_u, f_x_out)
def forward(self, x, edge_index, edge_attr, u, batch):
"""
"""
if not len(edge_attr):
return
src, dest = edge_index
# aggregate all edges which have the same destination
e_agg_node = scatter_add(edge_attr, dest, dim=0)
# print(src.shape)
# print()
out = torch.cat([x, e_agg_node, u[batch]], 1)
return self.phi_x(out)
class TGNNNode(torch.nn.Module):
"""
Node model for the TGNN.
"""
def __init__(self,
f_x,
f_u,
f_out):
if f_x_out is None:
f_x_out = f_x
super(TGNNNode, self).__init__()
self.phi_K = Linear(f_x + f_u, f_out)
self.phi_Q = Linear(f_x + f_u, f_out)
def forward(self, x, edge_index, edge_attr, u, batch):
a = edge_attr
src, dest = edge_index
# how to keep the information ?
x = scatter_add(a * x[src], dest, dim=0)
K = self.phi_K(torch.cat([x, u[batch]], 1))
Q = self.phi_Q(torch.cat([x, u[batch]], 1))
class GlobalModel(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_u_out=None):
"""
Global model : aggregates the edge attributes over the whole graph,
the node attributes over the whole graph, and uses those to compute
the next global value.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(GlobalModel, self).__init__()
if f_u_out is None:
f_u_out = f_u
self.phi_u = model_fn(f_e + f_x + f_u, f_u_out)
def forward(self, x, edge_index, edge_attr, u, batch):
"""
"""
src, dest = edge_index
# compute the batch index for all edges
e_batch = batch[src]
# aggregate all edges in the graph
e_agg = scatter_mean(edge_attr, e_batch, dim=0)
# aggregate all nodes in the graph
x_agg = scatter_mean(x, batch, dim=0)
out = torch.cat([x_agg, e_agg, u], 1)
return self.phi_u(out)
class GlobalModelAdd(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_u_out=None):
"""
Global model : aggregates the edge attributes over the whole graph,
the node attributes over the whole graph, and uses those to compute
the next global value.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(GlobalModelAdd, self).__init__()
if f_u_out is None:
f_u_out = f_u
self.phi_u = model_fn(f_e + f_x + f_u, f_u_out)
def forward(self, x, edge_index, edge_attr, u, batch):
"""
"""
src, dest = edge_index
# compute the batch index for all edges
e_batch = batch[src]
# aggregate all edges in the graph
e_agg = scatter_add(edge_attr, e_batch, dim=0)
# aggregate all nodes in the graph
x_agg = scatter_add(x, batch, dim=0)
out = torch.cat([x_agg, e_agg, u], 1)
return self.phi_u(out)
class NodeOnlyGlobalModel(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_u_out=None):
"""
Global model : aggregates the edge attributes over the whole graph,
the node attributes over the whole graph, and uses those to compute
the next global value.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(NodeOnlyGlobalModel, self).__init__()
if f_u_out is None:
f_u_out = f_u
self.phi_u = model_fn(f_x + f_u, f_u_out)
def forward(self, x, edge_index, edge_attr, u, batch):
"""
"""
# aggregate all nodes in the graph
x_agg = scatter_mean(x, batch, dim=0)
out = torch.cat([x_agg, u], 1)
return self.phi_u(out)
class GlobalModelNodeAttention(torch.nn.Module):
def __init__(self,
f_e,
f_x,
f_u,
model_fn,
f_u_out=None):
"""
This global model aggregates all node features by doing their
weighted mean, were the weights are computed by a gating (or attention)
model.
Arguments :
- f_e (int): number of edge features
- f_x (int): number of vertex features
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(GlobalModelNodeAttention, self).__init__()
if f_u_out is None:
f_u_out = f_u
self.phi_u = model_fn(f_x + f_u, f_u_out)
self.gating = model_fn(f_x + f_u, f_x) # use sth simpler maybe
def forward(self, x, edge_index, edge_attr, u, batch):
# attentions
a = self.gating(torch.cat([x, u[batch]], 1))
# aggregate attention-weighted nodes
x_agg = scatter_mean(x * a, batch, dim=0)
out = torch.cat([x_agg, u], 1)
return self.phi_u(out)
###############################################################################
# #
# Direct GN Models #
# #
###############################################################################
class DirectEdgeModel(torch.nn.Module):
def __init__(self,
f_e,
model_fn,
f_e_out=None):
"""
Arguments :
- f_e (int): number of edge features
- model_fn : function that takes input and output features and
returns a model.
"""
super(DirectEdgeModel, self).__init__()
if f_e_out is None:
f_e_out = f_e
self.phi_e = model_fn(f_e, f_e_out)
def forward(self, src, dest, edge_attr, u, batch):
"""
src [E, f_x] where E is number of edges and f_x is number of vertex
features : source node tensor
dest [E, f_x] : destination node tensor
edge_attr [E, f_e] where f_e is number of edge features : edge tensor
u [B, f_u] where B is number of batches (graphs) and f_u is number of
global features : global tensor
batch [E] : edge-batch mapping
"""
return self.phi_e(edge_attr)
class DirectNodeModel(torch.nn.Module):
def __init__(self,
f_x,
model_fn,
f_x_out=None):
"""
Arguments :
- f_x (int): number of vertex features
- model_fn : function that takes input and output features and
returns a model.
"""
super(DirectNodeModel, self).__init__()
if f_x_out is None:
f_x_out = f_x
self.phi_x = model_fn(f_x, f_x_out)
def forward(self, x, edge_index, edge_attr, u, batch):
"""
"""
return self.phi_x(x)
class DirectGlobalModel(torch.nn.Module):
def __init__(self,
f_u,
model_fn,
f_u_out=None):
"""
Arguments :
- f_u (int): number of global features
- model_fn : function that takes input and output features and
returns a model.
"""
super(DirectGlobalModel, self).__init__()
if f_u_out is None:
f_u_out = f_u
self.phi_u = model_fn(f_u, f_u_out)
def forward(self, x, edge_index, edge_attr, u, batch):
return self.phi_u(u)
###############################################################################
# #
# GN Blocks #
# #
###############################################################################
class MetaLayer(torch.nn.Module):
"""
GN block.
Taken from rusty1s' PyTorch Geometric library, check it out here :
https://github.com/rusty1s/pytorch_geometric
"""
def __init__(self, edge_model=None, node_model=None, global_model=None):
super(MetaLayer, self).__init__()
self.edge_model = edge_model
self.node_model = node_model
self.global_model = global_model
self.reset_parameters()
def reset_parameters(self):
for item in [self.node_model, self.edge_model, self.global_model]:
if hasattr(item, 'reset_parameters'):
item.reset_parameters()
def forward(self, x, edge_index, edge_attr=None, u=None, batch=None):
""""""
row, col = edge_index
if self.edge_model is not None:
edge_attr = self.edge_model(x[row], x[col], edge_attr, u,
batch if batch is None else batch[row])
if self.node_model is not None:
x = self.node_model(x, edge_index, edge_attr, u, batch)
if self.global_model is not None:
u = self.global_model(x, edge_index, edge_attr, u, batch)
return x, edge_attr, u
def __repr__(self):
return ('{}(\n'
' edge_model={},\n'
' node_model={},\n'
' global_model={}\n'
')').format(self.__class__.__name__, self.edge_model,
self.node_model, self.global_model)
class AttentionLayer(torch.nn.Module):
"""
GN block with attentive messages, inpired from GRANs.
This code is based upon the code for the MetaLayer from rusty1s' PyTorch
Geometric library, check it out here :
https://github.com/rusty1s/pytorch_geometric
"""
def __init__(self,
edge_model,
attention_model,
node_model,
global_model):
"""
Initialize the AttentionLayer.
This layer performs a message-passing round, but with attention weights
on the edge features in the node computation step.
Maybe complexify the node model when using this, in terms of what
information it uses to update the nodes, if we only aggregate the node
features in the global attributes.
Arguments:
- edge_model : model that takes as input the source and destination
node features of each edge, and the previous edge features, and
returns the next edge features.
- attention_model : model that takes the same input as the edge
model, and outputs attention vectors for each edge
- node_model : model that takes as input the updated edges, the
attention features, and the previous node features, computes
the sum of all edge features flowing into the considered node
weighted by their attention vectors, and uses this sum to
update the node features
- global_model : model that computes the global attribute by
aggregating all edges and nodes. (no attention here ? or maybe
only aggregate the nodes ?)
"""
super(AttentionLayer, self).__init__()
self.edge_model = edge_model
self.attention_model = attention_model
self.node_model = node_model
self.global_model = global_model
self.reset_parameters()
def reset_parameters(self):
for item in [
self.edge_model,
self.attention_model,
self.node_model,
self.global_model]:
if hasattr(item, 'reset_parameters'):
item.reset_parameters
def forward(self, x, edge_index, e, u, batch):
"""
Forward pass
"""
src, dest = edge_index
e = self.edge_model(x[src], x[dest], e, u, batch[src])
a = self.attention_model(x[src], x[dest], e, u, batch[src])
x = self.node_model(x, edge_index, e * a, u, batch)
u = self.global_model(x, edge_index, e, u, batch)
return x, e, u
def __repr__(self):
return ('{}(\n'
' edge_model={},\n'
' attention_model={},\n'
' node_model={},\n'
' global_model={}\n'
')').format(self.__class__.__name__,
self.edge_model,
self.attention_model,
self.node_model,
self.global_model)
class CosineAttentionLayer(torch.nn.Module):
"""
Implementation of the Graph mAtching Network with cosine similarity used to
compute attentions between node features.
"""
def __init__(self,
edge_model,
attention_function,
node_model,
global_model):
"""
Initializes the Cosine Attention Layer.
This layer is similar to the usual MetaLayer, with an additional
twist : the node feature update expects additional input from another
graph, in the form of attentions between nodes of the current graph
and the nodes of the other graph. This additional input vector has the
same size as number of nodes in the current graph.
This vector contains the sum of source node features weighted by their
attentions over the destination nodes. The attentions are computed
as the ratio of the exp of cosine similarity of node i and j over the
sum of exps of all cosine similarities between node i and j', node i
being the destination node of the current graph and nodes j and j' the
source nodes.
"""
super(CosineAttentionLayer, self).__init__()
self.edge_model = edge_model
self.node_model = node_model # this needs a specific node model
self.global_model = global_model
self.attention_function = attention_function
self.reset_parameters()
def reset_parameters(self):
for item in [self.node_model, self.edge_model, self.global_model]:
if hasattr(item, 'reset_parameters'):
item.reset_parameters()
def forward(self,
x,
x_src,
edge_index,
cg_edge_index,
edge_attr,
u,
batch,
batch_src):
"""
Similar to MetaLayer, but has two additional terms, x_src and
cg_edge_index.
x_src is the tensor of node features of the second graph.
cg_edge_index is the cross-graph connectivity (complete by default)
"""
row, col = edge_index
edge_attr = self.edge_model(x[row],
x[col],
edge_attr,
u,
batch if batch is None else batch[row])
# maybe change the inputs for this, because it does not have the same
# format as the other functions
a = self.attention_function(x, x_src, cg_edge_index, batch_src)
x = self.node_model(x, a, edge_index, edge_attr, u, batch)
u = self.global_model(x, edge_index, edge_attr, u, batch)
return x, edge_attr, u
def __repr__(self):
return ('{}(\n'
' edge_model={},\n'
' node_model={},\n'
' global_model={}\n'
')').format(self.__class__.__name__, self.edge_model,
self.node_model, self.global_model)
class CrossGraphAttentionLayer(torch.nn.Module):
"""
This GNN layer computes scalar attentions between every node pair from
graph1 and graph2 respectively, and multiplies it by the node features
before the edge and node models.
Note that the cross-graph attentions are applied to the node features
before any edge message-passing is done.
"""
def __init__(self,
attention_function,
edge_model,
node_model,
global_model):
"""
Initializes the layer.
"""
super(CrossGraphAttentionLayer, self).__init__()
self.attention_function = attention_function
self.edge_model = edge_model
self.node_model = node_model # this needs a specific node model
self.global_model = global_model
self.reset_parameters()
def reset_parameters(self):
for item in [self.node_model, self.edge_model, self.global_model]:
if hasattr(item, 'reset_parameters'):
item.reset_parameters()
def forward(self,
x,
x_src,
edge_index,
cg_edge_index,
edge_attr,
u,
batch,
batch_src):
"""
Forward pass.
Similar to the usual MetaLayer, but has additional inputs for the
source graph node features (x_src), the source graph batch (batch_src)
and the cross-graph edge index tensor (cg_edge_index).
"""
row, col = edge_index
a = self.attention_function(x, x_src, cg_edge_index, batch, batch_src)
x = x * a
edge_attr = self.edge_model(x[row],
x[col],
edge_attr,
u,
batch[row])
# this cross-graph function modulates the node features of the current
# graph
x = self.node_model(x, edge_index, edge_attr, u, batch)
u = self.global_model(x, edge_index, edge_attr, u, batch)
return x, edge_attr, u
class TGNNLayer(torch.nn.Module):
"""
TGNN Layer.
"""
def __init__(self):
pass