EdgeConv
- class dgl.nn.pytorch.conv.EdgeConv(in_feat, out_feat, batch_norm=False, allow_zero_in_degree=False)[source]
Bases:
Module
EdgeConv 层来自 Dynamic Graph CNN for Learning on Point Clouds
它可以描述如下:
\[h_i^{(l+1)} = \max_{j \in \mathcal{N}(i)} ( \Theta \cdot (h_j^{(l)} - h_i^{(l)}) + \Phi \cdot h_i^{(l)})\]其中 \(\mathcal{N}(i)\) 是 \(i\) 的邻居。 \(\Theta\) 和 \(\Phi\) 是线性层。
注意
原始公式在最大运算符内包含一个ReLU。这相当于首先应用最大运算符,然后应用ReLU。
- Parameters:
in_feat (int) – 输入特征大小;即,\(h_j^{(l)}\)的维度数。
out_feat (int) – 输出特征大小;即 \(h_i^{(l+1)}\) 的维度数。
batch_norm (bool) – 是否在消息中包含批量归一化。默认值:
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 EdgeConv
>>> # 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 = EdgeConv(10, 2) >>> res = conv(g, feat) >>> res tensor([[-0.2347, 0.5849], [-0.2347, 0.5849], [-0.2347, 0.5849], [-0.2347, 0.5849], [-0.2347, 0.5849], [-0.2347, 0.5849]], grad_fn=<CopyReduceBackward>)
>>> # 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, 5) >>> conv = EdgeConv(5, 2, 3) >>> res = conv(g, (u_fea, v_fea)) >>> res tensor([[ 1.6375, 0.2085], [-1.1925, -1.2852], [ 0.2101, 1.3466], [ 0.2342, -0.9868]], grad_fn=<CopyReduceBackward>)
- forward(g, feat)[source]
Description
前向计算
- param g:
图表。
- type g:
DGLGraph
- param feat:
\((N, D)\) 其中 \(N\) 是节点数量, \(D\) 是特征维度数量。
如果给出一对张量,图必须是一个单二分图,只有一种边类型,并且这两个张量在除第一轴外的所有轴上必须具有相同的维度。
- type feat:
张量或张量对
- returns:
新节点功能。
- 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
.