How to set an environment variable when using gcloud ML engine?

百般思念 提交于 2020-03-04 21:17:00

问题


All,

(Environments: Windows 7, Python 3.6, Keras & tensorflow libs, gcloud ml engine)

I am running certain Keras ML model examples using gcloud ml engine as introduced here. Everything was fine but I just got various results among multiple runs although I was using the same training and validation data. My goal is to make reproductive training results from multiple runs.

I googled it for a while and found some solutions in this Keras Q&A regarding making reproductive results. Basically they first suggested this:

First, you need to set the PYTHONHASHSEED environment variable to 0 before the program starts (not within the program itself).

I know I could set the variables locally on my own machine, or I could set it when I deploy gcloud function as introduced here.

But, I just do not know how to set environment variables when I was using gcloud ML engine (on server side but NOT on local). So I cannot set "PYTHONHASHSEED=0" on the gcloud server when my model programs running there.

BTW, in general I know the randomness is a useful nature in the ML field but I am not very familiar about the topic of making reproductive results yet, so any thoughts regarding this topic are also welcome. Thanks!

Daqi

PS: I have tried to setting the environment variables in running time below:

import os
os.environ["PYTHONHASHSEED"] = "0" 
print(hash("keras"))

But it cannot make the effect that "setting the variables before the program starts". So by having this code, I still cannot get the same hash results from the multiple runs. On the other hand, on local, if I set "PYTHONHASHSEED=0" before running the code, I may get the same hash results.


回答1:


I don't believe the Cloud ML Engine API provides a mechanism to set environment variables. However, you might be able to workaround this by writing a wrapper script (NB: UNTESTED CODE):

import os
import subprocess

env = os.environ.copy()
env["PYTHONHASHSEED"] = "0"

subprocess.check_call(['python', 'main.py'], env=env)


来源:https://stackoverflow.com/questions/52616287/how-to-set-an-environment-variable-when-using-gcloud-ml-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!