Elena' s AI Blog

TensorFlow: Global and Operation-level Seeds

15 Jan 2022 (updated: 29 Dec 2025) / 10 minutes to read

Elena Daehnhardt


Coding robot illustration


TL;DR:
  • Set tf.random.set_seed() for reproducibility across runs. Use operation-level seeds for specific operations. Essential for cross-validation and consistent results between experiments.

Previous: Part 7 β€” TensorFlow: Regression Model

Next: Part 9 β€” TensorFlow: Evaluating the Regression Model

Reproducible Randomness in TensorFlow: Why Seeds Matter

A random seed is an integer that initializes a pseudo-random number generator so it produces the same sequence of values on every run. When training Machine Learning models, we want to avoid ordering biases in the data while keeping that data order identical between runs or system restarts β€” for example, in Cross-Validation experiments. TensorFlow provides two seed types, global and operation-level, to achieve reproducibility of results (tf.random.set_seed documentation).

Global vs Operation-level Seeds in TensorFlow

TensorFlow defines two kinds of seeds, and their interaction determines reproducibility:

Seed type Set with Scope
Global seed tf.random.set_seed(value) All random operations in the session
Operation-level seed seed= argument, e.g. tf.random.shuffle(tensor, seed=value) A single operation

To begin, let’s create a mutable tensor with tf.Variable.

# Create a variable tensor
tensor = tf.Variable([[[1, 2, 3],
                       [4, 5, 6]],
                      [[7, 8, 9],
                       [10, 11, 12]],
                      [[13, 14, 15],
                       [16, 17, 18]]])

In the code below, we use assign method to change the first element (which is a matrice) in tensor. We fillled its values with zeros.

# Change elements of the first tensor element
tensor[0].assign([[0, 0, 0], [0, 0, 0]])
<tf.Variable 'UnreadVariable' shape=(3, 2, 3) dtype=int32, numpy=
array([[[ 0,  0,  0],
        [ 0,  0,  0]],

       [[ 7,  8,  9],
        [10, 11, 12]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32)>

In TensorFlow, we have global and operation-level seeds.

The global seed we define with set_seed:

# Set a random seed with value of 57
tf.random.set_seed(57)

The operation-level we can define directly in operation such as when shuffling the tensor

tf.random.shuffle(tensor, seed=57)

The following sections cover all four combinations of these two seed types and whether each one guarantees reproducible results.

Scenario 1: No Seeds Defined (Not Reproducible)

When neither a global nor an operation-level seed is defined, TensorFlow chooses an arbitrary seed from its pool. The result is different values every time the code re-runs, so this scenario does not guarantee any reproducibility of results.

tf.random.shuffle(tensor)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 7,  8,  9],
        [10, 11, 12]],

       [[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32)>

The second run changes the data order:

tf.random.shuffle(tensor)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 7,  8,  9],
        [10, 11, 12]],

       [[13, 14, 15],
        [16, 17, 18]],

       [[ 0,  0,  0],
        [ 0,  0,  0]]], dtype=int32) >

The data order will differ from the first run when we restart the environment.

tf.random.shuffle(tensor)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 0,  0,  0],
        [ 0,  0,  0]],

       [[ 7,  8,  9],
        [10, 11, 12]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32) >
tf.random.shuffle(tensor)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]],

       [[ 7,  8,  9],
        [10, 11, 12]]], dtype=int32) >

Scenario 2: Only Global Seed Defined

We can define just the global seed with the tf.random.set_seed() function. In this case, TensorFlow combines the defined global seed with an arbitrary operation seed. After a restart, the same shuffled data order is reproduced when we re-run the code.

tf.random.set_seed(seed=57)
tf.random.shuffle(tensor)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]],

       [[ 7,  8,  9],
        [10, 11, 12]]], dtype=int32) >
tf.random.shuffle(tensor)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 7,  8,  9],
        [10, 11, 12]],

       [[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32) >

Thus, we see precisely the same order of data between restarts.

Scenario 3: Only Operation-level Seed Defined

When defining only an operation-level seed value, TensorFlow uses a default global seed plus the defined operation seed. Rerunning the code produces the same results, and restarting produces the same sequence of results.

tf.random.shuffle(tensor, seed=24)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 7,  8,  9],
        [10, 11, 12]],

       [[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32) >
tf.random.shuffle(tensor, seed=24)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]],

       [[ 7,  8,  9],
        [10, 11, 12]]], dtype=int32) >

Scenario 4: Both Seeds Defined (Fully Reproducible)

Defining both a global seed and an operation-level seed is the best case for reproducible randomness. With both set, TensorFlow produces the same sequence of results across reruns and after restarting the environment. This is the recommended configuration for cross-validation and experiment comparison.

tf.random.set_seed(seed=57)
tf.random.shuffle(tensor, seed=75)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[13, 14, 15],
        [16, 17, 18]],

       [[ 7,  8,  9],
        [10, 11, 12]],

       [[ 0,  0,  0],
        [ 0,  0,  0]]], dtype=int32)>
tf.random.shuffle(tensor, seed=75)
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 7,  8,  9],
        [10, 11, 12]],

       [[ 0,  0,  0],
        [ 0,  0,  0]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32)>

Did you like this post? Please let me know if you have any comments or suggestions.

Python posts that might be interesting for you



Summary: Choosing the Right Seed for Reproducibility

Reproducible randomness in TensorFlow depends on which seeds you set. Setting both a global seed (tf.random.set_seed()) and an operation-level seed= argument is the only configuration that guarantees the same sequence of results across reruns and restarts β€” the requirement for fair cross-validation and experiment comparison. For the official API reference, see the tf.random.set_seed documentation.

For writing this post, I have used the TensorFlow documentation and tutorials at Udemy, TensorFlow Developer Certificate in 2022: Zero to Mastery.

TensorFlow Seeds FAQ

What is the difference between a global seed and an operation-level seed in TensorFlow?

A global seed, set with tf.random.set_seed(value), applies to every random operation in the session, while an operation-level seed, passed directly as the seed= argument of a function like tf.random.shuffle(tensor, seed=value), applies only to that single operation. TensorFlow combines both seeds to determine the random sequence.

How do I make tf.random.shuffle reproducible across restarts?

Set both a global seed and an operation-level seed: call tf.random.set_seed(57) once, then pass seed= to each operation, e.g. tf.random.shuffle(tensor, seed=75). This is the only one of the four seed scenarios that guarantees the same sequence of results across reruns and environment restarts.

Why does tf.random.shuffle give different results every run?

If no seed is set, TensorFlow picks an arbitrary seed from its pool, so tf.random.shuffle(tensor) returns a different order on every run and after every restart. Set a global seed with tf.random.set_seed() and an operation-level seed= argument to fix the order.

Why is reproducible randomness important in machine learning?

Reproducible randomness lets you compare experiments fairly: cross-validation, hyperparameter tuning, and debugging all require that data shuffling and weight initialization produce identical results between runs, so any change in performance comes from the model, not from random variation.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2022) 'TensorFlow: Global and Operation-level Seeds', daehnhardt.com, 15 January 2022. Available at: https://daehnhardt.com/blog/2022/01/15/tf-seeds/
All Posts