简体   繁体   English

numpy.random状态的差异消失了

[英]Discrepancy of the state of `numpy.random` disappears

There are two python runs of the same project with different settings, but with the same random seeds. 同一项目的两个python运行具有不同的设置,但具有相同的随机种子。

The project contains a function that returns a couple of random numbers using numpy.random.uniform . 该项目包含一个函数,该函数使用numpy.random.uniform返回几个随机数。

Regardless of other uses of numpy.random in the python process, series of the function calls in both of the runs generate the same sequences, until some point. 不管python进程中numpy.random的其他用途如何, numpy.random中的一系列函数调用都会生成相同的序列,直到某个时刻为止。

And after generating different results for one time at that point, they generate the same sequences again, for some period. 在那一点上一次产生不同的结果之后,它们又在一段时间内再次产生相同的序列。

I haven't tried using numpy.random.RandomState yet, but how is this possible? 我还没有尝试使用numpy.random.RandomState ,但是这怎么可能呢?

Is it just a coincidence that somewhere something which uses numpy.random caused the discrepancy and fixed it again? 某处使用numpy.random引起差异并再次解决问题,这仅仅是一个巧合吗?

I'm curious if it is the only possibility or there is another explanation. 我很好奇这是否是唯一的可能性,或者还有其他解释。

Thanks in advance. 提前致谢。

ADD: I forgot to mention that there was no seeding at that point. 添加:我忘了提到当时还没有播种。

When you use the random module in numpy, each randomly generated number (regardless of the distribution/function) uses the same "global" instance of RandomState . 在numpy中使用random模块时,每个随机生成的数字(无论分布/函数如何)都使用RandomState的相同“全局”实例。 When you set the seed using numpy.random.seed() , you set the seed of the 'global' instance of RandomState . 使用numpy.random.seed()设置种子时,将设置numpy.random.seed()的“全局”实例的RandomState This is the same principle as the random library in Python. 这与Python中的random库的原理相同。

I'm not sure of the specific implementation of the numpy random functions, but I suspect that each random function will make the underlying Mersenne Twister advance a number of 'steps', with the number of steps not necessarily being the same between different random functions. 我不确定numpy随机函数的具体实现方式,但是我怀疑每个随机函数都会使底层的Mersenne Twister前进多个“步骤”,而不同random函数之间的步骤数量不一定相同。

So, if the order of every call to a random function is not the same between separate runs, then you may see divergence in the generated sequence of random numbers, with convergence again if the Mersenne Twister 'steps' line up again. 因此,如果在不同的运行之间每次调用random函数的顺序都不相同,那么您可能会看到所生成的随机数序列存在差异,如果Mersenne Twister再次“逐步”排列,则会再次收敛。

You could get around this by initialising a separate RandomState instance for each function you are using. 您可以通过为所使用的每个函数初始化一个单独的RandomState实例来解决此问题。 For example: 例如:

import numpy as np

seed = 12345
r_uniform = np.random.RandomState(seed)
r_randint = np.random.RandomState(seed)

a_random_uniform_number = r_uniform.uniform()
a_random_int = r_randint.randint(10)

You might want to set different seeds for each instance - this will depend on what you are using these pseudo-random numbers for. 您可能想为每个实例设置不同的种子-这取决于您使用这些伪随机数的目的。

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

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