AdversarialLoss

class AdversarialLoss(inverse_softmax_temperature=1.0, reduction='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, neg_scores, neg_weights, label_smoothing=None, num_entities=None)[source]

Calculate the loss for the given scores.

Parameters
  • pos_scores (FloatTensor) – shape: s_p a tensor of positive scores

  • neg_scores (FloatTensor) – shape: s_n a tensor of negative scores

  • neg_weights (FloatTensor) – shape: s_n the adversarial weights of the negative scores

  • label_smoothing (Optional[float]) – An optional label smoothing parameter.

  • num_entities (Optional[int]) – The number of entities (required for label-smoothing).

Return type

FloatTensor

Returns

a scalar loss value

abstract negative_loss_term_unreduced(neg_scores, label_smoothing=None, num_entities=None)[source]

Calculate the loss for the negative scores without reduction.

Parameters
  • neg_scores (FloatTensor) – any shape the negative scores

  • label_smoothing (Optional[float]) – the label smoothing parameter

  • num_entities (Optional[int]) – the number of entities (required for label-smoothing)

Return type

FloatTensor

Returns

scalar the unreduced loss term for negative scores

abstract positive_loss_term(pos_scores, label_smoothing=None, num_entities=None)[source]

Calculate the loss for the positive scores.

Parameters
  • pos_scores (FloatTensor) – any shape the positive scores

  • label_smoothing (Optional[float]) – the label smoothing parameter

  • num_entities (Optional[int]) – the number of entities (required for label-smoothing)

Return type

FloatTensor

Returns

scalar the reduced loss term for positive scores

process_lcwa_scores(predictions, labels, label_smoothing=None, num_entities=None)[source]

Process scores from LCWA training loop.

Parameters
  • predictions (FloatTensor) – shape: (batch_size, num_entities) The scores.

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

  • label_smoothing (Optional[float]) – An optional label smoothing parameter.

  • num_entities (Optional[int]) – The number of entities (required for label-smoothing).

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.