现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.cuda.manual_seed 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.cuda.manual_seed 是 PyTorch 中用于设置 CUDA 随机种子的函数。为当前 CUDA 设备设置随机种子,确保 CUDA 操作的可重复性。

函数定义

torch.cuda.manual_seed(seed)

使用示例

实例

import torch

# 设置 CUDA 随机种子
if torch.cuda.is_available():
    torch.cuda.manual_seed(42)
    x = torch.randn(3, 3).cuda()
    print(f"Random tensor on CUDA: {x}")

    # 相同种子产生相同结果
    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 参考手册