YouTip LogoYouTip

Pytorch Torch Seed

```html PyTorch torch.seed Function | Tutorial [Tutorial -- Learning not only technology, but also dreams!]

PyTorch Tutorial

PyTorch torch.seed Function

The torch.seed() function sets the random seed for generating random numbers in PyTorch. This function is used to ensure reproducibility of results when running code that involves random operations, such as weight initialization or data shuffling.

Syntax:

torch.seed()

Parameters:

  • None

Returns:

  • A 64-bit unsigned integer representing the newly generated random seed.

Example:

import torch

# Set the random seed
torch.seed()

# Now random operations will be based on the new seed
x = torch.rand(3, 3)
print(x)

Note:

  • Calling torch.seed() automatically seeds all random number generators used by PyTorch, including those for CPU and CUDA (if available).
  • This function does not accept any arguments. It generates a random seed from the system's entropy source.
  • If you need to set a specific seed value, use torch.manual_seed(seed) instead.

Related Functions:

  • torch.manual_seed() β€” Set a specific seed value.
  • torch.cuda.manual_seed() β€” Set the seed for CUDA random number generation.
  • torch.cuda.manual_seed_all() β€” Set the seed for all CUDA devices.
```
← Pytorch Torch Set_Default_DtypPytorch Torch Scatter_Reduce β†’