mindspore_gl.nn.DOTGATConv

查看源文件
class mindspore_gl.nn.DOTGATConv(in_feat_size: int, out_feat_size: int, num_heads: int, bias=False)[源代码]

在GAT中应用点积版的self-attention。 来自论文 Graph Attention Network

\[h_i^{(l+1)} = \sum_{j\in \mathcal{N}(i)} \alpha_{i, j} h_j^{(l)}\]

\(\alpha_{i, j}\) 表示节点 \(i\) 和节点 \(j\) 之间的attention分数。

\[\begin{split}\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{split}\]
参数:
  • in_feat_size (int) - 输入节点特征大小。

  • out_feat_size (int) - 输出节点特征大小。

  • num_heads (int) - GAT中使用的attention头数。

  • bias (bool, 可选) - 是否使用偏置。默认值:False

输入:
  • x (Tensor) - 输入节点特征。Shape为 \((N,*)\) ,其中 \(N\) 是节点数, \(*\) 可以是任何shape。

  • *g (Graph) - 输入图。

输出:
  • Tensor,输出节点特征。Shape为 \((N, num\_heads, out\_feat\_size)\)

异常:
  • TypeError - 如果 in_feat_size 不是正整数。

  • TypeError - 如果 out_feat_size 不是正整数。

  • TypeError - 如果 num_heads 不是正整数。

  • TypeError - 如果 bias 不是bool。

支持平台:

Ascend GPU

样例:

>>> import mindspore as ms
>>> from mindspore_gl.nn import DOTGATConv
>>> from mindspore_gl import GraphField
>>> n_nodes = 4
>>> n_edges = 8
>>> feat_size = 16
>>> src_idx = ms.Tensor([0, 0, 0, 1, 1, 1, 2, 3], ms.int32)
>>> dst_idx = ms.Tensor([0, 1, 3, 1, 2, 3, 3, 2], ms.int32)
>>> ones = ms.ops.Ones()
>>> nodes_feat = ones((n_nodes, feat_size), ms.float32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> out_size = 4
>>> conv = DOTGATConv(feat_size, out_size, num_heads=2, bias=True)
>>> ret = conv(nodes_feat, *graph_field.get_graph())
>>> print(ret.shape)
(4, 2, 4)