简体   繁体   English

如何根据 numpy 文档避免 numpy.random.seed 的最佳实践?

[英]How to do the best practice in avoiding numpy.random.seed according to the numpy documentation?

In the process of fixing the random seed for reproducibility I have found great SO answers with something along numpy.random.seed(123) and then got confused by the last numpy.random.seed documentation (from june 2020).在修复随机种子以实现可重复性的过程中,我发现了很好的 SO 答案,其中包含numpy.random.seed(123)的内容,然后被最后的numpy.random.seed 文档(从 2020 年 6 月起)弄糊涂了。

This is a convenience, legacy function.这是一个方便的传统 function。 The best practice is to not reseed a BitGenerator, rather to recreate a new one.最佳做法是不要重新植入 BitGenerator,而是重新创建一个新的。

What I understand is that it creates new random states.我的理解是它创造了新的随机状态。 How to then use those for reproducibility?那么如何使用这些来实现重现性呢? Bellow is the sample code provided with the documentation and a ramdom test. Bellow 是随文档和随机测试一起提供的示例代码。 Can someone please tell me how to use rs to fix the uniform sampling rand() ?有人可以告诉我如何使用rs来修复统一采样rand()吗?

from numpy.random import MT19937
from numpy.random import RandomState, SeedSequence, rand 

rs = RandomState(MT19937(SeedSequence(123456789)))

rs = RandomState(MT19937(SeedSequence(987654321)))

rand()

rs is set to a reproducible sequence of seemingly random (but obviously not) numbers. rs设置为看似随机(但显然不是)数字的可重现序列。 All of random number functions are available for your use.所有随机数功能都可供您使用。

>>> rs = RandomState(MT19937(SeedSequence(123456789)))
>>> rs.random(20)
array([0.16693771, 0.1963513 , 0.75563051, 0.72778686, 0.88007369,
       0.15063958, 0.27400923, 0.88465501, 0.6783961 , 0.40250501,
       0.58925838, 0.32977293, 0.42650604, 0.24460162, 0.7896271 ,
       0.98224806, 0.25327893, 0.32868043, 0.06819438, 0.62491713])
// Show that the results are reproducible
>>> rs = RandomState(MT19937(SeedSequence(123456789)))
>>> rs.random(20)
array([0.16693771, 0.1963513 , 0.75563051, 0.72778686, 0.88007369,
       0.15063958, 0.27400923, 0.88465501, 0.6783961 , 0.40250501,
       0.58925838, 0.32977293, 0.42650604, 0.24460162, 0.7896271 ,
       0.98224806, 0.25327893, 0.32868043, 0.06819438, 0.62491713])

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

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