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

PyTorch torch.cuda.manual_seed function

PyTorch torch.cuda.manual_seed function


PyTorch torch reference manual PyTorch torch reference manual

torch.cuda.manual_seed is a PyTorch function for setting the CUDA random seed. It sets the random seed for the current CUDA device to ensure reproducible CUDA operations.

Function definition

torch.cuda.manual_seed(seed)


Usage examples

Example

import

torch

Set the CUDA random seed

if

torch.

cuda

.

is_available

(

)

:

    torch.

cuda

.

manual_seed

(

42

)

    x

=

torch.

randn

(

3

,

3

)

.

cuda

(

)

   

print

(

f

"Random tensor on CUDA: {x}"

)

   

The same seed produces the same results

    torch.

cuda

.

manual_seed

(

42

)

    y

=

torch.

randn

(

3

,

3

)

.

cuda

(

)

   

print

(

f

"Random tensor with same seed: {y}"

)

else

:

   

print

(

"CUDA not available"

)


PyTorch torch reference manual PyTorch torch reference manual