PyTorch torch.set_rng_state function
PyTorch torch reference manual
torch.set_rng_state is a function in PyTorch used to set the random number generator state.
Function definition
torch.set_rng_state(new_state)
Parameter Description
new_state- the new random number generator state (Tensor type)
Usage examples
Example
import
torch
Save the current random number generator state
saved_state
=
torch.
get_rng_state
(
)
Set the random seed
torch.
manual_seed
(
123
)
Generate some random numbers
(
"First random numbers:"
,
torch.
rand
(
3
)
)
Restore the previously saved state
torch.
set_rng_state
(
saved_state
)
Regenerate random numbers, which should be the same as before
(
"Random numbers after restoring:"
,
torch.
rand
(
3
)
)