MLCrack

    ML MATH & LINEAR ALGEBRA · INTERVIEW QUESTION

    Explain learning rate scheduling strategies. Which are most effective?

    🔴 advanced · Calculus & Optimization

    Hint: Think about warm-up, cosine annealing, and one-cycle policy.

    Answer

    Learning rate schedules adjust η\eta during training:

    1. Step decay: reduce by factor every kk epochs

    2. Exponential decay: ηt=η0⋅γt\eta_t = \eta_0 \cdot \gamma^t

    3. Cosine annealing (most popular): ηt=ηmin⁡+ηmax⁡−ηmin⁡2(1+cos⁡πtT)\eta_t = \eta_{\min} + \frac{\eta_{\max} - \eta_{\min}}{2}\left(1 + \cos\frac{\pi t}{T}\right)

    4. Warm-up: linearly increase η\eta from 0 to target over initial steps. Essential for transformers.

    5. One-cycle: warm up → cosine decay below initial LR.

    python
    # Best practice for LLM training optimizer = torch.optim.AdamW(model.parameters(), lr=3e-4) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer, T_max=total_steps, eta_min=1e-6 ) # With warmup (using transformers library) from transformers import get_cosine_schedule_with_warmup scheduler = get_cosine_schedule_with_warmup( optimizer, num_warmup_steps=2000, num_training_steps=100000 )

    Default for LLM training: AdamW + linear warmup (2000 steps) + cosine decay.