How Do I Get Reproducible Results With Keras?
I'm trying to get reproducible results with Keras, however every time I run the program I get different results. I've set the python hash seed, the Numpy random seed, the random se
Solution 1:
Because you're using Keras with Tensorflow as backend, you will find it is pretty hard to get reproducible result especially when GPU is enable. However, there is still a method to achieve this.
First, do not use GPU.
import osos.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"os.environ["CUDA_VISIBLE_DEVICES"] = ""
Second, as you've did in code, set seed for Numpy, Random, TensorFlow and so on.
import tensorflow as tf
import numpy as np
import random as rn
sd = 1# Here sd means seed.
np.random.seed(sd)
rn.seed(sd)
os.environ['PYTHONHASHSEED']=str(sd)
from keras import backend as K
config = tf.ConfigProto(intra_op_parallelism_threads=1,inter_op_parallelism_threads=1)
tf.set_random_seed(sd)
sess = tf.Session(graph=tf.get_default_graph(), config=config)
K.set_session(sess)
One final word, both two pieces of code should be placed at the begining of your code.
Solution 2:
with TENSORFLOW 2
import tensorflow as tf
tf.random.set_seed(33)
os.environ['PYTHONHASHSEED'] = str(33)
np.random.seed(33)
random.seed(33)
session_conf = tf.compat.v1.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1
)
sess = tf.compat.v1.Session(
graph=tf.compat.v1.get_default_graph(),
config=session_conf
)
tf.compat.v1.keras.backend.set_session(sess)
Solution 3:
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)
#-----------------------------------------------------------------#
Post a Comment for "How Do I Get Reproducible Results With Keras?"