PyTorch torch.cuda.manual_seed Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.cuda.manual_seedis a function in PyTorch used to set the CUDA random seed. It sets the random seed for the current CUDA device to ensure the reproducibility of CUDA operations.

Function Definition

torch.cuda.manual_seed(seed)

Usage Example

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 result
    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 参考手册Pytorch torch Reference Manual

Other Extensions