Fix #22162: Add input validation for PReLU and LeakyReLU layers#22344
Fix #22162: Add input validation for PReLU and LeakyReLU layers#22344thakoreh wants to merge 1 commit intokeras-team:masterfrom
Conversation
…yers - PReLU: Validate alpha_initializer is not None - LeakyReLU: Validate negative_slope is not NaN Both cases now raise clear ValueError at instantiation time instead of failing silently during training, following fail-fast principle. Tests added: - test_prelu_invalid_alpha_initializer_none - test_invalid_nan_negative_slope
|
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. |
Summary of ChangesHello, 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 robustness of Keras's 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
Activity
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.
Code Review
This pull request introduces valuable input validation for the PReLU and LeakyReLU layers, ensuring that invalid configurations for alpha_initializer and negative_slope are caught at instantiation time with clear error messages. This aligns well with the fail-fast principle and improves the developer experience. The changes are correct and include corresponding unit tests. I have one minor suggestion to improve code consistency.
| "Argument `alpha` is deprecated. Use `negative_slope` instead." | ||
| ) | ||
| super().__init__(**kwargs) | ||
| import math |
There was a problem hiding this comment.
For consistency with other files in the Keras codebase (e.g., keras/src/layers/layer.py, keras/src/regularizers/regularizers.py) and to adhere to general Python style guidelines (PEP 8), standard library imports like math should be placed at the top of the file. Please move this import to the top-level imports section.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #22344 +/- ##
=======================================
Coverage 82.90% 82.90%
=======================================
Files 594 594
Lines 65844 65849 +5
Branches 10293 10295 +2
=======================================
+ Hits 54590 54595 +5
Misses 8638 8638
Partials 2616 2616
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:
|
Summary
Fixes #22162
Adds input validation for
PReLUandLeakyReLUlayers to catch invalid configurations at instantiation time rather than during training.Changes
PReLU
alpha_initializeris notNoneValueErrorwith clear message ifNoneis passedLeakyReLU
negative_slopeis notNaNmath.isnan(negative_slope)for float valuesValueErrorwith clear message ifNaNis passedRationale
Both cases violate the fail-fast principle. Without this validation:
PReLUwithalpha_initializer=Nonewould fail later duringbuild()or trainingLeakyReLUwithnegative_slope=NaNwould produce non-numeric gradientsTests Added
test_prelu_invalid_alpha_initializer_none- verifies PReLU rejectsNonetest_invalid_nan_negative_slope- verifies LeakyReLU rejectsNaNExample
Before:
After: