作者:膈应人的ID | 来源:互联网 | 2023-01-29 06:18
1> Thomas Pinet..:
您的模型定义中肯定缺少种子。可以在这里找到详细的文档: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本身在其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/17360801/
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)