首页 > 解决方案 > Issue with reproducibility across different sessions

问题描述

I am trying to make a script reproducible but having some issue. The code is in Tensorflow 2.x and doesn't use keras API. It has many layers build with tf.compat. The model is created and trained with a function get_model()

os.environ['tf_deterministic_ops'] = "2"
os.environ["PYTHONHASHSEED"] = "2"
random.seed(2)
np.random.seed(2)
tf.random.set_seed(2)
for i in range(5):
    get_model()

So, I am training the same model 5 times above, and the results are same every time. But When I run the following script 5 times, the results are different. Am I doing something wrong? Any help will be great. Thanks!

os.environ['tf_deterministic_ops'] = "2"
os.environ["PYTHONHASHSEED"] = "2"
random.seed(2)
np.random.seed(2)
tf.random.set_seed(2)
get_model()

标签: pythontensorflowdeep-learning

解决方案


我想说一切正常:设置随机种子总是产生相同的随机变量序列,例如

import numpy as np
# set seed
np.random.seed(2)

for i in range(5):
    print(np.random.randint(10))     # prints:  8, 8, 6, 2, 8

for i in range(5):
    np.random.seed(2)
    print(np.random.randint(10))     # prints:  8, 8, 8, 8, 8

因此,运行您的第二个脚本五次每次都会产生相同的模型,因为您在每次执行时都将种子设置为 2。在您的第一个脚本中迭代 for 循环会生成与前五个输出中的变量相对应的模型,其中seed=2. 使用我的代码示例:您的第二个脚本将始终创建模型“8”,您的第一个脚本创建模型“8”、“8”、“6”、“2”和“8”。


推荐阅读