2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Kindly check this answer on how to get reproducible results with Keras: stackoverflow.com/a/52897289/6204860 $\endgroup$
    – pcko1
    Sep 19 '19 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$ Sep 20 '19 at 10:44
  • $\begingroup$ the reproducibility is an issue because of using the GPU $\endgroup$ Nov 13 '20 at 6:19
0
$\begingroup$

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

tf.set_random_seed(SEED)
os.environ['PYTHONHASHSEED'] = str(SEED)
np.random.seed(SEED)
rn.seed(SEED)

session_conf = tf.ConfigProto(
    intra_op_parallelism_threads=1, 
    inter_op_parallelism_threads=1
)
sess = tf.Session(
    graph=tf.get_default_graph(), 
    config=session_conf
)
K.set_session(sess)
#-----------------------------------------------------------------#
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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