Shortcuts

torcheval.metrics.functional.multiclass_f1_score

torcheval.metrics.functional.multiclass_f1_score(input: Tensor, target: Tensor, *, num_classes: Optional[int] = None, average: Optional[str] = 'micro') Tensor[source]

Compute f1 score, which is defined as the harmonic mean of precision and recall. We convert NaN to zero when f1 score is NaN. This happens when either precision or recall is NaN or when both precision and recall are zero. Its class version is torcheval.metrics.MultiClassF1Score. See also binary_f1_score

Parameters:
  • input (Tensor) – Tensor of label predictions. It could be the predicted labels, with shape of (n_sample, ). It could also be probabilities or logits with shape of (n_sample, n_class). torch.argmax will be used to convert input into predicted labels.
  • target (Tensor) – Tensor of ground truth labels with shape of (n_sample, ).
  • num_classes – Number of classes.
  • average
    • 'micro' [default]:
      Calculate the metrics globally.
    • 'macro':
      Calculate metrics for each class separately, and return their unweighted mean. Classes with 0 true and predicted instances are ignored.
    • 'weighted'
      Calculate metrics for each class separately, and return their weighted sum. Weights are defined as the proportion of occurrences of each class in “target”. Classes with 0 true and predicted instances are ignored.
    • None:
      Calculate the metric for each class separately, and return the metric for every class.

Examples:

>>> import torch
>>> from torcheval.metrics.functional import multiclass_f1_score
>>> input = torch.tensor([0, 2, 1, 3])
>>> target = torch.tensor([0, 1, 2, 3])
>>> multiclass_f1_score(input, target, num_classes=4)
tensor(0.5000)

>>> input = torch.tensor([0, 2, 1, 3])
>>> target = torch.tensor([0, 1, 2, 3])
>>> multiclass_f1_score(input, target, num_classes=4, average=None)
tensor([1., 0., 0., 1.])

>>> input = torch.tensor([0, 0, 1, 1, 1])
>>> target = torch.tensor([0, 0, 0, 0, 1])
>>> multiclass_f1_score(input, target, num_classes=2, average="macro")
tensor(0.5833)

>>> input = torch.tensor([[0.9, 0.1, 0, 0], [0.1, 0.2, 0.4, 0.3], [0, 1.0, 0, 0], [0, 0, 0.2, 0.8]])
>>> target = torch.tensor([0, 1, 2, 3])
>>> multiclass_f1_score(input, target, num_classes=4)
tensor(0.5)

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources