
When using tf.random.get_global_generator to get the global generator, you need to be careful about device placement. There are yet other ways to create generators, such as from explicit states, which are not covered by this guide. A generator created this way will start from a non-deterministic state, depending on e.g. See the Algorithms section below for more information about it.Īnother way to create a generator is with om_non_deterministic_state. from_seed also takes an optional argument alg which is the RNG algorithm that will be used by this generator: g1 = tf._seed(1, alg='philox') The easiest is om_seed, as shown above, that creates a generator from a seed. There are multiple ways to create a generator object. You can get a tf.random.Generator by manually creating an object of the class or call tf.random.get_global_generator() to get the default global generator: g1 = tf._seed(1)

Because the state is managed by tf.Variable, it enjoys all facilities provided by tf.Variable such as easy checkpointing, automatic control-dependency and thread safety. It maintains an internal state (managed by a tf.Variable object) which will be updated every time random numbers are generated. The tf.random.Generator class is used in cases where you want each RNG call to produce different results. Physical_devices = tf.config.list_physical_devices("CPU") # Creates some virtual devices (cpu:0, cpu:1, etc.) for using distribution strategy Warning: The old RNGs from TF 1.x such as tf.random.uniform and tf.random.normal are not yet deprecated but strongly discouraged. Calling these functions with the same arguments (which include the seed) and on the same device will always produce the same results. Through the purely-functional stateless random functions like tf.random.stateless_uniform. Each such object maintains a state (in tf.Variable) that will be changed after each number generation. Through the explicit use of tf.random.Generator objects.

TensorFlow provides two approaches for controlling the random number generation process: Note: The random numbers are not guaranteed to be consistent across TensorFlow versions. This document describes how you can control the random number generators, and how these generators interact with other tensorflow sub-systems. TensorFlow provides a set of pseudo-random number generators (RNG), in the tf.random module.
