AGNNConv

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

Bases: Module

基于注意力的图神经网络层来自 基于注意力的图神经网络用于半监督学习

\[H^{l+1} = P H^{l}\]

其中 \(P\) 计算如下:

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

其中 \(\beta\) 是一个单一的标量参数。

Parameters:
  • init_beta (float, optional) – 公式中的 \(\beta\),一个单一的标量参数。

  • learn_beta (bool, optional) – 如果为True,\(\beta\) 将是一个可学习的参数。

  • allow_zero_in_degree (bool, optional) – 如果图中存在0入度的节点,这些节点的输出将无效,因为没有消息会传递给这些节点。这对某些应用程序是有害的,可能导致无声的性能退化。如果检测到输入图中存在0入度的节点,此模块将引发DGLError。通过设置为True,它将抑制检查并让用户自行处理。默认值:False

注意

零入度节点将导致无效的输出值。这是因为没有消息会传递到这些节点,聚合函数将在空输入上应用。避免这种情况的常见做法是如果图是同质的,则为每个节点添加自环,这可以通过以下方式实现:

>>> g = ... # a DGLGraph
>>> g = dgl.add_self_loop(g)

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.

示例

>>> 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]))
>>> g = dgl.add_self_loop(g)
>>> 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.]],
    grad_fn=<BinaryReduceBackward>)
forward(graph, feat)[source]

Description

计算AGNN层。

param graph:

图表。

type graph:

DGLGraph

param feat:

输入特征的形状为 \((N, *)\),其中 \(N\) 是节点的数量,\(*\) 可以是任何形状。如果给定一对 torch.Tensor,这对必须包含两个形状为 \((N_{in}, *)\)\((N_{out}, *)\) 的张量,后一个张量中的 \(*\) 必须与前一个相同。

type feat:

torch.Tensor

returns:

输出特征的形状为 \((N, *)\),其中 \(*\) 应与输入形状相同。

rtype:

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.