-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Fix: Remove redundant epsilon in loss mask weight calculation #21908
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
base: master
Are you sure you want to change the base?
Fix: Remove redundant epsilon in loss mask weight calculation #21908
Conversation
Add a test for divide_no_nan to ensure it handles division by zero correctly.
Summary of ChangesHello @Abhinavexists, 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 addresses a critical numerical instability issue within the Keras loss calculation by refining the Highlights
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
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly identifies and resolves a numerical instability issue in the loss mask weight calculation by removing a redundant epsilon. The fix is sound, and the use of ops.divide_no_nan is appropriate for handling division by zero gracefully. The addition of a new test case, test_divide_no_nan, is a valuable contribution as it ensures the behavior of the underlying operation is verified. Overall, the changes are well-executed and improve the robustness of the loss calculation.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #21908 +/- ##
==========================================
+ Coverage 76.30% 82.59% +6.29%
==========================================
Files 580 580
Lines 60029 60226 +197
Branches 9432 9444 +12
==========================================
+ Hits 45803 49742 +3939
+ Misses 11750 8050 -3700
+ Partials 2476 2434 -42
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:
|
hertschuh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
keras/src/losses/loss_test.py
Outdated
| loss = loss_fn(y_true, y_pred) | ||
| self.assertDType(loss, backend.floatx()) | ||
|
|
||
| def test_divide_no_nan(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already tested: https://github.com/keras-team/keras/blob/master/keras/src/ops/numpy_test.py#L2681-L2690
So no need to test this here.
Additionally, it looks like this case of all zeros mask is already covered:
https://github.com/keras-team/keras/blob/master/keras/src/losses/loss_test.py#L125-L129
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test have been removed
This PR fixes incorrect use of epsilon in mask weight calculation that caused numerical instability when all mask values are False.
In
keras/src/losses/loss.py, the mask weight calculation used:This is problematic because: