PyTorch torch.cuda.seed_all 函数
torch.cuda.seed_all 是 PyTorch 中用于设置所有 CUDA 设备随机种子的函数。为所有 CUDA 设备设置随机种子(功能与 manual_seed_all 相同)。
函数定义
torch.cuda.seed_all()
使用示例
实例
import torch
# 为所有 CUDA 设备设置随机种子
if torch.cuda.is_available():
torch.cuda.seed_all()
x = torch.randn(3, 3).cuda()
print(f"Random tensor: {x}")
print(f"CUDA device count: {torch.cuda.device_count()}")
else:
print("CUDA not available")
# 为所有 CUDA 设备设置随机种子
if torch.cuda.is_available():
torch.cuda.seed_all()
x = torch.randn(3, 3).cuda()
print(f"Random tensor: {x}")
print(f"CUDA device count: {torch.cuda.device_count()}")
else:
print("CUDA not available")

Pytorch torch 参考手册