Shortcuts

torcheval.metrics.functional.multilabel_accuracy

torcheval.metrics.functional.multilabel_accuracy(input: Tensor, target: Tensor, *, threshold: float = 0.5, criteria: str = 'exact_match') Tensor

Compute multilabel accuracy score, which is the frequency of input matching target. Its class version is torcheval.metrics.MultilabelAccuracy.

Parameters:
  • input (Tensor) – Tensor of label predictions with shape of (n_sample, n_class). torch.where(input < threshold, 0, 1)` will be applied to the input.

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

  • threshold – Threshold for converting input into predicted labels for each sample.

  • criteria

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

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

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

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

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

Examples:

>>> import torch
>>> from torcheval.metrics.functional import multilabel_accuracy
>>> input = torch.tensor([[0, 1], [1, 1], [0, 0], [0, 1]])
>>> target = torch.tensor([[0, 1], [1, 0], [0, 0], [1, 1]])
>>> multilabel_accuracy(input, target)
tensor(0.5)  # 2 / 4

>>> input = torch.tensor([[0, 1], [1, 1], [0, 0], [0, 1]])
>>> target = torch.tensor([[0, 1], [1, 0], [0, 0], [1, 1]])
>>> multilabel_accuracy(input, target, criteria="hamming")
tensor(0.75)  # 6 / 8

>>> input = torch.tensor([[0, 1], [1, 1], [0, 0], [0, 1]])
>>> target = torch.tensor([[0, 1], [1, 0], [0, 0], [1, 1]])
>>> multilabel_accuracy(input, target, criteria="overlap")
tensor(1)  # 4 / 4

>>> input = torch.tensor([[0, 1], [1, 1], [0, 0], [0, 1]])
>>> target = torch.tensor([[0, 1], [1, 0], [0, 0], [1, 1]])
>>> multilabel_accuracy(input, target, criteria="contain")
tensor(0.75)  # 3 / 4, input[0],input[1],input[2]

>>> input = torch.tensor([[0, 1], [1, 1], [0, 0], [0, 1]])
>>> target = torch.tensor([[0, 1], [1, 0], [0, 0], [1, 1]])
>>> multilabel_accuracy(input, target, criteria="belong")
tensor(0.75)  # 3 / 4, input[0],input[1],input[3]

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