What is the difference between torch.manual.seed() and torch.set_rng_state()?

JimW: <p>I have trained a stochastic model. I first saved the checkpoint file from that epoch and then loaded it back to my evaluation code.</p>
<p>Here is the first experiment I tried:</p>
<pre><code class="lang-auto">torch.manual.seed(0)
for i in range(10):   # Repeat the evaluation on this same model 10 times.
    for image, label in data_no_shuffle:
          preds = mymodel(image)
          ....
    print("Accuracy at {} is {}".format(i, accuracy))
</code></pre>
<p>The accuracy numbers are quite different in the above experiment.</p>
<p>The 2nd experiment I did is like this:</p>
<pre><code class="lang-auto">for i in range(10):   # Repeat the evaluation on this same model 10 times.
     torch.manual.seed(0)
     for image, label in data_no_shuffle:
          preds = mymodel(image)
           ....
     print("Accuracy at {} is {}".format(i, accuracy))
</code></pre>
<p>And I see the accuracy is a same value everytime, which makes sense.<br>
However, if I do this:<br>
prev_state = torch.random.get_rng_state()</p>
<pre><code class="lang-auto">for i in range(10):   # Repeat the evaluation on this same model 10 times.
    torch.random.set_rng_state(prev_state)
    for image, label in data_no_shuffle:
          preds = mymodel(image)
          ....
    print("Accuracy at {} is {}".format(i, accuracy))
    prev_state = torch.random.get_rng_state()
</code></pre>
<p>The accuray from the above experiment became random again.</p>
<p>So what is the difference between torch.manual.seed() and torch.random.set_rng_state()?  If I were to reproduce result of a stochastic model multiple times in python code, which one should I use?</p>

soulitzer: <p>Shouldn’t you be doing:</p>
<pre><code class="lang-auto">rng_state = torch.random.get_rng_state()
for i in range(10):   # Repeat the evaluation on this same model 10 times.
    torch.random.set_rng_state(rng_state)
    for image, label in data_no_shuffle:
          preds = mymodel(image)
          ....
    print("Accuracy at {} is {}".format(i, accuracy))
</code></pre>
<p>What you are currently doing is a noop because you are getting the rng state after the state has been updated, and in the following iteration immediately setting the rng state to the value just saved</p>

JimW: <p>Thanks for the guidance. I am not very familiar with these two functions.<br>
What operation will cause rng_state to change?<br>
Which one should I use to make the result reproducable (seed or rng_state)?</p>

soulitzer: <p>Doing any random operation, e.g. drop_out, rand, randn will advance the rng_state.</p>
<p>If you want to make your results reproduce across runs, e.g. you have a blah.py file and you run want running python blah.py two times in a row to produce the same result - you should use a seed.</p>
<p>If you don’t care about reproducibility between runs (in fact you might even want results to be different for each run), and all you care about is that each iteration of the loop computes the function and produces the same results - you can use getting and setting rng state as in the above.</p>

JimW: <p>Thank yhou so much. Can I ask several questions about torch.manual_seed?<br>
I am not sure if seed value 0 something special.  I mean sometimes, I will see a seed equal to a long int number like “105481905719405” (Just a fake example, maybe it is not correct), instead of a integer like 0, 1, 2, 3.<br>
So am I correct to say seed also changes over time?  For example, you set it to 0 at the beginning, and later after 1 epoch, it becomes 127350985103900, and it will become something more different after 2 epochs.<br>
Does the absolute value of seed mean anything?  Can I say torch.manual_seed(0) and torch.manual_seed(1)  are closer to each other, but torch.manual_seed(100) is far away from both of them?</p>

soulitzer: <p>Usually the seed is passed to some function which will use that seed to initialize the rng state. I’m not familiar with the inner workings of that function, but I don’t think you should be able to infer “closeness”.</p>
<blockquote>
<p>So am I correct to say seed also changes over time?</p>
</blockquote>
<p>It’s more correct to say that the rng state changes over time across iterations.</p>