简体   繁体   English

(numpy)个随机种子有范围吗?

[英]Is there a scope for (numpy) random seeds?

My question is related to What is the scope of a random seed in Python? 我的问题与Python中的随机种子的作用范围有关? . In the case of above question, it is clarified that there is a (hidden) global Random() instance in the module for random . 在上述问题的情况下,需要说明的是,模块中存在一个用于random的(隐藏)全局Random()实例。

1) I would like to clarify whether setting the random seed in one module will cause this to be the random seed in other modules and whether there are certain things to be aware of. 1)我想澄清一下是否在一个模块中设置随机种子会导致它成为其他模块中的随机种子,以及是否需要注意某些事项。

For instance: Given: moduleA.py, moduleB.py 例如:给定:moduleA.py,moduleB.py

moduleA.py: moduleA.py:

import random 
import moduleB
random.seed(my_seed)
moduleB.randomfct()

moduleB.py: moduleB.py:

import random 
def randomfct():
    #do_things_using_random

Does moduleB also use my_seed , or do I have to pass the seed to moduleB.py and set it again? moduleB是否还使用my_seed ,还是必须将种子传递给moduleB.py并再次进行设置?

2) Does the order of setting the random seed / importing play any role? 2)设置随机种子/导入的顺序是否起作用?

For example in moduleA.py : 例如在moduleA.py

import random 
random.seed(my_seed)
import moduleB

3) Is this also the case for setting numpy random seeds, eg np.random.seed(42) ? 3)设置numpy随机种子也是如此,例如np.random.seed(42)吗?

The CPython random.py implementation is very readable. CPython random.py实现非常易读。 I recommend having a look: https://github.com/python/cpython/blob/3.6/Lib/random.py 我建议看看: https : //github.com/python/cpython/blob/3.6/Lib/random.py

Anyway, that version of python creates a global random.Random() object and assigns it directly to the random module. 无论如何,该版本的python会创建一个全局random.Random()对象,并将其直接分配给random模块。 This object contains a seed(a) method which acts as a module function when you call random.seed(a) . 该对象包含一个seed(a)方法,该方法在调用random.seed(a)充当模块函数 Thus the seed state is shared across your entire program. 因此,种子状态在整个程序中共享。

1) Yes. 1)是的。 moduleA and moduleB uses the same seed. moduleAmoduleB使用相同的种子。 Importing random in moduleA creates the global random.Random() object. moduleA导入random moduleA创建全局random.Random()对象。 Reimporting it in moduleB just gives you the same module and maintains the originally created random.Random() object. moduleB重新导入它只会为您提供相同的模块,并维护最初创建的random.Random()对象。

2) No. Not in the example you gave, but in general yes it can matter. 2)否。您提供的示例中没有,但是总的来说很重要。 You might use moduleB before you set the seed in moduleA thus your seed wasn't set. 您可以使用moduleB设置在播种前moduleA这样你的种子没有设置。

3) Hard to tell. 3)很难说。 Much more complicated code base. 更复杂的代码库。 That said, I would think it works the same way. 也就是说,我认为它的工作方式相同。 The authors of numpy would really have to try to make it work in a different way than how it works in the python implementation. numpy的作者确实必须尝试使其工作方式不同于python实现中的工作方式。

In general, if you are worried about seed state, I recommend creating your own random objects and pass them around for generating random numbers. 通常,如果您担心种子状态,建议您创建自己的随机对象并将其传递以生成随机数。

So, for answering your first question: 因此,对于回答第一个问题:

Does moduleB also use my_seed, or do I have to pass the seed to moduleB.py and set it again? moduleB是否还使用my_seed,还是必须将种子传递给moduleB.py并重新设置?

Yes, it does, For example, ran the following: 是的,确实如此,例如,运行了以下命令:

ModuleA: ModuleA:

import moduleb
import random 
random.seed(0)
my_random()

ModuleB ModuleB

import random
def my_random():
    print(random.randint(0,5))

This will always print 3, as the seed is set. 设置种子后,它将始终打印3。 The general rule is that the main python module that has to be run should call the random.seed() function and this creates a seed that is shared among all the imported modules. 一般规则是必须运行的主要python模块应调用random.seed()函数,这将创建一个种子,该种子在所有导入的模块之间共享。 This is only changed if you explicitly call random.seed again from some other module. 仅当您从其他模块再次显式调用random.seed时,才可以更改此设置。

For question 2: 对于问题2:

Does the order of setting the random seed / importing play any role? 设置随机种子/导入的顺序是否起作用?

No it doesn't. 不,不是。 Unless you call the random function before setting seed. 除非您在设置种子之前调用random函数。

For question 3: 对于问题3:

Is this also the case for setting numpy random seeds, eg np.random.seed(42)? 设置numpy随机种子(例如np.random.seed(42))是否也是如此?

So, the issue that comes with using np.random.seed() is that they are not thread safe and that's why they don't behave similarly. 因此,使用np.random.seed()带来的问题是它们不是线程安全的,这就是为什么它们的行为不同。 More details can be found at: This Stackoverflow answer 可以在以下位置找到更多详细信息: 这个Stackoverflow答案

In jupyter notebook, random.seed seems to have cell scope. 在jupyter笔记本中,random.seed似乎具有单元格范围。 For instance, random.seed(1) is needed to be specified in both two consecutive cells to get the same result with the following code: 例如,需要在两个连续的单元格中都指定random.seed(1),以使用以下代码获得相同的结果:

Cell 1: 单元格1:

np.random.seed(1)
np.random.random_sample(4)

Cell 2: 单元格2:

np.random.seed(1)
np.random.random(4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM