2017-07-21 50 views
2

每次我在jupyter筆記本中運行Keras的LSTM網絡時,我得到了不同的結果,並且我搜索了很多,並且嘗試了一些不同的解決方案,但沒有一個他們的工作,這裏有一些解決方案,我想:如何在使用Tensorflow後端運行Keras時獲得可重現的結果

  1. 集numpy的隨機種子

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

  2. 設置tensorflow隨機種子

    from tensorflow import set_random_seed set_random_seed(random_seed)

  3. 一套內置的隨機種子

    import random random.seed(random_seed)

  4. 集PYTHONHASHSEED

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

  5. 在jupyter筆記本kernel.json添加PYTHONHASHSEED

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

和我的ENV的版本是:

Keras: 2.0.6 
Tensorflow: 1.2.1 
CPU or GPU: CPU 

,這是我的代碼:

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') 
+0

結果可以因爲不同的原因(例如,變量隨機引發)而變化。所以除非你提供一些模型代碼,否則我們只能提供很多幫助 – dv3

回答

3

種子絕對是您的模型定義缺失。詳細的文檔可以在這裏找到:https://keras.io/initializers/

實質上,您的圖層使用隨機變量作爲其參數的基礎。因此你每次都會得到不同的輸出。

一個例子:

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

Keras自己有一個關於在他們的常見問題部分得到reproduceable結果部分:(https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development)。它們具有以下的代碼段,以產生可再現的結果:

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) 
相關問題