Shortcuts

torcheval.metrics.functional.binary_auprc

torcheval.metrics.functional.binary_auprc(input: Tensor, target: Tensor, *, num_tasks: int = 1) Tensor

计算AUPRC,也称为平均精度,这是二分类中精确率-召回率曲线下的面积。 其类版本是torcheval.metrics.BinaryAUPRC

精确率定义为 \(\frac{T_p}{T_p+F_p}\),它是模型预测为正例的样本中真正为正例的概率。 召回率定义为 \(\frac{T_p}{T_p+F_n}\),它是真正为正例的样本被模型预测为正例的概率。

精确率-召回率曲线在x轴上绘制召回率,在y轴上绘制精确率,两者都在0和1之间。此函数返回该曲线下的面积。如果面积接近1,模型支持一个阈值,该阈值能够正确识别高比例的真阳性,同时拒绝足够的假例,使得大多数真实预测都是真阳性。

Parameters:
  • input (Tensor) – 标签预测的张量 它应该是预测的标签、概率或logits,形状为(num_tasks, n_sample)或(n_sample, )。

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

  • num_tasks (int) – 需要进行BinaryAUPRC计算的任务数量。默认值为1。每个任务的Binary AUPRC将独立计算。结果等同于对每一行调用binary_auprc。

示例:

>>> import torch
>>> from torcheval.metrics.functional import binary_auprc
>>> input = torch.tensor([0.1, 0.5, 0.7, 0.8])
>>> target = torch.tensor([1, 0, 1, 1])
>>> binary_auprc(input, target)
tensor(0.9167) # scalar returned with 1D input tensors

>>> input = torch.tensor([[1, 1, 1, 0]])
>>> target = torch.tensor([[1, 0, 1, 0]])
>>> binary_auprc(input, target)
tensor([0.6667]) # 1D tensor returned with 2D input tensors

>>> input = torch.tensor([[0.1, 0.5, 0.7, 0.8],
>>>                       [1, 1, 1, 0]])
>>> target = torch.tensor([[1, 0, 1, 1],
>>>                        [1, 0, 1, 0]])
>>> binary_auprc(input, target, num_tasks=2)
tensor([0.9167, 0.6667])