Skip to content

Precision raises an NonComputableError for all zeros model predictions #2007

@vfdev-5

Description

@vfdev-5

🐛 Bug description

Following #1991 (comment)

from ignite.metrics import Precision
p = Precision()
p.update((torch.zeros(4), torch.randint(0, 2, (4,))))
p.compute()
> NotComputableError: Precision must have at least one example before it can be computed.

which is wrong as we do call update.

This is related to the check

/opt/conda/lib/python3.8/site-packages/ignite/metrics/precision.py in compute(self)
     51         is_scalar = not isinstance(self._positives, torch.Tensor) or self._positives.ndim == 0
     52         if is_scalar and self._positives == 0:
---> 53             raise NotComputableError(
     54                 f"{self.__class__.__name__} must have at least one example before it can be computed."
     55             )

where self._positives is 0 for all zeros predictions but is thought as uninitialized.

I think this should be related to a change in pytorch that previously had torch.tensor([0, 0, 0]).sum() as 1d tensor and now is 0d tensor.

Environment

  • PyTorch Version (e.g., 1.4): 1.8
  • Ignite Version (e.g., 0.3.0): 0.4.4
  • OS (e.g., Linux):
  • How you installed Ignite (conda, pip, source):
  • Python version:
  • Any other relevant information:

cc @liebkne

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions