torcheval.metrics.BinaryRecall¶
- class torcheval.metrics.BinaryRecall(*, threshold: float = 0.5, device: device | None = None)¶
计算二分类任务的召回率得分,该得分计算为真阳性与真阳性和假阴性之和的比率。其函数版本为
torcheval.metrics.functional.binary_recall()
。当类在真实标签中实例为零(当 TP + FN = 0 时),我们将 NaN 转换为 0。- 参数:
threshold (float, 默认值 0.5) – 用于将输入转换为每个样本预测标签的阈值。
torch.where(input < threshold, 0, 1)
将应用于input
。
示例
>>> import torch >>> from torcheval.metrics.classification import BinaryRecall >>> metric = BinaryRecall() >>> input = torch.tensor([0, 0, 1, 1]) >>> target = torch.tensor([0, 1, 1, 1]) >>> metric.update(input, target) >>> metric.compute() tensor(0.6667) # 2 / 3 >>> metric = BinaryRecall() >>> input = torch.tensor([0, 0.2, 0.4, 0.7]) >>> target = torch.tensor([1, 0, 1, 1]) >>> metric.update(input, target) >>> metric.compute() tensor(0.3333) # 1 / 3 >>> metric = BinaryRecall(threshold=0.4) >>> input = torch.tensor([0, 0.2, 0.4, 0.7]) >>> target = torch.tensor([1, 0, 1, 1]) >>> metric.update(input, target) >>> metric.compute() tensor(0.5000) # 1 / 2
- __init__(*, threshold: float = 0.5, device: device | None = None) None ¶
初始化度量对象及其内部状态。
使用
self._add_state()
初始化度量类状态变量。状态变量应为torch.Tensor
、torch.Tensor
列表、以torch.Tensor
作为值的字典或torch.Tensor
的 deque。
方法
__init__
(*[, threshold, device])初始化度量对象及其内部状态。
计算
()返回召回率得分。
load_state_dict
(state_dict[, strict])从 state_dict 加载度量状态变量。
merge_state
(metrics)实现此方法以更新当前度量的状态变量,使其成为当前度量和输入度量的合并状态。
重置
()将度量状态变量重置为其默认值。
state_dict
()在 state_dict 中保存度量状态变量。
to
(device, *args, **kwargs)将度量状态变量中的张量移动到设备。
update
(input, target)使用真实标签和预测更新状态。
属性
设备
Metric.to()
的最后一个输入设备。