快捷方式

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

计算二元混淆矩阵,一个 2x2 的张量,包含计数((真阳性,假阴性),(假阳性,真阴性))

参数:
  • 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':

      跨预测标准化,即行加起来等于 1。

    • 'true':

      跨条件正标准化,即列加起来等于 1。

    • 'all'

      跨所有示例标准化,即所有矩阵条目加起来等于 1。

示例

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

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源