CrossEntropyLoss
- class CrossEntropyLoss(reduction='mean')[source]
Bases:
SetwiseLoss
The cross entropy loss that evaluates the cross entropy after softmax output.
Note
The related
torch
module istorch.nn.CrossEntropyLoss
, but it can not be used interchangeably in PyKEEN because of the extended functionality implemented in PyKEEN’s loss functions.Initialize the loss.
- Parameters:
reduction (
str
) – the reduction, cf. _Loss.__init__
Methods Summary
process_lcwa_scores
(predictions, labels[, ...])Process scores from LCWA training loop.
process_slcwa_scores
(positive_scores, ...[, ...])Process scores from sLCWA training loop.
Methods Documentation
- 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.