NSSALoss

class NSSALoss(margin=9.0, adversarial_temperature=1.0, reduction='mean')[source]

Bases: AdversarialLoss

The self-adversarial negative sampling loss function proposed by [sun2019].

Initialize the NSSA loss.

Parameters:
  • margin (float) – The loss’s margin (also written as gamma in the reference paper)

  • adversarial_temperature (float) –

    The negative sampling temperature (also written as alpha in the reference paper)

    Note

    The adversarial temperature is the inverse of the softmax temperature used when computing the weights! Its name is only kept for consistency with the nomenclature of [sun2019].

  • 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’}.

Note

The default hyperparameters are based on the experiments for FB15k-237 in [sun2019].

Attributes Summary

hpo_default

The default strategy for optimizing the loss's hyper-parameters

synonyms

synonyms of this loss

Methods Summary

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.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Any]] = {'adversarial_temperature': {'high': 1.0, 'low': 0.5, 'type': <class 'float'>}, 'margin': {'high': 30, 'low': 3, 'q': 3, 'type': <class 'int'>}}

The default strategy for optimizing the loss’s hyper-parameters

synonyms: ClassVar[Set[str] | None] = {'Negative Sampling Self-Adversarial Loss', 'Self-Adversarial Negative Sampling Loss'}

synonyms of this loss

Methods Documentation

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

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