Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics.Accuracy is not calculated correctly when the first argument is of type float16 #4840

Closed
pgagarinov opened this issue Nov 24, 2020 · 3 comments · Fixed by #4837
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers help wanted Open to be worked on

Comments

@pgagarinov
Copy link
Contributor

🐛 Bug

To Reproduce

>a = tensor([0.5015, 0.5068, 0.4597, 0.5176, 0.5063, 0.4873, 0.5073, 0.5049, 0.4871,
        0.4939, 0.5132, 0.5151, 0.5269, 0.5229, 0.4797, 0.5435],
       device='cuda:0', dtype=torch.float16)
>b = tensor([1., 0., 1., 1., 1., 0., 1., 0., 0., 0., 0., 1., 1., 0., 0., 0.],
       device='cuda:0')

>acc = metrics.Accuracy(compute_on_step = True).to('cuda')

>acc(a, b)
tensor(0., device='cuda:0')

Expected behavior

Same as for float32:

>a = tensor([0.5015, 0.5068, 0.4597, 0.5176, 0.5063, 0.4873, 0.5073, 0.5049, 0.4871,
        0.4939, 0.5132, 0.5151, 0.5269, 0.5229, 0.4797, 0.5435],
       device='cuda:0')
>b = tensor([1., 0., 1., 1., 1., 0., 1., 0., 0., 0., 0., 1., 1., 0., 0., 0.],
       device='cuda:0')

>acc(a, b)
tensor(0.6250, device='cuda:0')

Additional context

I run training with precision=16, the model spits out float16 logits which I pass to F.softmax and then to metrics.Accuracy.
The type of F.softmax(logit) depends on whether precision=16 is specified or not.
Precision, recall and F1 metrics seem to be calculated correctly.

@pgagarinov pgagarinov added bug Something isn't working help wanted Open to be worked on labels Nov 24, 2020
@SkafteNicki
Copy link
Member

The problem is this check:
https://github.com/PyTorchLightning/pytorch-lightning/blob/78076ea0d99e4ba1f76a7992b7090812258c0d4d/pytorch_lightning/metrics/utils.py#L98-L100
as it assumes preds are float. It will probably be solved by PR #4837 but need to check up on that.

@tchaton
Copy link
Contributor

tchaton commented Nov 27, 2020

Hey @SkafteNicki,

Any update there ?

Best,
T.C

@SkafteNicki
Copy link
Member

@tchaton just confirmed that it will be solved when PR #4837 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Open to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants