简体   繁体   English

为什么使用numpy.random.seed不是一个好习惯?

[英]Why using numpy.random.seed is not a good practice?

I want to do reproducible tests that use random numbers as inputs. 我想做可重复的测试,使用随机数作为输入。 I am used to invoke rng in Matlab and numpy.random.seed in Python. 我习惯在Matlab中调用rng,在Python中调用numpy.random.seed。 However, I noticed that the Notes section of seed's help reads: 但是,我注意到种子帮助的Notes部分是:

This is a convenience, legacy function. 这是一种方便的遗留功能。

The best practice is to not reseed a BitGenerator, rather to recreate a new one. 最佳做法是不重新设置BitGenerator,而是重新创建一个新的BitGenerator。 This method is here for legacy reasons. 此方法出于遗留原因。 This example demonstrates best practice. 此示例演示了最佳实践。

 from numpy.random import MT19937 from numpy.random import RandomState, SeedSequence rs = RandomState(MT19937(SeedSequence(123456789))) # Later, you want to restart the stream rs = RandomState(MT19937(SeedSequence(987654321))) 

Does anyone know what are the caveats of using seed compared to the docstring suggestion? 有没有人知道使用种子与docstring建议相比有什么注意事项?

From https://numpy.org/neps/nep-0019-rng-policy.html 来自https://numpy.org/neps/nep-0019-rng-policy.html

The preferred best practice for getting reproducible pseudorandom numbers is to instantiate a generator object with a seed and pass it around. 获得可再现的伪随机数的首选最佳实践是使用种子实例化生成器对象并将其传递。 The implicit global RandomState behind the numpy.random.* convenience functions can cause problems, especially when threads or other forms of concurrency are involved. numpy.random。*便捷函数后面的隐式全局RandomState可能会导致问题,尤其是涉及线程或其他形式的并发时。 Global state is always problematic. 全球国家总是有问题的。 We categorically recommend avoiding using the convenience functions when reproducibility is involved. 我们断然建议在涉及可重复性时避免使用便利功能。

That said, people do use them and use numpy.random.seed() to control the state underneath them. 也就是说,人们确实使用它们并使用numpy.random.seed()来控制它们下面的状态。 It can be hard to categorize and count API usages consistently and usefully, but a very common usage is in unit tests where many of the problems of global state are less likely. 可能难以对API用法进行一致且有用的分类和计数,但一种非常常见的用法是在单元测试中,其中许多全局状态问题的可能性较小。

This NEP does not propose removing these functions or changing them to use the less-stable Generator distribution implementations. 该NEP不建议删除这些功能或更改它们以使用不太稳定的Generator分发实现。 Future NEPs might. 未来的NEP可能会。

Specifically, the initial release of the new PRNG subsystem SHALL leave these convenience functions as aliases to the methods on a global RandomState that is initialized with a Mersenne Twister BitGenerator object. 具体来说,新PRNG子系统的初始版本应该将这些便利函数作为使用Mersenne Twister BitGenerator对象初始化的全局RandomState上的方法的别名。 A call to numpy.random.seed() will be forwarded to that BitGenerator object. 对numpy.random.seed()的调用将被转发到该BitGenerator对象。 In addition, the global RandomState instance MUST be accessible in this initial release by the name numpy.random.mtrand._rand: Robert Kern long ago promised scikit-learn that this name would be stable. 另外,全局RandomState实例必须在这个初始版本中可以通过名称numpy.random.mtrand._rand访问:Robert Kern很久以前承诺scikit-知道这个名字是稳定的。 Whoops. 哎呦。

In order to allow certain workarounds, it MUST be possible to replace the BitGenerator underneath the global RandomState with any other BitGenerator object (we leave the precise API details up to the new subsystem). 为了允许某些变通方法,必须可以将全局RandomState下的BitGenerator替换为任何其他BitGenerator对象(我们将精确的API详细信息留给新的子系统)。 Calling numpy.random.seed() thereafter SHOULD just pass the given seed to the current BitGenerator object and not attempt to reset the BitGenerator to the Mersenne Twister. 之后调用numpy.random.seed()应该只将给定的种子传递给当前的BitGenerator对象,而不是尝试将BitGenerator重置为Mersenne Twister。 The set of numpy.random.* convenience functions SHALL remain the same as they currently are. numpy.random。*便利函数的集合应保持与它们当前相同。 They SHALL be aliases to the RandomState methods and not the new less-stable distributions class (Generator, in the examples above). 它们应该是RandomState方法的别名,而不是新的不太稳定的分布类(上面的例子中的Generator)。 Users who want to get the fastest, best distributions can follow best practices and instantiate generator objects explicitly. 想要获得最快,最好的发行版的用户可以遵循最佳实践并明确地实例化生成器对象。

This NEP does not propose that these requirements remain in perpetuity. 该NEP并未建议永久保留这些要求。 After we have experience with the new PRNG subsystem, we can and should revisit these issues in future NEPs. 在我们拥有新PRNG子系统的经验之后,我们可以而且应该在未来的NEP中重新审视这些问题。

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

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