Before You Start
- Required knowledge
- Small neural networks, feature representations, freezing layers, and validation.
- Libraries used
- NumPy, scikit-learn
- Dataset and source
- Bundled digits images; digit parity is the pretraining task and digit classification is the limited-label transfer task.
- Expected output
- Comparisons of transferred/frozen features and randomly initialized features using a NumPy MLP, not an ImageNet backbone.
Lesson Videos
Transfer Learning & Fine-Tuning — Theory & Concepts
Read this before attempting the exercises. This lab teaches the transfer-learning idea in a small, fully transparent NumPy setup.
1. Why Transfer Learning?
Training from scratch means the model must learn useful features and the final decision rule at the same time.
Transfer learning reuses features learned on one task and adapts them to a new task.
That is especially useful when:
- ●the target dataset is small
- ●the source and target tasks share structure
- ●training from scratch would overfit or waste data
In modern deep learning this often means:
- ●pretrain on a large source dataset
- ●keep most learned features
- ●replace or adapt the final prediction head
2. This Lab's Setup
This notebook does not use a large pretrained CNN. Instead, it simulates the same idea with a small MLP on sklearn.datasets.load_digits.
Source task
Pretraining task:
- ●input: 64 pixel features from an 8x8 digit image
- ●label:
even vs odd
Target task
Fine-tuning task:
- ●same input features
- ●label:
digit class 0-9
Why this works:
- ●both tasks use the same handwritten-digit inputs
- ●parity pretraining can still teach useful stroke/shape features
- ●those learned features may help the harder 10-class task
3. What Counts as "Features" in This Lab?
The model has two parts:
input x
->
feature extractor: h = ReLU(x W1 + b1)
->
prediction head: logits = h W2 + b2
Interpretation:
- ●
(W1, b1)= feature extractor - ●
(W2, b2)= task-specific head
So in this lab:
- ●freeze features means keep
(W1, b1)fixed - ●replace head means reinitialize
(W2, b2)for the new task - ●fine-tune means start from pretrained
(W1, b1)and then update all parameters
This is the MLP version of:
- ●CNN backbone = feature extractor
- ●classifier layer = head
4. Three Training Modes
Scratch
Train the full model on the small target dataset with random initialization.
Use this as the baseline.
Frozen transfer
Steps:
- ●pretrain on parity
- ●copy pretrained
(W1, b1) - ●create a fresh 10-class head
(W2, b2) - ●train only the head
Why it can help:
- ●fewer trainable parameters
- ●lower variance
- ●less overfitting on small target data
Fine-tuning
Steps:
- ●pretrain on parity
- ●initialize from pretrained weights
- ●update both the feature extractor and the head
Why it can help:
- ●lets the representation adapt to the target task
- ●can beat frozen transfer if optimization is careful
5. Why Frozen Transfer Can Beat Scratch
When the target set is small, scratch training has to learn everything from limited supervision.
Frozen transfer starts with a representation that already captures useful handwritten-digit structure, so the head only needs to learn:
- ●which hidden patterns matter for each digit
- ●how to map those features to 10 classes
That often makes learning more data-efficient.
6. Why Fine-Tuning Needs a Smaller Learning Rate
After pretraining, the weights already contain useful structure.
If the learning rate is too large during fine-tuning:
- ●the pretrained representation can be overwritten too quickly
- ●validation performance can drop
- ●transfer can look worse than freezing
That is why fine-tuning usually uses:
- ●a smaller learning rate
- ●regularization such as weight decay
- ●sometimes fewer epochs
7. Why the Random-Frozen Control Matters
A good control question is:
If frozen transfer works, is it because the features are actually useful, or just because we trained fewer parameters?
To test that, compare:
- ●pretrained frozen: freeze
(W1, b1)learned on parity - ●random frozen: freeze a random
(W1, b1)and train only the head
If pretrained frozen beats random frozen on average, that is evidence that transfer is helping because the reused features contain signal, not just because the model has a smaller trainable head.
This is one of the most important conceptual checks in the notebook.
8. What You Should Expect
Typical pattern in this lab:
- ●
scratchis a reasonable baseline - ●
frozenoften does well in the low-data regime - ●
finetunecan match or beatfrozenif the LR is smaller - ●
random frozenis usually worse thanpretrained frozen
Do not over-interpret a single seed. The trend matters more than one run.
9. Practical Takeaways
- ●transfer learning is about reusing representations, not just copying weights blindly
- ●frozen transfer is often the safest first move for small datasets
- ●fine-tuning is more flexible but easier to destabilize
- ●control experiments matter: compare against scratch and against random frozen features
Interview framing:
If the target dataset is small, first try a pretrained representation with a new head. Then compare frozen transfer, fine-tuning, and a scratch baseline. If needed, add a control showing that pretrained features outperform random frozen features.
Now switch to the lab and compare scratch, frozen, finetune, and the new random frozen control carefully.
Ready to practice?
Transfer Learning & Fine-Tuning (NumPy) — Student Lab
We simulate transfer learning with a small MLP on sklearn.datasets.load_digits.
Source task (pretrain): even vs odd. Target task (fine-tune): 10-class digit classification.
Section 0 — Data splits (simulate low-data target)
We create:
- ●a large pool for source pretraining
- ●a small target training set (e.g., 200 samples)
- ●a held-out validation set
FAANG gotcha: standardize using train stats only to avoid leakage.
Baseline: LogisticRegression on small target set
Section 1 — Baseline: LogisticRegression on small target set
This baseline is intentionally strong and hard to beat with tiny models.
Task 1.1
Train LogisticRegression on the small target train split and report train/validation accuracy.
A small MLP (feature extractor + head)
Section 2 — A small MLP (feature extractor + head)
We define MLP helper functions used in later sections.
Task 2.1
Implement init_mlp, forward, backward, accuracy, and train_mlp.
Train from scratch on the small target set
Section 3 — Train from scratch on the small target set
Task 3.1
Train an MLP from scratch on y10 using only the small target training set.
Pretrain on the source task (even vs odd)
Section 4 — Pretrain on the source task (even vs odd)
We pretrain the same MLP but with a 2-class head.
Task 4.1
Train a 2-class MLP on the source task.
Transfer: frozen features vs fine-tuning
Section 5 — Transfer: frozen features vs fine-tuning
We reuse (W1, b1) from pretraining and create a new 10-class head.
Task 5.1
Frozen features: update only (W2,b2) (new head).
Task 5.2
Fine-tuning: update all parameters starting from the pretrained init (usually smaller LR).
Section 6 — Compare results
Which is best on validation: scratch, frozen, or finetune?
Control experiment: pretrained frozen vs random frozen
Section 7 — Control experiment: pretrained frozen vs random frozen
This task isolates the value of transfer.
A frozen-head model can perform well for two different reasons:
- ●the reused features are genuinely useful
- ●or we simply reduced the number of trainable parameters
To separate those effects, compare:
- ●pretrained frozen: reuse
(W1, b1)from parity pretraining, train only the new head - ●random frozen: keep a random
(W1, b1)fixed, train only the head
Task 7.1
Run both settings across several seeds and compare the mean validation accuracy.
Expected interpretation: if pretrained frozen beats random frozen on average, the transferred features are carrying useful signal.