快捷方式

torcheval.metrics.BinaryConfusionMatrix

class torcheval.metrics.BinaryConfusionMatrix(*, threshold: float = 0.5, normalize: str | None = None, device: device | None = None)

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

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

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

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

  • normalize (str) –

    • None [默认值]

      给出原始计数 ('none' 也是默认值)

    • 'pred':

      按预测类别进行归一化,即行加起来为 1。

    • 'true':

      按条件正例进行归一化,即列加起来为 1。

    • 'all'

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

  • device (torch.device) – 内部张量的设备

示例

>>> import torch
>>> from torcheval.metrics import BinaryConfusionMatrix
>>> input = torch.tensor([0, 1, 0.7, 0.6])
>>> target = torch.tensor([0, 1, 1, 0])
>>> metric = BinaryConfusionMatrix()
>>> metric.update(input, target)
>>> metric.compute()
tensor([[1, 1],
        [0, 2]])

>>> input = torch.tensor([0, 1, 0.7, 0.6])
>>> target = torch.tensor([0, 1, 1, 0])
>>> metric = BinaryConfusionMatrix(threshold=1)
>>> metric.update(input, target)
>>> metric.compute()
tensor([[0, 1],
        [2, 1]])

>>> input = torch.tensor([1, 1, 0, 0])
>>> target = torch.tensor([0, 1, 1, 1])
>>> metric = BinaryConfusionMatrix()
>>> metric.update(input, target)
>>> metric.compute()
tensor([[0., 1.],
        [2., 1.]])
>>> metric.normalized("pred")
tensor([[0.0000, 0.5000],
        [1.0000, 0.5000]])
>>> metric.normalized("true")
tensor([[0.0000, 1.0000],
        [0.6667, 0.3333]])
>>> metric.normalized("all")
tensor([[0.0000, 0.5000],
        [1.0000, 0.5000]])

>>> input = torch.tensor([1, 1, 0, 0])
>>> target = torch.tensor([0, 1, 1, 1])
>>> metric = BinaryConfusionMatrix(normalize="true")
>>> metric.update(input, target)
>>> metric.compute()
tensor([[0.0000, 1.0000],
        [0.6667, 0.3333]])
>>> metric.normalized(None)
tensor([[0., 1.],
        [2., 1.]])
__init__(*, threshold: float = 0.5, normalize: str | None = None, device: device | None = None) None

初始化一个指标对象及其内部状态。

使用 self._add_state() 初始化指标类的状态变量。状态变量应该为 torch.Tensortorch.Tensor 的列表、以 torch.Tensor 作为值的字典或 torch.Tensor 的双端队列。

方法

__init__(*[, threshold, normalize, device])

初始化一个指标对象及其内部状态。

compute()

返回混淆矩阵。

load_state_dict(state_dict[, strict])

从 state_dict 加载指标状态变量。

merge_state(metrics)

实现此方法以更新当前指标的状态变量,使其成为当前指标和输入指标的合并状态。

normalized([normalize])

返回归一化的混淆矩阵

reset()

将指标状态变量重置为其默认值。

state_dict()

在 state_dict 中保存指标状态变量。

to(device, *args, **kwargs)

将指标状态变量中的张量移动到设备上。

update(input, target)

更新混淆矩阵 :param input: 形状为 (n_sample,) 的标签预测张量。将对输入应用 torch.where(input < threshold, 0, 1)。 :type input: Tensor :param target: 形状为 (n_sample,) 的真实标签张量。 :type target: Tensor。

属性

device

Metric.to() 的最后一个输入设备。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取适合初学者和高级开发者的深入教程

查看教程

资源

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

查看资源