How to get reproducible result when running Keras with Tensorflow backend

176coding picture 176coding · Jul 21, 2017 · Viewed 8.2k times · Source

Every time I run LSTM network with Keras in jupyter notebook, I got a different result, and I have googled a lot, and I have tried some different solutions, but none of they are work, here are some solutions I tried:

  1. set numpy random seed

    random_seed=2017 from numpy.random import seed seed(random_seed)

  2. set tensorflow random seed

    from tensorflow import set_random_seed set_random_seed(random_seed)

  3. set build-in random seed

    import random random.seed(random_seed)

  4. set PYTHONHASHSEED

    import os os.environ['PYTHONHASHSEED'] = '0'

  5. add PYTHONHASHSEED in jupyter notebook kernel.json

    { "language": "python", "display_name": "Python 3", "env": {"PYTHONHASHSEED": "0"}, "argv": [ "python", "-m", "ipykernel_launcher", "-f", "{connection_file}" ] }

and the version of my env is:

Keras: 2.0.6
Tensorflow: 1.2.1
CPU or GPU: CPU

and this is my code:

model = Sequential()
model.add(LSTM(16, input_shape=(time_steps,nb_features), return_sequences=True))
model.add(LSTM(16, input_shape=(time_steps,nb_features), return_sequences=False))
model.add(Dense(8,activation='relu'))        
model.add(Dense(1,activation='linear'))
model.compile(loss='mse',optimizer='adam')

Answer

Thomas Pinetz picture Thomas Pinetz · Jul 21, 2017

The seed is definitely missing from your model definition. A detailed documentation can be found here: https://keras.io/initializers/.

In essence your layers use random variables as their basis for their parameters. Therefore you get different outputs every time.

One example:

model.add(Dense(1, activation='linear', 
               kernel_initializer=keras.initializers.RandomNormal(seed=1337),
               bias_initializer=keras.initializers.Constant(value=0.1))

Keras themselves have a section about getting reproduceable results in their FAQ section: (https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development). They have the following code snippet to produce reproducable results:

import numpy as np
import tensorflow as tf
import random as rn

# The below is necessary in Python 3.2.3 onwards to
# have reproducible behavior for certain hash-based operations.
# See these references for further details:
# https://docs.python.org/3.4/using/cmdline.html#envvar-PYTHONHASHSEED
# https://github.com/fchollet/keras/issues/2280#issuecomment-306959926

import os
os.environ['PYTHONHASHSEED'] = '0'

# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.

np.random.seed(42)

# The below is necessary for starting core Python generated random numbers
# in a well-defined state.

rn.seed(12345)

# Force TensorFlow to use single thread.
# Multiple threads are a potential source of
# non-reproducible results.
# For further details, see: https://stackoverflow.com/questions/42022950/which-seeds-have-to-be-set-where-to-realize-100-reproducibility-of-training-res

session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)

from keras import backend as K

# The below tf.set_random_seed() will make random number generation
# in the TensorFlow backend have a well-defined initial state.
# For further details, see: https://www.tensorflow.org/api_docs/python/tf/set_random_seed

tf.set_random_seed(1234)

sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)