The Random Seed

username: ForeverZH0204
cooked: <p>Today i use such method to set random seed<br>
The <code>args.seed</code> is default to 1</p>
<pre><code>torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)
</code></pre>
<p>But everytime the result is slightly different, why?<br>
I use PyTorch0.4</p>

username: ptrblck
cooked: <p>Are you using the GPU with cuDNN?<br>
If so, you could set the cuDNN behavior to be deterministic, which would unfortunately trade performance for determinism.</p>
<pre><code class="lang-auto">torch.backends.cudnn.deterministic = True
</code></pre>
<p>Also, are you using some other libraries sampling random numbers?<br>
If so, you should also seed them.</p>

username: ForeverZH0204
cooked: <p>No, i use none of them.<br>
If i haven’t use</p>
<pre><code>import cudnn
</code></pre>
<p>means no use of the module of cuDNN,i only use torch and torchvision</p>
<p>Does <code>Xavier Initialization</code> matter?I use such way to init my network.</p>

username: ptrblck
cooked: <p>The initializations should yield the same random numbers, if the seed was set.<br>
To see, if you are using cuDNN, use <code>print(torch.backends.cudnn.enabled)</code>.</p>

username: pinocchio
cooked: <p>Why can’t you write meaningful titles? It makes everyone’s life harder. Please write meaningful titles.</p>
<hr>
<p>related:</p>
<ul>
<li><a href="https://discuss.pytorch.org/t/best-practices-for-seeding-random-numbers-on-gpu/18751" class="inline-onebox">Best practices for seeding random numbers on gpu?</a></li>
<li><a href="https://discuss.pytorch.org/t/the-random-seed/19516/4" class="inline-onebox">The Random Seed</a></li>
<li><a href="https://discuss.pytorch.org/t/best-practices-for-generating-a-random-seed-to-seed-pytorch/52894/2" class="inline-onebox">Best practices for generating a random seed to seed Pytorch?</a></li>
</ul>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/57416925/best-practices-for-generating-a-random-seeds-to-seed-pytorch" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/3167448/pinocchio" target="_blank" rel="nofollow noopener">
    <img alt="Pinocchio" src="https://www.gravatar.com/avatar/ba92064b084638cb903cbd271120be64?s=128&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="128" height="128">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/57416925/best-practices-for-generating-a-random-seeds-to-seed-pytorch" target="_blank" rel="nofollow noopener">Best practices for generating a random seeds to seed Pytorch?</a>
</h4>

<div class="tags">
  <strong>pytorch</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/3167448/pinocchio" target="_blank" rel="nofollow noopener">
    Pinocchio
  </a>
  on <a href="https://stackoverflow.com/questions/57416925/best-practices-for-generating-a-random-seeds-to-seed-pytorch" target="_blank" rel="nofollow noopener">04:20PM - 08 Aug 19 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>