在何处设置“ os.environ ['PYTHONHASHSEED'] ='0'”以在Google Colab中获得可重复的结果

时间:2019-11-02 11:41:28

标签: python-3.x tensorflow keras google-colaboratory reproducible-research

我想在Google Colab中训练CNN,并尝试获得可重复的结果。在下面的代码中,我不确定再现性说明的顺序是否正确。 我想知道os.environ['PYTHONHASHSEED']='0'是否在正确的位置,还是应该在代码中首先出现在import numpy as np等之前?


import numpy as np
import tensorflow as tf
import random as rn

import os
os.environ['PYTHONHASHSEED']='0'
np.random.seed(1)
rn.seed(1)

from keras import backend as K

if 'tensorflow' == K.backend():
  import tensorflow as tf
  from keras.backend.tensorflow_backend import set_session
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = true
  config.gpu_options.allow_growth = True
  config.gpu_options.visible_device_liste = "0"
  set_session(tf.Session(config=config))

tf.set_random_seed(1) 
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)   
K.set_session(sess)   


!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials 

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

如果有人可以看一下这段代码并确认顺序是对还是错,我将感到非常高兴和感谢。

0 个答案:

没有答案