Machine Learning
Reproducibility
Keras
TensorFlow
Deep Learning

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.

Practice ML system design

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:

python
1import os
2import random
3import numpy as np
4import tensorflow as tf
5
6SEED = 42
7
8os.environ["PYTHONHASHSEED"] = str(SEED)
9random.seed(SEED)
10np.random.seed(SEED)
11tf.keras.utils.set_random_seed(SEED)
12tf.config.experimental.enable_op_determinism()

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.

python
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset = dataset.shuffle(buffer_size=len(x_train), seed=SEED, reshuffle_each_iteration=False)
dataset = dataset.batch(32)

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.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Dense(64, activation="relu", input_shape=(20,)),
3    tf.keras.layers.Dense(1)
4])
5
6model.compile(optimizer="adam", loss="mse")

Build and compile the model after setting seeds, not before.

A minimal reproducible training example

python
1import os
2import random
3import numpy as np
4import tensorflow as tf
5
6SEED = 42
7os.environ["PYTHONHASHSEED"] = str(SEED)
8random.seed(SEED)
9np.random.seed(SEED)
10tf.keras.utils.set_random_seed(SEED)
11tf.config.experimental.enable_op_determinism()
12
13x = np.random.rand(200, 10).astype("float32")
14y = np.random.rand(200, 1).astype("float32")
15
16model = tf.keras.Sequential([
17    tf.keras.layers.Dense(16, activation="relu", input_shape=(10,)),
18    tf.keras.layers.Dense(1),
19])
20
21model.compile(optimizer="adam", loss="mse")
22
23history = model.fit(x, y, epochs=3, batch_size=32, shuffle=False, verbose=0)
24print(history.history["loss"])

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_seed and 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
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.