Answers.Body: <p>There is this nice function from Kaggle:</p>
  <pre><code>def seed_everything(seed=1234):
      random.seed(seed)
      os.environ['PYTHONHASHSEED'] = str(seed)
      np.random.seed(seed)
      torch.manual_seed(seed)
      torch.cuda.manual_seed(seed)
      torch.backends.cudnn.deterministic = True
  
  seed_everything()
  </code></pre>
  <p>There are several version of this function for torch, tensorflow and combinations. This specific one is from <a href="https://www.kaggle.com/bminixhofer/deterministic-neural-networks-using-pytorch" rel="nofollow noreferrer">here</a>.</p>
  <p>Run this before you run your model. But not only the fitting function. It needs to be before the validation split function as well.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-13T16:35:06.100Z
Answers.LastActivityDate: 2021-09-13T16:35:06.100Z
Answers.UserDisplayName: Soerendip
Body: <p>I'm using PyTorch (1.7.1), PyTorch Geometric (1.6.3), NVIDIA Cuda (11.2).</p>
  <p>I need to make a neural network reproducible for a competition.</p>
  <p>However, when I try:</p>
  <pre><code>device = torch.device('cuda:0')
  rand = 123
  torch.manual_seed(rand)
  torch.cuda.manual_seed(rand)
  torch.cuda.manual_seed_all(rand)
  torch.backends.cudnn.deterministic = True
  torch.backends.cudnn.benchmark = False
  np.random.seed(rand)
  random.seed(rand)
  </code></pre>
  <p>the results are different each time I run the code. How can I fix it?</p>
  <p>(For a reference, it always comes out the same in <code>device = torch.device('cpu')</code>.)</p>
  
Tags: <machine-learning><deep-learning><pytorch>
Title: Reproducible PyTorch Model
ThreadText: # Reproducible PyTorch Model
  
  (Asked by: username_0 on 2021-09-11)
  
  I'm using PyTorch (1.7.1), PyTorch Geometric (1.6.3), NVIDIA Cuda (11.2).
  
  I need to make a neural network reproducible for a competition.
  
  However, when I try:
  
  ```
  device = torch.device('cuda:0')
  rand = 123
  torch.manual_seed(rand)
  torch.cuda.manual_seed(rand)
  torch.cuda.manual_seed_all(rand)
  torch.backends.cudnn.deterministic = True
  torch.backends.cudnn.benchmark = False
  np.random.seed(rand)
  random.seed(rand)
  
  ```
  
  the results are different each time I run the code. How can I fix it?
  
  (For a reference, it always comes out the same in `device = torch.device('cpu')`.)
  
  **Tags:** machine-learning, deep-learning, pytorch
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-09-13. Score: 2
  
  There is this nice function from Kaggle:
  
  ```
  def seed_everything(seed=1234):
      random.seed(seed)
      os.environ['PYTHONHASHSEED'] = str(seed)
      np.random.seed(seed)
      torch.manual_seed(seed)
      torch.cuda.manual_seed(seed)
      torch.backends.cudnn.deterministic = True
  
  seed_everything()
  
  ```
  
  There are several version of this function for torch, tensorflow and combinations. This specific one is from [here](https://www.kaggle.com/bminixhofer/deterministic-neural-networks-using-pytorch).
  
  Run this before you run your model. But not only the fitting function. It needs to be before the validation split function as well.
  
  
  ---
  
  