【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
相关推荐
ECH00O002 分钟前
10-Fine-tuning/微调:给AI上"专业课"
人工智能
V搜xhliang02468 分钟前
多模态数据采集与标注
人工智能·目标检测·计算机视觉·知识图谱
CPU NULL12 分钟前
Google Banana pro 画卡通信息图
人工智能·ai作画·aigc·nano banana
心.c13 分钟前
从 ReAct 到 Plan-and-Execute:AI Agent 推理架构的理解与选择
人工智能·react.js·架构
人工智能AI技术15 分钟前
GPT-5.4逆向NES游戏ROM:我是如何用AI自动生成可运行复古游戏代码的
人工智能
zkrb77715 分钟前
AI误删生产库预警|中科热备:以硬核技术筑牢云上容灾防线
人工智能
咕泡科技16 分钟前
从0到1系统学习大模型:一份接地气的入门指南
人工智能·python·学习
KerwinChou_CN21 分钟前
大模型 RAG 中 RRF(Reciprocal Rank Fusion倒数排序融合)是什么
人工智能·后端·python
头发够用的程序员22 分钟前
GPU 流水线底层探索:从 SIMT 前端到 SIMD 后端的全链路解析
arm开发·人工智能·嵌入式硬件·深度学习·硬件架构·边缘计算
1-1=029 分钟前
PlayGround AI —— 使用图片生图器轻而易举
人工智能