TensorFlow
seed
machine learning
randomization
duplicate

What is a seed in TensorFlow?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow, a seed is the starting value for a pseudo-random number generator. It does not remove randomness from a machine learning workflow, but it makes that randomness repeatable enough to debug a model, compare experiments, and verify that a change in results came from code rather than chance.

Core Sections

Why TensorFlow has seeds at all

Randomness shows up in many TensorFlow workflows:

  • weight initialization
  • dropout
  • dataset shuffling
  • random augmentations
  • sampling from distributions

These APIs are not producing true physical randomness. They generate deterministic sequences from an initial state. That initial state is what the seed controls.

python
1import tensorflow as tf
2
3tf.random.set_seed(123)
4
5print(tf.random.uniform((3,)))
6print(tf.random.uniform((3,)))

If you rerun the same program with the same seed and the same execution path, TensorFlow will produce the same sequence again.

Global seed versus op-level seed

TensorFlow supports a global seed and, in some APIs, a seed specific to one operation. The global seed sets the broad random stream. An operation seed locks one call more tightly.

python
1import tensorflow as tf
2
3tf.random.set_seed(123)
4
5x = tf.random.uniform((2,), seed=7)
6y = tf.random.uniform((2,), seed=7)
7
8print(x)
9print(y)

The important idea is scope. A global seed gives consistency across the program. An op seed gives consistency for that exact random op. Most projects only need a global seed unless they are testing a very specific random branch.

Reproducibility needs more than TensorFlow

TensorFlow is rarely the only source of randomness. Python, NumPy, and dataset utilities can all inject variation. That is why reproducible experiments usually set several seeds together.

python
1import random
2import numpy as np
3import tensorflow as tf
4
5random.seed(123)
6np.random.seed(123)
7tf.random.set_seed(123)

If you forget one of those layers, the run may still drift even though TensorFlow itself is seeded correctly.

Dataset order matters too:

python
1dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5])
2dataset = dataset.shuffle(
3    buffer_size=5,
4    seed=123,
5    reshuffle_each_iteration=False
6)
7
8for value in dataset:
9    print(int(value))

Without the shuffle seed, two training runs can see examples in different orders and diverge.

What a seed does not promise

A seed improves repeatability, but it is not a full determinism guarantee. GPU kernels, parallel execution, library versions, hardware differences, and nondeterministic ops can still change results. That is why two machines with the same seed may still differ slightly.

A practical reproducibility setup usually includes:

  • fixed seeds
  • pinned package versions
  • controlled hardware or backend
  • stable dataset order

So the right mental model is not "a seed makes everything identical." The right model is "a seed narrows one major source of variation."

Where seeds are most useful

Seeds are especially helpful when:

  • debugging exploding or unstable training
  • comparing two architectures fairly
  • writing tests for data pipelines
  • teaching or documenting a repeatable example

They are less important when you are intentionally running multiple random trials to estimate variability. In that case, the seed is still useful, but you may vary it on purpose across runs.

Common Pitfalls

  • Setting only the TensorFlow seed and forgetting Python, NumPy, or dataset shuffle state.
  • Assuming identical seeds guarantee identical results across all hardware and library versions.
  • Changing the number or order of random calls and expecting later values in the sequence to stay unchanged.
  • Treating a seed as a performance or accuracy optimization instead of a reproducibility control.
  • Forgetting that some ops or backends remain nondeterministic even when the seed is fixed.

Summary

  • A seed defines the starting state for TensorFlow's pseudo-random generators.
  • 'tf.random.set_seed(...) is the standard global control point.'
  • Some TensorFlow APIs also accept an op-specific seed for finer control.
  • Reproducibility usually requires seeding Python, NumPy, and data shuffling as well.
  • Seeds are necessary for repeatable experiments, but they do not eliminate every source of nondeterminism.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.