Python 如何检索 NumPy 随机数生成器的当前种子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32172054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I retrieve the current seed of NumPy's random number generator?
提问by Mast
The following imports NumPy and sets the seed.
以下导入 NumPy 并设置种子。
import numpy as np
np.random.seed(42)
However, I'm not interested in setting the seed but more in reading it. random.get_state()
does not seem to contain the seed. The documentationdoesn't show an obvious answer.
但是,我对设置种子不感兴趣,但更喜欢阅读它。random.get_state()
似乎不包含种子。该文件并没有表现出明显的答案。
How do I retrieve the current seed used by numpy.random
, assuming I did not set it manually?
numpy.random
假设我没有手动设置它,如何检索 使用的当前种子?
I want to use the current seed to carry over for the next iteration of a process.
我想使用当前的种子来进行流程的下一次迭代。
采纳答案by ali_m
The short answer is that you simply can't (at least not in general).
简短的回答是你根本不能(至少一般不能)。
The Mersenne TwisterRNG used by numpy has 219937-1 possible internal states, whereas a single 64 bit integer has only 264possible values. It's therefore impossible to map every RNG state to a unique integer seed.
numpy 使用的Mersenne TwisterRNG 有 2 19937-1 个可能的内部状态,而单个 64 位整数只有 2 64 个可能的值。因此不可能将每个 RNG 状态映射到唯一的整数种子。
You canget and set the internal state of the RNG directly using np.random.get_state
and np.random.set_state
. The output of get_state
is a tuple whose second element is a (624,)
array of 32 bit integers. This array has more than enough bits to represent every possible internal state of the RNG (2624 * 32> 219937-1).
您可以直接使用np.random.get_state
和获取和设置 RNG 的内部状态np.random.set_state
。的输出get_state
是一个元组,其第二个元素是一个(624,)
32 位整数数组。这个数组有足够多的位来表示 RNG 的每个可能的内部状态 (2 624 * 32> 2 19937-1)。
The tuple returned by get_state
can be used much like a seed in order to create reproducible sequences of random numbers. For example:
返回的元组get_state
可以像种子一样使用,以创建可重复的随机数序列。例如:
import numpy as np
# randomly initialize the RNG from some platform-dependent source of entropy
np.random.seed(None)
# get the initial state of the RNG
st0 = np.random.get_state()
# draw some random numbers
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26 3 1 68 21]
# set the state back to what it was originally
np.random.set_state(st0)
# draw again
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26 3 1 68 21]
回答by Dong Justin
Check the first element of the array returned by np.random.get_state()
, it seems exactly the random seed to me.
检查由 返回的数组的第一个元素,np.random.get_state()
对我来说这似乎正是随机种子。
回答by vestland
This contribution is intended to serve as a clarification to the right answer from ali_m, and as an important correction to the answer from Dong Justin.
此贡献旨在澄清 ali_m 的正确答案,并作为对 Dong Justin 答案的重要更正。
These are my findings:
这些是我的发现:
- After setting the random seed using
np.random.seed(X)
you canfind it again usingnp.random.get_state()[1][0]
. - It will, however, be of little use to you.
- 使用设置随机种子后,
np.random.seed(X)
您可以使用 再次找到它np.random.get_state()[1][0]
。 - 然而,它对你没有多大用处。
The output from the following code sections will show you why both statements are correct.
以下代码部分的输出将向您展示为什么这两个语句都是正确的。
Statement 1 - you can find the random seed using np.random.get_state()[1][0]
.
语句 1 - 您可以使用np.random.get_state()[1][0]
.
If you set the random seed using np.random.seed(123)
, you can retrieve the random state as a tuple using state = np.random.get_state()
. Below is a closer look at state
(I'm using the Variable explorer in Spyder). I'm using a screenshot since using print(state)
will flood your console because of the size of the array in the second element of the tuple.
如果使用 设置随机种子np.random.seed(123)
,则可以使用将随机状态作为元组检索state = np.random.get_state()
。下面是仔细看看state
(我在 Spyder 中使用变量资源管理器)。我正在使用屏幕截图,因为使用print(state)
会因为元组第二个元素中数组的大小而淹没您的控制台。
You can easily see 123
as the first number in the array contained in the second element. And using seed = np.random.get_state()[1][0]
willgive you 123
. Perfect? Not quite, because:
您可以轻松地123
将数组中的第一个数字视为包含在第二个元素中。并采用seed = np.random.get_state()[1][0]
将给予你123
。完美的?不完全是,因为:
Statement 2 - It will, however, be of little use to you:
陈述 2 - 但是,它对您没有多大用处:
It may not seem so at first though, because you coulduse np.random.seed(123)
, retrieve the same number with seed = np.random.get_state()[1][0]
, reset the seed with np.random.seed(444)
, and then (seemingly) set it back to the 123
scenario with np.random.seed(seed)
. But then you'd already know what your random seed wasbefore, so you wouldn't need to do it that way. The next code section will also show that you can nottake the first number of any random state using np.random.get_state()[1][0]
and expect to recreate that exact scenario. Note that you'll most likely have to shut down and restart your kernel completely(or call np.random.seed(None)
) in order to be able to see this.
乍一看似乎并非如此,因为您可以使用np.random.seed(123)
,使用检索相同的数字seed = np.random.get_state()[1][0]
,使用 重置种子np.random.seed(444)
,然后(似乎)将其设置回使用 的123
场景np.random.seed(seed)
。但是你之前已经知道你的随机种子是什么,所以你不需要那样做。下一个代码部分还将显示您不能使用任何随机状态的第一个数字np.random.get_state()[1][0]
并期望重新创建该确切场景。请注意,您很可能必须关闭并重新启动你的内核完全(或电话np.random.seed(None)
),以便能够看到这一点。
The following snippet uses np.random.randint()
to generate 5 random integers between -10 and 10, as well as storing some info about the process:
以下代码段用于np.random.randint()
生成 -10 到 10 之间的 5 个随机整数,以及存储有关该过程的一些信息:
Snippet 1
片段 1
# 1. Imports
import pandas as pd
import numpy as np
# 2. set random seed
#seedSet = None
seedSet = 123
np.random.seed(seedSet)
# 3. describe random state
state = np.random.get_state()
state5 = np.random.get_state()[1][:5]
seedState = np.random.get_state()[1][0]
# 4. generate random numbers
random = np.random.randint(-10, 10, size = 5)
# 5. organize and present findings
df = pd.DataFrame.from_dict({'seedSet':seedSet, 'seedState':seedState, 'state':state, 'random':random})
print(df)
Notice that the column named seedState
is the same as the first number under state
. I could have printed it as a stand-alone number, but I wanted to keep it all in the same place. Also notice that, seedSet = 123
, and np.random.seed(seedSet)
so far have been commented out. And because no random seed has been set, your numbers will differ from mine. But that is not what is important here, but rather the internal consisteny of your results:
请注意,命名的列seedState
与 下的第一个数字相同state
。我可以把它作为一个独立的数字打印出来,但我想把它都放在同一个地方。另请注意,seedSet = 123
, 和np.random.seed(seedSet)
到目前为止已被注释掉。而且由于没有设置随机种子,您的数字将与我的不同。但这不是这里重要的,而是结果的内部一致性:
Output 1:
输出 1:
random seedSet seedState state
0 2 None 1558056443 1558056443
1 -1 None 1558056443 1808451632
2 4 None 1558056443 730968006
3 -4 None 1558056443 3568749506
4 -6 None 1558056443 3809593045
In this particular case seed = np.random.get_state()[1][0]
equals 1558056443
. And following the logic from Dong Justins answer (as well as my own answer prior to this edit), you could set the random seed with np.random.seed(1558056443)
and obtain the same random state. The next snippet will show that you can not:
在这种特殊情况下seed = np.random.get_state()[1][0]
等于1558056443
。并按照 Dong Justins 回答的逻辑(以及我在此编辑之前自己的回答),您可以设置随机种子np.random.seed(1558056443)
并获得相同的随机状态。下一个片段将表明您不能:
Snippet 2
片段 2
# 1. Imports
import pandas as pd
import numpy as np
# 2. set random seed
#seedSet = None
seedSet = 1558056443
np.random.seed(seedSet)
# 3. describe random state
#state = np.random.get_state()
state = np.random.get_state()[1][:5]
seedState = np.random.get_state()[1][0]
# 4. generate random numbers
random = np.random.randint(-10, 10, size = 5)
# 5. organize and present findings
df = pd.DataFrame.from_dict({'seedSet':seedSet, 'seedState':seedState, 'state':state, 'random':random})
print(df)
Output 2:
输出 2:
random seedSet seedState state
0 8 1558056443 1558056443 1558056443
1 3 1558056443 1558056443 1391218083
2 7 1558056443 1558056443 2754892524
3 -8 1558056443 1558056443 1971852777
4 4 1558056443 1558056443 2881604748
See the difference? np.random.get_state()[1][0]
is identical for Output 1 and Output 2, but the rest of the output is not (most importantly the random numbers are not the same). So, as ali_m already has clearly stated:
看到不同?np.random.get_state()[1][0]
输出 1 和输出 2 相同,但输出的其余部分不同(最重要的是随机数不相同)。所以,正如 ali_m 已经明确说明的那样:
It's therefore impossible to map every RNG state to a unique integer seed.
因此不可能将每个 RNG 状态映射到唯一的整数种子。