ApproxMRR

class lightning_ir.loss.approximate.ApproxMRR(temperature: float = 1)[source]

Bases: ApproxLossFunction

Approximate Mean Reciprocal Rank (MRR) loss function for ranking tasks.

__init__(temperature: float = 1)[source]

Initialize the ApproxMRR loss function.

Parameters:

temperature (float) – Temperature parameter for scaling the scores. Defaults to 1.

Methods

__init__([temperature])

Initialize the ApproxMRR loss function.

compute_loss(output, batch)

Compute the ApproxMRR loss.

get_mrr(ranks, targets[, k])

Compute the Mean Reciprocal Rank (MRR) for the given ranks and targets.

compute_loss(output: LightningIROutput, batch: TrainBatch) torch.Tensor[source]

Compute the ApproxMRR loss.

Parameters:
  • output (LightningIROutput) – The output from the model containing scores.

  • batch (TrainBatch) – The training batch containing targets.

Returns:

The computed loss.

Return type:

torch.Tensor

static get_mrr(ranks: Tensor, targets: Tensor, k: int | None = None) Tensor[source]

Compute the Mean Reciprocal Rank (MRR) for the given ranks and targets.

Parameters:
  • ranks (torch.Tensor) – The ranks of the items.

  • targets (torch.Tensor) – The relevance scores of the items.

  • k (int | None) – Optional cutoff for the ranks. If provided, only computes MRR for the top k items.

Returns:

The computed MRR values.

Return type:

torch.Tensor