PyTorch torch.set_rng_state Function
PyTorch torch Reference Manual
torch.set_rng_stateIs a function in PyTorch used to set the state of the random number generator.
Function Definition
torch.set_rng_state(new_state)
Parameter Description
new_state- The new random number generator state (Tensor type)
Usage Example
Example
import torch
# Save the current random number generator state
saved_state = torch.get_rng_state()
# Set random seed
torch.manual_seed(123)
# Generate some random numbers
print("First random numbers:", torch.rand(3))
# Restore the previously saved state
torch.set_rng_state(saved_state)
# Regenerate random numbers, should be the same as before
print("Random numbers after restore:", torch.rand(3))
# Save the current random number generator state
saved_state = torch.get_rng_state()
# Set random seed
torch.manual_seed(123)
# Generate some random numbers
print("First random numbers:", torch.rand(3))
# Restore the previously saved state
torch.set_rng_state(saved_state)
# Regenerate random numbers, should be the same as before
print("Random numbers after restore:", torch.rand(3))
Other Extensions