Skip to content
E
Educatum

Describe the concept of learning rate scheduling and its role in optimizing the training process of generative models over time.

Curated by Educatum AI teamLast updated:
Training Techniques
Direct Answer

Learning rate scheduling adjusts the optimizer step size over training—typically via an initial linear warmup followed by cosine or Warmup-Stable-Decay annealing—to stabilize early updates and enable fine-grained convergence. In large language models and diffusion models trained per step rather than per epoch, schedules prevent early gradient divergence from large batch sizes or uninitialized layers. For GANs, pairing schedules with separate generator and discriminator learning rates (TTUR) further stabilizes adversarial dynamics.

Study Card: Learning Rate Scheduling in Generative Models

Direct Answer

Learning rate scheduling dynamically adjusts the optimizer’s step size ηt\eta_t across training steps tt rather than holding it constant. A constant learning rate creates a rigid trade-off: too large causes parameter oscillation or divergence around sharp minima; too small leads to painfully slow early optimization and poor exploration.

Modern generative models (LLMs, diffusion models, VAEs) almost universally update schedules per step (not per epoch, since LLM pre-training often performs only 1–2 passes over trillions of tokens) using linear warmup followed by cosine decay:

ηt={ηmax⁡⋅tTwt<Twηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡(t−TwT−Twπ))Tw≤t≤T\eta_t = \begin{cases} \eta_{\max} \cdot \dfrac{t}{T_w} & t < T_w \\ \eta_{\min} + \dfrac{1}{2}(\eta_{\max} - \eta_{\min})\left(1 + \cos\left(\dfrac{t - T_w}{T - T_w}\pi\right)\right) & T_w \le t \le T \end{cases}

Alternatively, Warmup-Stable-Decay (WSD) holds ηmax⁡\eta_{\max} flat during the main compute phase and rapidly decays at the end—enabling continual pre-training without choosing TT upfront. In GANs, scheduling is also paired with the Two Time-Scale Update Rule (TTUR), using distinct learning rates for discriminator and generator to ensure local Nash convergence.

Key Terms

  • Learning Rate (ηt\eta_t): Step-size hyperparameter scaling parameter updates during gradient-based optimization.
  • Linear Warmup (TwT_w): Ramping ηt\eta_t from near 00 to ηmax⁡\eta_{\max} over early steps so adaptive moment estimates (like Adam’s second moment vtv_t) and LayerNorm/attention scales stabilize before large steps occur.
  • Cosine Annealing / WSD: Smoothly decaying ηt\eta_t down to a minimum (typically ηmin⁡≈0.1 ηmax⁡\eta_{\min} \approx 0.1\,\eta_{\max} in LLMs) to settle into flatter, better-generalizing basins.
  • Step-Level vs. Epoch-Level Scheduling: Stepping the scheduler after each optimizer batch step (standard in Transformers/Diffusion) rather than once per dataset pass.
  • TTUR (Two Time-Scale Update Rule): Setting a higher learning rate for the discriminator than the generator in GANs to stabilize minimax updates.

Example

Suppose you train a 7B-parameter autoregressive LLM on a massive corpus with global batch size of 4M tokens using AdamW:

  • Starting directly at ηmax⁡=3×10−4\eta_{\max} = 3 \times 10^{-4} without warmup often triggers gradient spikes in early steps when attention weights and Adam second-moment statistics are uncalibrated.
  • Using a 2,000-step linear warmup stabilizes initial gradients, after which cosine decay gradually lowers the rate to 3×10−53 \times 10^{-5} over the remaining token budget—capturing rapid initial loss drops early and fine-grained token distribution calibration at the tail.

Similarly, in a diffusion UNet/DiT or VAE, decaying the learning rate at the end of training reduces stochastic minibatch noise in weight updates, yielding sharper high-frequency details.

Code Implementation

import torch
import torch.nn as nn
import torch.optim as optim


class SimpleGenerator(nn.Module):
    def __init__(self, latent_dim, hidden_dim, output_dim):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(latent_dim, hidden_dim),
            nn.GELU(),
            nn.Linear(hidden_dim, output_dim),
        )

    def forward(self, z):
        return self.net(z)


torch.manual_seed(0)
latent_dim, hidden_dim, output_dim = 16, 64, 32
model = SimpleGenerator(latent_dim, hidden_dim, output_dim)

# Fixed synthetic target mapping to demonstrate generative reconstruction training
teacher_proj = torch.randn(latent_dim, output_dim)
optimizer = optim.AdamW(model.parameters(), lr=1e-2, weight_decay=1e-2)
loss_fn = nn.MSELoss()

total_steps = 60
warmup_steps = 10

# Standard LLM/Diffusion pattern: Linear warmup followed by Cosine annealing (stepped per batch)
warmup = optim.lr_scheduler.LinearLR(optimizer, start_factor=0.1, total_iters=warmup_steps)
cosine = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=total_steps - warmup_steps, eta_min=1e-4)
scheduler = optim.lr_scheduler.SequentialLR(optimizer, schedulers=[warmup, cosine], milestones=[warmup_steps])

for step in range(1, total_steps + 1):
    z = torch.randn(32, latent_dim)
    targets = torch.tanh(z @ teacher_proj)
    preds = model(z)
    loss = loss_fn(preds, targets)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    scheduler.step()

    if step in (1, warmup_steps, total_steps // 2, total_steps):
        lr = optimizer.param_groups[0]["lr"]
        print(f"Step {step:2d}/{total_steps} | LR: {lr:.6f} | Loss: {loss.item():.4f}")
  • Adaptive Optimizers (AdamW, Lion, Adafactor): Per-parameter momentum and variance scaling complement—rather than replace—global learning rate schedules.
  • Warmup-Stable-Decay (WSD): Increasingly used in modern LLM pre-training because checkpoints from the stable phase can be branched and annealed at any target token count.
  • Cyclical & Cosine Restarts (SGDR): Periodically raising and decaying ηt\eta_t to escape sharp local minima or ensemble checkpoints.
  • Linear & Sqrt LR Scaling: Scaling ηmax⁡\eta_{\max} when increasing distributed batch size (alongside proportional warmup extensions).
  • Gradient Clipping: Works alongside warmup to bound transient gradient explosions before optimizer moment estimates adapt.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.