如何将 numpy 种子恢复到原来的状态?
how to restore numpy seed back to its former state?
我有一个函数可以做一些随机的事情..
我想给它输入种子,所以对于相同的种子,输出将是相同的..
但是像这样在开始时启动种子:
def generate_random_number(seed):
np.random.seed(seed)
magics = np.random.random(1)
more_magics = ...
return magics, more_magics
会导致调用此方法后,种子被破坏
有没有办法“保存”当前种子状态并在以后恢复?
所以它看起来像这样:
def generate_random_number_without_ruining_for_others(seed):
old_seed = np.random.get_seed()
np.random.seed(seed)
magics = np.random.random(1)
more_magics = ...
np.random.seed(old_seed)
return magics, more_magics
当然,命令:np.random.get_seed()
是一个虚构的函数..有这样的东西吗?
或者在不破坏每个人的随机状态的情况下生成“随机”(直到种子输入)输出的替代方法?
(实际的随机过程在实践中要复杂得多,它会循环生成矩阵,所以为了简单起见,我想坚持在开始时启动种子的格式)
据我所知,您无法重置默认的随机生成器。实际上 seed
的文档说不要使用它:
This is a convenience, legacy function.
The best practice is to not reseed a BitGenerator, rather to
recreate a new one. 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)))
在此基础上,您可以构建自己的随机引擎并保存随机状态。这是一个例子:
import numpy as np
rs = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(123456789)))
savedState = rs.get_state() # Save the state
v1 = rs.randint(0, 100, 7) # array([28, 72, 18, 84, 6, 78, 92])
v2 = rs.randint(0, 100, 7) # array([60, 91, 4, 29, 43, 68, 52])
rs.set_state(savedState) # Reset the state
v3 = rs.randint(0, 100, 7) # array([28, 72, 18, 84, 6, 78, 92])
v4 = rs.randint(0, 100, 7) # array([60, 91, 4, 29, 43, 68, 52])
assert(np.array_equal(v1, v3)) # True
assert(np.array_equal(v2, v4)) # True
我有一个函数可以做一些随机的事情..
我想给它输入种子,所以对于相同的种子,输出将是相同的..
但是像这样在开始时启动种子:
def generate_random_number(seed):
np.random.seed(seed)
magics = np.random.random(1)
more_magics = ...
return magics, more_magics
会导致调用此方法后,种子被破坏
有没有办法“保存”当前种子状态并在以后恢复?
所以它看起来像这样:
def generate_random_number_without_ruining_for_others(seed):
old_seed = np.random.get_seed()
np.random.seed(seed)
magics = np.random.random(1)
more_magics = ...
np.random.seed(old_seed)
return magics, more_magics
当然,命令:np.random.get_seed()
是一个虚构的函数..有这样的东西吗?
或者在不破坏每个人的随机状态的情况下生成“随机”(直到种子输入)输出的替代方法?
(实际的随机过程在实践中要复杂得多,它会循环生成矩阵,所以为了简单起见,我想坚持在开始时启动种子的格式)
据我所知,您无法重置默认的随机生成器。实际上 seed
的文档说不要使用它:
This is a convenience, legacy function.
The best practice is to not reseed a BitGenerator, rather to
recreate a new one. 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)))
在此基础上,您可以构建自己的随机引擎并保存随机状态。这是一个例子:
import numpy as np
rs = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(123456789)))
savedState = rs.get_state() # Save the state
v1 = rs.randint(0, 100, 7) # array([28, 72, 18, 84, 6, 78, 92])
v2 = rs.randint(0, 100, 7) # array([60, 91, 4, 29, 43, 68, 52])
rs.set_state(savedState) # Reset the state
v3 = rs.randint(0, 100, 7) # array([28, 72, 18, 84, 6, 78, 92])
v4 = rs.randint(0, 100, 7) # array([60, 91, 4, 29, 43, 68, 52])
assert(np.array_equal(v1, v3)) # True
assert(np.array_equal(v2, v4)) # True