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

计算二元混淆矩阵,一个2乘2的张量,包含计数((真正例,假反例),(假正例,真反例))

Parameters:
  • input (Tensor) – 形状为 (n_sample,) 的标签预测张量。 torch.where(input < threshold, 0, 1) 将应用于输入。

  • target (Tensor) – 形状为 (n_sample,) 的真实标签张量。

  • threshold (float, 默认 0.5) – 用于将输入转换为每个样本的预测标签的阈值。 torch.where(input < threshold, 0, 1) 将应用于 input

  • normalize

    • None [默认]:

      提供原始计数('none' 也默认为此)

    • 'pred':

      在预测上进行归一化,即使行加起来为一。

    • 'true':

      在条件正例上进行归一化,即使列加起来为一。

    • 'all'

      在所有示例上进行归一化,即使所有矩阵条目加起来为一。

示例:

>>> 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]])