Shortcuts

torcheval.metrics.functional.binary_confusion_matrix

torcheval.metrics.functional.binary_confusion_matrix(input: Tensor, target: Tensor, *, threshold: float = 0.5, normalize: str | None = None) Tensor

Compute binary confusion matrix, a 2 by 2 tensor with counts ( (true positive, false negative) , (false positive, true negative) )

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

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

  • threshold (float, default 0.5) – Threshold for converting input into predicted labels for each sample. torch.where(input < threshold, 0, 1) will be applied to the input.

  • normalize

    • None [default]:

      Give raw counts (‘none’ also defaults to this)

    • 'pred':

      Normalize across the prediction, i.e. such that the rows add to one.

    • 'true':

      Normalize across the condition positive, i.e. such that the columns add to one.

    • 'all'

      Normalize across all examples, i.e. such that all matrix entries add to one.

Examples:

>>> import torch
>>> from torcheval.metrics.functional import binary_confusion_matrix
>>> input = torch.tensor([0, 1, 0.7, 0.6])
>>> target = torch.tensor([0, 1, 1, 0])
>>> binary_confusion_matrix(input, target)
tensor([[1, 1],
        [0, 2]])

>>> input = torch.tensor([1, 1, 0, 0])
>>> target = torch.tensor([0, 1, 1, 1])
>>> binary_confusion_matrix(input, target, threshold=1)
tensor([[0, 1],
        [2, 1]])

>>> input = torch.tensor([1, 1, 0, 0])
>>> target = torch.tensor([0, 1, 1, 1])
>>> binary_confusion_matrix(input, target, normalize="true")
tensor([[0.0000, 1.0000],
        [0.6667, 0.3333]])

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