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

Negative ignore_index for the Accuracy metric #362

Merged
merged 25 commits into from Feb 25, 2022

Conversation

yassersouri
Copy link
Contributor

As with the CrossEntropy loss in Pytorch, it is usual to have negative value for ignore_index.
Currently the check in _check_classifiction_inputs (specifically _basic_input_validation) does not allow a negative value for target.

I have currently created some tests to show the problem.
If this is something that you think should be fixed, I can write a fix for it.

My proposal is to add ignore_index as an additional input to the _basic_input_validation and slightly change the implementation.
Please let me know if this is something that makes sense and you guys want it fixed.

@pep8speaks
Copy link

pep8speaks commented Jul 9, 2021

Hello @yassersouri! Thanks for updating this PR.

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

Comment last updated at 2021-07-26 07:40:46 UTC

@codecov
Copy link

codecov bot commented Jul 9, 2021

Codecov Report

Merging #362 (e804319) into master (21ba650) will decrease coverage by 0%.
The diff coverage is 96%.

@@          Coverage Diff          @@
##           master   #362   +/-   ##
=====================================
- Coverage      95%    95%   -0%     
=====================================
  Files         167    167           
  Lines        6906   6931   +25     
=====================================
+ Hits         6565   6585   +20     
- Misses        341    346    +5     

@yassersouri
Copy link
Contributor Author

Codecov Report

Merging #362 (6667e8c) into master (3de06f9) will decrease coverage by 20.05%.

How is it possible to decrease the coverage by adding a test?!

@Borda Borda added the enhancement New feature or request label Jul 9, 2021
@yassersouri
Copy link
Contributor Author

yassersouri commented Jul 12, 2021

@SkafteNicki All I need is a thumbs up or thumbs down on this.
Currently for me it makes a lot of sense to have negative value for ignore_index.

@SkafteNicki
Copy link
Member

@yassersouri fine by me. The changes probably need to be done to the StatScore class as the other classification metric inherit from this.

@Borda
Copy link
Member

Borda commented Jul 26, 2021

@yassersouri @SkafteNicki how is it going here? 🐰

@mergify mergify bot added the has conflicts label Aug 2, 2021
@Borda Borda force-pushed the master branch 2 times, most recently from 58f1e14 to caf7b0b Compare August 10, 2021 08:54
@yassersouri
Copy link
Contributor Author

Hi again! Sorry for disappearing for a while. I will work on this pull request again.
There were some issues regarding the way the ignored items are removed from the predictions and targets that won't work in this new scenario. I am thinking about two ways to solve it.

  1. Adding a new index (num_classes + 1) for the ignore index and deal with it that way.
  2. Converting the inputs from d-dimensional to 1-dimensional so it is easier to remove the ignored indices.

Currently I think option 1 is messy as it requires making changes at multiple locations. I will try to implement version 2 and then gather your feedback.

@Borda Borda added this to the v0.5 milestone Aug 18, 2021
@yassersouri
Copy link
Contributor Author

I just resolved the conflicts, please ignore the review request.

@yassersouri yassersouri mentioned this pull request Aug 24, 2021
@Borda
Copy link
Member

Borda commented Jan 19, 2022

This seems quite abundant; shall we close it?
cc: @PyTorchLightning/core-metrics

@tridao
Copy link
Contributor

tridao commented Feb 10, 2022

Having this option would be quite useful.
For example, training masked language models (e.g. BERT) involves computing accuracy only on the masked tokens, while the other tokens are replaced with index -100. Currently torchmetrics.Accuracy errors with "The target has to be a non-negative tensor".

@mergify mergify bot removed the has conflicts label Feb 17, 2022
@stancld stancld requested a review from Borda February 19, 2022 14:37
@stancld
Copy link
Contributor

stancld commented Feb 25, 2022

@Borda @SkafteNicki Could you please have a look at this PR? I tried to fix some minor issues but I'm still not fully familiar with the torchmetrics classification package :]

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.

lgtm

@mergify mergify bot added the ready label Feb 25, 2022
@Borda Borda enabled auto-merge (squash) February 25, 2022 12:50
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.

Also LGTM, this should probably be extended to other classes when we try to refactor classification

@Borda Borda merged commit 9f94086 into Lightning-AI:master Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants