By the end
- Distinguish pretraining, feature extraction, and fine-tuning.
- Freeze and unfreeze parameters deliberately.
- Compare a task head with full-model adaptation.
- Document source-target mismatch and limitations.
Week 13 · Hands-on foundations
When should we freeze, adapt, or replace a pretrained representation?
The current Week 12 notebook demonstrates BERT, GPT-2, DistilBERT, vision transformers, sentiment, generation, and question answering. The newer LLM notebooks add objectives, feature extraction, prompting, and alignment. Those examples change quickly. The durable lesson is transfer: a model learns a representation on one task or dataset, then a smaller target task tests whether that representation transfers.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Unfreeze the final backbone layer, use a smaller learning rate, and compare three seeds against the frozen baseline.
Core ideas
A model first learns patterns from a source task or large dataset. The learned representation reflects that objective and data; it is not a universal definition of meaning.
A frozen backbone produces features while a small task head learns from target labels. This is efficient and often a strong low-data baseline.
Some or all pretrained parameters continue updating on target data. Fine-tuning can adapt a representation, but it can also overfit, forget useful structure, or amplify source-target mismatch.
Mathematical intuition
Feature extraction freezes backbone parameters φ and learns head parameters θ. Fine-tuning also updates some or all of φ.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
import torch
backbone=torch.nn.Linear(4,3)
for p in backbone.parameters(): p.requires_grad=False
print([p.requires_grad for p in backbone.parameters()])Predict the result, then click Run.
backbone, head = 15, 8
frozen_trainable = head
full_trainable = backbone + head
print(frozen_trainable, full_trainable)Predict the result, then click Run.
frozen=[0.78,0.80,0.79]
fine=[0.79,0.85,0.74]
print(round(sum(frozen)/3,3), round(sum(fine)/3,3))Predict the result, then click Run.
Failure checks
What goes wrong: Source capability is assumed to transfer automatically.
Check: Test on target-domain held-out cases.
What goes wrong: All parameters update despite few labels.
Check: Establish the frozen-feature baseline first.
What goes wrong: Target test examples influenced pretraining or adaptation.
Check: Document data provenance and keep an independent evaluation set.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. The durable lesson is transfer: a model learns a representation on one task or dataset, then a smaller target task tests whether that representation transfers.
Standard answer: A model first learns patterns from a source task or large dataset. The learned representation reflects that objective and data; it is not a universal definition of meaning.
Standard answer: A frozen backbone produces features while a small task head learns from target labels. This is efficient and often a strong low-data baseline.
Standard answer: Some or all pretrained parameters continue updating on target data. Fine-tuning can adapt a representation, but it can also overfit, forget useful structure, or amplify source-target mismatch.
Standard answer: Feature extraction freezes backbone parameters φ and learns head parameters θ. Fine-tuning also updates some or all of φ. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Record source data and pretraining objective.
Standard answer: Test on target-domain held-out cases.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 14 is reserved entirely for the final PBL presentations and course synthesis.
Terminology
Go further
Next: Week 14 is reserved entirely for the final PBL presentations and course synthesis.