简体   繁体   English

通过numpy.random.seed设置的随机种子是否在子模块之间维护?

[英]Does a random seed set via numpy.random.seed maintain across submodules?

If I set a seed for my RNG eg numpy.random.seed(0) and I call a submodule, will the RNG's state be maintained? 如果我为RNG设置了一个种子,例如numpy.random.seed(0)并调用了一个子模块,那么RNG的状态会保持吗?

eg 例如

# some_lib.py
def do_thing():
  return numpy.random.rand()
# parent module
import some_lib
numpy.seed(0)
...
some_lib.do_thing()

Will the numpy state set by the parent be used by the child? 父母设置的numpy状态会被孩子使用吗?

The seed is a global value for all uses of numpy . 种子是numpy所有用途的全局值。 So as long as the child module doesn't reseed it, or pull values from it non-deterministically (effectively adjusting it to a new seed based on advancing the old), then the seed will be preserved. 因此,只要子模块不重新设定其种子状态,或不确定地从其获取值(在推进旧模块的基础上有效地将其调整为新种子),则将保留该种子。

Most PRNG libraries behave this way, because the alternative is pretty useless; 大多数PRNG库都是以这种方式运行的,因为替代方法几乎没有用。 for reproducible tests, you want to be able to set the seed once, and have everything rely on that stable seed. 对于可重现的测试,您希望能够设置一次种子,并使所有内容都依赖该稳定的种子。 If there was a per-module seed, the testing module couldn't seed the PRNG used by the module being tested. 如果存在每个模块的种子,则测试模块无法为被测试模块使用的PRNG注入种子。

在测试中,似乎numpy的RNG状态由子进程维护。

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

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