Stop 12 · Deep Learning FundamentalsTopics · 6/10

Weight Initialization & Learning Rate Schedules

Part of the Machine Learning Roadmap

In short

Two networks with the same layers and data can train beautifully or not at all, just because of where the weights started and how the step size changed over time. These two knobs quietly decide whether deep learning works.

Starting weights that are too big or too small make signals explode or vanish, so methods like Xavier and He initialisation pick sensible scales. Learning rate schedules, like step decay, cosine decay or warmup, change the step size during training for faster, more stable results.

The jaadu drives a small car carefully down a winding mountain ghat road, easing off on the hairpin bends while adjusting the side mirror.

Think of it like this

Learning to drive on a ghat road

Before you start, you set the mirrors and seat just right: not too far, not too close. That's initialisation. On the ghat road you drive slowly at first while you get a feel (warmup), cruise faster on the straight middle stretch, and slow right down near the hairpin bends at the end so you don't overshoot (decay). That's a learning rate schedule.

Watch it happen

Cosine schedule with warmup over 100 epochs

epoch 0
0
epoch 5
1
epoch 25
2
epoch 50
3
epoch 100
4

1/5Peak learning rate is 0.001. Warmup lasts 5 epochs, then cosine decay.

The big ideas

What you need to know

Bad starts explode or vanish

Weights too large make activations and gradients grow layer by layer. Too small and they shrink to nothing.

Xavier for tanh, He for ReLU

Both scale random weights by the number of inputs so signals keep a steady size. PyTorch's Linear default is close to He-style.

Decay the learning rate

Big steps early to make progress, small steps late to settle. Step decay, cosine and ReduceLROnPlateau are common.

Warmup helps big models

Start with a small rate for the first few epochs or steps. It's standard for transformers and large batches.

See it in code

Walk through the code

python
1import torch2import torch.nn as nn3from torch.optim.lr_scheduler import CosineAnnealingLR, LinearLR, SequentialLR4 5model = nn.Sequential(6    nn.Linear(20, 128), nn.ReLU(),7    nn.Linear(128, 128), nn.ReLU(),8    nn.Linear(128, 2),9)10 11def init_weights(m):12    if isinstance(m, nn.Linear):13        nn.init.kaiming_normal_(m.weight, nonlinearity="relu")   # He init14        nn.init.zeros_(m.bias)15 16model.apply(init_weights)17 18optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-2)19warmup = LinearLR(optimizer, start_factor=0.2, total_iters=5)20cosine = CosineAnnealingLR(optimizer, T_max=95)21scheduler = SequentialLR(optimizer, schedulers=[warmup, cosine], milestones=[5])22 23for epoch in range(100):24    # ... train for one epoch here (forward, loss, backward, optimizer.step()) ...25    scheduler.step()26    if epoch in (0, 4, 49, 99):27        print(epoch, scheduler.get_last_lr()[0])

1/5A small ReLU network for 20 input features and 2 classes.

He initialisation for a ReLU network, plus 5 epochs of warmup followed by cosine decay.

Save yourself the pain

Common mistakes

Oops

Initialising every weight to zero.

Do this instead

All neurons in a layer then compute the same thing and get the same gradient forever. Use random init like He or Xavier.

Oops

Calling scheduler.step() inside the batch loop when the schedule was set up in epochs.

Do this instead

Match the units. If T_max counts epochs, step once per epoch. If it counts batches, step every batch.

Oops

Using one fixed learning rate and giving up when the loss plateaus.

Do this instead

Try ReduceLROnPlateau or a cosine schedule. Lowering the rate often unlocks a further drop in loss.

Your mission

Break it, then fix it

~35 min

Quick check

Which initialisation is designed for layers followed by ReLU?

Go deeper

  1. 1Train a deep network with tiny random weights and with He initialisation, and compare.
  2. 2Add a CosineAnnealingLR or OneCycleLR scheduler to a PyTorch training loop.
  3. 3Plot the learning rate over epochs to check the schedule is what you expect.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge