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.
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
randommodule. - 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.
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.
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:
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:
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_seedaffects TensorFlow random ops, but reproducibility needs wider control.' - '
tf.keras.utils.set_random_seedis 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
- Tensorflow set_seed error when running autoencoder
- TensorFlow simple operations tensors vs Python variables
- TensorFlow simple recurrent neural network
- Tensorflow simultaneous prediction on GPU and CPU
- Tensorflow single sigmoid output with log loss vs two linear outputs with sparse softmax cross entropy loss for binary classification
- Tensorflow slicing
- Tensorflow summary adding a variable which does not belong to computational graph
- Tensorflow summary adding a variable which does not belong to computational graph

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