manual_seed(), initial_seed() and seed() in PyTorch
Tags: pytorch, manualseed, initialseed, seed
Published: 2024-05-24 13:34:12

*[My post](https://dev.to/hyperkai/rand-randlike-randn-randnlike-randint-and-randperm-in-pytorch-31nc) explains [rand()](https://pytorch.org/docs/stable/generated/torch.rand.html), [rand_like()](https://pytorch.org/docs/stable/generated/torch.rand_like.html), [randn()](https://pytorch.org/docs/stable/generated/torch.randn.html), [randn_like()](https://pytorch.org/docs/stable/generated/torch.randn_like.html), [randint()](https://pytorch.org/docs/stable/generated/torch.randint.html) and [randperm()](https://pytorch.org/docs/stable/generated/torch.randperm.html).

[manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html) can set a seed to generate the same random numbers as shown below:

*Memos:
- `initial_seed()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) 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()](https://pytorch.org/docs/stable/generated/torch.seed.html) is used next time. *`seed()` is explained at the end of this post. 
- In PyTorch, there are random number generators such as [rand()](https://pytorch.org/docs/stable/generated/torch.rand.html), [randn()](https://pytorch.org/docs/stable/generated/torch.randn.html), [randint()](https://pytorch.org/docs/stable/generated/torch.randint.html) and [randperm()](https://pytorch.org/docs/stable/generated/torch.randperm.html).

```python
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:

```python
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:

```python
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()](https://pytorch.org/docs/stable/generated/torch.initial_seed.html) 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.

```python
import torch

torch.manual_seed(seed=8)

torch.initial_seed() # 8
```

[seed()](https://pytorch.org/docs/stable/generated/torch.seed.html) 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.

```python
import torch

torch.seed() # 13141386358708808900

torch.seed() # 6222667032495401621

torch.seed() # 5598609927030438366
```