Skip to content

Conversation

@hertschuh
Copy link
Collaborator

@hertschuh hertschuh commented Nov 27, 2025

This is needed to get reproducible results with keras.random ops.

Also introduced a constant for "global_seed_generator".

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @hertschuh, 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 an issue where keras.random operations might not yield reproducible results after keras.utils.set_random_seed was called. The core change involves ensuring that the global SeedGenerator is properly reset when a new random seed is set, thereby enforcing consistent behavior across subsequent random number generation calls. This enhancement is critical for maintaining determinism in machine learning experiments and debugging.

Highlights

  • Reproducibility Fix: The keras.utils.set_random_seed function now explicitly clears the global SeedGenerator to ensure it is re-initialized with the new seed, guaranteeing reproducible results for keras.random operations.
  • Test Enhancement: A new test case, test_set_random_seed_with_global_seed_generator, has been added to validate that keras.random operations produce identical outputs after set_random_seed is called multiple times with the same seed.
  • Test Refactoring: The existing test test_set_random_seed was renamed to test_set_random_seed_with_seed_generator and its pytest.mark.skipif decorator was removed, indicating broader applicability.
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 correctly addresses an issue with reproducibility in keras.random operations by ensuring the global SeedGenerator is cleared when keras.utils.set_random_seed is invoked. The implementation is sound, and the new test case provides good coverage for the fix. I have a couple of suggestions: one to improve maintainability by replacing a magic string with a constant, and another to address a potential test failure on the NumPy backend.

reason="Numpy backend does not support random seed setting.",
)
def test_set_random_seed(self):
def test_set_random_seed_with_seed_generator(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This test was previously skipped for the NumPy backend. By removing the skipif decorator, this test will now run for the NumPy backend. However, the get_model_output function uses tf.data.Dataset, which is specific to TensorFlow and will likely cause the test to fail when run with the NumPy backend.

To ensure this test can run across all backends, you could modify get_model_output to not use tf.data.Dataset. For example:

def get_model_output():
    model = keras.Sequential(
        [
            keras.layers.Dense(10),
            keras.layers.Dropout(0.5),
            keras.layers.Dense(10),
        ]
    )
    x = np.random.random((32, 10)).astype("float32")
    return model.predict(x)

Alternatively, if the intention is to keep this test for TensorFlow-based backends only, the skipif decorator should be restored.

@codecov-commenter
Copy link

codecov-commenter commented Nov 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.56%. Comparing base (8287e48) to head (271999a).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21874      +/-   ##
==========================================
- Coverage   82.57%   82.56%   -0.02%     
==========================================
  Files         577      578       +1     
  Lines       59586    59789     +203     
  Branches     9347     9384      +37     
==========================================
+ Hits        49205    49366     +161     
- Misses       7975     8002      +27     
- Partials     2406     2421      +15     
Flag Coverage Δ
keras 82.38% <100.00%> (-0.02%) ⬇️
keras-jax 62.89% <100.00%> (+0.01%) ⬆️
keras-numpy 57.46% <100.00%> (-0.07%) ⬇️
keras-openvino 34.34% <100.00%> (-0.01%) ⬇️
keras-tensorflow 64.42% <100.00%> (+0.01%) ⬆️
keras-torch 63.60% <100.00%> (+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.

@hertschuh hertschuh marked this pull request as draft November 27, 2025 01:38
@hertschuh hertschuh force-pushed the seed_generator branch 2 times, most recently from 7063485 to fec12a0 Compare December 2, 2025 00:22
@hertschuh hertschuh marked this pull request as ready for review December 2, 2025 00:22
Copy link
Collaborator

@divyashreepathihalli divyashreepathihalli left a comment

Choose a reason for hiding this comment

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

left two comments. LGTM.


# Store seed in global state so we can query it if set.
global_state.set_global_attribute(GLOBAL_RANDOM_SEED, seed)
# Remove global SeedGenerator, it will be recreated from the seed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe the docstring can be updated to mention that it now resets the Keras global random generator?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

y2 = get_model_output()
self.assertAllClose(y1, y2)

def test_set_random_seed_with_global_seed_generator(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe also add a test to check that different seed produces different results. To make sure the reset mechanism doesn't accidentally lock the generator to a single state.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, added

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Dec 3, 2025
This is needed to get reproducible results with `keras.random` ops.

Also introduced a constant for `"global_seed_generator"`.
@google-ml-butler google-ml-butler bot removed the ready to pull Ready to be merged into the codebase label Dec 4, 2025
@hertschuh hertschuh merged commit 9fc8185 into keras-team:master Dec 4, 2025
9 of 10 checks passed
@hertschuh hertschuh deleted the seed_generator branch December 4, 2025 02:18
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.

5 participants