torcheval.metrics.BinaryConfusionMatrix¶
- class torcheval.metrics.BinaryConfusionMatrix(*, threshold: float = 0.5, normalize: str | None = None, device: device | None = None)¶
计算二元混淆矩阵,一个2乘2的张量,包含计数((真正例,假反例),(假正例,真反例))
- Parameters:
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 (str) –
None[默认]:给出原始计数('none' 也默认为此)
'pred':在预测类别上进行归一化,即使行加起来为一。
'true':在条件正例上进行归一化,即使列加起来为一。
'all'”在所有示例上进行归一化,即使所有矩阵条目加起来为一。
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.Tensor,一个torch.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: 张量 :param target: 形状为 (n_sample,) 的真实标签张量。 :type target: 张量。属性
deviceMetric.to()的最后一个输入设备。