GINConv

class dgl.nn.pytorch.conv.GINConv(apply_func=None, aggregator_type='sum', init_eps=0, learn_eps=False, activation=None)[source]

基类: Module

来自论文How Powerful are Graph Neural Networks? 的图同构网络层

\[h_i^{(l+1)} = f_\Theta \left((1 + \epsilon) h_i^{l} + \mathrm{aggregate}\left(\left\{h_j^{l}, j\in\mathcal{N}(i) \right\}\right)\right)\]

如果提供了每条边上的权重张量,则加权图卷积定义为

\[h_i^{(l+1)} = f_\Theta \left((1 + \epsilon) h_i^{l} + \mathrm{aggregate}\left(\left\{e_{ji} h_j^{l}, j\in\mathcal{N}(i) \right\}\right)\right)\]

其中 \(e_{ji}\) 是从节点 \(j\) 到节点 \(i\) 的边上的权重。请确保 e_{ji} 可以与 h_j^{l} 进行广播操作。

参数:
  • apply_func (callable activation function/layer or None) – 如果不是 None,将此函数应用于更新后的节点特征,即公式中的 \(f_\Theta\),默认值:None。

  • aggregator_type (str) – 使用的聚合器类型(summaxmean),默认值:'sum'。

  • init_eps (float, optional) – 初始 \(\epsilon\) 值,默认值:0

  • learn_eps (bool, optional) – 如果为 True,\(\epsilon\) 将是一个可学习参数。默认值:False

  • activation (callable activation function/layer or None, optional) – 如果不是 None,将对更新后的节点特征应用一个激活函数。默认值:None

示例

>>> import dgl
>>> import numpy as np
>>> import torch as th
>>> from dgl.nn import GINConv
>>>
>>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3]))
>>> feat = th.ones(6, 10)
>>> lin = th.nn.Linear(10, 10)
>>> conv = GINConv(lin, 'max')
>>> res = conv(g, feat)
>>> res
tensor([[-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
        0.8843, -0.8764],
        [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
        0.8843, -0.8764],
        [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
        0.8843, -0.8764],
        [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
        0.8843, -0.8764],
        [-0.4821,  0.0207, -0.7665,  0.5721, -0.4682, -0.2134, -0.5236,  1.2855,
        0.8843, -0.8764],
        [-0.1804,  0.0758, -0.5159,  0.3569, -0.1408, -0.1395, -0.2387,  0.7773,
        0.5266, -0.4465]], grad_fn=<AddmmBackward>)
>>> # With activation
>>> from torch.nn.functional import relu
>>> conv = GINConv(lin, 'max', activation=relu)
>>> res = conv(g, feat)
>>> res
tensor([[5.0118, 0.0000, 0.0000, 3.9091, 1.3371, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [5.0118, 0.0000, 0.0000, 3.9091, 1.3371, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [5.0118, 0.0000, 0.0000, 3.9091, 1.3371, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [5.0118, 0.0000, 0.0000, 3.9091, 1.3371, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [5.0118, 0.0000, 0.0000, 3.9091, 1.3371, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [2.5011, 0.0000, 0.0089, 2.0541, 0.8262, 0.0000, 0.0000, 0.1371, 0.0000,
         0.0000]], grad_fn=<ReluBackward0>)
forward(graph, feat, edge_weight=None)[source]

描述

计算图同构网络层。

参数 graph:

图。

类型 graph:

DGLGraph

参数 feat:

如果给定一个 torch.Tensor,则是形状为 \((N, D_{in})\) 的输入特征,其中 \(D_{in}\) 是输入特征的大小,\(N\) 是节点数。如果给定一对 torch.Tensor,这对张量必须包含形状为 \((N_{in}, D_{in})\)\((N_{out}, D_{in})\) 的两个张量。如果 apply_func 不是 None,则 \(D_{in}\) 应符合 apply_func 的输入维度要求。

类型 feat:

torch.Tensor 或 torch.Tensor 对

参数 edge_weight:

边上的可选张量。如果给定,卷积将根据消息进行加权。

类型 edge_weight:

torch.Tensor,可选

返回值:

形状为 \((N, D_{out})\) 的输出特征,其中 \(D_{out}\)apply_func 的输出维度。如果 apply_func 是 None,\(D_{out}\) 应与输入维度相同。

返回类型:

torch.Tensor