DotGatConv
- class dgl.nn.pytorch.conv.DotGatConv(in_feats, out_feats, num_heads, allow_zero_in_degree=False)[source]
Bases:
Module
在图注意力网络中应用自注意力的点积版本
\[h_i^{(l+1)} = \sum_{j\in \mathcal{N}(i)} \alpha_{i, j} h_j^{(l)}\]其中 \(\alpha_{ij}\) 是节点 \(i\) 和节点 \(j\) 之间的注意力分数:
\[ \begin{align}\begin{aligned}\alpha_{i, j} &= \mathrm{softmax_i}(e_{ij}^{l})\\e_{ij}^{l} &= ({W_i^{(l)} h_i^{(l)}})^T \cdot {W_j^{(l)} h_j^{(l)}}\end{aligned}\end{align} \]其中 \(W_i\) 和 \(W_j\) 将节点 \(i\) 和节点 \(j\) 的特征转换为相同的维度,以便在计算节点特征的相似性时,可以使用点积。
- Parameters:
in_feats (int, 或 一对 整数) – 输入特征大小;即 \(h_i^{(l)}\) 的维度数。 DotGatConv 可以应用于同构图和单向 二分图。 如果该层要应用于单向二分图,
in_feats
指定源节点和目标节点的输入特征大小。如果 给定一个标量,源节点和目标节点的特征大小将取相同的值。out_feats (int) – 输出特征大小;即\(h_i^{(l+1)}\)的维度数。
num_heads (int) – 多头注意力机制中的头数
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
.
注意
零入度节点将导致无效的输出值。这是因为没有消息会传递到这些节点,聚合函数将在空输入上应用。避免这种情况的常见做法是如果图是同质的,则为每个节点添加自环,这可以通过以下方式实现:
>>> 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 DotGatConv
>>> # Case 1: Homogeneous graph >>> 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) >>> dotgatconv = DotGatConv(10, 2, num_heads=3) >>> res = dotgatconv(g, feat) >>> res tensor([[[ 3.4570, 1.8634], [ 1.3805, -0.0762], [ 1.0390, -1.1479]], [[ 3.4570, 1.8634], [ 1.3805, -0.0762], [ 1.0390, -1.1479]], [[ 3.4570, 1.8634], [ 1.3805, -0.0762], [ 1.0390, -1.1479]], [[ 3.4570, 1.8634], [ 1.3805, -0.0762], [ 1.0390, -1.1479]], [[ 3.4570, 1.8634], [ 1.3805, -0.0762], [ 1.0390, -1.1479]], [[ 3.4570, 1.8634], [ 1.3805, -0.0762], [ 1.0390, -1.1479]]], grad_fn=<BinaryReduceBackward>)
>>> # Case 2: Unidirectional bipartite graph >>> u = [0, 1, 0, 0, 1] >>> v = [0, 1, 2, 3, 2] >>> g = dgl.heterograph({('_N', '_E', '_N'):(u, v)}) >>> u_feat = th.tensor(np.random.rand(2, 5).astype(np.float32)) >>> v_feat = th.tensor(np.random.rand(4, 10).astype(np.float32)) >>> dotgatconv = DotGatConv((5,10), 2, 3) >>> res = dotgatconv(g, (u_feat, v_feat)) >>> res tensor([[[-0.6066, 1.0268], [-0.5945, -0.4801], [ 0.1594, 0.3825]], [[ 0.0268, 1.0783], [ 0.5041, -1.3025], [ 0.6568, 0.7048]], [[-0.2688, 1.0543], [-0.0315, -0.9016], [ 0.3943, 0.5347]], [[-0.6066, 1.0268], [-0.5945, -0.4801], [ 0.1594, 0.3825]]], grad_fn=<BinaryReduceBackward>)
- forward(graph, feat, get_attention=False)[source]
Description
在GCN中应用自注意力的点积版本。
- param graph:
图表
- type graph:
DGL图或二分图
- param feat:
如果给定一个torch.Tensor,输入特征的形状为\((N, D_{in})\),其中 \(D_{in}\)是输入特征的大小,\(N\)是节点的数量。 如果给定一对torch.Tensor,这对张量必须包含两个形状为 \((N_{in}, D_{in_{src}})\)和\((N_{out}, D_{in_{dst}})\)的张量。
- type feat:
torch.Tensor 或一对 torch.Tensor
- param get_attention:
是否返回注意力值。默认为False。
- type get_attention:
布尔值,可选
- returns:
torch.Tensor – 输出特征的形状为 \((N, D_{out})\),其中 \(D_{out}\) 是输出特征的大小。
torch.Tensor, 可选 – 形状为 \((E, 1)\) 的注意力值,其中 \(E\) 是边的数量。仅当
get_attention
为True
时返回。
- 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
.