tf.random.set_global_generator

Replaces the global generator with another Generator object.

View aliases

Main aliases

tf.random.experimental.set_global_generator

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.random.experimental.set_global_generator, tf.compat.v1.random.set_global_generator

tf.random.set_global_generator(
    generator
)

This function replaces the global generator with the provided generator object. A random number generator utilizes a tf.Variable object to store its state. The user shall be aware of caveats how set_global_generator interacts with tf.function:

  • tf.function puts restrictions on Variable creation thus one cannot freely create a new random generator instance inside tf.function. To call set_global_generator inside tf.function, the generator instance must have already been created eagerly.
  • tf.function captures the Variable during trace-compilation, thus a compiled f.function will not be affected set_global_generator as demonstrated by random_test.py/RandomTest.testResetGlobalGeneratorBadWithDefun .

For most use cases, avoid calling set_global_generator after program initialization, and prefer to reset the state of the existing global generator instead, such as,

rng = tf.random.get_global_generator()
rng.reset_from_seed(30)

Args

generator the new Generator object.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2024-04-26 UTC.