CS336.wiki
CS336 中文实验手册/训练与优化
反馈与讨论
ASSIGNMENT 01 / 4.4–4.5

学习率调度与梯度裁剪

2026 春季 · v26.0.3原文 p. 33–34对照 PDF ↗

4.4 学习率调度#

让损失最快下降的学习率往往随训练进度变化。训练 Transformer 时,通常采用学习率调度:开始使用较大学习率以更快更新,再逐渐衰减。有些调度还会重新升高学习率,即重启,以帮助越过局部极小值。

本作业实现训练 LLaMA [12] 所用的余弦退火调度。

调度器本质上是一个函数:接收当前步数 tt 和初始、最终学习率等参数,返回该步的学习率。最简单的调度是常数函数,任意步数都返回相同值。

带预热的余弦退火接收当前步数 tt、最大学习率 αmax⁡\alpha_{\max}、最小最终学习率 αmin⁡\alpha_{\min}、预热步数 TwT_w、余弦退火结束步数 TcT_c:

αt={tTwαmax⁡,t<Twαmin⁡+12(1+cos⁡(t−TwTc−Twπ))(αmax⁡−αmin⁡),Tw≤t≤Tcαmin⁡,t>Tc \alpha_t= \begin{cases} \frac{t}{T_w}\alpha_{\max}, & t<T_w\\ \alpha_{\min}+\frac12\left(1+\cos\left(\frac{t-T_w}{T_c-T_w}\pi\right)\right) (\alpha_{\max}-\alpha_{\min}), & T_w\le t\le T_c\\ \alpha_{\min}, & t>T_c \end{cases}

第一阶段为预热,第二阶段为余弦退火,结束后保持最小学习率。

练习 learning_rate_schedule1 分

实现带预热的余弦学习率调度

编写函数,接收 tt、αmax⁡\alpha_{\max}、αmin⁡\alpha_{\min}、TwT_w、TcT_c,按上述定义返回 αt\alpha_t。

实现 adapters.get_lr_cosine_schedule,通过 uv run pytest -k test_get_lr_cosine_schedule。

4.5 梯度裁剪#

训练中某些样本会产生很大的梯度,导致不稳定。常用的缓解方法是梯度裁剪:每次反向传播后、优化器更新前,限制梯度范数。

将全部参数的梯度视为 gg,计算其 ℓ2\ell_2 范数 ∥g∥2\|g\|_2。若低于最大值 MM,保持不变;否则乘以:

M∥g∥2+ε \frac{M}{\|g\|_2+\varepsilon}

其中 ε\varepsilon 是如 10−610^{-6} 的数值稳定项,裁剪后的范数略低于 MM。

练习 gradient_clipping1 分

实现梯度裁剪

编写函数,接收参数列表与最大 ℓ2\ell_2 范数,原地修改每个参数的梯度。采用 PyTorch 默认的 ε=10−6\varepsilon=10^{-6}。

实现 adapters.run_gradient_clipping,通过 uv run pytest -k test_gradient_clipping。

一起把这一章读得更好

发现翻译、公式或排版错误?欢迎反馈。概念疑问和学习心得,也可以在本章讨论中交流。

直接选中正文,点击旁边的「反馈这处错误」,即可在页内填写并提交,无需登录。
English original 展开本章英文原文 PDF 文本提取

下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。

4.4 Learning rate scheduling
The value for the learning rate that leads to the quickest decrease in loss often varies during training. In
training Transformers, it is typical to use a learning rate schedule, where we start with a bigger learning
rate, making quicker updates in the beginning, and slowly decay it to a smaller value as the model
trains.8 In this assignment, we will implement the cosine annealing schedule used to train LLaMA
[H. Touvron et al., 2023].
A scheduler is simply a function that takes the current step 𝑡 and other relevant parameters (such as the
initial and final learning rates), and returns the learning rate to use for the gradient update at step 𝑡. The
simplest schedule is the constant function, which will return the same learning rate given any 𝑡.
The cosine annealing learning rate schedule takes (i) the current iteration 𝑡, (ii) the maximum learning
rate 𝛼max , (iii) the minimum (final) learning rate 𝛼min , (iv) the number of warm-up iterations 𝑇𝑤 , and (v)
the final iteration of cosine annealing 𝑇𝑐 . The learning rate at iteration 𝑡 is defined as:
(Warm-up) If 𝑡 < 𝑇𝑤 , then 𝛼𝑡 = 𝑇𝑡 𝛼max .
                                            𝑤
                                                                                 𝑡−𝑇
(Cosine annealing) If 𝑇𝑤 ≤ 𝑡 ≤ 𝑇𝑐 , then 𝛼𝑡 = 𝛼min + 12 (1 + cos( 𝑇 −𝑇𝑤 𝜋))(𝛼max − 𝛼min ).
                                                                                  𝑐    𝑤


(Post-annealing) If 𝑡 > 𝑇𝑐 , then 𝛼𝑡 = 𝛼min .


 Problem (learning_rate_schedule): Implement cosine learning rate schedule with warmup
 (1 point)

  8
      It’s sometimes common to use a schedule where the learning rate rises back up (restarts) to help get past local minima.

   Write a function that takes 𝑡, 𝛼max , 𝛼min , 𝑇𝑤 and 𝑇𝑐 , and returns the learning rate 𝛼𝑡 according to
   the scheduler defined above. Then implement [adapters.get_lr_cosine_schedule] and make sure
   it passes uv run pytest -k test_get_lr_cosine_schedule.


4.5 Gradient clipping
During training, we can sometimes hit training examples that yield large gradients, which can destabilize
training. To mitigate this, one technique often employed in practice is gradient clipping. The idea is to
enforce a limit on the norm of the gradient after each backward pass before taking an optimizer step.
Given the gradient (for all parameters) 𝑔, we compute its ℓ2 -norm ‖𝑔‖2 . If this norm is less than a
maximum value 𝑀 , then we leave 𝑔 as is; otherwise, we scale 𝑔 down by a factor of ‖𝑔‖𝑀+𝜀 (where a small
                                                                                          2
𝜀, like 10−6 , is added for numeric stability). Note that the resulting norm will be just under 𝑀 .


 Problem (gradient_clipping): Implement gradient clipping (1 point)


   Write a function that implements gradient clipping. Your function should take a list of parameters
   and a maximum ℓ2 -norm. It should modify each parameter gradient in place. Use 𝜀 = 10−6 (the
   PyTorch default). Then, implement the adapter [adapters.run_gradient_clipping] and make sure
   it passes uv run pytest -k test_gradient_clipping.

关于这一章的疑问、理解与心得,都可以在这里交流。阅读无需登录;发表评论或回复时使用 GitHub 账号。

正在加载本章讨论…

欢迎交流概念与阅读疑问,请勿发布作业完整实现或题目答案。

输入中文、英文术语或题号,例如 RoPE、检查点、train_bpe。