RelGraphConv
- class dgl.nn.pytorch.conv.RelGraphConv(in_feat, out_feat, num_rels, regularizer=None, num_bases=None, bias=True, activation=None, self_loop=True, dropout=0.0, layer_norm=False)[source]
Bases:
Module
关系图卷积层来自使用图卷积网络建模关系数据
可以描述如下:
\[h_i^{(l+1)} = \sigma(\sum_{r\in\mathcal{R}} \sum_{j\in\mathcal{N}^r(i)}e_{j,i}W_r^{(l)}h_j^{(l)}+W_0^{(l)}h_i^{(l)})\]其中 \(\mathcal{N}^r(i)\) 是节点 \(i\) 关于关系 \(r\) 的邻居集合。\(e_{j,i}\) 是归一化因子。\(\sigma\) 是一个激活函数。\(W_0\) 是自环权重。
基础正则化通过以下方式分解 \(W_r\):
\[W_r^{(l)} = \sum_{b=1}^B a_{rb}^{(l)}V_b^{(l)}\]其中 \(B\) 是基数的数量,\(V_b^{(l)}\) 与系数 \(a_{rb}^{(l)}\) 线性组合。
块对角分解正则化将\(W_r\)分解为\(B\)个块对角矩阵。我们将\(B\)称为基的数量。
块正则化通过以下方式分解 \(W_r\):
\[W_r^{(l)} = \oplus_{b=1}^B Q_{rb}^{(l)}\]其中 \(B\) 是基数的数量,\(Q_{rb}^{(l)}\) 是具有形状 \(R^{(d^{(l+1)}/B)*(d^{l}/B)}\) 的块基数。
- Parameters:
in_feat (int) – Input feature size; i.e, the number of dimensions of \(h_j^{(l)}\).
out_feat (int) – Output feature size; i.e., the number of dimensions of \(h_i^{(l+1)}\).
num_rels (int) – Number of relations.
regularizer (str, optional) –
使用哪种权重正则化器(“basis”、“bdd”或
None
):“basis”用于基分解。
“bdd”用于块对角分解。
None
表示不应用正则化。
默认值:
None
。num_bases (int, optional) – 基础数量。当应用正则化器时生效。 如果
None
,则使用关系数量 (num_rels
)。默认值:None
。 请注意,当应用“bdd”正则化器时,in_feat
和out_feat
必须能被num_bases
整除。bias (bool, optional) – True if bias is added. Default:
True
.activation (callable, optional) – 激活函数。默认值:
None
。self_loop (bool, optional) – True to include self loop message. Default:
True
.dropout (float, optional) – 丢弃率。默认值:
0.0
layer_norm (bool, 可选) – 如果为True,则添加层归一化。默认值:
False
示例
>>> import dgl >>> import numpy as np >>> import torch as th >>> from dgl.nn import RelGraphConv >>> >>> g = dgl.graph(([0,1,2,3,2,5], [1,2,3,4,0,3])) >>> feat = th.ones(6, 10) >>> conv = RelGraphConv(10, 2, 3, regularizer='basis', num_bases=2) >>> etype = th.tensor([0,1,2,0,1,2]) >>> res = conv(g, feat, etype) >>> res tensor([[ 0.3996, -2.3303], [-0.4323, -0.1440], [ 0.3996, -2.3303], [ 2.1046, -2.8654], [-0.4323, -0.1440], [-0.1309, -1.0000]], grad_fn=<AddBackward0>)
- forward(g, feat, etypes, norm=None, *, presorted=False)[source]
前向计算。
- Parameters:
g (DGLGraph) – The graph.
feat (torch.Tensor) – A 2D tensor of node features. Shape: \((|V|, D_{in})\).
etypes (torch.Tensor 或 list[int]) – 一个1D整数张量,表示边的类型。形状:\((|E|,)\)。
norm (torch.Tensor, optional) – 一个1D张量的边范数值。形状:\((|E|,)\)。
presorted (bool, optional) – 输入图的边是否已按其类型排序。 在预排序的图上进行前向传播可能会更快。由
to_homogeneous()
创建的图自动满足此条件。 另请参阅reorder_graph()
以手动排序边。
- Returns:
New node features. Shape: \((|V|, D_{out})\).
- Return type:
torch.Tensor