CrossE交互

class CrossEInteraction(embedding_dim: int = 50, combination_activation: str | ~torch.nn.modules.module.Module | type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.activation.Tanh'>, combination_activation_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None, combination_dropout: float | None = 0.5)[source]

基础类:Interaction[Tensor, tuple[Tensor, Tensor], Tensor]

CrossE的有状态交互函数。

交互函数由以下给出

\[\textit{drop}( \textit{act}( \mathbf{c}_r \odot \mathbf{h} + \mathbf{c}_r \odot \mathbf{h} \odot \mathbf{r} + \mathbf{b}) ) )^T \mathbf{t}\]

其中 \(\mathbf{h}, \mathbf{c}_r, \mathbf{r}, \mathbf{t} \in \mathbb{R}^d\) 分别是头嵌入、关系交互向量、关系嵌入和尾嵌入。 \(\mathbf{b} \in \mathbb{R}^d\) 是一个全局偏置向量(这使得这个交互函数具有状态)。 \(\textit{drop}\) 表示 dropout,而 \(\textit{act}\) 是激活函数。

注意

CrossE论文描述了作为交互函数一部分的额外sigmoid激活。由于使用对数似然损失可能会导致数值问题(由于在log之前显式调用sigmoid),我们在实现中不使用它,而是选择数值稳定的变体。然而,模型本身有一个选项predict_with_sigmoid,可以在推理期间强制使用sigmoid。这也可能影响基于排名的评分,因为有限的数值精度可能导致多个选择得分完全相同。在这种情况下,排名的定义不明确,有几种竞争的方法可以打破平局。有关更多信息,请参见理解评估

实例化交互模块。

Parameters:
  • embedding_dim (int) – 嵌入维度。

  • combination_activation (HintOrType[nn.Module]) – 组合激活函数。

  • combination_activation_kwargs (Mapping[str, Any] | None) – 传递给组合激活函数构造函数的额外基于关键字的参数(如果尚未实例化)。

  • combination_dropout (float | None) – 一个可选的dropout,在组合之后和点积相似度之前应用。

注意

参数对 (combination_activation, combination_activation_kwargs) 用于 class_resolver.contrib.torch.activation_resolver

解析器的解释及其使用方法在 https://class-resolver.readthedocs.io/en/latest/中给出。

属性摘要

relation_shape

关系表示的符号形状

方法总结

forward(h, r, t)

评估交互函数。

属性文档

relation_shape: Sequence[str] = ('d', 'd')

关系表示的符号形状

方法文档

forward(h: Tensor, r: tuple[Tensor, Tensor], t: Tensor) Tensor[源代码]

评估交互函数。

另请参阅

Interaction.forward 提供了关于交互函数通用批处理形式的详细描述。

Parameters:
  • h (Tensor) – 形状: (*batch_dims, dim) 头部表示。

  • r (tuple[Tensor, Tensor]) – 形状: (*batch_dims, dim) 关系表示和关系特定的交互向量。

  • t (Tensor) – 形状: (*batch_dims, dim) 尾部表示。

Returns:

形状: batch_dims 分数。

Return type:

Tensor