Source code for torch_geometric.nn.conv.gin_conv

import torch
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.utils import remove_self_loops

from ..inits import reset

[docs]class GINConv(MessagePassing): r"""The graph isomorphism operator from the `"How Powerful are Graph Neural Networks?" <>`_ 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(GINConv, 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()
[docs] def reset_parameters(self): reset(self.nn)
[docs] def forward(self, x, edge_index): """""" x = x.unsqueeze(-1) if x.dim() == 1 else x edge_index, _ = remove_self_loops(edge_index) out = self.nn((1 + self.eps) * x + self.propagate(edge_index, x=x)) return out
def message(self, x_j): return x_j def __repr__(self): return '{}(nn={})'.format(self.__class__.__name__, self.nn)