InBatchCrossEntropy
- class lightning_ir.loss.in_batch.InBatchCrossEntropy(pos_sampling_technique: Literal['all', 'first'] = 'all', neg_sampling_technique: Literal['all', 'first', 'all_and_non_first'] = 'all', max_num_neg_samples: int | None = None)[source]
Bases:
InBatchLossFunctionIn-batch cross-entropy loss function for ranking tasks. Originally proposed in: Fast Single-Class Classification and the Principle of Logit Separation
- __init__(pos_sampling_technique: Literal['all', 'first'] = 'all', neg_sampling_technique: Literal['all', 'first', 'all_and_non_first'] = 'all', max_num_neg_samples: int | None = None)
Initialize the InBatchLossFunction.
- Parameters:
pos_sampling_technique (Literal["all", "first"]) – Technique for positive sample sampling.
neg_sampling_technique (Literal["all", "first", "all_and_non_first"]) – Technique for negative sample sampling.
max_num_neg_samples (int | None) – Maximum number of negative samples to consider. If None, all negative samples are considered.
- Raises:
ValueError – If the negative sampling technique is invalid for the given positive sampling technique.
Methods
compute_loss(output)Compute the in-batch cross-entropy loss.
- compute_loss(output: LightningIROutput) torch.Tensor[source]
Compute the in-batch cross-entropy loss.
- Parameters:
output (LightningIROutput) – The output from the model containing scores.
- Returns:
The computed loss.
- Return type:
torch.Tensor
- get_ib_idcs(output: LightningIROutput, batch: TrainBatch) Tuple[torch.Tensor, torch.Tensor]
Get in-batch indices for positive and negative samples.
- Parameters:
output (LightningIROutput) – The output from the model containing scores.
batch (TrainBatch) – The training batch containing targets.
- Returns:
Indices of positive and negative samples.
- Return type:
Tuple[torch.Tensor, torch.Tensor]
- Raises:
ValueError – If scores are not present in the output.
- process_scores(output: LightningIROutput) torch.Tensor
Process the scores from the output.
- Parameters:
output (LightningIROutput) – The output from the model.
- Returns:
The scores tensor.
- Return type:
torch.Tensor
- process_targets(scores: torch.Tensor, batch: TrainBatch) torch.Tensor
Process the targets from the batch.
- Parameters:
scores (torch.Tensor) – The scores tensor.
batch (TrainBatch) – The training batch.
- Returns:
The processed targets tensor.
- Return type:
torch.Tensor