PyTorch torch.manual_seed Function
Pytorch torch Reference Manual
torch.manual_seedis a function in PyTorch used to set the seed for the random number generator. Setting the seed ensures the reproducibility of results.
This is very important when experimental results need to be reproducible, such as in debugging, paper reproduction, and other scenarios.
Function Definition
torch.manual_seed(seed)
Parameters:
seed(int): The random seed.
Return Value:
Nothing
Usage Examples
Example 1: Setting the Seed to Ensure Reproducibility
Example
import torch
# Set the random seed
torch.manual_seed(42)
# The random numbers generated each time are the same
x = torch.randn(3)
print("First time:", x)
# Reset the same seed
torch.manual_seed(42)
y = torch.randn(3)
print("Second time:", y)
print("Results are the same:", torch.equal(x, y))
# Set the random seed
torch.manual_seed(42)
# The random numbers generated each time are the same
x = torch.randn(3)
print("First time:", x)
# Reset the same seed
torch.manual_seed(42)
y = torch.randn(3)
print("Second time:", y)
print("Results are the same:", torch.equal(x, y))
The output is:
第一次: tensor([ 0.3367, 0.1288, 0.2345]) 第二次: tensor([ 0.3367, 0.1288, 0.2345]) 结果相同: True
Example 2: Fully Reproducible Training
Example
import torch
import random
import numpy as np
def set_seed(seed=42):
# Set the PyTorch seed
torch.manual_seed(seed)
# Set the NumPy seed
np.random.seed(seed)
# Set the Python random seed
random.seed(seed)
# Ensure good CUDA determinism (if used)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set the seed
set_seed(42)
# Generate random data
x = torch.randn(3, 4)
print(x)
import random
import numpy as np
def set_seed(seed=42):
# Set the PyTorch seed
torch.manual_seed(seed)
# Set the NumPy seed
np.random.seed(seed)
# Set the Python random seed
random.seed(seed)
# Ensure good CUDA determinism (if used)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set the seed
set_seed(42)
# Generate random data
x = torch.randn(3, 4)
print(x)
The output is:
tensor([[ 0.3367, 0.1288, 0.2345, 0.2303],
[-1.1229, -0.1863, 0.1735, -0.5524],
[ 0.6351, -0.2582, 0.4602, -0.5270]])
To fully guarantee reproducibility, you need to set the seeds of PyTorch, NumPy, and Python random simultaneously.
Other Extensions