MarginPairwiseLoss
- class MarginPairwiseLoss(margin: float = 1.0, margin_activation: str | Module | None = None, reduction: str = 'mean')[source]
基础类:
PairwiseLoss广义边际排序损失。
\[L(k, \bar{k}) = g(f(\bar{k}) - f(k) + \lambda)\]其中 \(k\) 是正三元组,\(\bar{k}\) 是负三元组,\(f\) 是交互函数(例如,
pykeen.models.TransE有 \(f(h,r,t)=-||\mathbf{e}_h+\mathbf{e}_r-\mathbf{e}_t||_p\)),\(g(x)\) 是一个激活 函数,如 ReLU 或 softmax,\(\lambda\) 是边界。初始化边距损失实例。
- Parameters:
margin (float) – 正负分数应该分开的边距。
margin_activation (str | Module | None) – 一个边界激活函数。默认为
'relu',即 \(h(\Delta) = max(0, \Delta + \lambda)\),这是默认的“边界损失”。使用'softplus'会导致“软边界”公式,如 https://arxiv.org/abs/1703.07737 中讨论的那样。reduction (str) – 用于将批次中的单个损失值聚合为标量损失值的归约操作的名称。可选值为 {‘mean’, ‘sum’}。
属性摘要
优化损失超参数的默认策略
方法总结
forward(pos_scores, neg_scores)计算边际损失。
process_lcwa_scores(predictions, labels[, ...])处理来自LCWA训练循环的分数。
process_slcwa_scores(positive_scores, ...[, ...])处理来自sLCWA训练循环的分数。
属性文档
- hpo_default: ClassVar[Mapping[str, Any]] = {'margin': {'high': 3, 'low': 0, 'type': <class 'float'>}, 'margin_activation': {'choices': {'hard', 'relu', 'soft', 'softplus'}, 'type': 'categorical'}}
优化损失超参数的默认策略
方法文档
- process_lcwa_scores(predictions: Tensor, labels: Tensor, label_smoothing: float | None = None, num_entities: int | None = None) Tensor[来源]
处理来自LCWA训练循环的分数。
- 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: