In [0]:
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
TensorFlow provides a set of pseudo-random number generators (RNG), in the tf.random
module. This document describes how you can control the random number generators, and how these generators interact with other tensorflow sub-systems.
TensorFlow provides two approaches for controlling the random number generation process:
Through the explicit use of tf.random.Generator
objects. Each such object maintains a state (in tf.Variable
) that will be changed after each number generation.
Through the purely-functional stateless random functions like tf.random.stateless_uniform
. Calling these functions with the same arguments (which include the seed) and on the same device will always produce the same results.
Warning: The old RNGs from TF 1.x such as tf.random.uniform
and tf.random.normal
are not yet deprecated but strongly discouraged.
Warning: The random numbers are not guaranteed to be consistent across TensorFlow versions, see: Version Compatibility
In [0]:
import tensorflow as tf
# Creates 2 virtual devices cpu:0 and cpu:1 for using distribution strategy
physical_devices = tf.config.experimental.list_physical_devices("CPU")
tf.config.experimental.set_virtual_device_configuration(
physical_devices[0], [
tf.config.experimental.VirtualDeviceConfiguration(),
tf.config.experimental.VirtualDeviceConfiguration()
])
tf.random.Generator
classThe tf.random.Generator
class is used in cases where you want each RNG call to produce different results. It maintains an internal state (managed by a tf.Variable
object) which will be updated every time random numbers are generated. 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.
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:
In [0]:
g1 = tf.random.Generator.from_seed(1)
print(g1.normal(shape=[2, 3]))
g2 = tf.random.get_global_generator()
print(g2.normal(shape=[2, 3]))
There are multiple ways to create a generator object. The easiest is Generator.from_seed
, as shown above, that creates a generator from a seed. A seed is any non-negative integer. from_seed
also takes an optional argument alg
which is the RNG algorithm that will be used by this generator:
In [0]:
g1 = tf.random.Generator.from_seed(1, alg='philox')
print(g1.normal(shape=[2, 3]))
See the Algorithms section below for more information about it.
Another way to create a generator is with Generator.from_non_deterministic_state
. A generator created this way will start from a non-deterministic state, depending on e.g. time and OS.
In [0]:
g = tf.random.Generator.from_non_deterministic_state()
print(g.normal(shape=[2, 3]))
There are yet other ways to create generators, such as from explicit states, which are not covered by this guide.
When using tf.random.get_global_generator
to get the global generator, you need to be careful about device placement. The global generator is created (from a non-deterministic state) at the first time tf.random.get_global_generator
is called, and placed on the default device at that call. So, for example, if the first site you call tf.random.get_global_generator
is within a tf.device("gpu")
scope, the global generator will be placed on the GPU, and using the global generator later on from the CPU will incur a GPU-to-CPU copy.
There is also a function tf.random.set_global_generator
for replacing the global generator with another generator object. This function should be used with caution thought, because the old global generator may have been captured by a tf.function
(as a weak reference), and replacing it will cause it to be garbage collected, breaking the tf.function
. A better way to reset the global generator is to use one of the "reset" functions such as Generator.reset_from_seed
, which won't create new generator objects.
In [0]:
g = tf.random.Generator.from_seed(1)
print(g.normal([]))
print(g.normal([]))
g.reset_from_seed(1)
print(g.normal([]))
In many applications one needs multiple independent random-number streams, independent in the sense that they won't overlap and won't have any statistically detectable correlations. This is achieved by using Generator.split
to create multiple generators that are guaranteed to be independent of each other (i.e. generating independent streams).
In [0]:
g = tf.random.Generator.from_seed(1)
print(g.normal([]))
new_gs = g.split(3)
for new_g in new_gs:
print(new_g.normal([]))
print(g.normal([]))
split
will change the state of the generator on which it is called (g
in the above example), similar to an RNG method such as normal
. In addition to being independent of each other, the new generators (new_gs
) are also guaranteed to be independent of the old one (g
).
Spawning new generators is also useful when you want to make sure the generator you use is on the same device as other computations, to avoid the overhead of cross-device copy. For example:
In [0]:
with tf.device("cpu"): # change "cpu" to the device you want
g = tf.random.get_global_generator().split(1)[0]
print(g.normal([])) # use of g won't cause cross-device copy, unlike the global generator
Note: In theory, you can use constructors such as from_seed
instead of split
here to obtain a new generator, but by doing so you lose the guarantee that the new generator is independent of the global generator. You will also run the risk that you may accidentally create two generators with the same seed or with seeds that lead to overlapping random-number streams.
You can do splitting recursively, calling split
on splitted generators. There are no limits (barring integer overflow) on the depth of recursions.
In [0]:
g = tf.random.Generator.from_seed(1)
@tf.function
def foo():
return g.normal([])
print(foo())
The user needs to make sure that the generator object is still alive (not garbage-collected) when the function is called.
In [0]:
g = None
@tf.function
def foo():
global g
if g is None:
g = tf.random.Generator.from_seed(1)
return g.normal([])
print(foo())
print(foo())
In [0]:
num_traces = 0
@tf.function
def foo(g):
global num_traces
num_traces += 1
return g.normal([])
foo(tf.random.Generator.from_seed(1))
foo(tf.random.Generator.from_seed(2))
print(num_traces)
In [0]:
g = tf.random.Generator.from_seed(1)
strat = tf.distribute.MirroredStrategy(devices=["cpu:0", "cpu:1"])
with strat.scope():
def f():
print(g.normal([]))
results = strat.run(f)
Note that this usage may have performance issues because the generator's device is different from the replicas.
Creating generators inside strategy scopes is disallowed, because there is ambiguity on how to replicate a generator (e.g. should it be copied so that each replica gets the same random numbers, or 'split' so that each replica gets different random numbers).
In [0]:
strat = tf.distribute.MirroredStrategy(devices=["cpu:0", "cpu:1"])
with strat.scope():
try:
tf.random.Generator.from_seed(1)
except ValueError as e:
print("ValueError:", e)
Note that Strategy.run
will run its argument function in a strategy scope implicitly:
In [0]:
strat = tf.distribute.MirroredStrategy(devices=["cpu:0", "cpu:1"])
def f():
tf.random.Generator.from_seed(1)
try:
strat.run(f)
except ValueError as e:
print("ValueError:", e)
In [0]:
strat = tf.distribute.MirroredStrategy(devices=["cpu:0", "cpu:1"])
gs = tf.random.get_global_generator().split(2)
# to_args is a workaround for the absence of APIs to create arguments for
# run. It will be replaced when such APIs are available.
def to_args(gs):
with strat.scope():
def f():
return [gs[tf.distribute.get_replica_context().replica_id_in_sync_group]]
return strat.run(f)
args = to_args(gs)
def f(g):
print(g.normal([]))
results = strat.run(f, args=args)
In [0]:
print(tf.random.stateless_normal(shape=[2, 3], seed=[1, 2]))
print(tf.random.stateless_normal(shape=[2, 3], seed=[1, 2]))
Every stateless RNG requires a seed
argument, which needs to be an integer Tensor of shape [2]
. The results of the op are fully determined by this seed.
Both the tf.random.Generator
class and the stateless
functions support the Philox algorithm (written as "philox"
or tf.random.Algorithm.PHILOX
) on all devices.
Different devices will generate the same integer numbers, if using the same algorithm and starting from the same state. They will also generate "almost the same" float-point numbers, though there may be small numerical discrepancies caused by the different ways the devices carry out the float-point computation (e.g. reduction order).
See paper 'Parallel Random Numbers: As Easy as 1, 2, 3' for more details about these algorithms.