19

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')
176coding
  • 2,933
  • 4
  • 17
  • 18
  • results can vary because of different reasons (e.g. random initiation of variables). so unless you provide some model code we can only help so much – dv3 Jul 21 '17 at 06:43
  • Please have a look at my response here (https://stackoverflow.com/a/52897216/9024698) for when using the CPU. – Outcast Oct 22 '18 at 09:55
  • You've not yet accepted any answer. Which of the following worked for you or did you find any better solution? – Dr Nisha Arora Sep 15 '19 at 02:19

3 Answers3

13

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)
Thomas Pinetz
  • 6,948
  • 2
  • 27
  • 46
  • 3
    For clarity: the keras.io FAQ makes you seed all random number generators involved, making it unnecessary to _additionally_ supply seed arguments for layer initialization to create reproducible results. (My understanding, no guarantees though.) – tanius Mar 29 '18 at 13:32
  • Adding seed arguments to each layer would be too much, so do we need to paste the above code at the start of the notebook? Being from the non-programming background, I'm having a hard time understanding this code. – Dr Nisha Arora Sep 15 '19 at 02:18
  • @DrNishaArora yes. They are pseudo-random numbers. Pseudo-random numbers generate the same number sequence for the same seed. Normally, when you start Python's script the seed was initialized by the current timestamp. So, each running starts with a different seed. This code overrides initialized seed from timestamp by your constant value. @ThomasPintez Please update for v2.x: they work, but we must use `tf.compat.v1`. – Dzwiedziu-nkg Jan 06 '22 at 10:16
0

Keras + Tensorflow.

Step 1, disable GPU.

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""

Step 2, seed those libraries which are included in your code, say "tensorflow, numpy, random".

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)

Make sure these two pieces of code are included at the start of your code, then the result will be reproducible.

guorui
  • 871
  • 2
  • 9
  • 21
0

I resolved this issue by adding os.environ['TF_DETERMINISTIC_OPS'] = '1'

Here an example:

import os
os.environ['TF_DETERMINISTIC_OPS'] = '1'
#rest of the code
#TensorFlow version 2.3.1
Francesco Laiti
  • 1,791
  • 2
  • 13
  • 19