InfoNCELoss

class InfoNCELoss(margin: float = 0.02, log_adversarial_temperature: float = -2.995732273553991, reduction: str = 'mean')[source]

基础类:CrossEntropyLoss

[wang2022]提出的带有附加边际的InfoNCE损失。

这个损失等同于CrossEntropyLoss,其中分数已经被转换:

  • 正分数减去边距 gamma,然后除以温度 tau

    \[f'(k) = \frac{f(k) - \gamma}{\tau}\]
  • 负分数仅除以温度 tau

    \[f'(k^-) = \frac{f(k^-)}{\tau}\]

初始化损失。

Parameters:
Raises:

ValueError – 如果边距为负数

属性摘要

DEFAULT_LOG_ADVERSARIAL_TEMPERATURE

hpo_default

优化损失超参数的默认策略

方法总结

process_lcwa_scores(predictions, labels[, ...])

处理来自LCWA训练循环的分数。

process_slcwa_scores(positive_scores, ...[, ...])

处理来自sLCWA训练循环的分数。

属性文档

DEFAULT_LOG_ADVERSARIAL_TEMPERATURE: ClassVar[float] = -2.995732273553991
hpo_default: ClassVar[Mapping[str, Any]] = {'log_adversarial_temperature': {'high': 3.0, 'low': -3.0, 'type': <class 'float'>}, 'margin': {'high': 0.1, 'low': 0.01, 'type': <class 'float'>}}

优化损失超参数的默认策略

方法文档

process_lcwa_scores(predictions: Tensor, labels: Tensor, label_smoothing: float | None = None, num_entities: int | None = None) Tensor[来源]

处理来自LCWA训练循环的分数。

Parameters:
  • predictions (Tensor) – 形状: (batch_size, num_entities) 分数。

  • labels (Tensor) – 形状: (batch_size, num_entities) 标签。

  • label_smoothing (float | None) – 一个可选的标签平滑参数。

  • num_entities (int | None) – 实体数量(用于标签平滑的必需参数)。

Returns:

一个标量损失值。

Return type:

Tensor

process_slcwa_scores(positive_scores: Tensor, negative_scores: Tensor, label_smoothing: float | None = None, batch_filter: Tensor | None = None, num_entities: int | None = None) Tensor[source]

处理来自sLCWA训练循环的分数。

Parameters:
  • positive_scores (Tensor) – 形状: (batch_size, 1) 正三元组的分数。

  • negative_scores (Tensor) – 形状: (batch_size, num_neg_per_pos) 或 (num_unfiltered_negatives,) 负三元组的分数,可以是密集的2D形状,或者如果它们已经被过滤,可以是稀疏形状。如果它们以稀疏形状给出,还需要提供batch_filter。

  • label_smoothing (float | None) – 一个可选的标签平滑参数。

  • batch_filter (Tensor | None) – 形状: (batch_size, num_neg_per_pos) 一个可选的负分数过滤器,用于保留哪些负分数。仅在负分数已被预过滤时提供。

  • num_entities (int | None) – 实体的数量。仅在启用标签平滑时需要。

Returns:

一个标量损失项。

Return type:

Tensor