Shortcuts

torcheval.metrics.functional.topk_multilabel_accuracy

torcheval.metrics.functional.topk_multilabel_accuracy(input: Tensor, target: Tensor, *, criteria: str = 'exact_match', k: int = 2) Tensor

Compute multilabel accuracy score, which is the frequency of the top k label predicted matching target. Its class version is torcheval.metrics.TopKMultilabelAccuracy.

Parameters:
  • input (Tensor) – Tensor of logits/probabilities with shape of (n_sample, n_class).

  • target (Tensor) – Tensor of ground truth labels with shape of (n_sample, n_class).

  • criteria

  • [default] (- 'exact_match') – The set of top-k labels predicted for a sample must exactly match the corresponding set of labels in target. Also known as subset accuracy.

  • 'hamming' (-) – Fraction of top-k correct labels over total number of labels.

  • 'overlap' (-) – The set of top-k labels predicted for a sample must overlap with the corresponding set of labels in target.

  • 'contain' (-) – The set of top-k labels predicted for a sample must contain the corresponding set of labels in target.

  • 'belong' (-) – The set of top-k labels predicted for a sample must (fully) belong to the corresponding set of labels in target.

  • k – Number of top probabilities to be considered. K should be an integer greater than or equal to 1.

Examples:

>>> import torch
>>> from torcheval.metrics.functional import topkmultilabel_accuracy
>>> input = torch.tensor([[0.1, 0.5, 0.2], [0.3, 0.2, 0.1], [0.2, 0.4, 0.5], [0, 0.1, 0.9]])
>>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [1, 1, 1], [0, 1, 0]])
>>> topkmultilabel_accuracy(input, target, k = 2)
tensor(0)  # 0 / 4

>>> input = torch.tensor([[0.1, 0.5, 0.2], [0.3, 0.2, 0.1], [0.2, 0.4, 0.5], [0, 0.1, 0.9]])
>>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [1, 1, 1], [0, 1, 0]])
>>> topkmultilabel_accuracy(input, target, criteria="hamming", k = 2)
tensor(0.583)  # 7 / 12

>>> input = torch.tensor([[0.1, 0.5, 0.2], [0.3, 0.2, 0.1], [0.2, 0.4, 0.5], [0, 0.1, 0.9]])
>>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [1, 1, 1], [0, 1, 0]])
>>> topkmultilabel_accuracy(input, target, criteria="overlap", k = 2)
tensor(1)  # 4 / 4

>>> input = torch.tensor([[0.1, 0.5, 0.2], [0.3, 0.2, 0.1], [0.2, 0.4, 0.5], [0, 0.1, 0.9]])
>>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [1, 1, 1], [0, 1, 0]])
>>> topkmultilabel_accuracy(input, target, criteria="contain", k = 2)
tensor(0.5)  # 2 / 4

>>> input = torch.tensor([[0.1, 0.5, 0.2], [0.3, 0.2, 0.1], [0.2, 0.4, 0.5], [0, 0.1, 0.9]])
>>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [1, 1, 1], [0, 1, 0]])
>>> topkmultilabel_accuracy(input, target, criteria="belong", k = 2)
tensor(0.25)  # 1 / 4

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