SAGEConv
- class dgl.nn.pytorch.conv.SAGEConv(in_feats, out_feats, aggregator_type, feat_drop=0.0, bias=True, norm=None, activation=None)[source]
Bases:
Module
GraphSAGE 层来自 大型图上的归纳表示学习
\[ \begin{align}\begin{aligned}h_{\mathcal{N}(i)}^{(l+1)} &= \mathrm{aggregate} \left(\{h_{j}^{l}, \forall j \in \mathcal{N}(i) \}\right)\\h_{i}^{(l+1)} &= \sigma \left(W \cdot \mathrm{concat} (h_{i}^{l}, h_{\mathcal{N}(i)}^{l+1}) \right)\\h_{i}^{(l+1)} &= \mathrm{norm}(h_{i}^{(l+1)})\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)\]其中 \(e_{ji}\) 是从节点 \(j\) 到节点 \(i\) 的边上的标量权重。 请确保 \(e_{ji}\) 可以与 \(h_j^{l}\) 进行广播。
- Parameters:
in_feats (int, 或 一对 整数) –
输入特征大小;即,\(h_i^{(l)}\) 的维度数。
SAGEConv 可以应用于同构图和单向 二分图。 如果该层应用于单向二分图,
in_feats
指定源节点和目标节点的输入特征大小。如果 给定一个标量,源节点和目标节点的特征大小将取相同的值。如果聚合器类型是
gcn
,源节点和目标节点的特征大小 需要相同。out_feats (int) – Output feature size; i.e, the number of dimensions of \(h_i^{(l+1)}\).
aggregator_type (str) – 使用的聚合器类型 (
mean
,gcn
,pool
,lstm
).feat_drop (float) – Dropout rate on features, default:
0
.bias (bool) – If True, adds a learnable bias to the output. Default:
True
.norm (callable activation function/layer or None, optional) – If not None, applies normalization to the updated node features.
activation (callable activation function/layer or None, optional) – If not None, applies an activation function to the updated node features. Default:
None
.
示例
>>> import dgl >>> import numpy as np >>> import torch as th >>> from dgl.nn import SAGEConv
>>> # 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) >>> conv = SAGEConv(10, 2, 'pool') >>> res = conv(g, feat) >>> res tensor([[-1.0888, -2.1099], [-1.0888, -2.1099], [-1.0888, -2.1099], [-1.0888, -2.1099], [-1.0888, -2.1099], [-1.0888, -2.1099]], grad_fn=<AddBackward0>)
>>> # 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_fea = th.rand(2, 5) >>> v_fea = th.rand(4, 10) >>> conv = SAGEConv((5, 10), 2, 'mean') >>> res = conv(g, (u_fea, v_fea)) >>> res tensor([[ 0.3163, 3.1166], [ 0.3866, 2.5398], [ 0.5873, 1.6597], [-0.2502, 2.8068]], grad_fn=<AddBackward0>)
- forward(graph, feat, edge_weight=None)[source]
Description
计算GraphSAGE层。
- param graph:
图表。
- type graph:
DGLGraph
- 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 edge_weight:
边缘上的可选张量。如果提供,卷积将根据消息进行加权。
- type edge_weight:
torch.Tensor, 可选的
- returns:
输出特征的形状为 \((N_{dst}, D_{out})\),其中 \(N_{dst}\) 是输入图中目标节点的数量,\(D_{out}\) 是输出特征的大小。
- rtype:
torch.Tensor