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:
  • inverse_softmax_temperature (float) – the inverse of the softmax temperature

  • reduction (str) – the name of the reduction operation, cf. Loss.__init__()

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:

Tensor

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.

Parameters:
  • neg_scores (Tensor) – any shape the negative scores

  • label_smoothing (float | None) – the label smoothing parameter

  • num_entities (int | None) – the number of entities (required for label-smoothing)

Returns:

scalar the unreduced loss term for negative scores

Return type:

Tensor

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.

Parameters:
  • pos_scores (Tensor) – any shape the positive scores

  • label_smoothing (float | None) – the label smoothing parameter

  • num_entities (int | None) – the number of entities (required for label-smoothing)

Returns:

scalar the reduced loss term for positive scores

Return type:

Tensor

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:
  • predictions (Tensor) – shape: (batch_size, num_entities) The scores.

  • labels (Tensor) – shape: (batch_size, num_entities) The labels.

  • 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:

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]

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:

Tensor