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,则模型支持一个阈值,该阈值可以正确识别高比例的真正例,同时拒绝足够的假例,以便大多数真实预测都是真正例。
- 参数:
input (Tensor) – 标签预测的张量,应该是预测标签、概率或 logits,形状为 (num_tasks, n_sample) 或 (n_sample, )。
target (Tensor) – 真实标签的张量,形状为 (num_tasks, n_sample) 或 (n_sample, )。
num_tasks (int) – 需要计算二分类 AUPRC 的任务数量。默认值为 1。每个任务的二分类 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])