【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
相关推荐
曦樂~12 小时前
【机器学习】分类Classification
人工智能·机器学习·分类
cd_9492172112 小时前
新北洋亮相2026 CHINASHOP:以“智印零售全生态”赋能效率与增长
大数据·人工智能·零售
lizz66612 小时前
Hermes-Agent:使用SOUL.md设定智能体身份
人工智能
3Dmax效果图渲染研习社12 小时前
ai生成的视频有没有版权?注意事项
人工智能·ai作画·aigc
llm大模型算法工程师weng12 小时前
人格识别模型:理解人类行为差异的心理测量工具
人工智能
IoT物联网产品手记12 小时前
IoT产品模块化架构设计:从功能堆叠到能力组合的系统方法
大数据·人工智能·物联网
西西弗Sisyphus12 小时前
PyTorch 里的矩阵乘法
pytorch·矩阵·matmul·torch.mm·bmm
C系语言12 小时前
ONNX Runtime安装
人工智能·python·深度学习
OneThingAI13 小时前
网心技术 | NemoClaw 深度解析,企业级 AI 运行时
人工智能·aigc·agent·openclaw·onethingai
慕涯AI13 小时前
Agent 30 课程开发指南 - 第16课
人工智能·python