Precision raises an NonComputableError for all zeros model predictions
See original GitHub issue🐛 Bug description
Following https://github.com/pytorch/ignite/issues/1991#issuecomment-844860265
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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Precision Metric: must have at least one example before it can ...
For example, multiclass case, let's assume the predictions to be same for all ... Precision raises an NonComputableError for all zeros model ......
Read more >Binary classification - Class 1 testing metrics are all zeros
I am working on a binary classification problem for Spam/Not spam emails using Keras and tensorflow. Training accuracy is perfect and the ......
Read more >arXiv:2210.13741v1 [quant-ph] 25 Oct 2022
class capacity, the model will also increase the function classes until they are rich enough to achieve zero training.
Read more >Model Predictions are all Tensors Full of Zeros
I'm working to build a prediction model that is able to take general information about the weather and location of an accident to...
Read more >Getting Accurate Predicted Counts When There Are No Zeros ...
We previously examined why a linear regression and negative binomial regression were not viable models for predicting the expected length of stay in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After reading discussion #335 and some experiments I’d like to propose another solution. It is based on the code proposed in that discussion, but the approach is slightly different. It uses the
__new__
magics, so it wraps the methods on instance creation, so no need to callsuper()
and user cannot break it by overriding__init__
,reset
,update
etc.What do you think?
yes, it’s true
torch.sum
with defaultkeepdim=False
will squeeze the dim, 1 to 0 dim in our case.Thus in a corner case withg zeros:
will produce
self._positives
equalstensor(0)
with size oftorch.Size([])
,ndim=0
and later when checking
self._positives == 0
will produce True and raise Error, since