Skip to content

Conversation

@Abhinavexists
Copy link

@Abhinavexists Abhinavexists commented Nov 3, 2025

Fix: #21768

added input validation for the noise_shape parameter in keras.layers.Dropout to prevent invalid values from being passed at layer initialization time.

This is an initial implementation focusing on basic validation logic. The implementation
intentionally:

  • Validates input types and values at initialization
  • Does not yet implement broadcasting validation against input shape (deferred for future
    iteration)
  • Does not include comprehensive tests (will be added once approach is approved)

@google-cla
Copy link

google-cla bot commented Nov 3, 2025

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 enhances the stability and predictability of the keras.layers.Dropout layer by implementing comprehensive input validation for its noise_shape parameter. By catching invalid shape configurations at initialization, it prevents potential runtime errors and ensures that the layer operates with well-formed parameters, contributing to a more robust Keras API.

Highlights

  • Input Validation for noise_shape: Added robust input validation for the noise_shape parameter within the keras.layers.Dropout layer's initialization to prevent invalid values from being passed.
  • New Validation Method: Introduced a new private method, _validate_noise_shape, to encapsulate the validation logic for the noise_shape parameter, ensuring cleaner code and separation of concerns.
  • Validation Checks: The validation ensures that noise_shape is either None or a tuple/list of integers. It also verifies that all non-None elements within the shape are positive integers.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 introduces input validation for the noise_shape parameter in the Dropout layer, which is a great addition for improving the layer's robustness. The implementation is clean and handles various invalid inputs correctly by raising ValueError with descriptive messages. I have one suggestion to refine an error message to be more precise, aligning it even better with the code's behavior and the Keras API design guidelines. Overall, this is a valuable improvement.

Comment on lines +66 to +70
raise ValueError(
f"Invalid value received for argument `noise_shape`. "
f"Expected a tuple or list of integers. "
f"Received: noise_shape={noise_shape}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The error message here states that noise_shape is expected to be a "tuple or list of integers". However, the code correctly attempts to convert any iterable to a tuple by calling tuple(noise_shape). To avoid potential confusion for users who might pass another valid iterable type (like a set or a generator), it would be more accurate to mention "iterable" in the error message. This change would make the feedback more precise, which is encouraged by the Keras API design guidelines.1

Suggested change
raise ValueError(
f"Invalid value received for argument `noise_shape`. "
f"Expected a tuple or list of integers. "
f"Received: noise_shape={noise_shape}"
)
raise ValueError(
f"Invalid value received for argument `noise_shape`. "
f"Expected an iterable of integers (e.g., a tuple or list). "
f"Received: noise_shape={noise_shape}"
)

Style Guide References

Footnotes

  1. Error messages should be contextual, informative, and actionable. A good error message should clearly and precisely state what was expected to help the user fix the issue.

@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2025

Codecov Report

❌ Patch coverage is 43.75000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.66%. Comparing base (664b8b6) to head (2648443).

Files with missing lines Patch % Lines
keras/src/layers/regularization/dropout.py 43.75% 6 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21819      +/-   ##
==========================================
- Coverage   82.67%   82.66%   -0.02%     
==========================================
  Files         577      577              
  Lines       59429    59444      +15     
  Branches     9315     9321       +6     
==========================================
+ Hits        49131    49137       +6     
- Misses       7898     7904       +6     
- Partials     2400     2403       +3     
Flag Coverage Δ
keras 82.48% <43.75%> (-0.02%) ⬇️
keras-jax 63.31% <43.75%> (-0.01%) ⬇️
keras-numpy 57.55% <43.75%> (-0.01%) ⬇️
keras-openvino 34.33% <6.25%> (-0.01%) ⬇️
keras-tensorflow 64.13% <43.75%> (-0.01%) ⬇️
keras-torch 63.61% <43.75%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

To limit noise_shape in func keras.layers.Dropout()

3 participants