【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
相关推荐
geneculture几秒前
面向知识贡献自动化估值与清算的协同智能框架:为AI时代的基础性智力劳动设计一个公平、透明、可扩展的回报体系(带跨学科专家15份同行评议)
人工智能
لا معنى له3 分钟前
综述翻译:Embodied Science: Closing the Discovery Loop withAgentic Embodied AI
人工智能·笔记·学习
workflower3 分钟前
相比传统聊天式AI,AI Agent具备的核心能力
人工智能·语言模型·集成测试·软件工程·软件构建·软件需求
帐篷Li4 分钟前
Claude的/dream功能:让AI拥有“睡眠记忆“的魔法
人工智能
码农垦荒笔记7 分钟前
Anthropic Claude Mythos 泄露深度解读:Capybara 模型性能远超 Opus 4.6,AI 安全新拐点
人工智能·ai 安全·anthropic·claude mythos·ai 前沿
CS创新实验室8 分钟前
高性能计算综述:AI融合、能效优化与量子计算的挑战
人工智能·量子计算
Master_oid12 分钟前
机器学习36:机器学习概述
人工智能·机器学习
AI浩12 分钟前
SKILLRL: 通过递归技能增强强化学习进化智能体
人工智能
唐维康13 分钟前
2026年昆明理工大学计算机类考研预估调剂名额分析(人工智能、软件工程)
人工智能·考研·软件工程
光仔December17 分钟前
【从0学习Spring AI Alibaba】3、阿里云百炼平台API Key 申请指南
人工智能·ai大模型·spring ai·阿里云百炼·apikey申请