mindspore_gl.nn.SAGEConv

class mindspore_gl.nn.SAGEConv(in_feat_size: int, out_feat_size: int, aggregator_type: str = 'pool', bias=True, norm=None, activation=None)[源代码]

GraphSAGE层。来自论文 Inductive Representation Learning on Large Graphs

\[ \begin{align}\begin{aligned}\begin{split}h_{\mathcal{N}(i)}^{(l+1)} = \mathrm{aggregate} \left(\{h_{j}^{l}, \forall j \in \mathcal{N}(i) \}\right) \\\end{split}\\\begin{split}h_{i}^{(l+1)} = \sigma \left(W \cdot \mathrm{concat} (h_{i}^{l}, h_{\mathcal{N}(i)}^{l+1}) \right)\\\end{split}\\h_{i}^{(l+1)} = \mathrm{norm}(h_{i}^{l})\end{aligned}\end{align} \]

如果提供了各个边的权重,则加权图卷积定义为:

\[h_{\mathcal{N}(i)}^{(l+1)} = \mathrm{aggregate} \left(\{e_{ji} h_{j}^{l}, \forall j \in \mathcal{N}(i) \}\right)\]
参数:
  • in_feat_size (int) - 输入节点特征大小。

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

  • aggregator_type (str) - 聚合器的类型,应在’pool’、’lstm’和’mean’中。默认值:pool。

  • bias (bool) - 是否使用偏差。默认值:True。

  • norm (mindspore.nn.Cell) - 归一化函数单元。默认值:None。

  • activation (mindspore.nn.Cell) - 激活函数Cell。默认值:None。

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

  • edge_weight (Tensor) - 边权重。Shape为 \((N\_e,)\) 其中 \(N\_e\) 是边的数量。

  • g (Graph) - 输入图。

输出:
  • Tensor,输出特征Shape为 \((N,D\_out)\) 其中 \(N\) 是节点数, \(D\_out\) 可以是任何形状。

异常:
  • KeyError - 如果 aggregator 类型不是pool、lstm或mean。

  • TypeError - 如果 in_feat_sizeout_feat_size 不是int。

  • TypeError - 如果 bias 不是bool。

  • TypeError - 如果 activation 类型不是mindspore.nn.Cell。

  • TypeError - 如果 norm 类型不是mindspore.nn.Cell。

支持平台:

Ascend GPU

样例:

>>> import mindspore as ms
>>> from mindspore import nn
>>> from mindspore.numpy import ones
>>> from mindspore_gl.nn import SAGEConv
>>> from mindspore_gl import GraphField
>>> n_nodes = 4
>>> n_edges = 7
>>> feat_size = 4
>>> src_idx = ms.Tensor([0, 1, 1, 2, 2, 3, 3], ms.int32)
>>> dst_idx = ms.Tensor([0, 0, 2, 1, 3, 0, 1], ms.int32)
>>> ones = ms.ops.Ones()
>>> feat = ones((n_nodes, feat_size), ms.float32)
>>> graph_field = GraphField(src_idx, dst_idx, n_nodes, n_edges)
>>> sageconv = SAGEConv(in_feat_size=4, out_feat_size=2, activation=nn.ReLU())
>>> edge_weight = ones((n_edges, 1), ms.float32)
>>> res = sageconv(feat, edge_weight, *graph_field.get_graph())
>>> print(res.shape)
(4,2)