【AI】Pytorch 平台随机种子设置说明

目录

随机种子设置(CPU操作)

GPU随机种子设置(GPU操作)

GPU操作确定性设置


随机种子设置(CPU操作)

python 复制代码
torch.manual_seed(42)  # Setting the seed

当产生随机数的时候,CPU和GPU之间的随机种子并不同步,因此,还需要另外设置GPU上面的随机种子,确保代码可复现。

GPU随机种子设置(GPU操作)

python 复制代码
# GPU operations have a separate seed we also want to set
if torch.cuda.is_available():
    torch.cuda.manual_seed(42)
    torch.cuda.manual_seed_all(42)

GPU操作确定性设置

在GPU上面,有些操作是为了运行效率,是随机执行的,但是为了保证后期代码在同一个机器上面可复现,会额外设置所有GPU上面的操作,都尽可能是确定性的。

python 复制代码
# Additionally, some operations on a GPU are implemented stochastic for efficiency
# We want to ensure that all operations are deterministic on GPU (if used) for reproducibility
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
相关推荐
Westward-sun.3 分钟前
OpenCV + dlib 人脸关键点检测学习笔记(68点)
人工智能·笔记·opencv·学习·计算机视觉
SCBAiotAigc12 分钟前
2026.4.13:vim编程简单配置
人工智能·ubuntu·vim·具身智能
飞哥数智坊13 分钟前
全新 SOLO 帮我做 PPT,半小时出稿,效果直接惊艳
人工智能·solo
飞哥数智坊14 分钟前
Gemini-3.1-Pro vs Gemini-3-Flash:效果与花费的真实对比
人工智能·ai编程·gemini
IT大师兄吖15 分钟前
SAM3 提示词 图片分割 ComfyUI 懒人整合包
人工智能
幻风_huanfeng17 分钟前
人工智能之数学基础:内点法和外点法的区别和缺点
人工智能·算法·机器学习·内点法·外点法
luoganttcc19 分钟前
一个 warp 同时 运行 32 个thread 就是 同时 运行 32 core
人工智能
AIData搭子22 分钟前
溯源难题破解:搭建原始文件与向量数据之间的映射关系
人工智能