How can I retrieve the current seed of NumPy's

2019-02-05 11:12发布

The following imports NumPy and sets the seed.

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 documentation doesn't show an obvious answer.

How do I retrieve the current seed used by numpy.random, assuming I did not set it manually?

I want to use the current seed to carry over for the next iteration of a process.

3条回答
Ridiculous、
2楼-- · 2019-02-05 11:33

Check the first element of the array returned by np.random.get_state(), it seems exactly the random seed to me.

查看更多
爷的心禁止访问
3楼-- · 2019-02-05 11:37

The short answer is that you simply can't (at least not in general).

The Mersenne Twister RNG used by numpy has 219937-1 possible internal states, whereas a single 64 bit integer has only 264 possible values. It's therefore impossible to map every RNG state to a unique integer seed.

You can get 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).

The tuple returned by get_state can be used much like a seed in order to create reproducible sequences of random numbers. For example:

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]
查看更多
别忘想泡老子
4楼-- · 2019-02-05 11:46

I have edited this answer heavily after I found out some more details about the problem at hand. As it now stands, I'm hoping that it serves well as a clarification to the answer from ali_m, and as an important correction to the answer from Dong Justin.

These are my findings:

  1. After setting the random seed using np.random.seed(X) you can find it again using np.random.get_state()[1][0].
  2. It will, however, be of little use to you.

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].

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.

enter image description here

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] will give you 123. Perfect? Not quite, because:

Statement 2 - It will, however, be of little use to you:

It may seem so at first though, because you could use 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'll already know what your random seed was before, so you wouldn't need to do it that way. The next code section will also show that you can not take 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.

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:

Snippet 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:

Output 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 wll as my own answer prior to this edit), you could get set the random seed with np.random.seed(1558056443) and obtain the same random state. The next snippet will show that you can not:

Snippet 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:

   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:

It's therefore impossible to map every RNG state to a unique integer seed.

查看更多
登录 后发表回答