Source code for torch_geometric.nn.conv.res_gated_graph_conv

from typing import Callable, Optional, Tuple, Union

from torch import Tensor
from torch.nn import Parameter, Sigmoid

from torch_geometric.nn.conv import MessagePassing
from torch_geometric.nn.dense.linear import Linear
from torch_geometric.typing import Adj, PairTensor

from ..inits import zeros


[docs]class ResGatedGraphConv(MessagePassing): r"""The residual gated graph convolutional operator from the `"Residual Gated Graph ConvNets" <https://arxiv.org/abs/1711.07553>`_ paper .. math:: \mathbf{x}^{\prime}_i = \mathbf{W}_1 \mathbf{x}_i + \sum_{j \in \mathcal{N}(i)} \eta_{i,j} \odot \mathbf{W}_2 \mathbf{x}_j where the gate :math:`\eta_{i,j}` is defined as .. math:: \eta_{i,j} = \sigma(\mathbf{W}_3 \mathbf{x}_i + \mathbf{W}_4 \mathbf{x}_j) with :math:`\sigma` denoting the sigmoid function. Args: in_channels (int or tuple): Size of each input sample, or :obj:`-1` to derive the size from the first input(s) to the forward method. A tuple corresponds to the sizes of source and target dimensionalities. out_channels (int): Size of each output sample. act (callable, optional): Gating function :math:`\sigma`. (default: :meth:`torch.nn.Sigmoid()`) bias (bool, optional): If set to :obj:`False`, the layer will not learn an additive bias. (default: :obj:`True`) root_weight (bool, optional): If set to :obj:`False`, the layer will not add transformed root node features to the output. (default: :obj:`True`) **kwargs (optional): Additional arguments of :class:`torch_geometric.nn.conv.MessagePassing`. Shapes: - **inputs:** node features :math:`(|\mathcal{V}|, F_{in})` or :math:`((|\mathcal{V_s}|, F_{s}), (|\mathcal{V_t}|, F_{t}))` if bipartite, edge indices :math:`(2, |\mathcal{E}|)` - **outputs:** node features :math:`(|\mathcal{V}|, F_{out})` or :math:`(|\mathcal{V_t}|, F_{out})` if bipartite """ def __init__( self, in_channels: Union[int, Tuple[int, int]], out_channels: int, act: Optional[Callable] = Sigmoid(), root_weight: bool = True, bias: bool = True, **kwargs, ): kwargs.setdefault('aggr', 'add') super().__init__(**kwargs) self.in_channels = in_channels self.out_channels = out_channels self.act = act self.root_weight = root_weight if isinstance(in_channels, int): in_channels = (in_channels, in_channels) self.lin_key = Linear(in_channels[1], out_channels) self.lin_query = Linear(in_channels[0], out_channels) self.lin_value = Linear(in_channels[0], out_channels) if root_weight: self.lin_skip = Linear(in_channels[1], out_channels, bias=False) else: self.register_parameter('lin_skip', None) if bias: self.bias = Parameter(Tensor(out_channels)) else: self.register_parameter('bias', None) self.reset_parameters()
[docs] def reset_parameters(self): self.lin_key.reset_parameters() self.lin_query.reset_parameters() self.lin_value.reset_parameters() if self.lin_skip is not None: self.lin_skip.reset_parameters() if self.bias is not None: zeros(self.bias)
[docs] def forward(self, x: Union[Tensor, PairTensor], edge_index: Adj) -> Tensor: """""" if isinstance(x, Tensor): x: PairTensor = (x, x) k = self.lin_key(x[1]) q = self.lin_query(x[0]) v = self.lin_value(x[0]) # propagate_type: (k: Tensor, q: Tensor, v: Tensor) out = self.propagate(edge_index, k=k, q=q, v=v, size=None) if self.root_weight: out = out + self.lin_skip(x[1]) if self.bias is not None: out = out + self.bias return out
def message(self, k_i: Tensor, q_j: Tensor, v_j: Tensor) -> Tensor: return self.act(k_i + q_j) * v_j