torcheval.metrics.functional.binary_accuracy¶
- torcheval.metrics.functional.binary_accuracy(input: Tensor, target: Tensor, *, threshold: float = 0.5) Tensor¶
计算二进制准确率分数,即输入与目标匹配的频率。 其类版本是
torcheval.metrics.BinaryAccuracy。- 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。
示例:
>>> import torch >>> from torcheval.metrics.functional import binary_accuracy >>> input = torch.tensor([0, 0, 1, 1]) >>> target = torch.tensor([1, 0, 1, 1]) >>> binary_accuracy(input, target) tensor(0.75) # 3 / 4 >>> input = torch.tensor([0, 0.2, 0.6, 0.7]) >>> target = torch.tensor([1, 0, 1, 1]) >>> binary_accuracy(input, target, threshold=0.7) tensor(0.5) # 2 / 4