Skip to content
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

keras.utils.set_random_seed does not set seeds #19322

Closed
markodjordjic opened this issue Mar 17, 2024 · 3 comments
Closed

keras.utils.set_random_seed does not set seeds #19322

markodjordjic opened this issue Mar 17, 2024 · 3 comments
Assignees

Comments

@markodjordjic
Copy link

OS: Windows 11
Python == 3.11.0 64 bit
Keras == 3.0.5

keras.utils.set_random_seed() does not set the reproducibility for (at least) Numpy operations.

import numpy as np
import keras

seed = 0

keras.utils.set_random_seed(seed=seed)
random_number_generator = np.random.default_rng()

gamma =
random_number_generator.gamma(shape=2, size=(100, 1))

Multiple runs will produce different random values.

With random_number_generator = np.random.default_rng(seed=seed) multiple runs will produce same random values.

@markodjordjic
Copy link
Author

This did work: np.random.gamma(shape=2, size=(100, 1)) But it should be more clear in the documentation that this is the limitation on how to use the setting of the seed for numpy via Keras.

@fchollet
Copy link
Collaborator

The set_random_seed() docstring says:

Calling this utility is equivalent to the following:

import random
import numpy as np
from keras.utils.module_utils import tensorflow as tf
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)

Calling np.random.seed(seed) does not affect np.random.default_rng() because by definition default_rng() is standalone and locally-seeded. It does depend on the global seed state. This is well documented by NumPy.

@markodjordjic
Copy link
Author

Many, thanks! @SuryanarayanaY and @fchollet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants