【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
相关推荐
深小乐5 小时前
我在 Anthropic ELI5 上加了5样东西,做成了更好用的 ELI5+
人工智能
东风破_5 小时前
《LangGraph Memory:为什么 Agent 需要 Checkpointer?》
人工智能
锋行天下5 小时前
LangGraph 同级节点之间修改数据先后问题
人工智能
u1301305 小时前
AI 日报(2026年9月12日)
人工智能
东风破_6 小时前
《LangGraph Human-in-the-loop:Interrupt 如何让 Agent 等待人工确认》
人工智能
2601_962218616 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
智塑未来6 小时前
中文短剧出海翻译工具横评:VMEG AI、鬼手、Vozo AI、ElevenLabs怎么选?
人工智能
东风破_6 小时前
《LangGraph 条件路由与循环:如何让 Agent 自己决定下一步?》
人工智能
我爱吃土豆16 小时前
AI 编程工具远程开发指南:Codex 桌面版与 WorkBuddy 通过 SSH 连接云服务器实践
服务器·人工智能·ssh
袁俪6 小时前
推理成本门槛
人工智能