CompGCN层

class CompGCNLayer(input_dim: int, output_dim: int | None = None, dropout: float = 0.0, use_bias: bool = True, use_relation_bias: bool = False, composition: str | ~pykeen.nn.compositions.CompositionModule | None = None, attention_heads: int = 4, attention_dropout: float = 0.1, activation: str | ~torch.nn.modules.module.Module | type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.linear.Identity'>, activation_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None, edge_weighting: str | type[~pykeen.nn.weighting.EdgeWeighting] | None = <class 'pykeen.nn.weighting.SymmetricEdgeWeighting'>)[source]

基础类: Module

CompGCN模型的单层。

初始化模块。

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

  • output_dim (int | None) – 输出维度。如果为None,则等于输入维度。

  • dropout (float) – 用于前向和后向边的dropout。

  • use_bias (bool) – # TODO: 我们真的需要这个吗?它位于一个强制性的批量归一化层之前 是否使用偏置。

  • use_relation_bias (bool) – 是否在关系转换中使用偏置。

  • 组合 (提示[CompositionModule]) – 组合函数。

  • attention_heads (int) – 使用注意力加权时的注意力头数量

  • attention_dropout (float) – 用于注意力消息加权的Dropout

  • activation (HintOrType[nn.Module]) – 要使用的激活函数。

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

  • edge_weighting (HintType[EdgeWeighting]) – 一个预先实例化的 EdgeWeighting,一个类,或者一个名称,用于通过 class_resolver 查找。

方法总结

forward(x_e, x_r, edge_index, edge_type)

更新实体和关系表示。

message(x_e, x_r, edge_index, edge_type, weight)

执行消息传递。

reset_parameters()

重置模型的参数。

方法文档

forward(x_e: Tensor, x_r: Tensor, edge_index: Tensor, edge_type: Tensor) tuple[Tensor, Tensor][来源]

更新实体和关系表示。

\[X_E'[e] = \frac{1}{3} \left( X_E W_s + \left( \sum_{h,r,e \in T} \alpha(h, e) \phi(X_E[h], X_R[r]) W_f \right) + \left( \sum_{e,r,t \in T} \alpha(e, t) \phi(X_E[t], X_R[r^{-1}]) W_b \right) \right)\]
Parameters:
  • x_e (Tensor) – 形状: (num_entities, input_dim) 实体表示。

  • x_r (Tensor) – 形状: (2 * num_relations, input_dim) 关系表示(包括反向关系)。

  • edge_index (Tensor) – 形状: (2, num_edges) 边索引,每个三元组的源实体和目标实体的对。

  • edge_type (Tensor) – 形状 (num_edges,) 每条三元组的边类型,即关系ID。

Returns:

形状: (num_entities, output_dim) / (2 * num_relations, output_dim) 更新后的实体和关系表示。

Return type:

tuple[Tensor, Tensor]

message(x_e: Tensor, x_r: Tensor, edge_index: Tensor, edge_type: Tensor, weight: Parameter) Tensor[来源]

执行消息传递。

Parameters:
  • x_e (Tensor) – 形状: (num_entities, input_dim) 实体表示。

  • x_r (Tensor) – 形状: (2 * num_relations, input_dim) 关系表示(包括逆关系)。

  • edge_index (Tensor) – 形状: (2, num_edges) 边索引,每个三元组的源实体和目标实体的对。

  • edge_type (Tensor) – 形状 (num_edges,) 每条三元组的边类型,即关系ID。

  • weight (Parameter) – 变换权重。

Returns:

更新后的实体表示。

Return type:

Tensor

reset_parameters()[来源]

重置模型的参数。