torcheval.metrics.functional.reciprocal_rank¶
- torcheval.metrics.functional.reciprocal_rank(input: Tensor, target: Tensor, *, k: int | None = None) Tensor ¶
Compute the reciprocal rank of the correct class among the top predicted classes. Its class version is
torcheval.metrics.ReciprocalRank
.- Parameters:
input (Tensor) – Predicted unnormalized scores (often referred to as logits) or class probabilities of shape (num_samples, num_classes).
target (Tensor) – Ground truth class indices of shape (num_samples,).
k (int, optional) – Number of top class probabilities to be considered.
Examples:
>>> import torch >>> from torcheval.metrics.functional import reciprocal_rank >>> input = torch.tensor([[0.3, 0.1, 0.6], [0.5, 0.2, 0.3], [0.2, 0.1, 0.7], [0.3, 0.3, 0.4]]) >>> target = torch.tensor([2, 1, 1, 0]) >>> reciprocal_rank(input, target) tensor([1.0000, 0.3333, 0.3333, 0.5000]) >>> reciprocal_rank(input, target, k=2) tensor([1.0000, 0.0000, 0.0000, 0.5000])