本文介绍了我需要在导入numpy或tensorflow的所有模块中设置种子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用以tensorflow作为后端的keras训练深度学习模型时,我试图产生可再现的结果.

I am trying to produce reproducible results while training a deep learning model using keras with tensorflow as backend.

我浏览了此文档: https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development 设置numpy,python和我用于训练的train.py文件中的tf随机种子.

I went through this document: https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development to set numpy's, python's and tf's random seed in the train.py file which I use for training.

现在,此文件从其他两个模块utils.pymodel.py导入一些功能.在这两个文件中,我的顶部都是import numpy as npimport tensorflow as tf.我的问题是-从不同的模块导入并设置随机种子如何工作?

Now, this file imports some functions from two other modules utils.py and model.py. In both these files, I have import numpy as np and import tensorflow as tf at the top. My question is - how does importing from different modules and setting random seeds work?

a)在导入语句之后,是否需要在每个文件中设置随机种子?

a) Do I need to set random seed in each file after the import statement?

b)或者,我是否只需要在train.py中设置这些种子,并在执行这些设置种子命令后从其他模块进行所有导入?

b) Or, do I just need to set these seeds in the train.py and do all the imports from other modules after these setting seeds commands?

c)是否还需要在import tensorflow as tf之后执行tf.set_random_seed(1)?

c) Does tf.set_random_seed(1) needs to be done after import tensorflow as tf also?

d)即使我不导入tensorflow或keras,而仅从keras导入图层,我也需要设置tf.set_random_seed(1)吗?

d) Do I need to set tf.set_random_seed(1) even if I am not importing tensorflow or keras and just importing layers from keras?

推荐答案

首先,使用tensorflow.keras而不是keras.

First of all, use the tensorflow.keras instead of keras.

通常,以以下方式在主脚本中使用种子即可.

Usually, it suffices to use the seed in the main script in the following manner.

import random
random.seed(1)
import numpy as np
np.random.seed(1)
import tensorflow as tf
tf.random.set_seed(1)

但是,如果您有多个模块并且它们具有一些随机操作(例如权重初始化),则将这些行添加到每个模块中.

But, if you have multiple modules and they have some randomized operation (such as weight initialization), then add these lines to each of your module.

此外,这些方法并不能保证100%的可重复性,如果您使用的是GPU,则可能还会因此而产生一些随机性.

Additionally, these only don't guarantee 100% reproducibility, if you are using a GPU, there maybe some randomness due to that too.

您可以使用 https://github.com/NVIDIA/tensorflow-determinism

os.environ['TF_DETERMINISTIC_OPS'] = '1'对于tensorflow == 2.1.0

os.environ['TF_DETERMINISTIC_OPS'] = '1' For tensorflow==2.1.0

对于张量流< 2.1

For tensorflow < 2.1

import tensorflow as tf
from tfdeterminism import patch
patch()

这篇关于我需要在导入numpy或tensorflow的所有模块中设置种子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:42