Add ignore_class parameter to SparseCategoricalCrossentropy #22284
Add ignore_class parameter to SparseCategoricalCrossentropy #22284hertschuh merged 6 commits intokeras-team:masterfrom
Conversation
…d corresponding tests - Introduced ignore_class option to SparseCategoricalCrossentropy to allow ignoring specific classes during loss computation. - Added unit tests to validate functionality of ignore_class with both standard and weighted inputs, as well as with logits.
Summary of ChangesHello @cpuguy96, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces the ignore_class parameter to the SparseCategoricalCrossentropy metric, aligning its functionality with the corresponding loss function. The changes correctly plumb the new parameter through the class constructor and serialization logic. Comprehensive unit tests have been added to validate the new functionality under various conditions, including with sample weights and logits. The implementation is sound and consistent with the existing codebase.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #22284 +/- ##
=======================================
Coverage 82.87% 82.88%
=======================================
Files 594 594
Lines 65798 65799 +1
Branches 10288 10288
=======================================
+ Hits 54533 54536 +3
+ Misses 8643 8642 -1
+ Partials 2622 2621 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…d y_pred to tensors when ignore_class is specified
hertschuh
left a comment
There was a problem hiding this comment.
Thanks for filling this gap!
keras/src/losses/losses.py
Outdated
| y_true = ops.convert_to_tensor(y_true) | ||
| y_pred = ops.convert_to_tensor(y_pred) |
There was a problem hiding this comment.
This should not be needed, please revert.
And if it is needed, this is an indication that there is a different bug.
There was a problem hiding this comment.
This was added due to test failures in the new tests added only for torch.
FAILED keras/src/metrics/probabilistic_metrics_test.py::SparseCategoricalCrossentropyTest::test_ignore_class - TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'Tensor'
FAILED keras/src/metrics/probabilistic_metrics_test.py::SparseCategoricalCrossentropyTest::test_ignore_class_from_logits - TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'Tensor'
FAILED keras/src/metrics/probabilistic_metrics_test.py::SparseCategoricalCrossentropyTest::test_ignore_class_weighted - TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'Tensor'
I updated the function to use ops.multiply instead of * to remove the need of calling ops.convert_to_tensor.
There was a problem hiding this comment.
Yes, that is the right fix.
…ing instead of element-wise multiplication Prevents needed to use `ops.convert_to_tensor` before multiplication for torch tests.
Introduced ignore_class option to SparseCategoricalCrossentropy to allow ignoring specific classes during loss computation. Added unit tests to validate functionality of ignore_class with both standard and weighted inputs, as well as with logits.