Chapter 21 of 42 · ~18 min read & lab+75 XP
Chapter 21 · Deep Learning

Transfer Learning & Fine-Tuning: Backbone Freezing, Linear Probing & Discriminative LRs

Why nobody trains ResNets or ViTs from scratch on 1,000 images: the Dataset-Size × Domain-Shift matrix, the 2-stage LP-FT (Linear Probe → Fine-Tune) recipe, and layer-wise Discriminative Learning Rates!

Why ML Engineers & Data Scientists Actually Need This

Training a vision backbone from random weights on 800 medical X-rays overfits miserably at 65% accuracy — fine-tuning a pretrained ConvNeXt/ViT hits 94%+ in 3 minutes!

ML / AI Engineer

95% of real-world industry Deep Learning is Transfer Learning: starting from strong pretrained weights and adapting safely without catastrophic forgetting.

Data Scientist

Using LP-FT (freeze & warm up head first, then unfreeze with discriminative LRs) reliably beats naive end-to-end fine-tuning on out-of-distribution test sets!

Interview & Debug Edge

Classic applied DL interview question: 'You replace the classification head of a pretrained ResNet with a random Linear layer and immediately fine-tune all layers at lr = 1e-3. What goes wrong?'

Step 0 · Test Your Gut Before Any Math!

What happens if you fine-tune an entire pretrained backbone immediately alongside a randomly initialized classification head at a high LR?

What You Will Master in This Chapter
  • Choose between Frozen Backbone (Linear Probing), Partial Unfreezing, and Full Fine-Tuning based on dataset size and domain shift
  • Apply the LP-FT (Linear Probe then Fine-Tune) 2-stage recipe to prevent random head gradients from distorting pretrained features
  • Configure PyTorch Optimizer Parameter Groups for layer-wise Discriminative Learning Rates (1e-5 early → 1e-3 head)
  • Avoid the classic BatchNorm Eval Trap when freezing a pretrained CNN backbone
Plain-English Symbol Decoder · Click Any Symbol Before We Begin
1 / 2
θ={θearly,θmid,θhead}\theta = \{\theta_{\text{early}}, \theta_{\text{mid}}, \theta_{\text{head}}\}Say aloud:“Parameters partitioned into early, mid, and head groups”
Plain-English Meaning
Layer-wise parameter groups assigned Discriminative Learning Rates (ηearly≪ηmid≪ηhead\eta_{\text{early}} \ll \eta_{\text{mid}} \ll \eta_{\text{head}}).
Everyday Example
early: 1e-5, mid: 1e-4, head: 1e-3 inside torch.optim.AdamW([...]).
Story-First Intuition, Visual Diagrams & Interactive Walkthroughs

Learning to play tennis when you already play badminton is Transfer Learning: you don't re-learn how to run or track a ball (your frozen early layers!). You keep your core coordination intact and gently adapt your wrist swing (top layers + head)!

1. The Dataset-Size × Domain-Shift Matrix

Transfer Learning Strategy Matrix (Dataset Size × Domain Similarity)Small Data + SimilarFreeze Backbone → Train Head Only ✓Large Data / Shifted DomainUnfreeze Top Blocks / Full Fine-Tune ✓
Transfer Learning Decision Matrix: Small dataset + Similar domain → Freeze Backbone (Linear Probe); Large dataset or Shifted domain → Unfreeze & Fine-Tune with a low LR!
  • Small data + Similar domain: Freeze backbone (requires_grad=False) and train ONLY the linear head.
  • Small data + Shifted domain (e.g., X-rays, satellite): Linear probe first, then gently unfreeze only the top 1–2 blocks with heavy augmentation.
  • Large data: Use LP-FT to fine-tune the full model with Discriminative LRs!
Animated Mini-Lab • Transfer Learning

Backbone Freezing Strategy Selector

Freeze Backbone (Linear Probe only) — avoids overfitting on few samples ✓

2. The 2-Stage LP-FT Recipe (Linear Probe → Fine-Tune)

2-Stage LP-FT Recipe: Linear Probe First → Then Unfreeze & Fine-Tune!Backbone (Frozen ❄️)requires_grad = FalseStage 1: Train HeadStabilize random headStage 2: UnfreezeFine-tune @ 10× lower LR ✓
Linear Probing trains ONLY the new classification head (1% of params, zero overfitting); Full Fine-Tuning adapts deep features to your target domain!

Never shock a pretrained backbone with random head gradients! Run Stage 1 (Linear Probe) with the backbone frozen for 1–3 epochs, then enter Stage 2 (Fine-Tune) by unfreezing the backbone at a 10×–50× smaller learning rate.

3. Discriminative Learning Rates in PyTorch

Layer-Wise Discriminative Learning Rates (Protect Early Features!)Early Layers (Edges)η = 1e-5Mid Layers (Parts)η = 1e-4New Task Headη = 1e-3
Discriminative Learning Rates protect general low-level edge filters with a tiny LR (1e-5) while letting the newly initialized task head learn 100× faster (1e-3)!

Early layers detect universal edges and textures that barely need to move (1e-5); deeper semantic layers adapt moderately (1e-4); the new task head moves fastest (1e-3). Pass multiple param-group dicts directly to AdamW!

Pause & Check · Quick Intuition Pulse

When freezing a pretrained ResNet backbone (param.requires_grad = False), why should you ALSO keep frozen BatchNorm layers in .eval() mode?

“Wait, I'm Confused...” · Teacher Office Hours Q&A

The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.

YES! Always match the exact input preprocessing (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) that the pretrained backbone was originally trained with!

Interactive Visual Lab
Lab Challenge: Watch the 60 FPS LP-FT Two-Stage Unfreezing Schedule protect pretrained features.
Interactive Animation • Transfer Learning Deep Lab

LP-FT Two-Stage Transfer Learning Schedule

Warm up the random head first so huge initial gradients never destroy pretrained backbone weights.

Stage 1 / 3
[Stage 1: Linear Probe]Backbone frozen ❄️ · Train new head @ η=1e-3
[Stage 2: Partial Unfreeze]Unfreeze top blocks @ η=1e-4 · Preserve edges
[Stage 3: Full LP-FT]Discriminative LRs (1e-5 → 1e-3) · Peak Val Acc ✓
0%
Stage 1: Linear Probe
Intuition Takeaway: Master Transfer Learning with the LP-FT 2-stage recipe, Discriminative Learning Rates, frozen BatchNorm hygiene, and exact pretrained input transforms.
Where This Powers Real ML, Deep Learning & Generative AI

Medical Imaging, Manufacturing QC & Domain Classification

Turns 500–5,000 labeled domain images into production-grade >95% classifiers in minutes using pretrained ViT/ConvNeXt backbones.

Production NumPy & PyTorch Code Patterns
optimizer = torch.optim.AdamW([
    {"params": model.early_blocks.parameters(), "lr": 1e-5},
    {"params": model.late_blocks.parameters(),  "lr": 1e-4},
    {"params": model.head.parameters(),         "lr": 1e-3},
], weight_decay=0.01)
Common ML Interview Traps & Numerical Pitfalls
Trap #1: Forgetting to match the pretrained backbone's normalization mean/std and input resolution.
Best Practice: Load the exact transforms attached to the pretrained weights (weights.transforms() in torchvision or timm.data.resolve_model_data_config)!
Chapter Mastery Check (+75 XP)
Score: 0 / 1
1Why does LP-FT (Linear Probe first, then Fine-Tune) outperform naive end-to-end fine-tuning?

Apply What You Learned — Solve Matching Coding Problems