Reproducibility
Keras
TensorFlow
Machine Learning
Deep Learning

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.

Practice ML system design

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.

python
1import keras
2import tensorflow as tf
3
4keras.utils.set_random_seed(812)
5tf.config.experimental.enable_op_determinism()

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

python
1import keras
2import numpy as np
3import tensorflow as tf
4
5keras.utils.set_random_seed(812)
6tf.config.experimental.enable_op_determinism()
7
8x = np.random.randn(100, 10).astype("float32")
9y = np.random.randn(100, 1).astype("float32")
10
11model = keras.Sequential([
12    keras.layers.Dense(32, activation="relu", input_shape=(10,)),
13    keras.layers.Dense(1),
14])
15
16model.compile(optimizer="adam", loss="mse")
17model.fit(x, y, epochs=3, batch_size=16, shuffle=False, verbose=0)
18
19print(model.get_weights()[0][0, 0])

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.

python
1import tensorflow as tf
2
3dataset = tf.data.Dataset.from_tensor_slices((x, y))
4dataset = dataset.shuffle(buffer_size=100, seed=812, reshuffle_each_iteration=False)
5dataset = dataset.batch(16)

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:

  1. fix seeds
  2. enable deterministic behavior
  3. control data order
  4. 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
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.