Tensorflow
set_random_seed
random_seed_issue
duplicate_question
machine_learning

Tensorflow set_random_seed not working

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

If tf.random.set_seed seems like it is not working, the usual problem is that only one source of randomness has been controlled. Reproducibility in TensorFlow depends on Python, NumPy, data pipelines, layer initialization, and sometimes deterministic GPU kernels, not just one TensorFlow seed call.

What tf.random.set_seed Actually Controls

tf.random.set_seed makes TensorFlow random ops deterministic relative to the same program and execution order. It does not automatically freeze every other library or every source of non-determinism in your training run.

That means the following can still change results:

  • Python's random module.
  • NumPy random generators.
  • Dataset shuffling.
  • Dropout and initializer behavior when the overall program order changes.
  • GPU kernels that are not deterministic by default.

So the seed is often working, but it is only covering part of the system.

Use A Full Reproducibility Setup

For modern TensorFlow projects, a better starting point is tf.keras.utils.set_random_seed, which seeds Python, NumPy, and TensorFlow together.

python
1import tensorflow as tf
2
3tf.keras.utils.set_random_seed(1234)
4tf.config.experimental.enable_op_determinism()
5
6values = tf.random.normal([3])
7print(values.numpy())

If you rerun the same script in the same environment, that output should remain stable.

For older code, you may still see tf.random.set_seed(1234), but it is only one piece of the reproducibility story.

Control Data Pipeline Randomness Too

A very common reason for "different results every run" is an unseeded input pipeline. If the dataset is shuffled differently each time, the model sees examples in a different order and training diverges even though TensorFlow's global random generator is seeded.

python
1import tensorflow as tf
2
3tf.keras.utils.set_random_seed(1234)
4tf.config.experimental.enable_op_determinism()
5
6dataset = tf.data.Dataset.from_tensor_slices(([1, 2, 3, 4], [0, 1, 0, 1]))
7dataset = dataset.shuffle(buffer_size=4, seed=1234, reshuffle_each_iteration=False)
8dataset = dataset.batch(2)
9
10for batch_x, batch_y in dataset:
11    print(batch_x.numpy(), batch_y.numpy())

The key detail is reshuffle_each_iteration=False. Without that, each epoch may still get a new order.

Model Code Can Still Introduce Variation

Even with seeds set, results can differ if the code path changes between runs. Random ops are deterministic only when they are executed in the same order.

For example:

  • Conditional branches that create layers differently.
  • Data augmentation with separate random calls.
  • Parallel execution on nondeterministic kernels.

That is why reproducibility is easier in a minimal, single-process experiment than in a large distributed training job.

If you want layer-level clarity, seed those components explicitly when needed:

python
1from tensorflow import keras
2
3initializer = keras.initializers.GlorotUniform(seed=1234)
4dropout = keras.layers.Dropout(0.2, seed=1234)

Global seeding is usually enough, but explicit component seeds can make debugging easier.

Expect Cross-Platform Differences

Reproducibility is strongest when the environment is identical. Changing TensorFlow versions, CUDA versions, drivers, or hardware can still alter low-level math behavior. The model may remain statistically equivalent while individual floating-point values differ slightly.

This is especially noticeable on GPUs because some operations may use algorithms optimized for speed rather than determinism unless you enable deterministic execution.

So there are really two goals:

  • Repeatability within the same environment.
  • Portability across environments.

The first is much easier than the second.

A Minimal Repeatable Example

Here is a complete example that should produce the same model output on repeated runs in the same setup:

python
1import numpy as np
2import tensorflow as tf
3from tensorflow import keras
4
5tf.keras.utils.set_random_seed(42)
6tf.config.experimental.enable_op_determinism()
7
8X = np.array([[0.0], [1.0], [2.0], [3.0]], dtype="float32")
9y = np.array([[0.0], [2.0], [4.0], [6.0]], dtype="float32")
10
11model = keras.Sequential([
12    keras.layers.Dense(4, activation="relu", input_shape=(1,)),
13    keras.layers.Dense(1)
14])
15
16model.compile(optimizer="adam", loss="mse")
17model.fit(X, y, epochs=20, batch_size=2, shuffle=False, verbose=0)
18
19print(model.predict(X, verbose=0).round(4))

Notice that shuffle=False is also deliberate. It removes one more source of variation.

Common Pitfalls

The most common mistake is setting only the TensorFlow seed and leaving NumPy, Python random, or dataset shuffling uncontrolled.

Another mistake is expecting identical results across different machines, drivers, or TensorFlow versions. Seeds improve repeatability, but they do not erase environment differences.

People also forget that operation order matters. Reordering layer creation or random augmentation calls changes the sequence of random numbers even if the seed is the same.

Finally, not every GPU op is deterministic unless you enable deterministic execution. If exact reproducibility matters, configure that explicitly.

Summary

  • 'tf.random.set_seed affects TensorFlow random ops, but reproducibility needs wider control.'
  • 'tf.keras.utils.set_random_seed is a better one-stop setup for modern projects.'
  • Seed dataset shuffling and disable reshuffling when you need identical epochs.
  • Deterministic execution matters, especially on GPUs.
  • Exact reproducibility is easiest within the same software and hardware environment.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the 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.