manual_seed, initial_seed and seed in PyTorch

hyperkai

Super Kai (Kazuya Ito)

Posted on May 24, 2024

manual_seed, initial_seed and seed in PyTorch

Buy Me a Coffee

*My post explains how to create and acceess a tensor.

manual_seed() can set a specific seed to generate the same random numbers as shown below:

*Memos:

  • initial_seed() can be used with torch but not with a tensor.
  • The 1st argument with torch is seed(Required-Type:int, float, bool or number of str).
  • A positive and negative seed is different.
  • You must use manual_seed() just before a random number generator each time otherwise the same random numbers are not generated.
  • The effect of manual_seed() lasts until manual_seed() or seed() is used next time. *seed() is explained at the end of this post.
  • In PyTorch, there are random number generators such as rand(), randn(), randint(), randperm() and normal().
  • My post explains rand() and rand_like().
  • My post explains randn() and randn_like().
  • My post explains randint() and randperm().
  • My post explains normal().
import torch

torch.manual_seed(seed=8)
torch.rand(3) # tensor([0.5979, 0.8453, 0.9464])

torch.manual_seed(seed=8)
torch.rand(3) # tensor([0.5979, 0.8453, 0.9464])
Enter fullscreen mode Exit fullscreen mode

Be careful, not using manual_seed() just before a random number generator each time cannot generate the same random numbers as shown below:

import torch

torch.manual_seed(seed=8)
torch.rand(3) # tensor([0.5979, 0.8453, 0.9464])

# torch.manual_seed(seed=8)
torch.rand(3) # tensor([0.2965, 0.5138, 0.6443])
Enter fullscreen mode Exit fullscreen mode

And, you can use several types of values for seed argument as shown below:

import torch

torch.manual_seed(seed=8)
torch.rand(3) # tensor([0.5979, 0.8453, 0.9464])

torch.manual_seed(seed=8.)
torch.rand(3) # tensor([0.5979, 0.8453, 0.9464])

torch.manual_seed(seed=True)
torch.rand(3) # tensor([0.7576, 0.2793, 0.4031])

torch.manual_seed(seed='8')
torch.rand(3) # tensor([0.5979, 0.8453, 0.9464])

torch.manual_seed(seed='-8')
torch.rand(3) # tensor([0.8826, 0.3959, 0.5738])
Enter fullscreen mode Exit fullscreen mode

initial_seed() can get the current seed as shown below:

*Memos:

  • initial_seed() can be used with torch but not with a tensor.
  • An initial seed is randomly set.
import torch

torch.manual_seed(seed=8)

torch.initial_seed() # 8
Enter fullscreen mode Exit fullscreen mode

seed() can randomly set a seed to generate random numbers as shown below:
*Memos:

  • seed() can be used with torch but not with a tensor:
  • The effect of seed() lasts until seed() or manual_seed() is used next time.
import torch

torch.seed() # 13141386358708808900

torch.seed() # 6222667032495401621

torch.seed() # 5598609927030438366
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
hyperkai
Super Kai (Kazuya Ito)

Posted on May 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related