Generate TensorFlow Tensor full of random numbers in a given range by using TensorFlow's random_uniform operation

Generate TensorFlow Tensor full of random numbers in a given range by using TensorFlow's random_uniform operation

First, we import TensorFlow as tf.

```
import tensorflow as tf
```

Then we print out the TensorFlow version we are using.

```
print(tf.__version__)
```

We are using TensorFlow 1.5.0.

In this video, we’re going to generate two example TensorFlow tensors full of random numbers in a given range by using tf.random_uniform operation.

For the first example, we’ll generate random float32 numbers.

```
tf_ru_float_ex = tf.random_uniform([2, 4, 6], minval=0, maxval=1, dtype=tf.float32)
```

We create a 2x4x6, with a data type float32, the min value is 0, the max value is 1, and we’re using tf.random_uniform, and we’re going to assign it to the Python variable tf_ru_float_ex.

Note that though we specify a min value and max value, the TensorFlow defaults are zero for the min value and one for the max value if we’re using the float32.

The other thing to take into account is that when we do the random uniform, the min value is included in the range while the max value is not included in the range.

Let’s print out the tf_ru_float_ex Python variable to see what we have.

```
print(tf_ru_float_ex)
```

We see that it’s a TensorFlow tensor, we see that the shape is 2x4x6, and we see the data type is float32.

Because we haven’t run it in a TensorFlow session yet, it doesn’t have any values attached to it.

For the second example, we’ll generate random int32 numbers.

```
tf_ru_int_ex = tf.random_uniform([2, 4, 6], minval=0, maxval=100, dtype=tf.int32)
```

So we’re going to use the tf.random_uniform, the shape we want is 2x4x6, the min value is going to be 0, the max value is going to be 100, and this time the data type is tf.int32, and we’re going to assign it to the Python variable tf_ru_int_ex.

Let’s print this variable we just created.

```
print(tf_ru_int_ex)
```

We see that it’s a TensorFlow tensor, the shape is 2x4x6, and the data type is int32.

Like the previous example, because we haven’t run it in a TensorFlow session, it doesn’t have any values yet.

All right, now that we’ve created our TensorFlow tensors, it’s time to run the computational graph.

Let’s launch the graph in a session

```
sess = tf.Session()
```

And let’s initialize all the global variables in the graph.

```
sess.run(tf.global_variables_initializer())
```

Next, we print out our two tensors to see how tf.random_uniform operation did.

Let’s now print out our first TensorFlow tensor.

```
print(sess.run(tf_ru_float_ex))
```

Great! We see that it is a two by one, two, three, four by one, two, three, four, five, six.

That’s what we expect.

We see that all the numbers are floating point numbers, and we see that the tensor’s full of random numbers pulled from the random uniform distribution with a minimum value of zero and a max value of one.

So none of the numbers are below zero and none of the numbers are above the number one.

Let’s now print our second TensorFlow tensor example.

```
print(sess.run(tf_ru_int_ex))
```

We see, again, that it is a 2x4x6 tensor.

We see that the numbers are all integer numbers, and we see that the tensor’s full of random numbers pulled from the random uniform distribution, with a minimum value of zero, we see a zero here, and a max value of 100.

So none of the numbers are below zero; none of the numbers are above 100.

Perfect! It worked.

We were able to generate a TensorFlow tensor full or random numbers in a given range by using the tf.random_uniform operation.

Receive the Data Science Weekly Newsletter every Thursday

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form.

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