๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #311

Feature Extraction

Feature extraction is the simplest, cheapest form of transfer learning: keep the entire pretrained model completely frozen, and train only a small new "head" (typically a single linear layer) on top of its outputs.

The Setup

\[ \hat{y} = \text{Head}_{\text{new, trainable}}\big(\text{Backbone}_{\text{pretrained, FROZEN}}(x)\big) \]

The pretrained backbone acts purely as a fixed feature extractor โ€” it processes the input exactly as it always would, producing rich, general representations, but none of its own weights ever get updated. Only the new head's (typically few) parameters are trained on your target task's data.

Why This Approach Is Attractive

AdvantageWhy
Extremely fast to trainOnly a small head's worth of parameters needs gradient computation and updating
Works well with very little dataFar fewer trainable parameters means far less risk of overfitting on a small target dataset
Preserves the pretrained knowledge exactlyZero risk of "forgetting" anything learned during pretraining, since nothing in the backbone changes

When Feature Extraction Works Best

This approach works particularly well when the target task is reasonably similar to what the model was originally pretrained on โ€” the pretrained features are already well-suited, and only a lightweight mapping from those features to your specific output categories needs to be learned. When the target task is meaningfully different from the pretraining domain, feature extraction alone may not adapt well enough, motivating the fine-tuning approaches covered in the next two notes.

Code

import torch
import torch.nn as nn
import torchvision.models as models

backbone = models.resnet50(weights="IMAGENET1K_V2")

# Freeze EVERY parameter in the pretrained backbone
for param in backbone.parameters():
    param.requires_grad = False

# Replace the final layer with a new, TRAINABLE head for your specific task
backbone.fc = nn.Linear(backbone.fc.in_features, 10)   # this new layer's params DO require grad by default

# Only the new head's parameters will actually receive gradient updates during training
optimizer = torch.optim.Adam(backbone.fc.parameters(), lr=0.001)   # note: only .fc.parameters(), not the whole model

Common Mistakes

  • Forgetting to explicitly set requires_grad = False on the frozen backbone's parameters โ€” without this, the backbone's weights would still receive gradients and be updated during training, defeating the entire point of feature extraction.
  • Passing the whole model's parameters (including the frozen backbone) to the optimizer โ€” even with requires_grad=False correctly set, it's cleaner and clearer to explicitly optimize only the new head's parameters.

Interview Relevance

Q: "When would feature extraction (fully frozen backbone) be preferable to fine-tuning any of the backbone's own weights?" When the target dataset is small (reducing overfitting risk from having fewer trainable parameters matters a lot), when the target task is reasonably similar to the original pretraining task (so the pretrained features are already well-suited without adjustment), or when training speed/compute is a significant constraint, since only the small new head needs gradient computation.

Practice Question

Why does feature extraction carry a much lower overfitting risk than full fine-tuning, when working with a very small target dataset?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Feature Extraction โ€“ FAQs

Quick answers about learning Feature Extraction in Deep Learning.

This free note from CodingNow 2.0 explains Feature Extraction in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Feature Extraction, is 100% free with no signup required.
With focused practice, most students grasp Feature Extraction in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now