*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
isseed
(Required-Type:int
,float
,bool
ornumber
ofstr
). - 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 untilmanual_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])
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])
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])
initial_seed() can get the current seed as shown below:
*Memos:
-
initial_seed()
can be used withtorch
but not with a tensor. - An initial seed is randomly set.
import torch
torch.manual_seed(seed=8)
torch.initial_seed() # 8
seed() can randomly set a seed to generate random numbers as shown below:
*Memos:
-
seed()
can be used withtorch
but not with a tensor: - The effect of
seed()
lasts untilseed()
ormanual_seed()
is used next time.
import torch
torch.seed() # 13141386358708808900
torch.seed() # 6222667032495401621
torch.seed() # 5598609927030438366
Top comments (0)