Class Metrics
参考地址:Module metrics — PyTorch-Metrics 0.4.0 documentation (torchmetrics.readthedocs.io)
Input types
出于分类指标的目的,输入(predictions和targets)分为以下类别(N 代表批次大小,C 代表类别数):
Type | preds shape | preds dtype | target shape | target dtype |
---|---|---|---|---|
Binary(二值) | (N,) | float | (N,) | binary * |
Multi-class(多类别) | (N,) | int | (N,) | int |
Multi-class with logits or probabilities(具有对数或概率的多类) | (N, C) | float | (N,) | int |
Multi-label(多标签) | (N, …) | float | (N, …) | binary * |
Multi-dimensional multi-class(多维多类) | (N, …) | int | (N, …) | int |
Multi-dimensional multi-class with logits or probabilities(具有对数或概率的多维多类) | (N, C, …) | float | (N, …) | int |
*dtype binary 表示 0 或 1 的整数
NOTE:
大小为 1 的所有维度(N 除外)在开始时都被“Squeezed out挤出”,因此,例如,形状为 (N, 1) 的张量被视为 (N, )。
当predictions或targets是整数时,假设类标签从 0 开始,即可能的类标签是 0、1、2、3 等。以下是不同输入类型的一些示例。
# Binary inputs
binary_preds = torch.tensor([0.6, 0.1, 0.9])
binary_target = torch.tensor([1, 0, 2])
# Multi-class inputs
mc_preds = torch.tensor([0, 2, 1])
mc_target = torch.tensor([0, 1, 2])
# Multi-class inputs with probabilities
mc_preds_probs = torch.tensor([[0.8, 0.2, 0], [0.1, 0.2, 0.7], [0.3, 0.6, 0.1]])
mc_target_probs = torch.tensor([0, 1, 2])
# Multi-label inputs
ml_preds = torch.tensor([[0.2, 0.8, 0.9], [0.5, 0.6, 0.1], [0.3, 0.1, 0.1]])
ml_target = torch.tensor([[0, 1, 1], [1, 0, 0], [0, 0, 0]])
使用多类参数
在某些情况下,您的输入可能看起来是(多维)多类,但实际上是二元/多标签——例如,如果预测和目标都是整数(二元)张量。 或者也可以反过来,您希望将二元/多标签输入视为 2 类(多维)多类输入。
对于这些情况,这种区别会产生影响的指标公开了多类参数。 让我们看看如何在 StatScores 指标示例中使用它。
from torchmetrics.functional import stat_scores
# These inputs are supposed to be binary, but appear as multi-class
preds = torch.tensor([0, 1, 0])
target = torch.tensor([1, 1, 0])
正如您在下面看到的,默认情况下,输入被视为多类。 我们可以设置 multiclass=False 将输入视为binary - 这与预先将预测转换为浮点数相同。
>>> stat_scores(preds, target, reduce='macro', num_classes=2)
tensor([[1, 1, 1, 0, 1],
[1, 0, 1, 1, 2]])
>>> stat_scores(preds, target, reduce='macro', num_classes=1, multiclass=False)
tensor([[1, 0, 1, 1, 2]])
>>> stat_scores(preds.float(), target, reduce='macro', num_classes=1)
tensor([[1, 0, 1, 1, 2]])
接下来,考虑相反的例子:输入是binary (因为预测是概率),但我们希望将它们视为2-class multi-class,以获得两个类的度量。
preds = torch.tensor([0.2, 0.7, 0.3])
target = torch.tensor([1, 1, 0])
在这种情况下,我们可以设置 multiclass=True,将输入视为多类。
>>> stat_scores(preds, target, reduce='macro', num_classes=1)
tensor([[1, 0, 1, 1, 2]])
>>> stat_scores(preds, target, reduce='macro', num_classes=2, multiclass=True)
tensor([[1, 1, 1, 0, 1],
[1, 0, 1, 1, 2]])
Accuracy
Accuracy的计算公式:
其中 是a tensor of target values, is a tensor of predictions.
对于具有概率或 logit 预测的多类和多维多类数据,参数 top_k 将此度量推广到 Top-K 准确度度量:对于每个样本,top-K 最高概率或 logit 分数项被考虑以找到 正确的标签。
对于多标签和多维多类输入,该指标默认计算“global(全局)”准确度,分别计算所有标签或子样本。 这可以通过设置subset_accuracy=True 更改为子集精度(这需要正确预测样本中的所有标签或子样本)。
例子:
>>> import torch
>>> from torchmetrics import Accuracy
>>> target = torch.tensor([0, 1, 2, 3])
>>> preds = torch.tensor([0, 2, 1, 3])
>>> accuracy = Accuracy()
>>> accuracy(preds, target)
tensor(0.5000)
>>> target = torch.tensor([0, 1, 2])
>>> preds = torch.tensor([[0.1, 0.9, 0], [0.3, 0.1, 0.6], [0.2, 0.5, 0.3]])
>>> accuracy = Accuracy(top_k=2)
>>> accuracy(preds, target)
tensor(0.6667)
AveragePrecision
计算平均精度分数,将精度召回曲线(PR曲线)汇总为一个数字。 适用于二元和多类问题。 在多类的情况下,将基于一对一的方法计算值。
格式要求:
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass) tensor with probabilities, where C is the number of classes.target
(long tensor):(N, ...)
with integer labels
Example (binary case):
>>> from torchmetrics import AveragePrecision
>>> pred = torch.tensor([0, 1, 2, 3])
>>> target = torch.tensor([0, 1, 1, 1])
>>> average_precision = AveragePrecision(pos_label=1)
>>> average_precision(pred, target)
tensor(1.)
Example (multiclass case):
>>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05, 0.05],
... [0.05, 0.75, 0.05, 0.05, 0.05],
... [0.05, 0.05, 0.75, 0.05, 0.05],
... [0.05, 0.05, 0.05, 0.75, 0.05]])
>>> target = torch.tensor([0, 1, 3, 2])
>>> average_precision = AveragePrecision(num_classes=5)
>>> average_precision(pred, target)
[tensor(1.), tensor(1.), tensor(0.2500), tensor(0.2500), tensor(nan)]
AUC
使用梯形规则计算曲线下面积 (AUC)
Forward 接受两个输入张量,它们应该是一维的并且具有相同数量的元素
AUROC
接收器操作特性曲线下的计算区域 (ROC AUC)。 适用于二元、多标签和多类问题。 在多类的情况下,将基于一对一的方法计算值。
Forward accepts
-
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass) tensor with probabilities, where C is the number of classes. -
target
(long tensor):(N, ...)
or(N, C, ...)
with integer labels对于非二进制输入,如果 preds 和目标张量具有相同的大小,则输入将被解释为多标签,如果 preds 比目标张量多一维,则输入将被解释为多类。
Example (binary case):
>>>
>>> from torchmetrics import AUROC
>>> preds = torch.tensor([0.13, 0.26, 0.08, 0.19, 0.34])
>>> target = torch.tensor([0, 0, 1, 1, 1])
>>> auroc = AUROC(pos_label=1)
>>> auroc(preds, target)
tensor(0.5000)
Example (multiclass case):
>>>
>>> preds = torch.tensor([[0.90, 0.05, 0.05],
... [0.05, 0.90, 0.05],
... [0.05, 0.05, 0.90],
... [0.85, 0.05, 0.10],
... [0.10, 0.10, 0.80]])
>>> target = torch.tensor([0, 1, 1, 2, 2])
>>> auroc = AUROC(num_classes=3)
>>> auroc(preds, target)
tensor(0.7778)
BinnedAveragePrecision
计算平均精度分数,将精度召回曲线汇总为一个数字。 适用于二元和多类问题。 在多类的情况下,将基于一对一的方法计算值。
通过计算 num_thresholds 桶/阈值(在 0 和 1 之间均匀分布)的精度和召回率,在常量内存中执行计算。
Forward accepts
-
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass) tensor with probabilities, where C is the number of classes. -
target
(long tensor):(N, ...)
with integer labels
Example (binary case):
>>>
>>> from torchmetrics import BinnedAveragePrecision
>>> pred = torch.tensor([0, 1, 2, 3])
>>> target = torch.tensor([0, 1, 1, 1])
>>> average_precision = BinnedAveragePrecision(num_classes=1, num_thresholds=10)
>>> average_precision(pred, target)
tensor(1.0000)
Example (multiclass case):
>>>
>>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05, 0.05],
... [0.05, 0.75, 0.05, 0.05, 0.05],
... [0.05, 0.05, 0.75, 0.05, 0.05],
... [0.05, 0.05, 0.05, 0.75, 0.05]])
>>> target = torch.tensor([0, 1, 3, 2])
>>> average_precision = BinnedAveragePrecision(num_classes=5, num_thresholds=10)
>>> average_precision(pred, target)
[tensor(1.0000), tensor(1.0000), tensor(0.2500), tensor(0.2500), tensor(-0.)]
BinnedPrecisionRecallCurve
计算不同阈值的精确召回对。 适用于二元和多类问题。 在多类的情况下,将基于一对一的方法计算值。
通过计算 num_thresholds 桶/阈值(在 0 和 1 之间均匀分布)的精度和召回率,在常量内存中执行计算。
Forward accepts
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass) tensor with probabilities, where C is the number of classes.target
(long tensor):(N, ...)
or(N, C, ...)
with integer labels
Example (binary case):
>>>
>>> from torchmetrics import BinnedPrecisionRecallCurve
>>> pred = torch.tensor([0, 0.1, 0.8, 0.4])
>>> target = torch.tensor([0, 1, 1, 0])
>>> pr_curve = BinnedPrecisionRecallCurve(num_classes=1, thresholds=5)
>>> precision, recall, thresholds = pr_curve(pred, target)
>>> precision
tensor([0.5000, 0.5000, 1.0000, 1.0000, 1.0000, 1.0000])
>>> recall
tensor([1.0000, 0.5000, 0.5000, 0.5000, 0.0000, 0.0000])
>>> thresholds
tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
Example (multiclass case):
>>>
>>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05, 0.05],
... [0.05, 0.75, 0.05, 0.05, 0.05],
... [0.05, 0.05, 0.75, 0.05, 0.05],
... [0.05, 0.05, 0.05, 0.75, 0.05]])
>>> target = torch.tensor([0, 1, 3, 2])
>>> pr_curve = BinnedPrecisionRecallCurve(num_classes=5, thresholds=3)
>>> precision, recall, thresholds = pr_curve(pred, target)
>>> precision
[tensor([0.2500, 1.0000, 1.0000, 1.0000]),
tensor([0.2500, 1.0000, 1.0000, 1.0000]),
tensor([2.5000e-01, 1.0000e-06, 1.0000e+00, 1.0000e+00]),
tensor([2.5000e-01, 1.0000e-06, 1.0000e+00, 1.0000e+00]),
tensor([2.5000e-07, 1.0000e+00, 1.0000e+00, 1.0000e+00])]
>>> recall
[tensor([1.0000, 1.0000, 0.0000, 0.0000]),
tensor([1.0000, 1.0000, 0.0000, 0.0000]),
tensor([1.0000, 0.0000, 0.0000, 0.0000]),
tensor([1.0000, 0.0000, 0.0000, 0.0000]),
tensor([0., 0., 0., 0.])]
>>> thresholds
[tensor([0.0000, 0.5000, 1.0000]),
tensor([0.0000, 0.5000, 1.0000]),
tensor([0.0000, 0.5000, 1.0000]),
tensor([0.0000, 0.5000, 1.0000]),
tensor([0.0000, 0.5000, 1.0000])]
BinnedRecallAtFixedPrecision
给定提供的最小精度阈值,计算可能的最高召回值。
通过计算 num_thresholds 桶/阈值(在 0 和 1 之间均匀分布)的精度和召回率,在常量内存中执行计算。
Forward accepts
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass) tensor with probabilities, where C is the number of classes.target
(long tensor):(N, ...)
with integer labels
Example (binary case):
>>> from torchmetrics import BinnedRecallAtFixedPrecision
>>> pred = torch.tensor([0, 0.2, 0.5, 0.8])
>>> target = torch.tensor([0, 1, 1, 0])
>>> average_precision = BinnedRecallAtFixedPrecision(num_classes=1, num_thresholds=10, min_precision=0.5)
>>> average_precision(pred, target)
(tensor(1.0000), tensor(0.1111))
Example (multiclass case):
>>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05, 0.05],
... [0.05, 0.75, 0.05, 0.05, 0.05],
... [0.05, 0.05, 0.75, 0.05, 0.05],
... [0.05, 0.05, 0.05, 0.75, 0.05]])
>>> target = torch.tensor([0, 1, 3, 2])
>>> average_precision = BinnedRecallAtFixedPrecision(num_classes=5, num_thresholds=10, min_precision=0.5)
>>> average_precision(pred, target)
(tensor([1.0000, 1.0000, 0.0000, 0.0000, 0.0000]),
tensor([6.6667e-01, 6.6667e-01, 1.0000e+06, 1.0000e+06, 1.0000e+06]))
CohenKappa
计算衡量inter-annotator一致性的 Cohen 的 kappa 分数。 它被定义为
其中, :一致的经验概率
:两个注释者随机分配标签时的预期一致性(使用每个注释器的经验先验对类标签进行估计。)
适用于二进制、多类和多标签数据。 接受来自模型输出的概率或预测中的整数类值。 适用于多维预测和目标。
kappa系数是用在统计学中评估一致性的一种方法,取值范围是[-1,1],实际应用中,一般是[0,1],与ROC曲线中一般不会出现下凸形曲线的原理类似。这个系数的值越高,则代表模型实现的分类准确度越高。
-
P0表示总体分类精度
-
Pe表示SUM(第i类真实样本数*第i类预测出来的样本数)/样本总数平方
Forward accepts
preds
(float or long tensor):(N, ...)
or(N, C, ...)
where C is the number of classestarget
(long tensor):(N, ...)
>>> from torchmetrics import CohenKappa
>>> target = torch.tensor([1, 1, 0, 0])
>>> preds = torch.tensor([0, 1, 0, 0])
>>> cohenkappa = CohenKappa(num_classes=2)
>>> cohenkappa(preds, target)
tensor(0.5000)
ConfusionMatrix
计算混淆矩阵。 适用于二进制、多类和多标签数据。 接受来自模型输出或预测中的整数类值的概率或对数。 适用于多维 preds 和目标,但应注意额外的维度将被展平。
Forward accepts
-
preds
(float or long tensor):(N, ...)
or(N, C, ...)
where C is the number of classes -
target
(long tensor):(N, ...)
如果 preds 和 target 是相同的形状并且 preds 是一个浮点张量,我们使用 self.threshold 参数转换为整数标签。 二进制和多标签概率或 logits 就是这种情况。
如果 preds 有一个额外的维度,如在多类分数的情况下,我们在 dim=1 上执行 argmax。
如果使用多标签数据,将 is_multilabel 参数设置为 True 将确保每个标签计算混淆矩阵。
Example (binary data):
>>>
>>> from torchmetrics import ConfusionMatrix
>>> target = torch.tensor([1, 1, 0, 0])
>>> preds = torch.tensor([0, 1, 0, 0])
>>> confmat = ConfusionMatrix(num_classes=2)
>>> confmat(preds, target)
tensor([[2., 0.],
[1., 1.]])
Example (multiclass data):
>>>
>>> target = torch.tensor([2, 1, 0, 0])
>>> preds = torch.tensor([2, 1, 0, 1])
>>> confmat = ConfusionMatrix(num_classes=3)
>>> confmat(preds, target)
tensor([[1., 1., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Example (multilabel data):
>>>
>>> target = torch.tensor([[0, 1, 0], [1, 0, 1]])
>>> preds = torch.tensor([[0, 0, 1], [1, 0, 1]])
>>> confmat = ConfusionMatrix(num_classes=3, multilabel=True)
>>> confmat(preds, target)
tensor([[[1., 0.], [0., 1.]],
[[1., 0.], [1., 0.]],
[[0., 1.], [0., 1.]]])
F1
计算 F1 指标。 F1 指标对应于精度和召回分数的调和平均值。
适用于二进制、多类和多标签数据。 接受来自模型输出或预测中的整数类值的对数或概率。 适用于多维预测和目标。
Forward accepts
preds
(float or long tensor):(N, ...)
or(N, C, ...)
where C is the number of classestarget
(long tensor):(N, ...)
如果 preds 和 target 是相同的形状并且 preds 是一个浮动张量,我们使用 self.threshold 参数。 二进制和多标签 logits 就是这种情况。
如果 preds 有一个额外的维度,如在多类分数的情况下,我们在 dim=1 上执行 argmax。
注意:
在多维多类情况下什么被认为是样本取决于 mdmc_average 的值。
Example
>>>
>>> from torchmetrics import F1
>>> target = torch.tensor([0, 1, 2, 0, 1, 2])
>>> preds = torch.tensor([0, 2, 1, 0, 0, 1])
>>> f1 = F1(num_classes=3)
>>> f1(preds, target)
tensor(0.3333)
FBeta
计算公式:
其中 是一些积极的实际因素。 适用于二进制、多类和多标签数据。 接受来自模型输出的 logit 分数或概率或预测中的整数类值。 适用于多维预测和目标。
Forward accepts
preds
(float or long tensor):(N, ...)
or(N, C, ...)
where C is the number of classestarget
(long tensor):(N, ...)
如果 preds 和 target 是相同的形状并且 preds 是一个浮点张量,我们使用 self.threshold 参数转换为整数标签。 这就是二元和多标签 logits 和概率的情况。
如果 preds 有一个额外的维度,如在多类分数的情况下,我们在 dim=1 上执行 argmax。
Example
>>>
>>> from torchmetrics import FBeta
>>> target = torch.tensor([0, 1, 2, 0, 1, 2])
>>> preds = torch.tensor([0, 2, 1, 0, 0, 1])
>>> f_beta = FBeta(num_classes=3, beta=0.5)
>>> f_beta(preds, target)
tensor(0.3333)
HammingDistance
计算目标和预测之间的平均Hamming距离(也称为Hamming损失):
其中 是目标值的张量,是预测的张量, 是指该张量的第 i 个样本的第 l 个标签。
这与二进制数据的 1-accuracy 相同,而对于所有其他类型的输入,它分别处理每个可能的标签——这意味着,例如,多类数据被视为多标签。
Example
>>>
>>> from torchmetrics import HammingDistance
>>> target = torch.tensor([[0, 1], [1, 1]])
>>> preds = torch.tensor([[0, 1], [0, 1]])
>>> hamming_distance = HammingDistance()
>>> hamming_distance(preds, target)
tensor(0.2500)
Hinge
计算平均Hinge损失,通常用于支持向量机 (SVM)。 在二进制情况下,它被定义为:
其中, 是target, 是预测。
在多类情况下,当 multiclass_mode=None(默认)、multiclass_mode=MulticlassMode.CRAMMER_SINGER 或 multiclass_mode=“crammer-singer” 时,该度量将计算由 Crammer 和 Singer 定义的多类Hinge损失:
其中, is the target class (where is the number of classes), and is the predicted output per class。
在 multiclass_mode=MulticlassMode.ONE_VS_ALL 或 multiclass_mode=‘one-vs-all’ 的多类情况下,该度量将使用一对多的方法来计算铰链损失,给出每个入口坑的 C 输出向量 那个班级反对所有剩余的班级。
该度量可以通过设置 squared=True 选择性地输出平方铰链损失的平均值
只接受预测形状为 (N)(二进制)或(N, C)(多类)和目标形状为 (N) 的输入。
Example (binary case):
>>>
>>> import torch
>>> from torchmetrics import Hinge
>>> target = torch.tensor([0, 1, 1])
>>> preds = torch.tensor([-2.2, 2.4, 0.1])
>>> hinge = Hinge()
>>> hinge(preds, target)
tensor(0.3000)
Example (default / multiclass case):
>>>
>>> target = torch.tensor([0, 1, 2])
>>> preds = torch.tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]])
>>> hinge = Hinge()
>>> hinge(preds, target)
tensor(2.9000)
Example (multiclass example, one vs all mode):
>>>
>>> target = torch.tensor([0, 1, 2])
>>> preds = torch.tensor([[-1.0, 0.9, 0.2], [0.5, -1.1, 0.8], [2.2, -0.5, 0.3]])
>>> hinge = Hinge(multiclass_mode="one-vs-all")
>>> hinge(preds, target)
tensor([2.2333, 1.5000, 1.2333])
IoU
计算Iou,或 Jaccard 指数计算:
其中:A 和 B 都是相同大小的张量,包含整数类值。 它们可能需要从输入数据进行转换(请参阅下面的说明)。 请注意,它与 box IoU 不同。
适用于二进制、多类和多标签数据。 接受来自模型输出的概率或预测中的整数类值。 适用于多维预测和目标。
Forward accepts
preds
(float or long tensor):(N, ...)
or(N, C, ...)
where C is the number of classestarget
(long tensor):(N, ...)
如果 preds 和 target 是相同的形状并且 preds 是一个浮点张量,我们使用 self.threshold 参数转换为整数标签。 二进制和多标签概率就是这种情况。
如果 preds 有一个额外的维度,如在多类分数的情况下,我们在 dim=1 上执行 argmax。
>>> from torchmetrics import IoU
>>> target = torch.randint(0, 2, (10, 25, 25))
>>> pred = torch.tensor(target)
>>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]
>>> iou = IoU(num_classes=2)
>>> iou(pred, target)
tensor(0.9660)
KLDivergence
KLDivergence的计算公式:
其中 and are 概率分布, 通常表示数据的分布 and 通常是 的先验或近似. 需要注意的是,KL散度是一个非对称度量,例如: .
>>> import torch
>>> from torchmetrics.functional import kldivergence
>>> p = torch.tensor([[0.36, 0.48, 0.16]])
>>> q = torch.tensor([[1/3, 1/3, 1/3]])
>>> kldivergence(p, q)
tensor(0.0853)
MatthewsCorrcoef
计算 Matthews 相关系数,用于衡量分类的一般相关性或质量。 在二进制情况下,它被定义为:
其中 TP、TN、FP 和 FN 分别是真阳性、真阴性、假阳性和假阴性。 也适用于多标签或多类输入的情况。
Forward accepts
preds
(float or long tensor):(N, ...)
or(N, C, ...)
where C is the number of classestarget
(long tensor):(N, ...)
Example
>>>
>>> from torchmetrics import MatthewsCorrcoef
>>> target = torch.tensor([1, 1, 0, 0])
>>> preds = torch.tensor([0, 1, 0, 0])
>>> matthews_corrcoef = MatthewsCorrcoef(num_classes=2)
>>> matthews_corrcoef(preds, target)
tensor(0.5774)
Precision
Precision计算公式:
其中 和 分别表示真阳性和假阳性的数量。 通过使用 top_k 参数,该指标可以推广到 Precision@K。
减少方法(如何聚合精度分数)由平均参数控制,在多维多类情况下还由 mdmc_average 参数控制。 接受输入类型中列出的所有输入。
>>> from torchmetrics import Precision
>>> preds = torch.tensor([2, 0, 2, 1])
>>> target = torch.tensor([1, 1, 2, 0])
>>> precision = Precision(average='macro', num_classes=3)
>>> precision(preds, target)
tensor(0.1667)
>>> precision = Precision(average='micro')
>>> precision(preds, target)
tensor(0.2500)
PrecisionRecallCurve
计算不同阈值的精确召回对。 适用于二元和多类问题。 在多类的情况下,将基于一对一的方法计算值。
Forward accepts
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass) tensor with probabilities, where C is the number of classes.target
(long tensor):(N, ...)
or(N, C, ...)
with integer labels
Example (binary case):
>>>
>>> from torchmetrics import PrecisionRecallCurve
>>> pred = torch.tensor([0, 1, 2, 3])
>>> target = torch.tensor([0, 1, 1, 0])
>>> pr_curve = PrecisionRecallCurve(pos_label=1)
>>> precision, recall, thresholds = pr_curve(pred, target)
>>> precision
tensor([0.6667, 0.5000, 0.0000, 1.0000])
>>> recall
tensor([1.0000, 0.5000, 0.0000, 0.0000])
>>> thresholds
tensor([1, 2, 3])
Example (multiclass case):
>>>
>>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05, 0.05],
... [0.05, 0.75, 0.05, 0.05, 0.05],
... [0.05, 0.05, 0.75, 0.05, 0.05],
... [0.05, 0.05, 0.05, 0.75, 0.05]])
>>> target = torch.tensor([0, 1, 3, 2])
>>> pr_curve = PrecisionRecallCurve(num_classes=5)
>>> precision, recall, thresholds = pr_curve(pred, target)
>>> precision
[tensor([1., 1.]), tensor([1., 1.]), tensor([0.2500, 0.0000, 1.0000]),
tensor([0.2500, 0.0000, 1.0000]), tensor([0., 1.])]
>>> recall
[tensor([1., 0.]), tensor([1., 0.]), tensor([1., 0., 0.]), tensor([1., 0., 0.]), tensor([nan, 0.])]
>>> thresholds
[tensor([0.7500]), tensor([0.7500]), tensor([0.0500, 0.7500]), tensor([0.0500, 0.7500]), tensor([0.0500])]
Recall
Recall计算公式:
>>> from torchmetrics import Recall
>>> preds = torch.tensor([2, 0, 2, 1])
>>> target = torch.tensor([1, 1, 2, 0])
>>> recall = Recall(average='macro', num_classes=3)
>>> recall(preds, target)
tensor(0.3333)
>>> recall = Recall(average='micro')
>>> recall(preds, target)
tensor(0.2500)
ROC
计算接收器操作特性 (ROC)。 适用于二元、多类和多标签问题。 在多类的情况下,将基于一对一的方法计算值。
Forward accepts
preds
(float tensor):(N, ...)
(binary) or(N, C, ...)
(multiclass/multilabel) tensor with probabilities, where C is the number of classes/labels.target
(long tensor):(N, ...)
or(N, C, ...)
with integer labels
Example (binary case):
>>>
>>> from torchmetrics import ROC
>>> pred = torch.tensor([0, 1, 2, 3])
>>> target = torch.tensor([0, 1, 1, 1])
>>> roc = ROC(pos_label=1)
>>> fpr, tpr, thresholds = roc(pred, target)
>>> fpr
tensor([0., 0., 0., 0., 1.])
>>> tpr
tensor([0.0000, 0.3333, 0.6667, 1.0000, 1.0000])
>>> thresholds
tensor([4, 3, 2, 1, 0])
Example (multiclass case):
>>>
>>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05],
... [0.05, 0.75, 0.05, 0.05],
... [0.05, 0.05, 0.75, 0.05],
... [0.05, 0.05, 0.05, 0.75]])
>>> target = torch.tensor([0, 1, 3, 2])
>>> roc = ROC(num_classes=4)
>>> fpr, tpr, thresholds = roc(pred, target)
>>> fpr
[tensor([0., 0., 1.]), tensor([0., 0., 1.]), tensor([0.0000, 0.3333, 1.0000]), tensor([0.0000, 0.3333, 1.0000])]
>>> tpr
[tensor([0., 1., 1.]), tensor([0., 1., 1.]), tensor([0., 0., 1.]), tensor([0., 0., 1.])]
>>> thresholds
[tensor([1.7500, 0.7500, 0.0500]),
tensor([1.7500, 0.7500, 0.0500]),
tensor([1.7500, 0.7500, 0.0500]),
tensor([1.7500, 0.7500, 0.0500])]
Example (multilabel case):
>>>
>>> pred = torch.tensor([[0.8191, 0.3680, 0.1138],
... [0.3584, 0.7576, 0.1183],
... [0.2286, 0.3468, 0.1338],
... [0.8603, 0.0745, 0.1837]])
>>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [0, 0, 0], [0, 1, 1]])
>>> roc = ROC(num_classes=3, pos_label=1)
>>> fpr, tpr, thresholds = roc(pred, target)
>>> fpr
[tensor([0.0000, 0.3333, 0.3333, 0.6667, 1.0000]),
tensor([0., 0., 0., 1., 1.]),
tensor([0.0000, 0.0000, 0.3333, 0.6667, 1.0000])]
>>> tpr
[tensor([0., 0., 1., 1., 1.]),
tensor([0.0000, 0.3333, 0.6667, 0.6667, 1.0000]),
tensor([0., 1., 1., 1., 1.])]
>>> thresholds
[tensor([1.8603, 0.8603, 0.8191, 0.3584, 0.2286]),
tensor([1.7576, 0.7576, 0.3680, 0.3468, 0.0745]),
tensor([1.1837, 0.1837, 0.1338, 0.1183, 0.1138])]
Specificity
Specificity计算公式:
>>> from torchmetrics import Specificity
>>> preds = torch.tensor([2, 0, 2, 1])
>>> target = torch.tensor([1, 1, 2, 0])
>>> specificity = Specificity(average='macro', num_classes=3)
>>> specificity(preds, target)
tensor(0.6111)
>>> specificity = Specificity(average='micro')
>>> specificity(preds, target)
tensor(0.6250)
StatScores
计算真阳性、假阳性、真阴性、假阴性的数量。 与 I 类和 II 类错误以及混淆矩阵相关。
归约方法(统计数据如何聚合)由 reduce 参数控制,在多维多类情况下还由 mdmc_reduce 参数控制。
接受输入类型中列出的所有输入。
>>> from torchmetrics.classification import StatScores
>>> preds = torch.tensor([1, 0, 2, 1])
>>> target = torch.tensor([1, 1, 2, 0])
>>> stat_scores = StatScores(reduce='macro', num_classes=3)
>>> stat_scores(preds, target)
tensor([[0, 1, 2, 1, 1],
[1, 1, 1, 1, 2],
[1, 0, 3, 0, 1]])
>>> stat_scores = StatScores(reduce='micro')
>>> stat_scores(preds, target)
tensor([2, 2, 6, 2, 4])