Shortcuts

torcheval.metrics.functional.binary_f1_score

torcheval.metrics.functional.binary_f1_score(input: Tensor, target: Tensor, *, threshold: float = 0.5) Tensor

计算二进制的f1分数,即精确率和召回率的调和平均数。

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_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)