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