在Tensorflow后端运行Keras时如何获得可重现的结果

176*_*ing 8 deep-learning lstm keras tensorflow

每次我在jupyter笔记本中使用Keras运行LSTM网络时,都会得到不同的结果,并且我在Google上搜索了很多,并且尝试了一些不同的解决方案,但是它们都不起作用,下面是我尝试过的一些解决方案:

  1. 设置numpy随机种子

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

  2. 设置张量流随机种子

    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}" ] }

我的环境版本是:

Keras: 2.0.6
Tensorflow: 1.2.1
CPU or GPU: CPU
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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')
Run Code Online (Sandbox Code Playgroud)

Tho*_*etz 5

您的模型定义中肯定缺少种子。可以在这里找到详细的文档: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))
Run Code Online (Sandbox Code Playgroud)

Keras本身在其FAQ部分中提供了有关获得可复制结果的部分:(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: /sf/ask/2941606531/

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)
Run Code Online (Sandbox Code Playgroud)

  • 为清楚起见:keras.io 常见问题使您可以为所有涉及的随机数生成器提供种子,从而无需_额外地_为层初始化提供种子参数以创建可重现的结果。(我的理解,但不能保证。) (2认同)