Shortcuts

torcheval.metrics.functional.hit_rate

torcheval.metrics.functional.hit_rate(input: Tensor, target: Tensor, *, k: int | None = None) Tensor

Compute the hit rate of the correct class among the top predicted classes. Its class version is torcheval.metrics.HitRate.

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 predicted classes to be considered. If k is None, all classes are considered and a hit rate of 1.0 is returned.

Examples:

>>> import torch
>>> from torcheval.metrics.functional import hit_rate
>>> 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])
>>> hit_rate(input, target, k=2)
tensor([1.0000, 0.0000, 0.0000, 1.0000])

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources