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.
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.
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.
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.
If you forget one of those layers, the run may still drift even though TensorFlow itself is seeded correctly.
Dataset order matters too:
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
- What is a tensorflow session actually?
- What is an epoch in TensorFlow?
- What is Bazel in TensorFlow? When do I need to build again?
- What is difference between tf.truncated_normal and tf.random_normal?
- What is a term-vector algorithm?
- What is a threshold in a Precision-Recall curve?
- What is difference between tf.truncated_normal and tf.random_normal?
- What is difference frozen_inference_graph.pb and saved_model.pb?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.