What does seeds mean in PyTorch


(Ravneet Punia) #1

I am learning PyTorch and i came up with a code i.e “torch.manual_seed(7)” Explanation I got is its setting up random seed. But what is seed?? What its physical significance??


(Alban D) #2

Hi,

When you ask for random number in pytorch (with torch.rand() or t.uniform_() for example) these random numbers are generated from a specific algorithm.
A nice property of this algorithm is that you can fix it’s starting point and it will always generate the same random numbers afterwards. That way, you can have reproducible code even with random functions in it.
The seed is just this starting point.


(Ravneet Punia) #3

@albanD Thanks for answer, means when I am writing “torch.manual_seed(7)” I want same 7 random number ???


(Alban D) #4

That means that you change the “starting point” of the algorithm to be 7. And it will move from there and generate random numbers.
If later you set the starting point again to 7 and ask for random number, you will get the same !

import torch

print("Print 5 random numbers")
print(torch.rand(5))

print("Set the starting point to 15678 (this could be any integer number)")
torch.manual_seed(15678)

print("Print 5 random numbers")
print(torch.rand(5))

print("Print 5 more random numbers")
print(torch.rand(5))

print("Set the starting point to 15678 again")
torch.manual_seed(15678)

print("Print 5 random numbers")
print(torch.rand(5))