Skip to content

Conversation

@hertschuh
Copy link
Collaborator

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

This is needed to get reproducible results with `keras.random` ops.
@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.

# 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.
global_state.set_global_attribute("global_seed_generator", None)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve maintainability and avoid using 'magic strings', consider defining "global_seed_generator" as a module-level constant, similar to how GLOBAL_RANDOM_SEED is handled. This would make the code easier to read and maintain.

For example, you could add at the top of the file:

GLOBAL_SEED_GENERATOR_KEY = "global_seed_generator"

And then use it here:

global_state.set_global_attribute(GLOBAL_SEED_GENERATOR_KEY, None)

Ideally, this constant would be shared with keras/src/random/seed_generator.py to ensure consistency.

@codecov-commenter
Copy link

codecov-commenter commented Nov 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.58%. Comparing base (8287e48) to head (361f391).

❗ There is a different number of reports uploaded between BASE (8287e48) and HEAD (361f391). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (8287e48) HEAD (361f391)
keras 5 4
keras-tensorflow 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21874      +/-   ##
==========================================
- Coverage   82.57%   76.58%   -6.00%     
==========================================
  Files         577      577              
  Lines       59586    59587       +1     
  Branches     9347     9347              
==========================================
- Hits        49205    45632    -3573     
- Misses       7975    11486    +3511     
- Partials     2406     2469      +63     
Flag Coverage Δ
keras 76.44% <100.00%> (-5.95%) ⬇️
keras-jax 62.88% <100.00%> (+<0.01%) ⬆️
keras-numpy 57.52% <100.00%> (+<0.01%) ⬆️
keras-openvino 34.34% <100.00%> (+<0.01%) ⬆️
keras-tensorflow ?
keras-torch 63.58% <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
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.

3 participants