我正在培训有关COLAB的加强学习计划,并希望保持其可重复性,因此我在开始时设置了随机种子,
import random
random.seed(1)
import numpy as np
np.random.seed(1)
即Colab会不时杀死我的执行,因此我需要保存一些检查点,例如模型参数为了继续下去。现在我的问题是如何保存“播种”进度?我发现,如果我在恢复时重新固定种子,那么生成的随机数将返回初始执行。
例如,
>>> random.seed(40)
>>> random.random()
0.4586
>>> random.random()
0.8778
# the next is >>> random.random()
# 0.0318
# while continue execution
>>> random.seed(40)
>>> random.random()
0.4586 # I want this to be 0.0318
谢谢!
I am training a reinforcement learning program on Colab and wish to maintain its reproducibility so I set random seeds at the beginning by
import random
random.seed(1)
import numpy as np
np.random.seed(1)
The problem is that Colab would kill my execution from time to time, so I will need to save some checkpoints such as model parameters in order for it to continue. Now my question is how may I save the "seeding" progress? I found that if I reinit my seed while resuming, the random numbers generated go back to the initial execution.
For instance
>>> random.seed(40)
>>> random.random()
0.4586
>>> random.random()
0.8778
# the next is >>> random.random()
# 0.0318
# while continue execution
>>> random.seed(40)
>>> random.random()
0.4586 # I want this to be 0.0318
Thanks!
发布评论
评论(1)
感谢您指出正确的方向的 @Jasonharper的评论!
参考 -
参考 -
https://numpy.org/doc/stable/reference/random/generated/numpy.random.get_state.html;
Thanks for @jasonharper's comment for pointing out the right direction!
ref - https://www.w3schools.com/python/ref_random_setstate.asp
ref -
https://numpy.org/doc/stable/reference/random/generated/numpy.random.get_state.html;
https://numpy.org/doc/stable/reference/random/generated/numpy.random.set_state.html#numpy.random.set_state