torcheval.metrics.functional.reciprocal_rank¶
- torcheval.metrics.functional.reciprocal_rank(input: Tensor, target: Tensor, *, k: int | None = None) Tensor¶
计算正确类别在预测类别中的倒数排名。 其类版本是
torcheval.metrics.ReciprocalRank。- Parameters:
input (Tensor) – 预测的未归一化分数(通常称为logits)或形状为(num_samples, num_classes)的类别概率。
target (Tensor) – 形状为 (num_samples,) 的真实类别索引。
k (int, 可选) – 要考虑的顶部类别概率的数量。
示例:
>>> 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])