-
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
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,6 +1,4 @@ | ||
| import numpy as np | ||
| import pytest | ||
| import tensorflow as tf | ||
|
|
||
| import keras | ||
| from keras.src import backend | ||
|
|
@@ -9,11 +7,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( | ||
| [ | ||
|
|
@@ -23,11 +17,39 @@ def get_model_output(): | |
| ] | ||
| ) | ||
| x = np.random.random((32, 10)).astype("float32") | ||
| ds = tf.data.Dataset.from_tensor_slices(x).shuffle(32).batch(16) | ||
| return model.predict(ds) | ||
| return model.predict(x, batch_size=16) | ||
|
|
||
| rng_utils.set_random_seed(42) | ||
| y1 = get_model_output() | ||
| rng_utils.set_random_seed(42) | ||
|
|
||
| # Second call should produce different results. | ||
| y2 = get_model_output() | ||
| self.assertAllClose(y1, y2) | ||
| self.assertNotAllClose(y1, y2) | ||
|
|
||
| # Re-seeding should produce the same results as the first time. | ||
| rng_utils.set_random_seed(42) | ||
| y3 = get_model_output() | ||
| self.assertAllClose(y1, y3) | ||
|
|
||
| # Re-seeding with a different seed should produce different results. | ||
| rng_utils.set_random_seed(1337) | ||
| y4 = get_model_output() | ||
| self.assertNotAllClose(y1, y4) | ||
|
|
||
| def test_set_random_seed_with_global_seed_generator(self): | ||
|
Collaborator
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. 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.
Collaborator
Author
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. Good idea, added |
||
| rng_utils.set_random_seed(42) | ||
| y1 = backend.random.randint((32, 10), minval=0, maxval=1000) | ||
|
|
||
| # Second call should produce different results. | ||
| y2 = backend.random.randint((32, 10), minval=0, maxval=1000) | ||
| self.assertNotAllClose(y1, y2) | ||
|
|
||
| # Re-seeding should produce the same results as the first time. | ||
| rng_utils.set_random_seed(42) | ||
| y3 = backend.random.randint((32, 10), minval=0, maxval=1000) | ||
| self.assertAllClose(y1, y3) | ||
|
|
||
| # Re-seeding with a different seed should produce different results. | ||
| rng_utils.set_random_seed(1337) | ||
| y4 = backend.random.randint((32, 10), minval=0, maxval=1000) | ||
| self.assertNotAllClose(y1, y4) | ||
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.
maybe the docstring can be updated to mention that it now resets the Keras global random generator?
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.
Done