tf.random_uniform: Create TensorFlow Tensor With Random Uniform Distribution

Use TensorFlow's random_uniform operation to create a TensorFlow Tensor with a random uniform distribution

Use TensorFlow's random_uniform operation to create a TensorFlow Tensor with a random uniform distribution

Video Transcript


This video will show you how to use TensorFlow’s random uniform operation to create a TensorFlow tensor with a random uniform distribution.


First, we import TensorFlow as tf.

import tensorflow as tf


Next, we print out what version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.10.0.


Let’s start out by creating our tensor with generated values pulled from a random uniform distribution.

random_uniform_example = tf.random_uniform([2, 3, 4])

So we use tf.random_uniform, and we pass in a list that signifies the dimensional structure of our tensor, so 2x3x4, and we assign all of this to the Python variable random_uniform_example.

Note that since we didn’t specify data type, or dtype, it’s going to use 32-bit floating point numbers.

Also, since we didn’t specify a minimum or a maximum for the distribution draw, it’s going to assume an inclusive minimum of 0 and a maximum of 1, which doesn’t include the actual number 1


Let’s now print our random uniform tensor to see what we have.

print(random_uniform_example)

We see that it’s a TensorFlow tensor, we see that TensorFlow gave it the name random_uniform, the shape is 2x3x4 which is how we initialized it, and the data type is float32.


Now that we have created our TensorFlow tensor, it’s time to run the computational graph.


So we launch the graph in a session.

sess = tf.Session()


Then we initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


Next, let’s print our random_uniform_example tensor within a TensorFlow session.

print(sess.run(random_uniform_example))

So print sess.run random_uniform_example, and we can see our tensor.

We can see that all of the numbers are floating point numbers.

From a visual inspection, none of the numbers are smaller than 0 and none of the numbers are bigger than the number 1.


Instead of doing a visual inspection, let’s programmatically get the minimum number found in our random_uniform_example tensor using tf.reduce_min operation.

print(sess.run(tf.reduce_min(random_uniform_example)))

So that will return the smallest number inside of the tensor.

And we see that it is in fact bigger than the number 0.


Let’s also programmatically get the maximum number found inside of our random_uniform tensor example using tf.reduce_max operation.

print(sess.run(tf.reduce_max(random_uniform_example)))

This will return the maximum number found in the tensor.

We see that the maximum number in the tensor is in fact less than the number 1.


Perfect - We were able to use TensorFlow’s random uniform operation to create a TensorFlow tensor with a random uniform distribution.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.