-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgin_conv.py
58 lines (48 loc) · 2.32 KB
/
gin_conv.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
import torch
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.utils import remove_self_loops
from inits import reset
class GINConv2(MessagePassing):
r"""The graph isomorphism operator from the `"How Powerful are
Graph Neural Networks?" <https://arxiv.org/abs/1810.00826>`_ paper
.. math::
\mathbf{x}^{\prime}_i = h_{\mathbf{\Theta}} \left( (1 + \epsilon) \cdot
\mathbf{x}_i + \sum_{j \in \mathcal{N}(i)} \mathbf{x}_j \right),
here :math:`h_{\mathbf{\Theta}}` denotes a neural network, *.i.e.* a MLP.
Args:
nn (torch.nn.Module): A neural network :math:`h_{\mathbf{\Theta}}` that
maps node features :obj:`x` of shape :obj:`[-1, in_channels]` to
shape :obj:`[-1, out_channels]`, *e.g.*, defined by
:class:`torch.nn.Sequential`.
eps (float, optional): (Initial) :math:`\epsilon` value.
(default: :obj:`0`)
train_eps (bool, optional): If set to :obj:`True`, :math:`\epsilon`
will be a trainable parameter. (default: :obj:`False`)
**kwargs (optional): Additional arguments of
:class:`torch_geometric.nn.conv.MessagePassing`.
"""
def __init__(self, nn, eps=0, train_eps=False, **kwargs):
super(GINConv2, self).__init__(aggr='add', **kwargs)
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, size=None):
#import pdb;pdb.set_trace()
x_in = x if size is None else x[torch.unique(edge_index[1])] # without speficied edge_index, x_in = x
x = x.unsqueeze(-1) if x.dim() == 1 else x # extend to 2-D array, for those with only one vector
edge_index, _ = remove_self_loops(edge_index)
#import pdb;pdb.set_trace()
out = self.nn((1 + self.eps) * x_in + self.propagate(edge_index, x=x, size=size))
#out = self.nn(self.propagate(edge_index, x=x, size=size))
return out
def message(self, x_j):
return x_j
def __repr__(self):
return '{}(nn={})'.format(self.__class__.__name__, self.nn)