As we all know Keras backend uses Tensorflow and so it should give out some kind of results when we provide the same parameters, hyper-parameters, weights, and biases initialization at each layer, but still, the accuracy is different.

This may be because the batches of images that are fed at each step in both the models are not identical and get shuffled randomly.

Is there any way in which we can make sure that the same batch of images is fed into the model while eliminating the randomness?

I have tried using all the same parameters, hyperparameters, same weights, and biases initialization with seed values.

The accuracy of both the models is not the same.

  • $\begingroup$ Kindly check this answer on how to get reproducible results with Keras: stackoverflow.com/a/52897289/6204860 $\endgroup$
    – pcko1
    Commented Sep 19, 2019 at 10:08
  • $\begingroup$ I dont have a problem on how to produce reproducible results with keras. I have a problem of not get same accuracy with an identical keras and tensorflow model. $\endgroup$ Commented Sep 20, 2019 at 10:44
  • $\begingroup$ the reproducibility is an issue because of using the GPU $\endgroup$ Commented Nov 13, 2020 at 6:19

2 Answers 2


I created a rule to achieve reproducibility:

  • Works for python 3.6, not 3.7
  • First install Keras 2.2.4
  • After install tensorflow 1.9

And finally in the code:

import numpy as np
import random as rn
import tensorflow as tf
import keras
from keras import backend as K

#-----------------------------Keras reproducible------------------#
SEED = 1234

os.environ['PYTHONHASHSEED'] = str(SEED)

session_conf = tf.ConfigProto(
sess = tf.Session(

The issue is not with the framework you are using, but with the random number generator running in the background. If you are splitting your validation set in runtime, then make sure that you have a random seed set to some specific number. In order to eliminate the randomness of the random number generator, we should set seed.

You can set your random seed for TensorFlow using tf.random.set_seed(seed) variable and give it some value. The same could be achieved in pytorch using torch.manual_seed(0). But when you do this, other dependencies such as numpy and random also generates their own random number, so you need to set their seed as well. You can also use the function below to set all your seed at once. Just keep the consistency check when you run anything.

def set_seeds(seed=SEED):
    os.environ['PYTHONHASHSEED'] = str(seed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.