How to Get Reproducible Results Keras, 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
Reproducibility in Keras and TensorFlow means more than just setting one random seed. Training results can change because of Python randomness, NumPy randomness, TensorFlow ops, dataset shuffling, and hardware-level nondeterminism, so you usually need several controls at once.
Start by seeding every layer of randomness
A good baseline setup looks like this:
This handles:
- Python's built-in random module
- NumPy random operations
- TensorFlow and Keras random state
- deterministic execution for supported TensorFlow ops
If you skip one of these, reproducibility can still drift.
Make the data pipeline deterministic
Even with model seeds fixed, the input pipeline can reintroduce randomness. For example, shuffling a dataset without a fixed seed means batches arrive in a different order.
The important flag here is reshuffle_each_iteration=False. Without it, each epoch gets a different order even when a seed is present.
Build the same model the same way
Layer initialization also depends on randomness. If you seed properly before model creation, repeated runs are much more likely to match.
Build and compile the model after setting seeds, not before.
A minimal reproducible training example
Notice that shuffle=False is set in fit. If you enable shuffling without controlling it elsewhere, results can differ.
GPU and hardware caveats still matter
Deterministic settings improve repeatability, but some operations and environments are still sensitive to hardware, driver versions, and backend differences. A run on one GPU model may not be bit-for-bit identical to a run on another machine even with the same seed.
If you need the strongest possible repeatability:
- pin TensorFlow and library versions
- pin CUDA and cuDNN versions
- run on the same hardware
- avoid unsupported nondeterministic ops
For research reproducibility, environment capture is just as important as code.
Record the full experiment state
Even perfect seeds are not enough if the dataset version, preprocessing code, or dependency versions change. Save:
- the seed value
- library versions
- model code
- dataset snapshot or hash
- training hyperparameters
That turns "I set the seed" into something that can actually be reproduced later.
Know the difference between repeatable and identical
In some teams, reproducible means "the same conclusion and nearly the same metrics". In others, it means bit-for-bit identical weights and losses. TensorFlow can help with the second goal, but it is harder and more environment-sensitive. Be explicit about which standard you actually need before spending time chasing tiny nondeterministic differences.
Common Pitfalls
- Setting only
tf.random.set_seedand forgetting Python or NumPy randomness. - Shuffling datasets or training batches without controlling the shuffle seed.
- Building the model before seeding the runtime.
- Expecting exact reproducibility across different hardware and software stacks.
- Assuming deterministic ops are enabled by default in TensorFlow.
Summary
- Reproducibility in Keras and TensorFlow requires more than one seed call.
- Seed Python, NumPy, and TensorFlow together.
- Make the input pipeline deterministic, especially dataset shuffling.
- Enable deterministic TensorFlow ops where supported.
- For serious reproducibility, capture environment details as well as code and seeds.
Related reading
- How to get summary information on tensorflow `RNN`
- How to get the best model when using EarlyStopping callback in Keras?
- How to get the CUDA version?
- How to get the dimensions of a tensor in TensorFlow at graph construction time?
- How to get rid of tensorflow verbose messages with Keras
- How to get stable results with TensorFlow, setting random seed
- How to get sample weights and class weights for multi-label classification problem?
- how to get string value out of tf.tensor which dtype is string
.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.