Source code for torch_geometric.nn.conv.nn_conv

from typing import Union, Tuple, Callable
from torch_geometric.typing import OptTensor, OptPairTensor, Adj, Size

import torch
from torch import Tensor
from torch.nn import Parameter
from torch_geometric.nn.conv import MessagePassing

from ..inits import reset, uniform, zeros

[docs]class NNConv(MessagePassing): r"""The continuous kernel-based convolutional operator from the `"Neural Message Passing for Quantum Chemistry" <>`_ paper. This convolution is also known as the edge-conditioned convolution from the `"Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs" <>`_ paper (see :class:`torch_geometric.nn.conv.ECConv` for an alias): .. math:: \mathbf{x}^{\prime}_i = \mathbf{\Theta} \mathbf{x}_i + \sum_{j \in \mathcal{N}(i)} \mathbf{x}_j \cdot h_{\mathbf{\Theta}}(\mathbf{e}_{i,j}), where :math:`h_{\mathbf{\Theta}}` denotes a neural network, *.i.e.* a MLP. Args: in_channels (int or tuple): Size of each input sample. A tuple corresponds to the sizes of source and target dimensionalities. out_channels (int): Size of each output sample. nn (torch.nn.Module): A neural network :math:`h_{\mathbf{\Theta}}` that maps edge features :obj:`edge_attr` of shape :obj:`[-1, num_edge_features]` to shape :obj:`[-1, in_channels * out_channels]`, *e.g.*, defined by :class:`torch.nn.Sequential`. aggr (string, optional): The aggregation scheme to use (:obj:`"add"`, :obj:`"mean"`, :obj:`"max"`). (default: :obj:`"add"`) root_weight (bool, optional): If set to :obj:`False`, the layer will not add the transformed root node features to the output. (default: :obj:`True`) bias (bool, optional): If set to :obj:`False`, the layer will not learn an additive bias. (default: :obj:`True`) **kwargs (optional): Additional arguments of :class:`torch_geometric.nn.conv.MessagePassing`. """ def __init__(self, in_channels: Union[int, Tuple[int, int]], out_channels: int, nn: Callable, aggr: str = 'add', root_weight: bool = True, bias: bool = True, **kwargs): super(NNConv, self).__init__(aggr=aggr, **kwargs) self.in_channels = in_channels self.out_channels = out_channels self.nn = nn self.aggr = aggr if isinstance(in_channels, int): in_channels = (in_channels, in_channels) self.in_channels_l = in_channels[0] if root_weight: self.root = Parameter(torch.Tensor(in_channels[1], out_channels)) else: self.register_parameter('root', None) if bias: self.bias = Parameter(torch.Tensor(out_channels)) else: self.register_parameter('bias', None) self.reset_parameters()
[docs] def reset_parameters(self): reset(self.nn) if self.root is not None: uniform(self.root.size(0), self.root) zeros(self.bias)
[docs] def forward(self, x: Union[Tensor, OptPairTensor], edge_index: Adj, edge_attr: OptTensor = None, size: Size = None) -> Tensor: """""" if isinstance(x, Tensor): x: OptPairTensor = (x, x) # propagate_type: (x: OptPairTensor, edge_attr: OptTensor) out = self.propagate(edge_index, x=x, edge_attr=edge_attr, size=size) x_r = x[1] if x_r is not None and self.root is not None: out += torch.matmul(x_r, self.root) if self.bias is not None: out += self.bias return out
def message(self, x_j: Tensor, edge_attr: Tensor) -> Tensor: weight = self.nn(edge_attr) weight = weight.view(-1, self.in_channels_l, self.out_channels) return torch.matmul(x_j.unsqueeze(1), weight).squeeze(1) def __repr__(self): return '{}({}, {}, aggr="{}", nn={})'.format(self.__class__.__name__, self.in_channels, self.out_channels, self.aggr, self.nn)
ECConv = NNConv