torcheval.metrics.functional.binary_f1_score¶
- torcheval.metrics.functional.binary_f1_score(input: Tensor, target: Tensor, *, threshold: float = 0.5) Tensor ¶
计算二元 F1 分数,即精确率和召回率的调和平均数。
- 参数:
input (Tensor) – 标签预测张量,形状为 (n_sample,)。
torch.where(input < threshold, 0, 1)
将应用于输入。target (Tensor) – 真实标签张量,形状为 (n_sample,)。
threshold (float, default 0.5) – 用于将输入转换为每个样本的预测标签的阈值。
torch.where(input < threshold, 0, 1)
将应用于input
。
示例
>>> import torch >>> from torcheval.metrics.functional import binary_f1_score >>> input = torch.tensor([0, 1, 0.7, 0.6]) >>> target = torch.tensor([0, 1, 1, 0]) >>> binary_f1_score(input, target, threshold=0.5) tensor(0.8000) >>> input = torch.tensor([1, 1, 0, 0]) >>> target = torch.tensor([0, 1, 1, 1]) >>> binary_f1_score(input, target, threshold=1) tensor(0.4000)