How do you make a random tensor?

We have seen two methods to create a tensor. But how do we make a random tensor? Here the word random stands for shuffling the orders.

Let’s jump in and write some code to see how we can make random tensor first.

After that we will see how we can shuffle them.

import tensorflow as tf


# Creating two random but the same tensors
random_one = tf.random.Generator.from_seed(42) 
random_one = random_one.normal(shape=(3, 2)) 
random_two = tf.random.Generator.from_seed(42)
random_two = random_two.normal(shape=(3, 2))

random_one, random_two, random_one == random_two

We have created two random tensors. But they are equal.

How does that happen?

Firstly, the seed method stands for reproducibility. 

That means, each time we run the code, we get the same output.

(<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
 array([[-0.7565803 , -0.06854702],
        [ 0.07595026, -1.2573844 ],
        [-0.23193765, -1.8107855 ]], dtype=float32)>,
 <tf.Tensor: shape=(3, 2), dtype=float32, numpy=
 array([[-0.7565803 , -0.06854702],
        [ 0.07595026, -1.2573844 ],
        [-0.23193765, -1.8107855 ]], dtype=float32)>,
 <tf.Tensor: shape=(3, 2), dtype=bool, numpy=
 array([[ True,  True],
        [ True,  True],
        [ True,  True]])>)

Open the Google Colab and find how it works. 

Anyway, the shape is the same. And the data type is also the same.

As a result, we can expect that they might come out as identical.

The random tensors we’ve made are pseudorandom numbers.

But in reality they are not.

For example we can change the seed and see how they differ. Although the shape is the same.

# Creating two random but different tensors
random_three = tf.random.Generator.from_seed(42)
random_three = random_three.normal(shape=(3, 2))
random_four = tf.random.Generator.from_seed(11)
random_four = random_four.normal(shape=(3, 2))


random_three, random_four, random_one == random_three, random_three == random_four

As we see, from random one to random three, we have maintained the same order.

But the fourth tensor uses a different seed().

As an outcome, we can see that the first three tensors are identical.

But when we check whether the fourth one is equal or not, it says, false.

(<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
 array([[-0.7565803 , -0.06854702],
        [ 0.07595026, -1.2573844 ],
        [-0.23193763, -1.8107855 ]], dtype=float32)>,
 <tf.Tensor: shape=(3, 2), dtype=float32, numpy=
 array([[ 0.27305737, -0.29925638],
        [-0.3652325 ,  0.61883307],
        [-1.0130816 ,  0.28291714]], dtype=float32)>,
 <tf.Tensor: shape=(3, 2), dtype=bool, numpy=
 array([[ True,  True],
        [ True,  True],
        [ True,  True]])>,
 <tf.Tensor: shape=(3, 2), dtype=bool, numpy=
 array([[False, False],
        [False, False],
        [False, False]])>)

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

Twitter


Posted

in

, ,

by

Comments

Leave a Reply