-
Notifications
You must be signed in to change notification settings - Fork 19.7k
keras.utils.set_random_seed clear the global SeedGenerator.
#21874
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import numpy as np | ||
| import pytest | ||
| import tensorflow as tf | ||
|
|
||
| import keras | ||
|
|
@@ -9,11 +8,7 @@ | |
|
|
||
|
|
||
| class TestRandomSeedSetting(test_case.TestCase): | ||
| @pytest.mark.skipif( | ||
| backend.backend() == "numpy", | ||
| reason="Numpy backend does not support random seed setting.", | ||
| ) | ||
| def test_set_random_seed(self): | ||
| def test_set_random_seed_with_seed_generator(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was previously skipped for the NumPy backend. By removing the To ensure this test can run across all backends, you could modify 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 |
||
| def get_model_output(): | ||
| model = keras.Sequential( | ||
| [ | ||
|
|
@@ -31,3 +26,10 @@ def get_model_output(): | |
| rng_utils.set_random_seed(42) | ||
| y2 = get_model_output() | ||
| self.assertAllClose(y1, y2) | ||
|
|
||
| def test_set_random_seed_with_global_seed_generator(self): | ||
| rng_utils.set_random_seed(42) | ||
| y1 = backend.random.randint((32, 10), minval=0, maxval=1000) | ||
| rng_utils.set_random_seed(42) | ||
| y2 = backend.random.randint((32, 10), minval=0, maxval=1000) | ||
| self.assertAllClose(y1, y2) | ||
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.
To improve maintainability and avoid using 'magic strings', consider defining
"global_seed_generator"as a module-level constant, similar to howGLOBAL_RANDOM_SEEDis handled. This would make the code easier to read and maintain.For example, you could add at the top of the file:
And then use it here:
Ideally, this constant would be shared with
keras/src/random/seed_generator.pyto ensure consistency.