Set isolated random seed for module

Is there a way of setting the random seed specifically for a module or an object derived from a particular class?

E.g. I have a module environment.py that contains multiple classes generating data in a stochastic process that is then used to update a model online:

for t in time:
    observation = world.step(dt)
    model.update(observation)
    # action = model.decide_action()
    # world.apply_action(action)

I would like to be able to generate the same data for a given random seed for different models. I tried setting the global seed, but that did not produce the same data for different models. Unfortunately, in my case it is also not possible to pre-generate the whole data set as a workaround, as in the future I need to allow acting on the world (in which case I am aware the stochastic process will differ between runs).

1 Like

Hi,

You have few options depending on how you want to generate random numbers.

  • You can get and set the rng state using this. So you will have to manually get the state and reset it depending on your needs.
  • You can create your own Generator and pass it as an argument to inplace random function so that each module will have its own RNG.
1 Like

Sorry to open this up again, but I have some remaining questions. It is not exactly clear to me, why the following does not work independently of what the surrounding code would be:

# Set a deterministic random seed
torch.manual_seed(2019)

# Pregenerate the sequence of random numbers and wrap in generator
noise_generator = (t for t in torch.randn(10))

I would expect this code snippet to produce the same result regardless of what code surrounds it. Why is that not the case?

I assume that the solution would involve using torch.set_rng_state(state) instead of torch.manual_seed(2019). How can I generate a valid state from an integer seed?

Hi,

This will work but if you set the seed to a fixed value, the numbers you generate after will always be the same.
I though you wanted a way to have each module doing rng interleaved with others.

EDIT: You were right, this works. I had a different bug preventing the proper usage of the random number generator. Thanks for your help!

My current workaround was to pass pregenerated random tensors as generator objects to the modules manually. For some reason that does not result in the same output for me. E.g. in the above outlined logic I do,

import environment

torch.manual_seed(2019)
noise_generator = (t for t in torch.randn(10))
world = environment.World(noise_generator)

for t in time:
    observation = world.step(dt)
    model.update(observation)

but the observations are different for different models and I am not sure why that is the case.