Skip to content

Behaviors of AUROC and Average Precision are inconsistent when all labels are equal #2494

@weihua916

Description

@weihua916

🐛 Bug

When all labels are equal (either all zeros or all ones), the current implementation of AUROC and AveragePrecision have pretty different behaviors.
When labels are all ones, AUROC gives 0, while AP gives 1.
When labels are all zeros, AUROC gives 0, while Average Precision gives NaN.

I think it is better to add a flag such that both metrics would return NaN when all labels are equal to better inform users.

To Reproduce

>>> from torchmetrics import AUROC, AveragePrecision
>>> import torch
>>> auroc = AUROC(task = "binary")
>>> ap = AveragePrecision(task = "binary")
>>> preds = torch.randn(10)
>>> labels = torch.ones(10, dtype = torch.long)
>>> auroc(preds, labels)
/opt/homebrew/anaconda3/lib/python3.9/site-packages/torchmetrics/utilities/prints.py:43: UserWarning: No negative samples in targets, false positive value should be meaningless. Returning zero tensor in false positive score
  warnings.warn(*args, **kwargs)  # noqa: B028
tensor(0.)
>>> ap(preds, labels)
tensor(1.)
>>> labels = torch.zeros(10, dtype = torch.long)
>>> auroc(preds, labels)
/opt/homebrew/anaconda3/lib/python3.9/site-packages/torchmetrics/utilities/prints.py:43: UserWarning: No positive samples in targets, true positive value should be meaningless. Returning zero tensor in true positive score
  warnings.warn(*args, **kwargs)  # noqa: B028
tensor(0.)
>>> ap(preds, labels)
tensor(nan)

Expected behavior

when labels are all equal, it should return a NaN all the time.
at least, there can be a flag like equal_label_mode.

>>> ap = AveragePrecision(task = "binary", equal_label_mode = "nan")
>>> ap = AveragePrecision(task = "binary", equal_label_mode = "nan")

that give the expected behavior.

Environment

  • TorchMetrics version: installed via pip. '1.3.0.post0'
  • Python & PyTorch Version (e.g., 1.0): Python 3.9.12, torch 2.1.0
  • Any other relevant information such as OS (e.g., Linux): Linus

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions