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

Improve AUC numeric stability #224

Closed
wants to merge 9 commits into from
Closed

Conversation

SkafteNicki
Copy link
Member

@SkafteNicki SkafteNicki commented May 4, 2021

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

What does this PR do?

Fixes #219
In rare case when the input is very large, the dx here
https://github.com/PyTorchLightning/metrics/blob/cb6899bb47c7d30f8626d6ef8c28cc29efce82d6/torchmetrics/functional/classification/auc.py#L41-L49
will even for monotone increasing/decreasing (input that is already sorted) result in a mix of positive and negative errors making the algorithm raise the error. This happens when x[i] and x[i+1] is the same number but due to numerical stability x[i+1]-x[i] will randomly either be a small negative or positive number.

This PR solves it by adding a small tolerance to the checks.

I checked with the provided repo/script in the discussion that it correctly solves the error.

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@SkafteNicki SkafteNicki added the bug / fix Something isn't working label May 4, 2021
@pep8speaks
Copy link

pep8speaks commented May 4, 2021

Hello @SkafteNicki! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-05-05 17:35:31 UTC

@codecov
Copy link

codecov bot commented May 4, 2021

Codecov Report

Merging #224 (9692400) into master (33864db) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #224   +/-   ##
=======================================
  Coverage   96.80%   96.80%           
=======================================
  Files          92       92           
  Lines        3005     3005           
=======================================
  Hits         2909     2909           
  Misses         96       96           
Flag Coverage Δ
Linux 79.06% <66.66%> (ø)
Windows 79.06% <66.66%> (ø)
cpu 96.80% <100.00%> (ø)
macOS 96.80% <100.00%> (ø)
pytest 96.80% <100.00%> (ø)
python3.6 95.74% <100.00%> (ø)
python3.8 96.77% <100.00%> (+0.09%) ⬆️
python3.9 96.67% <100.00%> (ø)
torch1.3.1 95.74% <100.00%> (ø)
torch1.4.0 95.87% <100.00%> (?)
torch1.8.1 96.67% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
torchmetrics/functional/classification/auc.py 87.50% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 33864db...9692400. Read the comment docs.

@Borda Borda enabled auto-merge (squash) May 4, 2021 13:59
@Borda Borda added the ready label May 4, 2021
if (dx < 0).any():
if (dx <= 0).all():
if (dx + tol < 0).any():
if (dx <= tol).all():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I reading it correctly that if I have

dx = [-1e-7, 5e-7, 5e-7, ... 10000 times ...., 5e-7] then we'll deduce incorrect direction here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it seems that direction/sign is changed, so shall ve preserve it...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I am misunderstanding the question...
the tensor
dx = [1e-7, 5e-7, 5e-7, ... 10000 times ...., 5e-7]
implies that direction=1 right? so if we say that numerical instability leads to a change of sign in the first element
dx = [-1e-7, 5e-7, 5e-7, ... 10000 times ...., 5e-7]
then it will still be direction=1 with the change.

Copy link
Contributor

@maximsch2 maximsch2 May 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, let's maybe use this instead:
dx = [-1.0, 5e-7, 5e-7, ... 10000 times ...., 5e-7]
Now both (dx+tol<0).any() and (dx <= tol).all() are true and we'll discover direction as -1, wheras the whole thing is incorrectly sorted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I see the problem now...
Do you have anyway to solve this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numerical issues are usually tricky. I'm suggesting we remove checks for the internal callers of this function, let me quickly show an example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SkafteNicki , check out #230

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maximsch2 looks good, closing this in favour of yours

@Borda Borda disabled auto-merge May 4, 2021 16:32
@Borda Borda requested a review from a team May 4, 2021 22:16
@SkafteNicki SkafteNicki removed the ready label May 5, 2021
@SkafteNicki SkafteNicki closed this May 6, 2021
@Borda Borda deleted the auc_numeric_stability branch May 10, 2021 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants