PointwiseLoss

class PointwiseLoss(reduction: Literal['mean', 'sum'] = 'mean')[source]

Bases: Loss

Pointwise loss functions compute an independent loss term for each triple-label pair.

Initialize the loss.

Parameters:

reduction (Literal['mean', 'sum']) – the reduction, either "mean" or "sum"; cf. torch.nn.modules.loss._Loss

Attributes Summary

bcwa_keep_rows_without_positives

Whether BCWA scoring rows, i.e., (head, relation)-pairs, without any positive tail are kept.

Methods Summary

forward(x, target, weight)

Calculate the point-wise loss.

process_lcwa_scores(predictions, labels, *)

Process scores from LCWA training loop.

process_slcwa_scores(positive_scores, ...[, ...])

Process scores from sLCWA training loop.

Attributes Documentation

bcwa_keep_rows_without_positives: ClassVar[bool] = True

Whether BCWA scoring rows, i.e., (head, relation)-pairs, without any positive tail are kept. Rows which only contain negatives are informative for pointwise losses, but not for losses which contrast positives against negatives.

Methods Documentation

abstractmethod forward(x: Tensor, target: Tensor, weight: Tensor) → Tensor[source]

Calculate the point-wise loss.

Parameters:
  • x (Tensor) – The predictions.

  • target (Tensor) – The target values (between 0 and 1).

  • weight (Tensor) – The sample weights.

Returns:

The scalar loss value.

Return type:

Tensor

process_lcwa_scores(predictions: Tensor, labels: Tensor, *, label_smoothing: float | None = None, num_entities: int, weights: Tensor | None = None) → Tensor[source]

Process scores from LCWA training loop.

Parameters:
  • predictions (Tensor) – shape: (*shape) The scores.

  • labels (Tensor) – shape: (*shape) The labels.

  • label_smoothing (float | None) – An optional label smoothing parameter.

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

  • weights (Tensor | None) – shape: (*shape) Sample weights.

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, pos_weights: Tensor | None = None, neg_weights: Tensor | 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) – The number of entities. Only required if label smoothing is enabled.

  • pos_weights (Tensor | None) – shape: (batch_size, 1) Positive sample weights.

  • neg_weights (Tensor | None) – shape: (batch_size, num_neg_per_pos) Negative sample weights.

Returns:

A scalar loss term.

Return type:

Tensor