pythonrandom-seedseeding

How to save python random seed progress?


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!


Solution

  • 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