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

Corrected f_beta computation #4183

Merged
merged 7 commits into from
Oct 21, 2020
Merged

Conversation

abhinavg97
Copy link
Contributor

@abhinavg97 abhinavg97 commented Oct 16, 2020

Added METRIC_EPS in the denominator of computation to avoid nan values in f_beta score.

What does this PR do?

Will give the correct f_beta score and prevent outputting nan values for multilabel classification with macro.

Fixes #4187

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 your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

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 🙃 👍

Added METRIC_EPS in the denominator to avoid nan values in f_beta score.
@pep8speaks
Copy link

pep8speaks commented Oct 16, 2020

Hello @abhinavg97! Thanks for updating this PR.

Line 91:121: E501 line too long (121 > 120 characters)

Comment last updated at 2020-10-21 08:07:39 UTC

@mergify mergify bot requested a review from a team October 16, 2020 03:34
Made changes flake8 compliant
@codecov
Copy link

codecov bot commented Oct 16, 2020

Codecov Report

Merging #4183 into master will decrease coverage by 1%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #4183    +/-   ##
=======================================
- Coverage      90%     89%    -1%     
=======================================
  Files         103     103            
  Lines        7863    8313   +450     
=======================================
+ Hits         7064    7403   +339     
- Misses        799     910   +111     

@abhinavg97 abhinavg97 changed the title Update f_beta.py Corrected f_beta computation by adding METRIC_EPS Oct 16, 2020
@abhinavg97 abhinavg97 changed the title Corrected f_beta computation by adding METRIC_EPS Corrected f_beta computation by adding METRIC_EPS in denominator Oct 16, 2020
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

I think that this offset epsilon adding to all is not good, we shall check it there are zeros and then add it just there, well if know that some are rore just retunt the output...
@justusschock @SkafteNicki

@Borda Borda added bug Something isn't working Metrics labels Oct 16, 2020
@mergify mergify bot requested a review from a team October 16, 2020 07:12
@SkafteNicki
Copy link
Member

We have this function (still used in functional) that took care of this:
https://github.com/PyTorchLightning/pytorch-lightning/blob/130de22fd75253330320473d8081fd60698c3d64/pytorch_lightning/metrics/functional/reduction.py#L40-L78
We should probably reuse it in the new class api

Makes use of class_reduce for macro f_beta computation to avoid nans
Made flake8 compliant
@abhinavg97 abhinavg97 requested review from Borda and removed request for a team October 16, 2020 09:13
@mergify mergify bot requested a review from a team October 16, 2020 09:13
@SkafteNicki
Copy link
Member

@abhinavg97 is it correct that this happens when both recall and precision is 0 for a particular class?
Could we add a test case for this?

@abhinavg97
Copy link
Contributor Author

abhinavg97 commented Oct 16, 2020

Yes, that's right.

Here is an example:

predictions = torch.Tensor([[0,0],[0,1]])
labels = torch.Tensor([[0,1],[1,0]])

f_beta = Fbeta(num_classes=len(t[0]), average='macro', multilabel=True)

f_beta(predictions, labels)

> torch(nan)

Expected: torch(0)

Yeah I think we can add a test for this. Is it okay if I add the above case as a test?

@Borda
Copy link
Member

Borda commented Oct 16, 2020

can we add a test to verify that these changes fix the actual master...?

@SkafteNicki
Copy link
Member

@abhinavg97 can you add the following testcase to tests/metrics/classification/inputs and afterwards add to the corresponding parametrization of the f-beta tests:

# Generate edge multilabel edge case, where nothing matches (scores are undefined)
__temp_preds = torch.randint(high=2, size=(NUM_BATCHES, BATCH_SIZE, NUM_CLASSES))
__temp_target = abs(__temp_preds - 1)

_multilabel_inputs_no_match = Input(
    preds=__temp_preds,
    target=__temp_target
)

@abhinavg97
Copy link
Contributor Author

@abhinavg97 can you add the following testcase to tests/metrics/classification/inputs and afterwards add to the corresponding parametrization of the f-beta tests:

# Generate edge multilabel edge case, where nothing matches (scores are undefined)
__temp_preds = torch.randint(high=2, size=(NUM_BATCHES, BATCH_SIZE, NUM_CLASSES))
__temp_target = abs(__temp_preds - 1)

_multilabel_inputs_no_match = Input(
    preds=__temp_preds,
    target=__temp_target
)

Yes sure, will do. Thanks for the pointer to the input location :)

@abhinavg97 abhinavg97 changed the title Corrected f_beta computation by adding METRIC_EPS in denominator Corrected f_beta computation Oct 19, 2020
@edenlightning edenlightning added this to the 1.0.3 milestone Oct 19, 2020
Copy link
Member

@SkafteNicki SkafteNicki left a comment

Choose a reason for hiding this comment

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

LGTM

@mergify mergify bot requested a review from a team October 21, 2020 07:48
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

Great PR ! Thanks !

@@ -124,9 +125,11 @@ def compute(self):
precision = self.true_positives.sum().float() / (self.predicted_positives.sum() + METRIC_EPS)
Copy link
Member

Choose a reason for hiding this comment

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

do we really want to keep our metric systematically unprecise by adding some offset?

Copy link
Member

Choose a reason for hiding this comment

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

we can move towards class_reduce function which explicit handles nans, when we unify the class based metrics and functional metrics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, makes sense. Just checked and the tests pass without METRIC_EPS. Pushing the update now.

@Borda Borda added the ready PRs ready to be merged label Oct 21, 2020
@SkafteNicki SkafteNicki merged commit 5d1583d into Lightning-AI:master Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

F beta with macro computation outputs nans for certain inputs
7 participants