有没有办法读取 jupyter 笔记本中随机数生成器的“状态”?
例如,如果我运行一个指定神经网络架构的单元,然后在不指定种子的情况下对某些数据进行训练,有没有办法可以读取用于运行它的种子?
最佳答案
您确实可以读取(并存储)RNG 的当前状态,但这在每次使用时都会发生变化,即之后您无法执行您所描述的操作已运行该单元。
这是一个示例(由于您已使用keras
标记了问题,我假设您实际上对 Numpy RNG 感兴趣,这是 Keras 中使用的):
import numpy as np
current_state = np.random.get_state()
# produce some random numbers:
a = np.random.randn(3)
a
# array([-0.44270351, 1.42933504, 2.11385353])
# Now, restoring the RNG state and producing again 3 random numbers, you get the same result:
np.random.set_state(current_state)
b = np.random.randn(3)
b
# array([-0.44270351, 1.42933504, 2.11385353])
a == b
# array([ True, True, True], dtype=bool)
关于python - 读取 jupyter 笔记本的随机种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45306086/