RGCN层

class RGCNLayer(num_relations: int, input_dim: int = 32, output_dim: int | None = None, use_bias: bool = True, activation: str | Module | None = None, activation_kwargs: Mapping[str, Any] | None = None, self_loop_dropout: float = 0.2, decomposition: str | Decomposition | None = None, decomposition_kwargs: Mapping[str, Any] | None = None)[源代码]

基础类: Module

一个来自[schlichtkrull2018]的RGCN层,已更新以匹配官方实现。

该层使用单独的分解来处理前向和后向边(即“正常”和隐式创建的逆关系),以及自环的单独转换。

忽略掉队、分解和归一化,可以写成

\[y_i = \sigma( W^s x_i + \sum_{(e_j, r, e_i) \in \mathcal{T}} W^f_r x_j + \sum_{(e_i, r, e_j) \in \mathcal{T}} W^b_r x_j + b )\]

其中 \(b, W^s, W^f_r, W^b_r\) 是可训练的权重。\(W^f_r, W^b_r\) 是关系特定的,并且通常采用权重共享机制,参见分解。\(\sigma\) 是一个激活函数。两个求和中的各个项通常被加权。这是通过 EdgeWeighting 实现的。此外,RGCN 采用了边丢弃,然而,这需要在单个层之外完成,因为相同的边在所有层中被丢弃。相反,自环丢弃是特定于层的。

初始化层。

Parameters:
  • input_dim (int) – >0 输入维度

  • num_relations (int) – 关系的数量

  • output_dim (int | None) – >0 输出维度。如果没有给出,则使用输入维度。

  • use_bias (bool) – 是否使用可训练的偏置

  • activation (str | Module | None) – 使用的激活函数。默认为 None,即恒等函数作为激活函数。

  • activation_kwargs (Mapping[str, Any] | None) – 传递给激活函数以进行实例化的额外基于关键字的参数

  • self_loop_dropout (float) – 0 <= self_loop_dropout <= 1 用于自环的dropout

  • 分解 (str | Decomposition | None) – 使用的分解方法,参见 Decomposition 和 decomposition_resolver

  • decomposition_kwargs (Mapping[str, Any] | None) – 传递给分解以进行实例化的基于关键字的参数

方法总结

forward(x, source, target, edge_type[, ...])

计算丰富的实体表示。

方法文档

forward(x: Tensor, source: Tensor, target: Tensor, edge_type: Tensor, edge_weights: Tensor | None = None) Tensor[来源]

计算丰富的实体表示。

Parameters:
  • x (Tensor) – 形状: (num_entities, input_dim) 输入的实体表示。

  • source (Tensor) – 形状: (num_triples,) 每个三元组中源实体的索引。

  • target (Tensor) – 形状: (num_triples,) 每个三元组的目标实体的索引。

  • edge_type (Tensor) – 形状: (num_triples,) 每个三元组的关系类型。

  • edge_weights (Tensor | None) – 形状: (num_triples,) 每个三元组的标量边权重。

Returns:

形状: (num_entities, output_dim) 丰富的实体表示。

Return type:

Tensor