Sub topic 10
Learning Rates and Schedules for Fine-Tuning
Assume you know Python, PyTorch tensors and modules, and the basics of loss, gradients, and epochs. You should also understand that the classifier has a new head and a pretrained backbone.
A learning rate controls how much an optimizer changes a parameter after each gradient calculation. A rate that is too large can destroy useful pretrained features. A rate that is too small can make training appear stuck.
Use different rates for different parts
The new classification head starts with random weights, so it needs relatively large updates. The pretrained backbone already contains useful visual features, so it usually needs smaller updates.
This approach is called a discriminative learning rate. It assigns different learning rates to different parameter groups.
| Parameter group | Typical learning rate | Reason |
|---|---|---|
| New classification head | `1e-3` | Its weights need to learn from the start |
| Last unfrozen backbone stage | `1e-4` | Adjust features without overwriting them |
| Earlier backbone stages | `1e-5` or frozen | General features often need little change |
The exact values are starting points, not rules. Small datasets usually benefit from conservative rates.
import torch
# The backbone's final stage was unfrozen before this point.
optimizer = torch.optim.AdamW(
[
{"params": model.layer4.parameters(), "lr": 1e-4},
{"params": model.fc.parameters(), "lr": 1e-3},
],
weight_decay=1e-2,
)
for epoch in range(10):
train_loss, train_acc = run_epoch(
model, train_loader, criterion, device, optimizer
)
val_loss, val_acc = run_epoch(
model, val_loader, criterion, device
)
print(
f"epoch {epoch + 1}: "
f"train acc={train_acc:.3f}, val acc={val_acc:.3f}"
)Change the rate during training
A learning-rate schedule changes the rate as training progresses. Early training may need larger updates. Later training often benefits from smaller updates that settle near a good solution.
ReduceLROnPlateau lowers the rate when a monitored metric stops improving. This is useful when you do not know how many epochs the model needs.
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
mode="min",
factor=0.1,
patience=2,
min_lr=1e-6,
)
for epoch in range(15):
train_loss, train_acc = run_epoch(
model, train_loader, criterion, device, optimizer
)
val_loss, val_acc = run_epoch(
model, val_loader, criterion, device
)
# Call this once per epoch, after validation.
scheduler.step(val_loss)
current_rates = [group["lr"] for group in optimizer.param_groups]
print(
f"epoch {epoch + 1}: val loss={val_loss:.3f}, "
f"learning rates={current_rates}"
)With this scheduler, the rate is multiplied by 0.1 after two validation epochs without improvement. Monitor validation loss rather than training loss because training loss can keep improving while generalization gets worse.
Warm up before fine-tuning
When you unfreeze a backbone stage, its gradients may initially be larger or less stable than expected. A short warm-up raises the learning rate gradually or begins with a small rate. This reduces sudden changes to pretrained features.
A practical sequence is:
- Train only the new head for several epochs.
- Unfreeze the last backbone stage.
- Use a backbone rate about ten times smaller than the head rate.
- Reduce both rates when validation loss stops improving.
- Save the checkpoint with the best validation score.
Read the training curves
If training loss falls rapidly but validation loss rises, the rate may be too high, the model may be overfitting, or the validation split may differ from training data. If both losses barely change, increase the head rate slightly or check that the intended parameters have requires_grad=True.
The schedule should support the experiment, not hide its behavior. Record the learning rate and validation metrics for every epoch.
Exercise
Why should the new head usually have a higher learning rate than an unfrozen pretrained layer?
Show answer
The head starts with random weights, while the pretrained layer already contains useful features. Larger head updates help it learn the new class boundaries without rapidly overwriting the backbone.