GMMConv
- class dgl.nn.pytorch.conv.GMMConv(in_feats, out_feats, dim, n_kernels, aggregator_type='sum', residual=False, bias=True, allow_zero_in_degree=False)[source]
Bases:
Module
来自使用混合模型CNN在图和流形上进行几何深度学习的高斯混合模型卷积层
\[ \begin{align}\begin{aligned}u_{ij} &= f(x_i, x_j), x_j \in \mathcal{N}(i)\\w_k(u) &= \exp\left(-\frac{1}{2}(u-\mu_k)^T \Sigma_k^{-1} (u - \mu_k)\right)\\h_i^{l+1} &= \mathrm{aggregate}\left(\left\{\frac{1}{K} \sum_{k}^{K} w_k(u_{ij}), \forall j\in \mathcal{N}(i)\right\}\right)\end{aligned}\end{align} \]其中 \(u\) 表示顶点与其邻居之间的伪坐标,使用函数 \(f\) 计算,\(\Sigma_k^{-1}\) 和 \(\mu_k\) 是可学习的参数,分别表示高斯核的协方差矩阵和均值向量。
- Parameters:
in_feats (int) – 输入特征的数量;即\(x_i\)的维度数。
out_feats (int) – 输出特征的数量;即\(h_i^{(l+1)}\)的维度数。
dim (int) – 伪坐标的维度;即\(u_{ij}\)的维度数。
n_kernels (int) – 核的数量 \(K\).
aggregator_type (str) – 聚合器类型 (
sum
,mean
,max
). 默认值:sum
.残差 (bool) – 如果为True,则在此层内使用残差连接。默认值:
False
。bias (bool) – If True, adds a learnable bias to the output. Default:
True
.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 GMMConv
>>> # 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 = GMMConv(10, 2, 3, 2, 'mean') >>> pseudo = th.ones(12, 3) >>> res = conv(g, feat, pseudo) >>> res tensor([[-0.3462, -0.2654], [-0.3462, -0.2654], [-0.3462, -0.2654], [-0.3462, -0.2654], [-0.3462, -0.2654], [-0.3462, -0.2654]], 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) >>> pseudo = th.ones(5, 3) >>> conv = GMMConv((10, 5), 2, 3, 2, 'mean') >>> res = conv(g, (u_fea, v_fea), pseudo) >>> res tensor([[-0.1107, -0.1559], [-0.1646, -0.2326], [-0.1377, -0.1943], [-0.1107, -0.1559]], grad_fn=<AddBackward0>)
- forward(graph, feat, pseudo)[source]
Description
计算高斯混合模型卷积层。
- param graph:
图表。
- type graph:
DGLGraph
- param feat:
如果给出单个张量,输入特征的形状为 \((N, D_{in})\),其中 \(D_{in}\) 是输入特征的大小,\(N\) 是节点的数量。 如果给出一对张量,这对张量必须包含两个形状为 \((N_{in}, D_{in_{src}})\) 和 \((N_{out}, D_{in_{dst}})\) 的张量。
- type feat:
torch.Tensor
- param pseudo:
形状为\((E, D_{u})\)的伪坐标张量,其中 \(E\)是图的边数,\(D_{u}\) 是伪坐标的维度。
- type pseudo:
torch.Tensor
- returns:
The output feature of shape \((N, D_{out})\) where \(D_{out}\) is the output feature size.
- 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
.