SGConv
- class dgl.nn.pytorch.conv.SGConv(in_feats, out_feats, k=1, cached=False, bias=True, norm=None, allow_zero_in_degree=False)[source]
Bases:
Module
SGC层来自简化图卷积网络
\[H^{K} = (\tilde{D}^{-1/2} \tilde{A} \tilde{D}^{-1/2})^K X \Theta\]其中 \(\tilde{A}\) 是 \(A\) + \(I\)。 因此,图输入预期会添加自环边。
- Parameters:
in_feats (int) – 输入特征的数量;即,\(X\)的维度数。
out_feats (int) – 输出特征的数量;即,\(H^{K}\)的维度数。
k (int) – 跳数 \(K\). 默认值:
1
.cached (bool) –
如果为True,模块将在第一次前向调用时缓存
\[(\tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}})^K X\Theta\]这个参数应该只在Transductive Learning设置中设置为
True
。bias (bool) – If True, adds a learnable bias to the output. Default:
True
.norm (可调用的激活函数/层 或 None, 可选) – 如果不为None,则对更新的节点特征应用归一化。默认值:
False
。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 SGConv >>> >>> 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 = SGConv(10, 2, k=2) >>> res = conv(g, feat) >>> res tensor([[-1.9441, -0.9343], [-1.9441, -0.9343], [-1.9441, -0.9343], [-2.7709, -1.3316], [-1.9297, -0.9273], [-1.9441, -0.9343]], grad_fn=<AddmmBackward>)
- forward(graph, feat, edge_weight=None)[source]
Description
计算简化图卷积层。
- param graph:
图表。
- type graph:
DGLGraph
- param feat:
输入特征的形状为 \((N, D_{in})\),其中 \(D_{in}\) 是输入特征的大小,\(N\) 是节点的数量。
- type feat:
torch.Tensor
- param edge_weight:
edge_weight to use in the message passing process. This is equivalent to using weighted adjacency matrix in the equation above, and \(\tilde{D}^{-1/2}\tilde{A} \tilde{D}^{-1/2}\) is based on
dgl.nn.pytorch.conv.graphconv.EdgeWeightNorm
.- type edge_weight:
torch.Tensor, 可选的
- returns:
The output feature of shape \((N, D_{out})\) where \(D_{out}\) is size of output feature.
- 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
.
注意
如果
cache
设置为 True,feat
和graph
在训练期间不应更改,否则您将得到错误的结果。