How to Create Tensors with Random Values

By Matthew Scarpino

Many TensorFlow applications require tensors that contain random values instead of predetermined values. The tf package provides many functions for creating random-valued tensors and the following table lists five of them.

Creating Tensors with Random Values

Function Description
random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) Creates a tensor with normally distributed values
truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) Creates a tensor with normally distributed values excluding those lying outside two standard deviations
random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None) Creates a tensor with uniformly distributed values between the minimum and maximum values
random_shuffle(tensor, seed=None, name=None) Shuffles a tensor along its first dimension
set_random_seed(seed) Set the seed value for all random number generation in the graph

The random_normal and truncated_normal functions create tensors containing normally distributed values. Their arguments determine the characteristics of the distribution. This figure shows what a normal distribution looks like with a mean of 0.0 and a standard deviation (σ) of 1.0.

Values beyond three standard deviations from the mean are highly unlikely.

Standard deviation tells you how much a normally distributed variable is expected to vary from the mean. Approximately 68.2 percent of the time, a variable lies within one standard deviation from the mean, while 95.4 percent of the time, the variable lies within two standard deviations.

In the random_normal and truncated_normal functions, the default mean is 0.0, and the default standard deviation is 1.0. random_normal generates random values throughout the distribution, so very large and very small values are unlikely but possible. The following code calls random_normal to generate 20 random values:

rnd_ints = tf.random_normal([10], dtype=tf.float64)

In contrast, truncated_normal guarantees that the generated values lie within two standard deviations from the mean. Any value outside this range will be discarded and reselected. In this manner, truncated_normal ensures that the tensor won’t contain any improbably large or small values.

random_uniform creates a tensor containing uniformly distributed values that lie between a minimum and maximum. Because the distribution is uniform, every value is equally likely.

random_shuffle doesn’t create a new tensor, but randomly shuffles the values in an existing tensor. This shuffling is limited to the tensor’s first dimension.

Each function in the table accepts a seed parameter that initializes the random number generator. Setting a random seed is important to ensure that sequences aren’t repeated.

You can obtain and set a seed value by calling set_random_seed, which accepts a floating-point value and makes the argument the seed for every operation in the current graph.