Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproducible results using Keras with TensorFlow backend

I am using Keras to build a deep learning LSTM model, using TensorFlow backend. Each time I run the model, the result is different. Is there a way to fix the seed to create reproducible results? Thank you!

like image 204
Edamame Avatar asked Feb 05 '18 21:02

Edamame


People also ask

Can I use Keras and TensorFlow together?

Keras and TensorFlow are open source Python libraries for working with neural networks, creating machine learning models and performing deep learning. Because Keras is a high level API for TensorFlow, they are installed together.

Is TensorFlow backend for Keras?

At this time, Keras has two backend implementations available: the TensorFlow backend and the Theano backend.


1 Answers

As @Poete_Maudit said here: How to get reproducible results in keras

To get reproducible results you will have to do the following at the very beginning of your script (that will be forced to use a single CPU):

# Seed value (can actually be different for each attribution step)
seed_value= 0

# 1. Set `PYTHONHASHSEED` environment variable at a fixed value
import os
os.environ['PYTHONHASHSEED']=str(seed_value)

# 2. Set `python` built-in pseudo-random generator at a fixed value
import random
random.seed(seed_value)

# 3. Set `numpy` pseudo-random generator at a fixed value
import numpy as np
np.random.seed(seed_value)

# 4. Set `tensorflow` pseudo-random generator at a fixed value
import tensorflow as tf
tf.random.set_seed(seed_value) # tensorflow 2.x
# tf.set_random_seed(seed_value) # tensorflow 1.x

# 5. Configure a new global `tensorflow` session
from keras import backend as K
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)

Note: You cannot (anymore) get reproducible results using command: PYTHONHASHSEED=0 python3 script.py, as https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development might let you think, and you have to set PYTHONHASHSEED with os.environ within your script as in step #1. Also, this does NOT work for GPU usage.

like image 149
Charly Empereur-mot Avatar answered Sep 19 '22 04:09

Charly Empereur-mot