Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.set_rng_state function

PyTorch torch.set_rng_state function


PyTorch torch reference manual 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


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

print

(

"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

print

(

"Random numbers after restoring:"

,

torch.

rand

(

3

)

)


PyTorch torch reference manual PyTorch torch reference manual