如何保存Python随机种子进度?

发布于 2025-01-25 06:27:52 字数 585 浏览 0 评论 0 原文

我正在培训有关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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2025-02-01 06:27:52

感谢您指出正确的方向的 @Jasonharper的评论!

  1. 对于随机模块,使用getState()和setState()。

例如

>>> random.seed(40)
>>> random.random()
0.4586
>>> random.random()
0.8778
>>> state = random.getstate()
>>> random.random()
0.0318
>>> random.setstate(state)
>>> random.random()
0.0318

参考 -

  1. 对于numpy.random,使用get_state()和set_state()。这是它的工作方式

例如

>>> import numpy as np
>>> np.random.seed(1)
>>> np.random.rand(1,1)
array([[0.417022]])
>>> state = np.random.get_state()
>>> np.random.rand(1,1)
array([[0.72032449]])
>>> np.random.set_state(state)
>>> np.random.rand(1,1)
array([[0.72032449]])

参考 -
https://numpy.org/doc/stable/reference/random/generated/numpy.random.get_state.html;

Thanks for @jasonharper's comment for pointing out the right direction!

  1. For random module, use getstate() and setstate().

e.g.

>>> random.seed(40)
>>> random.random()
0.4586
>>> random.random()
0.8778
>>> state = random.getstate()
>>> random.random()
0.0318
>>> random.setstate(state)
>>> random.random()
0.0318

ref - https://www.w3schools.com/python/ref_random_setstate.asp

  1. For numpy.random, use get_state() and set_state(). Here's how it works

e.g.

>>> import numpy as np
>>> np.random.seed(1)
>>> np.random.rand(1,1)
array([[0.417022]])
>>> state = np.random.get_state()
>>> np.random.rand(1,1)
array([[0.72032449]])
>>> np.random.set_state(state)
>>> np.random.rand(1,1)
array([[0.72032449]])

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文