TensorFlow VS PyTorch之学习率衰减

本文介绍了在深度学习中使用TensorFlow和PyTorch调整学习率的方法,包括TensorFlow的指数衰减法和PyTorch的基于epoch的学习率下降方法,详细解释了参数设置和计算公式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在训练神经网络时,有些情况下,需要对学习率进行调整。在这里分别介绍TensorFlow和PyTorch的一种方法。

tf.train.exponential_decay()

TensorFlow提供了指数衰减法

tf.train.exponential_decay(learning_rate, global_step=global_step, decay_steps=100,decay_rate=0.99, staircase=True)

计算公式:

learning_rate * decay_rate^(global_step / decay_steps)

参数

  • learning_rate:初始学习率
  • global_step:计数器,每进行一次更新,加1
  • decay_steps:衰减步长
  • decay_rate:衰减系数
  • staircase:若为True,则学习率呈阶梯形式下降,即global / decay_steps为整数。相当于每隔decay_steps更新一次学习率;若为False,则学习率呈连续下降,即global / decay_steps为浮点型,每一步都会更新一次学习率。
    在这里阶梯状的蓝色部分为staircase为True的情况;连续的红色部分为staircase为False的情况。

torch.optim.lr_scheduler.StepLR()

PyTorch提供了基于epoch的学习率下降方法。该方法只是其中一种

torch.optim.lr_scheduler.StepLR(optimizer,step_size=100,gamma=0.99,last_epoch=-1)

计算公式:

learning_rate * gamma^(epoch / step_size)

参数

  • optimizer:自己定义的优化器
  • step_size:衰减步长,即每隔step_size个epoch,更新一次学习率
  • gamma(float):衰减系数
  • last_epoch(int):最后一次epoch的索引默认为-1
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.99)

# 之后搭配 scheduler.step()进行操作。

参考文献:https://www.cnblogs.com/happystudyeveryday/p/11144433.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值