下列文本提取自本地原版 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 查看记录 ↗