AdversarialLoss
- class AdversarialLoss(inverse_softmax_temperature: float = 1.0, reduction: str = 'mean')[source]
Bases:
SetwiseLoss
A loss with adversarial weighting of negative samples.
Initialize the adversarial loss.
- Parameters:
Methods Summary
forward
(pos_scores, neg_scores, neg_weights)Calculate the loss for the given scores.
negative_loss_term_unreduced
(neg_scores[, ...])Calculate the loss for the negative scores without reduction.
positive_loss_term
(pos_scores[, ...])Calculate the loss for the positive scores.
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: Tensor, neg_scores: Tensor, neg_weights: Tensor, label_smoothing: float | None = None, num_entities: int | None = None) Tensor [source]
Calculate the loss for the given scores.
- Parameters:
pos_scores (Tensor) – shape: s_p a tensor of positive scores
neg_scores (Tensor) – shape: s_n a tensor of negative scores
neg_weights (Tensor) – shape: s_n the adversarial weights of the negative scores
label_smoothing (float | None) – An optional label smoothing parameter.
num_entities (int | None) – The number of entities (required for label-smoothing).
- Returns:
a scalar loss value
- Return type:
- abstract negative_loss_term_unreduced(neg_scores: Tensor, label_smoothing: float | None = None, num_entities: int | None = None) Tensor [source]
Calculate the loss for the negative scores without reduction.
- abstract positive_loss_term(pos_scores: Tensor, label_smoothing: float | None = None, num_entities: int | None = None) Tensor [source]
Calculate the loss for the positive scores.
- process_lcwa_scores(predictions: Tensor, labels: Tensor, label_smoothing: float | None = None, num_entities: int | None = None) Tensor [source]
Process scores from LCWA training loop.
- Parameters:
- Returns:
A scalar loss value.
- Return type:
- 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]
Process scores from sLCWA training loop.
- Parameters:
positive_scores (Tensor) – shape: (batch_size, 1) The scores for positive triples.
negative_scores (Tensor) – 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 (float | None) – An optional label smoothing parameter.
batch_filter (Tensor | None) – 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 (int | None) – The number of entities. Only required if label smoothing is enabled.
- Returns:
A scalar loss term.
- Return type: