【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
相关推荐
2501_926978336 分钟前
认知边缘的双向耦合:从核技术类比到智力货币时代
人工智能·经验分享·笔记·ai写作
SQDN17 分钟前
Chatbox 1.22.1 获取不到模型?先验 /models,再对齐精确 model ID
人工智能·测试工具·机器学习·chatgpt·json
颜酱18 分钟前
08 | 把维度值同步到 Elasticsearch(生成阶段)
人工智能·python·langchain
小马哥crazymxm19 分钟前
Arxiv论文周选 (2026-W28)
论文阅读·人工智能·科技
ASKED_201920 分钟前
一文阐述国内网络环境下使用NIM方法
人工智能·系统架构
GlobalHRTalk21 分钟前
埃及名义雇主EOR业务概述与市场发展优势分析
大数据·运维·人工智能
董员外25 分钟前
RAG 系统进化论(二):Naive RAG,检索增强生成的最小闭环
前端·人工智能·后端
Zeeland25 分钟前
Agent 能完成一个任务,但它能持续追一个三个月的目标吗?
人工智能·github·openai
Sunlly26 分钟前
一次消息如何变成多步行动:拆开 OpenClaw 的 Agent Loop
人工智能
黄华SJ520it33 分钟前
甄味康商城系统开发|大健康新零售解决方案
人工智能·零售·系统开发