Training - PyTorch Lightning 分布式训练的 global_step 参数 (accumulate_grad_batches)

欢迎关注我的CSDN:https://spike.blog.csdn.net/

本文地址:https://blog.csdn.net/caroline_wendy/article/details/137640653

在 PyTorch Lightning 中,pl.Traineraccumulate_grad_batches 参数允许在执行反向传播和优化器步骤之前,累积多个批次的梯度。这样,可以增加有效的批次大小,而不会增加内存开销。例如,如果设置 accumulate_grad_batches=8,则会在执行优化器的 .step() 方法之前,累积 8 个批次的梯度。

accumulate_grad_batchesglobal_step 的关系:

  1. global_step 会在每次调用优化器的 .step() 方法后递增。
  2. 使用梯度累积,global_step 增长小于 批次(batch) 的数量
  3. 多个批次贡献到 1 个 global_step 的更新中。

例如,如果 accumulate_grad_batches=8,那么每 8 个批次,只会增加 1 次 global_step,如果多卡,则 global_step 表示单卡的次数。日志,如下:

bash 复制代码
[INFO] [CL] global_step: 0, iter_step: 8
[INFO] [CL] global_step: 1, iter_step: 16

其中 pl.Trainer 的源码:

bash 复制代码
    trainer = pl.Trainer(
        accelerator="gpu",
        # ...
        accumulate_grad_batches=args.accumulate_grad,
        strategy=strategy,  # 多机多卡配置
        num_nodes=args.num_nodes,  # 节点数
        devices=1,  # 每个节点 GPU 卡数
    )

输出日志:

bash 复制代码
log = {'epoch': self.trainer.current_epoch, 'step': self.trainer.global_step}
wandb.log(log)
相关推荐
Learn Beyond Limits27 分钟前
Mean Normalization|均值归一化
人工智能·神经网络·算法·机器学习·均值算法·ai·吴恩达
ACERT33342 分钟前
5.吴恩达机器学习—神经网络的基本使用
人工智能·python·神经网络·机器学习
C嘎嘎嵌入式开发1 小时前
(一) 机器学习之深度神经网络
人工智能·神经网络·dnn
Aaplloo1 小时前
【无标题】
人工智能·算法·机器学习
大模型任我行1 小时前
复旦:LLM隐式推理SIM-CoT
人工智能·语言模型·自然语言处理·论文笔记
tomlone1 小时前
AI大模型核心概念
人工智能
可触的未来,发芽的智生2 小时前
触摸未来2025.10.06:声之密语从生理构造到神经网络的声音智能革命
人工智能·python·神经网络·机器学习·架构
动能小子ohhh2 小时前
AI智能体(Agent)大模型入门【6】--编写fasteAPI后端请求接口实现页面聊天
人工智能·python·深度学习·ai编程
SCBAiotAigc2 小时前
huggingface里的数据集如何下载呢?
人工智能·python
我是Feri3 小时前
机器学习之线性回归的特征相关性:避免“双胞胎特征“干扰模型
人工智能·机器学习