MarginPairwiseLoss
- class MarginPairwiseLoss(margin, margin_activation, reduction='mean')[source]
Bases:
pykeen.losses.PairwiseLoss
Generalized margin ranking loss.
\[L(k, \bar{k}) = g(f(\bar{k}) - f(k) + \lambda)\]Where \(k\) are the positive triples, \(\bar{k}\) are the negative triples, \(f\) is the interaction function (e.g.,
pykeen.models.TransE
has \(f(h,r,t)=\mathbf{e}_h+\mathbf{r}_r-\mathbf{e}_t\)), \(g(x)\) is an activation function like the ReLU or softmax, and \(\lambda\) is the margin.Initialize the margin loss instance.
- Parameters
margin (
float
) – The margin by which positive and negative scores should be apart.margin_activation (
Union
[str
,Module
,None
]) – A margin activation. Defaults to'relu'
, i.e. \(h(\Delta) = max(0, \Delta + \lambda)\), which is the default “margin loss”. Using'softplus'
leads to a “soft-margin” formulation as discussed in https://arxiv.org/abs/1703.07737.reduction (
str
) – The name of the reduction operation to aggregate the individual loss values from a batch to a scalar loss value. From {‘mean’, ‘sum’}.
Methods Summary
forward
(pos_scores, neg_scores)Compute the margin loss.
process_lcwa_scores
(predictions, labels[, ...])Process scores from LCWA training loop.
process_slcwa_scores
(positive_scores, ...[, ...])Process scores from sLCWA training loop.
Methods Documentation
- forward(pos_scores, neg_scores)[source]
Compute the margin loss.
The scores have to be in broadcastable shape.
- Parameters
pos_scores (
FloatTensor
) – The positive scores.neg_scores (
FloatTensor
) – The negative scores.
- Return type
FloatTensor
- Returns
A scalar loss term.
- process_lcwa_scores(predictions, labels, label_smoothing=None, num_entities=None)[source]
Process scores from LCWA training loop.
- Parameters
- Return type
FloatTensor
- Returns
A scalar loss value.
- process_slcwa_scores(positive_scores, negative_scores, label_smoothing=None, batch_filter=None, num_entities=None)[source]
Process scores from sLCWA training loop.
- Parameters
positive_scores (
FloatTensor
) – shape: (batch_size, 1) The scores for positive triples.negative_scores (
FloatTensor
) – shape: (batch_size, num_neg_per_pos) or (num_unfiltered_negatives,) The scores for the negative triples, either in dense 2D shape, or in case they are already filtered, in sparse shape. If they are given in sparse shape, batch_filter needs to be provided, too.label_smoothing (
Optional
[float
]) – An optional label smoothing parameter.batch_filter (
Optional
[BoolTensor
]) – shape: (batch_size, num_neg_per_pos) An optional filter of negative scores which were kept. Given if and only if negative_scores have been pre-filtered.num_entities (
Optional
[int
]) – The number of entities. Only required if label smoothing is enabled.
- Return type
FloatTensor
- Returns
A scalar loss term.