How to get reproducible result when running Keras with Tensorflow backend
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 with a TensorFlow backend is not just a matter of setting one random seed. To get repeatable results, you need to control random number generation, deterministic operation behavior, data ordering, and sometimes even hardware or environment differences.
Start With Seed Control
Modern Keras provides a convenient way to seed the main random sources together.
keras.utils.set_random_seed(...) sets Python, NumPy, and backend-related random seeds in one step. enable_op_determinism() asks TensorFlow to prefer deterministic implementations where available.
Those two lines are the current foundation for reproducible training runs.
Why Seeds Alone Are Not Enough
A fixed seed helps with:
- weight initialization
- dropout randomness
- random layer behavior
- some shuffling behavior
But you can still get non-reproducible runs if:
- an op is nondeterministic on your hardware path
- the input pipeline changes example order
- multithreaded execution changes execution details
- the environment uses different library or driver versions
That is why seed-setting is necessary but not always sufficient.
A Small Reproducible Example
Notice the shuffle=False. If you shuffle training data differently between runs, you should expect different weight updates even with fixed seeds.
Input Pipelines Matter
The input order has to be reproducible too.
For example, with tf.data, random shuffling should be controlled explicitly.
If reshuffle_each_iteration=True, then each epoch changes the order, which may be desirable for training quality but is no longer fully reproducible in the same sense.
Deterministic Ops Come With Tradeoffs
TensorFlow's deterministic-op mode improves reproducibility, but it can reduce performance or limit certain fast execution paths. That is a reasonable tradeoff for experiments, debugging, and benchmark comparisons.
It is especially useful when you are trying to answer questions like:
- did this code change alter model quality
- did this hyperparameter matter
- can I reproduce a training run from last week
Without determinism, those comparisons get noisy quickly.
Environment Consistency Still Matters
Even with seeds and deterministic ops, exact bit-for-bit reproducibility can still depend on:
- TensorFlow version
- Keras version
- CUDA and cuDNN versions
- CPU versus GPU execution
- operating system and drivers
So reproducibility is best thought of as a layered process:
- fix seeds
- enable deterministic behavior
- control data order
- keep the software and hardware environment stable
That is the realistic answer.
Common Pitfalls
The most common mistake is setting a TensorFlow seed but forgetting Python, NumPy, or other randomness sources. keras.utils.set_random_seed(...) helps avoid that fragmented setup.
Another mistake is leaving dataset shuffling uncontrolled and then expecting repeatable results.
Developers also assume determinism is automatic on GPU. It is not; enabling deterministic ops matters.
Finally, do not overpromise reproducibility across different machines and library stacks. Exact repeatability is easiest when the environment is effectively identical.
Summary
- Use
keras.utils.set_random_seed(...)to seed the major randomness sources. - Enable deterministic TensorFlow ops with
tf.config.experimental.enable_op_determinism(). - Keep data ordering reproducible, especially when shuffling.
- Expect the environment to matter: versions, hardware, and drivers all influence repeatability.
- Reproducibility is a system-level setup, not just a one-line random seed fix.
Related reading
- How to Get Reproducible Results Keras, Tensorflow
- 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 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.