简体   繁体   English

如何使用 Keras 获得可重现的结果?

[英]How do I get reproducible results with Keras?

I'm trying to get reproducible results with Keras, however every time I run the program I get different results.我正在尝试使用 Keras 获得可重现的结果,但是每次运行程序时都会得到不同的结果。

I've set the python hash seed, the Numpy random seed, the random seed, the TensorFlow seed, and the kernel_initializer glorot_uniform seed, but I still don't get reproducible results. I've set the python hash seed, the Numpy random seed, the random seed, the TensorFlow seed, and the kernel_initializer glorot_uniform seed, but I still don't get reproducible results. Are there any other things I can do to get reproducible results?我还能做些什么来获得可重复的结果吗?

I expect the predictions to be the same, however they are not.我希望预测是相同的,但事实并非如此。 I get different results every single time.我每次都会得到不同的结果。

Because you're using Keras with Tensorflow as backend, you will find it is pretty hard to get reproducible result especially when GPU is enable.因为您使用 Keras 和 Tensorflow 作为后端,您会发现很难获得可重现的结果,尤其是在启用 GPU 的情况下。 However, there is still a method to achieve this.但是,仍然有一种方法可以实现这一点。

First, do not use GPU.首先,不要使用GPU。

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

Second, as you've did in code, set seed for Numpy, Random, TensorFlow and so on.其次,正如您在代码中所做的那样,为 Numpy、Random、TensorFlow 等设置种子。

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)

One final word, both two pieces of code should be placed at the begining of your code.最后一句话,两段代码都应该放在代码的开头。

with TENSORFLOW 2使用TENSORFLOW 2

import tensorflow as tf

tf.random.set_seed(33)
os.environ['PYTHONHASHSEED'] = str(33)
np.random.seed(33)
random.seed(33)

session_conf = tf.compat.v1.ConfigProto(
    intra_op_parallelism_threads=1, 
    inter_op_parallelism_threads=1
)
sess = tf.compat.v1.Session(
    graph=tf.compat.v1.get_default_graph(), 
    config=session_conf
)
tf.compat.v1.keras.backend.set_session(sess)

I created a rule to achieve reproducibility:我创建了一个规则来实现可重复性:

  • Works for python 3.6, not 3.7适用于 python 3.6,而不是 3.7
  • First install Keras 2.2.4首先安装 Keras 2.2.4
  • After install tensorflow 1.9安装 tensorflow 1.9 后

And finally in the code:最后在代码中:

import numpy as np
import random as rn
import tensorflow as tf
import keras
from keras import backend as K

#-----------------------------Keras reproducible------------------#
SEED = 1234

tf.set_random_seed(SEED)
os.environ['PYTHONHASHSEED'] = str(SEED)
np.random.seed(SEED)
rn.seed(SEED)

session_conf = tf.ConfigProto(
    intra_op_parallelism_threads=1, 
    inter_op_parallelism_threads=1
)
sess = tf.Session(
    graph=tf.get_default_graph(), 
    config=session_conf
)
K.set_session(sess)
#-----------------------------------------------------------------#

(Only tested for Tensorflow 2) (仅针对 Tensorflow 2 测试)

Besides setting the random seeds, I found that my RTX 3080 GPU would only give deterministic results if I used tf.float64 instead of the default of tf.float32 .除了设置随机种子外,我发现我的 RTX 3080 GPU 只有在我使用tf.float64而不是默认的tf.float32时才会给出确定性结果。 This appears to be due to rounding errors on the GPU, which leads to differences in the path taken during gradient descent.这似乎是由于 GPU 上的舍入误差导致的,这导致梯度下降期间所采用的路径存在差异。 Note that this does not guarantee reproducibility across different GPUs.请注意,这并不能保证跨不同 GPU 的重现性。 Different GPU architectures are not guaranteed to perform operations in exactly the same way.不同的 GPU 架构不能保证以完全相同的方式执行操作。 Such differences in implementation may cause differences in rounding, which can in turn affect the convergence of your model.这种实现上的差异可能会导致舍入差异,进而影响 model 的收敛性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 keras 中获得可重现的结果 - How to get reproducible results in keras 对于使用数据增强进行图像分类的卷积神经网络,如何在 keras 中获得可重现的结果? - How can I get reproducible results in keras for a convolutional neural network using data augmentation for image classification? 如何在开发过程中使用 Keras 获得可重现的结果? - How can I obtain reproducible results using Keras during development? 无法使用 Keras CNN Model 获得可重现的结果 - Cannot get Reproducible Results with Keras CNN Model 如何使用 AutoKeras 获得可重现的结果 - How to get Reproducible Results with AutoKeras 为什么即使我设置了随机种子,我也无法在 Keras 中获得可重现的结果? - Why can't I get reproducible results in Keras even though I set the random seeds? 如何在 Keras 中获得可重复的权重初始化? - How to get reproducible weights initializaiton in Keras? 在 Python 中使用 Keras 和 TensorFlow 无法重现结果 - Results not reproducible with Keras and TensorFlow in Python "如何使用 Scikit Learn 获得绝对可重现的结果?" - How to get absolutely reproducible results with Scikit Learn? 每次我得到不同的结果,我怎样才能让我的代码可重现? - I get different results every time, how can I make my code reproducible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM