# AGNNConv¶

class dgl.nn.pytorch.conv.AGNNConv(init_beta=1.0, learn_beta=True, allow_zero_in_degree=False)[source]

Bases: torch.nn.modules.module.Module

Attention-based Graph Neural Network layer from Attention-based Graph Neural Network for Semi-Supervised Learning

$H^{l+1} = P H^{l}$

where $$P$$ is computed as:

$P_{ij} = \mathrm{softmax}_i ( \beta \cdot \cos(h_i^l, h_j^l))$

where $$\beta$$ is a single scalar parameter.

Parameters
• init_beta (float, optional) – The $$\beta$$ in the formula, a single scalar parameter.

• learn_beta (bool, optional) – If True, $$\beta$$ will be learnable parameter.

• allow_zero_in_degree (bool, optional) – If there are 0-in-degree nodes in the graph, output for those nodes will be invalid since no message will be passed to those nodes. This is harmful for some applications causing silent performance regression. This module will raise a DGLError if it detects 0-in-degree nodes in input graph. By setting True, it will suppress the check and let the users handle it by themselves. Default: False.

Note

Zero in-degree nodes will lead to invalid output value. This is because no message will be passed to those nodes, the aggregation function will be appied on empty input. A common practice to avoid this is to add a self-loop for each node in the graph if it is homogeneous, which can be achieved by:

>>> g = ... # a DGLGraph


Calling add_self_loop will not work for some graphs, for example, heterogeneous graph since the edge type can not be decided for self_loop edges. Set allow_zero_in_degree to True for those cases to unblock the code and handle zero-in-degree nodes manually. A common practise to handle this is to filter out the nodes with zero-in-degree when use after conv.

Example

>>> import dgl
>>> import numpy as np
>>> import torch as th
>>> from dgl.nn import AGNNConv
>>>
>>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3]))
>>> feat = th.ones(6, 10)
>>> conv = AGNNConv()
>>> res = conv(g, feat)
>>> res
tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]],

forward(graph, feat)[source]

Compute AGNN layer.

Parameters
• graph (DGLGraph) – The graph.

• feat (torch.Tensor) – The input feature of shape $$(N, *)$$ $$N$$ is the number of nodes, and $$*$$ could be of any shape. If a pair of torch.Tensor is given, the pair must contain two tensors of shape $$(N_{in}, *)$$ and $$(N_{out}, *)$$, the $$*$$ in the later tensor must equal the previous one.

Returns

The output feature of shape $$(N, *)$$ where $$*$$ should be the same as input shape.

Return type

torch.Tensor

Raises

DGLError – If there are 0-in-degree nodes in the input graph, it will raise DGLError since no message will be passed to those nodes. This will cause invalid output. The error can be ignored by setting allow_zero_in_degree parameter to True.