如何使用Keras获得可重复的结果? [英] How do I get reproducible results with Keras?

查看:91
本文介绍了如何使用Keras获得可重复的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Keras获得可重复的结果,但是,每次运行程序时,我都会得到不同的结果.

I'm trying to get reproducible results with Keras, however every time I run the program I get different results.

我已经设置了python哈希种子,Numpy随机种子,随机种子,TensorFlow种子和kernel_initializer glorot_uniform种子,但是我仍然没有得到可复制的结果.我还能做些其他事情来获得可重复的结果吗?

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.

推荐答案

由于您使用的是Tensorflow作为后端的Keras,因此很难获得可重复的结果,尤其是在启用GPU的情况下.但是,仍然有一种方法可以实现此目的.

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. However, there is still a method to achieve this.

首先,不要使用GPU.

First, do not use GPU.

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

第二,就像您在代码中所做的一样,为Numpy,Random,TensorFlow等设置种子.

Second, as you've did in code, set seed for Numpy, Random, TensorFlow and so on.

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.

这篇关于如何使用Keras获得可重复的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆