Set isolated random seed for module

username: smonsays
cooked: <p>Is there a way of setting the random seed specifically for a module or an object derived from a particular class?</p>
<p>E.g. I have a module <code>environment.py</code> that contains multiple classes generating data in a stochastic process that is then used to update a model online:</p>
<pre><code class="lang-python">for t in time:
    observation = world.step(dt)
    model.update(observation)
    # action = model.decide_action()
    # world.apply_action(action)
</code></pre>
<p>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).</p>

username: albanD
cooked: <p>Hi,</p>
<p>You have few options depending on how you want to generate random numbers.</p>
<ul>
<li>You can get and set the rng state using <a href="https://pytorch.org/docs/stable/torch.html#torch.get_rng_state" rel="nofollow noopener">this</a>. So you will have to manually get the state and reset it depending on your needs.</li>
<li>You can create your own <a href="https://pytorch.org/docs/stable/torch.html#generators" rel="nofollow noopener">Generator</a> and pass it as an argument to inplace random function so that each module will have its own RNG.</li>
</ul>

username: smonsays
cooked: <p>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:</p>
<pre><code class="lang-python"># 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))
</code></pre>
<p>I would expect this code snippet to produce the same result regardless of what code surrounds it. Why is that not the case?</p>
<p>I assume that the solution would involve using <code>torch.set_rng_state(state)</code> instead of <code>torch.manual_seed(2019)</code>. How can I generate a valid <code>state</code> from an integer seed?</p>

username: albanD
cooked: <p>Hi,</p>
<p>This will work but if you set the seed to a fixed value, the numbers you generate after will always be the same.<br>
I though you wanted a way to have each module doing rng interleaved with others.</p>

username: smonsays
cooked: <p>EDIT: You were right, this works. I had a different bug preventing the proper usage of the random number generator. Thanks for your help!</p>
<p>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,</p>
<pre><code class="lang-python">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)
</code></pre>
<p>but the observations are different for different models and I am not sure why that is the case.</p>