DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

manual_seed(), initial_seed() and seed() in PyTorch

*My post explains rand(), rand_like(), randn(), randn_like(), randint() and randperm().

manual_seed() can set a 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(int, float, bool or number of str) with torch is seed(Required).
  • 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() and randperm().
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

Top comments (0)