24. Transfer Learning & Domain Adaptation
Leveraging pretrained representations: feature extraction vs full fine-tuning, strategic layer freezing, learning rate warmups, and domain adaptation techniques.
Transfer Learning: Complete Notes (Beginner to Advanced)
1. Transfer Learning#
Transfer learning is a machine learning technique where knowledge learned from one task or dataset is reused for another related task.
Instead of training a neural network entirely from random initialization:
Architecture & Data FlowRandom Initialization | v Train on New Dataset | v New Model
we start with a model that has already learned useful patterns:
Architecture & Data FlowPretrained Model | v Adapt to New Task | v New Model
Basic Idea#
Suppose a model was trained on a large image dataset.
It may learn:
textEarly layers: Edges, colors, textures Middle layers: Shapes and patterns Later layers: Object-specific features
For a new image task, many of the early and middle features may still be useful.
Therefore, instead of learning everything again, we can reuse the pretrained model.
Typical Flow#
Architecture & Data FlowLarge Dataset | v Pretrain Model | v Pretrained Weights | v New Dataset | v Adapt / Fine-Tune | v Target Model
2. Why Transfer Learning Is Useful#
Training a deep neural network from scratch can require:
- Large datasets
- Significant computation
- Long training times
- Careful optimization
Transfer learning can reduce these requirements when the source model and target task are sufficiently related.
Without Transfer Learning#
Architecture & Data FlowSmall Dataset | v Randomly initialized model | v Train everything | v Risk of overfitting
With Transfer Learning#
Architecture & Data FlowLarge Dataset | v Pretrained Model | v Reuse learned features | v Small Target Dataset | v Adapt model
Main Benefits#
- Faster convergence
- Less training data may be required
- Lower computational cost
- Often better performance than training from scratch
- Useful initialization for the target task
Transfer learning is not guaranteed to improve performance. If the source and target domains are poorly related, transferred knowledge can be unhelpful or even harmful.
3. Feature Extraction#
Feature extraction is a transfer-learning approach where the pretrained model is used primarily as a fixed feature extractor.
The pretrained layers are frozen:
Architecture & Data FlowPretrained Model | +---- Frozen layers | v Feature Representation | v New Task-Specific Head | v Prediction
Example#
Suppose a pretrained CNN has:
Architecture & Data FlowConvolution layers | v Feature extractor | v Original classifier
For a new classification task, we can:
Architecture & Data FlowRemove original classifier | v Keep pretrained feature extractor | v Add new classifier
Example:
textImage | Pretrained CNN | Frozen Features | New Linear Layer | New Classes
Why Freeze the Backbone?#
The pretrained layers already contain useful representations.
Freezing them:
- Reduces the number of trainable parameters
- Reduces computational cost
- Prevents rapid changes to pretrained features
- Can reduce overfitting on small datasets
4. Pretrained Models#
A pretrained model is a model whose parameters have already been learned from a previous training process, usually using a large dataset.
Examples in computer vision include:
- ResNet
- VGG
- EfficientNet
- MobileNet
Examples in NLP include:
- BERT
- T5
- GPT-family models
The pretrained weights provide a useful starting point for a new task.
Example#
A CNN pretrained on a large image dataset might already know useful visual patterns:
Architecture & Data FlowPixels | Edges | Textures | Shapes | Object-level features
A new task may only require adapting the later representation to the target labels.
5. Freezing Layers#
Freezing a layer means preventing its parameters from being updated during training.
If a parameter is frozen:
›gradient update -> not applied
Conceptually:
Architecture & Data FlowLayer 1 -> Frozen Layer 2 -> Frozen Layer 3 -> Frozen Layer 4 -> Trainable Layer 5 -> Trainable
The model can still perform forward propagation through frozen layers.
They simply do not receive parameter updates.
Why Freeze Layers?#
Common reasons include:
- Small target dataset
- Limited compute
- Useful pretrained representations
- Preventing overfitting
- Faster training
6. Freezing Layers in PyTorch#
A common PyTorch pattern is:
🐍 PythonInteractive WebAssemblyimport torch
import torch.nn as nn
from torchvision import models
model = models.resnet18(weights="DEFAULT")
for param in model.parameters():
param.requires_grad = False
Now the pretrained parameters are frozen.
We can replace the final classification layer:
🐍 PythonInteractive WebAssemblynum_features = model.fc.in_features
model.fc = nn.Linear(num_features, 10)
The new classification layer is trainable by default.
Conceptually:
›ResNet backbone -> frozen Final classifier -> trainable
The optimizer should generally receive only the parameters that are intended to be updated:
🐍 PythonInteractive WebAssemblyoptimizer = torch.optim.Adam(
model.fc.parameters(),
lr=1e-3
)
7. Unfreezing Layers#
Unfreezing means allowing previously frozen parameters to receive gradient updates.
For example:
Architecture & Data FlowInitially: Backbone -> Frozen Classifier -> Trainable
After initial training:
›Backbone -> Partially Unfrozen Classifier -> Trainable
This allows the pretrained representation to adapt to the target dataset.
Why Unfreeze Gradually?#
Earlier layers often learn more general patterns, while later layers tend to become more task-specific.
Therefore, a common strategy is:
textStart: Freeze most/all backbone Then: Unfreeze later layers If needed: Unfreeze more layers
This is not a universal rule, but it is a useful practical strategy.
8. Fine-Tuning#
Fine-tuning means continuing training of a pretrained model on a target dataset so that its parameters adapt to the new task.
A typical workflow is:
Architecture & Data FlowPretrained Model | v Replace / modify task-specific head | v Freeze some layers | v Train new head | v Unfreeze selected layers | v Fine-tune with smaller learning rate
Fine-tuning can involve updating:
- Only the final layers
- A subset of the backbone
- Most of the network
- The entire network
Therefore, feature extraction is a more restrictive form of transfer learning, while fine-tuning allows some pretrained parameters to change.
9. Fine-Tuning Learning Rate#
Fine-tuning commonly uses a smaller learning rate than training a new model from scratch.
Why?
Because the pretrained weights already contain useful information.
A very large learning rate can make the model change those useful representations too aggressively.
Conceptually:
textTraining from scratch: larger learning rate may be appropriate Fine-tuning: smaller learning rate is often preferred
Different parts of the network can also use different learning rates.
For example:
›Pretrained backbone -> 1e-5 New classifier -> 1e-3
This is called using discriminative learning rates or different parameter-group learning rates.
10. Full Fine-Tuning#
Full fine-tuning means allowing essentially all pretrained model parameters to be updated on the target dataset.
Architecture & Data FlowPretrained Model | v All layers trainable | v Target Dataset | v Adapted Model
Example#
Architecture & Data FlowLayer 1 -> Trainable Layer 2 -> Trainable Layer 3 -> Trainable Layer 4 -> Trainable Layer 5 -> Trainable Classifier -> Trainable
The model can adapt its entire representation to the new task.
Advantages#
- Maximum ability to adapt
- Useful when source and target domains differ substantially but are still compatible
- Can achieve strong target-task performance with sufficient data and compute
Disadvantages#
- More computation
- More trainable parameters
- Higher risk of overfitting on small datasets
- Can overwrite useful pretrained representations
- Usually requires more careful learning-rate selection
11. Feature Extraction vs Fine-Tuning vs Full Fine-Tuning#
| Approach | Backbone | New Head | Trainable parameters | Typical use |
|---|---|---|---|---|
| Feature extraction | Frozen | Trainable | Few | Small dataset / related task |
| Partial fine-tuning | Partially trainable | Trainable | Medium | Adapt later features |
| Full fine-tuning | Trainable | Trainable | Most/all | More adaptation needed |
Visual Comparison#
Architecture & Data FlowFeature Extraction [ Frozen Backbone ] -> [ Trainable Head ] Partial Fine-Tuning [ Frozen ] -> [ Trainable Backbone Layers ] -> [ Trainable Head ] Full Fine-Tuning [ Trainable Backbone ] -> [ Trainable Head ]
12. Domain Adaptation#
Domain adaptation is the process of adapting a model trained on a source domain so that it performs well on a different but related target domain.
A domain can be characterized by its data distribution and task context.
Architecture & Data FlowSource Domain | v Pretrained Model | v Adaptation | v Target Domain
Example#
Suppose a model is trained using:
›Source: Professional product photographs
but must work on:
›Target: Mobile phone photographs taken in real-world conditions
The task may be similar:
›Object classification
but the data distributions differ.
The model may need domain adaptation.
13. Source Domain and Target Domain#
Source Domain#
The domain from which knowledge is transferred.
Architecture & Data FlowSource data | v Source model
Target Domain#
The domain where the model will ultimately be used.
Architecture & Data FlowTarget data | v Adapted model
Conceptually:
Architecture & Data FlowSOURCE DOMAIN Large / available data | v Pretraining | v Source Model | v Adaptation | v TARGET DOMAIN New distribution | v Target Model
14. Domain Shift#
The difference between source and target data distributions is called domain shift.
For example:
textSource: Studio images Target: Outdoor images
The objects may be the same, but:
textLighting Background Camera quality Image style Object appearance
can differ.
Therefore:
Mathematical FormulationP_source(x) != P_target(x)
in a simplified view.
Domain adaptation attempts to reduce the performance degradation caused by such differences.
15. Domain Adaptation vs Transfer Learning#
These concepts are closely related but not identical.
Transfer Learning#
A broad concept:
Architecture & Data FlowKnowledge from source | v Reuse in target task/domain
Domain Adaptation#
A more specific setting where the target domain differs from the source domain and adaptation is needed.
Architecture & Data FlowSource Domain | v Model | v Different Target Domain | v Adaptation
So:
Architecture & Data FlowTransfer Learning | +-- Fine-Tuning | +-- Feature Extraction | +-- Domain Adaptation | +-- Other transfer strategies
The exact taxonomy can vary across literature.
16. Simple Transfer Learning Example#
Suppose we have:
textPretrained model: ImageNet classification Target task: Classify different types of flowers
The workflow can be:
textImage | Pretrained CNN | Frozen feature extractor | New flower classifier | Flower class
After training the new classifier, we may unfreeze later CNN layers:
textImage | Pretrained CNN | Partially fine-tuned features | Flower classifier | Flower class
This allows the model to learn flower-specific visual features.
17. Practical PyTorch Fine-Tuning Example#
🐍 PythonInteractive WebAssemblyimport torch
import torch.nn as nn
from torchvision import models
model = models.resnet18(weights="DEFAULT")
# Freeze pretrained layers
for param in model.parameters():
param.requires_grad = False
# Replace classifier
num_features = model.fc.in_features
model.fc = nn.Linear(num_features, 5)
# Train only classifier initially
optimizer = torch.optim.Adam(
model.fc.parameters(),
lr=1e-3
)
After the classifier has learned:
🐍 PythonInteractive WebAssembly# Unfreeze the final ResNet block
for param in model.layer4.parameters():
param.requires_grad = True
optimizer = torch.optim.Adam(
[
{"params": model.layer4.parameters(), "lr": 1e-5},
{"params": model.fc.parameters(), "lr": 1e-3},
]
)
This is an example of partial fine-tuning with different learning rates.
18. Transfer Learning Decision Process#
A practical approach is:
Architecture & Data FlowDo I have a pretrained model? | Yes | v Is source and target data reasonably related? | +----+----+ | | Yes No / weakly related | | v v Use transfer Consider whether learning transfer is useful | v How much target data? | +---+----------------+ | | Small Large | | v v Freeze more Fine-tune more layers layers
The correct strategy depends on:
- Dataset size
- Similarity between source and target
- Amount of domain shift
- Model size
- Compute available
- Risk of overfitting
19. Important Terminology#
Transfer Learning#
Reusing knowledge learned from one task/domain for another.
Feature Extraction#
Using pretrained representations while keeping the backbone frozen.
Pretrained Model#
A model whose parameters were learned previously on another dataset/task.
Freezing#
Preventing selected parameters from being updated.
Unfreezing#
Allowing previously frozen parameters to be updated.
Fine-Tuning#
Continuing training of a pretrained model so its parameters adapt to the target task.
Full Fine-Tuning#
Updating essentially all model parameters on the target task.
Domain Adaptation#
Adapting a model to a target domain whose distribution differs from the source domain.
20. Summary#
| Concept | Simple meaning |
|---|---|
| Transfer Learning | Reuse learned knowledge for a new task |
| Feature Extraction | Freeze pretrained features and train a new head |
| Pretrained Model | Model with previously learned weights |
| Freezing Layers | Prevent selected parameters from updating |
| Unfreezing Layers | Allow selected parameters to update |
| Fine-Tuning | Adapt pretrained parameters to a target task |
| Full Fine-Tuning | Update essentially the whole pretrained model |
| Domain Adaptation | Adapt a model to a different target data distribution |
21. Quick Recap#
Architecture & Data FlowTRANSFER LEARNING Large Source Dataset | v Pretrained Model | v Target Dataset | +-----------------------+ | | v v Feature Extraction Fine-Tuning | | Freeze backbone Unfreeze some/all | | Train new head Adapt weights | | +-----------+-----------+ | v Target Model
Architecture & Data FlowFEATURE EXTRACTION -> Backbone frozen -> New head trained FINE-TUNING -> Some pretrained layers updated FULL FINE-TUNING -> Essentially all model parameters updated DOMAIN ADAPTATION -> Adapt model from source distribution to a different target distribution
One-Line Mental Model#
Mathematical FormulationTransfer Learning = Don't learn everything from scratch; reuse a pretrained model and adapt it to the new problem.
24. Transfer Learning Checkpoint
Finished studying this notebook?
Mark this guide as completed to update your course progress roadmap.