Random Tensors

TensorFlow has several ops that create random tensors with different distributions. The random ops are stateful, and create new random values each time they are evaluated.

The seed keyword argument in these functions acts in conjunction with the graph-level random seed. Changing either the graph-level seed using set_random_seed or the op-level seed will change the underlying seed of these operations. Setting neither graph-level nor op-level seed, results in a random seed for all operations. See set_random_seed for details on the interaction between operation-level and graph-level random seeds.

Examples:

# Create a tensor of shape [2, 3] consisting of random normal values, with mean
# -1 and standard deviation 4.
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# Shuffle the first dimension of a tensor
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# Each time we run these ops, different results are generated
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))

# Set an op-level seed to generate repeatable sequences across sessions.
norm = tf.random_normal([2, 3], seed=1234)
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))

Another common use of random values is the initialization of variables. Also see the Variables How To.

# Use random uniform values in [0, 1) as the initializer for a variable of shape
# [2, 3]. The default type is float32.
var = tf.Variable(tf.random_uniform([2, 3]), name="var")
init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)
print(sess.run(var))

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

Outputs random values from a normal distribution.

Args:
  • shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  • mean: A 0-D Tensor or Python value of type dtype. The mean of the normal distribution.
  • stddev: A 0-D Tensor or Python value of type dtype. The standard deviation of the normal distribution.
  • dtype: The type of the output.
  • seed: A Python integer. Used to create a random seed for the distribution. See set_random_seed for behavior.
  • name: A name for the operation (optional).
Returns:

A tensor of the specified shape filled with random normal values.


tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

Outputs random values from a truncated normal distribution.

The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.

Args:
  • shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  • mean: A 0-D Tensor or Python value of type dtype. The mean of the truncated normal distribution.
  • stddev: A 0-D Tensor or Python value of type dtype. The standard deviation of the truncated normal distribution.
  • dtype: The type of the output.
  • seed: A Python integer. Used to create a random seed for the distribution. See set_random_seed for behavior.
  • name: A name for the operation (optional).
Returns:

A tensor of the specified shape filled with random truncated normal values.


tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)

Outputs random values from a uniform distribution.

The generated values follow a uniform distribution in the range [minval, maxval). The lower bound minval is included in the range, while the upper bound maxval is excluded.

For floats, the default range is [0, 1). For ints, at least maxval must be specified explicitly.

In the integer case, the random integers are slightly biased unless maxval - minval is an exact power of two. The bias is small for values of maxval - minval significantly smaller than the range of the output (either 2**32 or 2**64).

Args:
  • shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  • minval: A 0-D Tensor or Python value of type dtype. The lower bound on the range of random values to generate. Defaults to 0.
  • maxval: A 0-D Tensor or Python value of type dtype. The upper bound on the range of random values to generate. Defaults to 1 if dtype is floating point.
  • dtype: The type of the output: float32, float64, int32, or int64.
  • seed: A Python integer. Used to create a random seed for the distribution. See set_random_seed for behavior.
  • name: A name for the operation (optional).
Returns:

A tensor of the specified shape filled with random uniform values.

Raises:
  • ValueError: If dtype is integral and maxval is not specified.

tf.random_shuffle(value, seed=None, name=None)

Randomly shuffles a tensor along its first dimension.

The tensor is shuffled along dimension 0, such that each value[j] is mapped to one and only one output[i]. For example, a mapping that might occur for a 3x2 tensor is:

[[1, 2],       [[5, 6],
 [3, 4],  ==>   [1, 2],
 [5, 6]]        [3, 4]]
Args:
  • value: A Tensor to be shuffled.
  • seed: A Python integer. Used to create a random seed for the distribution. See set_random_seed for behavior.
  • name: A name for the operation (optional).
Returns:

A tensor of same shape and type as value, shuffled along its first dimension.


tf.random_crop(value, size, seed=None, name=None)

Randomly crops a tensor to a given size.

Slices a shape size portion out of value at a uniformly chosen offset. Requires value.shape >= size.

If a dimension should not be cropped, pass the full size of that dimension. For example, RGB images can be cropped with size = [crop_height, crop_width, 3].

Args:
  • value: Input tensor to crop.
  • size: 1-D tensor with size the rank of value.
  • seed: Python integer. Used to create a random seed. See set_random_seed for behavior.
  • name: A name for this operation (optional).
Returns:

A cropped tensor of the same rank as value and shape size.


tf.multinomial(logits, num_samples, seed=None, name=None)

Draws samples from a multinomial distribution.

Example:

# samples has shape [1, 5], where each value is either 0 or 1 with equal
# probability.
samples = tf.multinomial(tf.log([[10., 10.]]), 5)
Args:
  • logits: 2-D Tensor with shape [batch_size, num_classes]. Each slice [i, :] represents the unnormalized log probabilities for all classes.
  • num_samples: 0-D. Number of independent samples to draw for each row slice.
  • seed: A Python integer. Used to create a random seed for the distribution. See set_random_seed for behavior.
  • name: Optional name for the operation.
Returns:

The drawn samples of shape [batch_size, num_samples].


tf.random_gamma(shape, alpha, beta=None, dtype=tf.float32, seed=None, name=None)

Draws shape samples from each of the given Gamma distribution(s).

alpha is the shape parameter describing the distribution(s), and beta is the inverse scale parameter(s).

Example:

samples = tf.random_gamma([10], [0.5, 1.5]) # samples has shape [10, 2], where each slice [:, 0] and [:, 1] represents # the samples drawn from each distribution

samples = tf.random_gamma([7, 5], [0.5, 1.5]) # samples has shape [7, 5, 2], where each slice [:, :, 0] and [:, :, 1] # represents the 7x5 samples drawn from each of the two distributions

samples = tf.random_gamma([30], [[1.],[3.],[5.]], beta=[[3., 4.]]) # samples has shape [30, 3, 2], with 30 samples each of 3x2 distributions.

Args:
  • shape: A 1-D integer Tensor or Python array. The shape of the output samples to be drawn per alpha/beta-parameterized distribution.
  • alpha: A Tensor or Python value or N-D array of type dtype. alpha provides the shape parameter(s) describing the gamma distribution(s) to sample. Must be broadcastable with beta.
  • beta: A Tensor or Python value or N-D array of type dtype. Defaults to 1. beta provides the inverse scale parameter(s) of the gamma distribution(s) to sample. Must be broadcastable with alpha.
  • dtype: The type of alpha, beta, and the output: float16, float32, or float64.
  • seed: A Python integer. Used to create a random seed for the distributions. See set_random_seed for behavior.
  • name: Optional name for the operation.
Returns:
  • samples: a Tensor of shape tf.concat(shape, tf.shape(alpha + beta)) with values of type dtype.

tf.set_random_seed(seed)

Sets the graph-level random seed.

Operations that rely on a random seed actually derive it from two seeds: the graph-level and operation-level seeds. This sets the graph-level seed.

Its interactions with operation-level seeds is as follows:

  1. If neither the graph-level nor the operation seed is set: A random seed is used for this op.
  2. If the graph-level seed is set, but the operation seed is not: The system deterministically picks an operation seed in conjunction with the graph-level seed so that it gets a unique random sequence.
  3. If the graph-level seed is not set, but the operation seed is set: A default graph-level seed and the specified operation seed are used to determine the random sequence.
  4. If both the graph-level and the operation seed are set: Both seeds are used in conjunction to determine the random sequence.

To illustrate the user-visible effects, consider these examples:

To generate different sequences across sessions, set neither graph-level nor op-level seeds:

a = tf.random_uniform([1])
b = tf.random_normal([1])

print("Session 1")
with tf.Session() as sess1:
  print(sess1.run(a))  # generates 'A1'
  print(sess1.run(a))  # generates 'A2'
  print(sess1.run(b))  # generates 'B1'
  print(sess1.run(b))  # generates 'B2'

print("Session 2")
with tf.Session() as sess2:
  print(sess2.run(a))  # generates 'A3'
  print(sess2.run(a))  # generates 'A4'
  print(sess2.run(b))  # generates 'B3'
  print(sess2.run(b))  # generates 'B4'

To generate the same repeatable sequence for an op across sessions, set the seed for the op:

a = tf.random_uniform([1], seed=1)
b = tf.random_normal([1])

# Repeatedly running this block with the same graph will generate the same
# sequence of values for 'a', but different sequences of values for 'b'.
print("Session 1")
with tf.Session() as sess1:
  print(sess1.run(a))  # generates 'A1'
  print(sess1.run(a))  # generates 'A2'
  print(sess1.run(b))  # generates 'B1'
  print(sess1.run(b))  # generates 'B2'

print("Session 2")
with tf.Session() as sess2:
  print(sess2.run(a))  # generates 'A1'
  print(sess2.run(a))  # generates 'A2'
  print(sess2.run(b))  # generates 'B3'
  print(sess2.run(b))  # generates 'B4'

To make the random sequences generated by all ops be repeatable across sessions, set a graph-level seed:

tf.set_random_seed(1234)
a = tf.random_uniform([1])
b = tf.random_normal([1])

# Repeatedly running this block with the same graph will generate different
# sequences of 'a' and 'b'.
print("Session 1")
with tf.Session() as sess1:
  print(sess1.run(a))  # generates 'A1'
  print(sess1.run(a))  # generates 'A2'
  print(sess1.run(b))  # generates 'B1'
  print(sess1.run(b))  # generates 'B2'

print("Session 2")
with tf.Session() as sess2:
  print(sess2.run(a))  # generates 'A1'
  print(sess2.run(a))  # generates 'A2'
  print(sess2.run(b))  # generates 'B1'
  print(sess2.run(b))  # generates 'B2'
Args:
  • seed: integer.