【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
相关推荐
basketball61610 小时前
AI Infra 配置 Conda + CUDA + LibTorch + PyTorch 开发环境:解决版本漂移、编译报错的完整指南
人工智能·pytorch·conda·libtorch
AI技术新视界10 小时前
符号诞生之前:视觉通用智能如何开启 AGI 的物理进化之路
人工智能·llm·agi
公爵爱学习10 小时前
无人机视觉识别前序-Linux-YOLO安装
python·yolo·计算机视觉·conda·无人机
Henry-SAP10 小时前
GPT-6Astra开启AI自主执行新时代
人工智能·云原生·sap·erp
手写码匠10 小时前
华为云Flexus+DeepSeek征文|DeepSeek 应用监控告警体系实战:从“用户投诉才知道“到“故障前就发现“
人工智能·深度学习·算法·aigc
小宋102110 小时前
大模型成本怎么控制:Token统计、缓存与模型路由实战
人工智能·缓存
阿里云大数据AI技术10 小时前
DataWorks Data Agent 实战课堂(七):数据治理 Agent——AI 驱动的自动化治理
人工智能·agent
天一生水water10 小时前
基于重构的无监督/单类时间序列异常检测
人工智能·深度学习·重构·transformer
知识分享小能手10 小时前
深度学习学习教程,从入门到精通,数值计算 — 知识点详解(4)
人工智能·深度学习·学习
三声三视10 小时前
审计清单函数名写成 def?tri-checklist 的 diff 解析在 Python 改名场景下悄悄翻车
人工智能·ai·skillhub·tri-checklist·tri-skills