使用 Tensorflow 后端运行 Keras 时如何获得可重现的结果 [英] How to get reproducible result when running Keras with Tensorflow backend
问题描述
每次我在 jupyter notebook 中使用 Keras 运行 LSTM 网络时,都会得到不同的结果,并且我搜索了很多,并且尝试了一些不同的解决方案,但都没有奏效,以下是我尝试过的一些解决方案:
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:
设置 numpy 随机种子
set numpy random seed
random_seed=2017从 numpy.random 导入种子种子(随机种子)
设置 tensorflow 随机种子
set tensorflow random seed
from tensorflow import set_random_seedset_random_seed(random_seed)
设置内置随机种子
随机导入random.seed(random_seed)
设置 PYTHONHASHSEED
set PYTHONHASHSEED
导入操作系统os.environ['PYTHONHASHSEED'] = '0'
在 jupyter notebook kernel.json 中添加 PYTHONHASHSEED
add PYTHONHASHSEED in jupyter notebook kernel.json
<代码>{"语言": "蟒蛇","display_name": "Python 3",环境":{PYTHONHASHSEED":0"},argv":[Python","-m","ipykernel_launcher",-F",{connection_file}"]}
我的 env 版本是:
and the version of my env is:
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')
推荐答案
您的模型定义中肯定缺少种子.可以在此处找到详细文档:https://keras.io/initializers/.
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.
一个例子:
model.add(Dense(1, activation='linear',
kernel_initializer=keras.initializers.RandomNormal(seed=1337),
bias_initializer=keras.initializers.Constant(value=0.1))
Keras 自己在他们的常见问题部分有一个关于获得可重现结果的部分:(https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development一>).他们有以下代码片段来产生可重现的结果:
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)
这篇关于使用 Tensorflow 后端运行 Keras 时如何获得可重现的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!