Table Of Contents

Shuffle Tensor Elements

In [34]:
import tensorflow as tf
In [35]:
startingArr = [
    [10,13],
    [21,31],
    [1,9]
]

myTensor = tf.constant(startingArr)
myTensor
Out [35]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[10, 13],
       [21, 31],
       [ 1,  9]], dtype=int32)>
In [36]:
tf.random.shuffle(myTensor)
Out [36]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 1,  9],
       [21, 31],
       [10, 13]], dtype=int32)>

Shuffle With Seed

When a global seed is set, random will "always do the same thing", sort of "removing" 1 level of randomization.
Every time this notebook, or python process instance is run, the "random" outputs will remain constant.

In [37]:
tf.random.set_seed(12)
secondTensor = tf.constant(startingArr)
secondTensor
Out [37]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[10, 13],
       [21, 31],
       [ 1,  9]], dtype=int32)>
In [38]:
tf.random.shuffle(secondTensor)
Out [38]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[21, 31],
       [10, 13],
       [ 1,  9]], dtype=int32)>
In [39]:
tf.random.shuffle(secondTensor)
Out [39]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 1,  9],
       [21, 31],
       [10, 13]], dtype=int32)>

Shuffle With Global and local seed

A local seed, set in the tf.random.shuffle call as a parameter, will be combined with the global seed.
This local seed will "tell" tensorflow to create a "new" random order based on the local seed.

In [40]:
thirdTensor = tf.constant(startingArr)
thirdTensor
Out [40]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[10, 13],
       [21, 31],
       [ 1,  9]], dtype=int32)>
In [41]:
tf.random.shuffle(thirdTensor, seed=12)
Out [41]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 1,  9],
       [21, 31],
       [10, 13]], dtype=int32)>
In [42]:
tf.random.shuffle(thirdTensor, seed=13)
Out [42]:
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[21, 31],
       [10, 13],
       [ 1,  9]], dtype=int32)>

Docs