Transfer learning starts a new task from parameters learned on a previous task. For image classification, a common starting point is a pretrained feature extractor and a new classification head fitted to the target labels.
Understanding Transfer Learning in Deep Neural Networks: A Step-by-Step Guide
In the realm of deep learning, transfer learning has become a powerful technique for leveraging pre-trained models to tackle new but related tasks. This approach not only reduces the time and computational resources required to train models from scratch but also often leads to better performance due to the reuse of already-learned features.
What is Transfer Learning?
Transfer learning is a machine learning technique where a model developed for one task is reused as the starting point for a model on a second, similar task. For example, a model trained to recognize cars can be repurposed to recognize trucks, with some adjustments. This approach is particularly useful when you have a large, complex model that has been trained on a vast dataset, and you want to apply it to a smaller, related dataset without starting the learning process from scratch.
Key Components of Transfer Learning
In transfer learning, there are several key components to understand:
- Base Model: This is the pre-trained model that was initially developed for a different task. It has already learned various features from a large dataset and can provide a strong starting point for the new task.
- New Model: This is the model you want to train for your new task. It will incorporate the layers or features from the base model but will also have new layers added or some layers adjusted to fit the new task requirements.
- Frozen Layers: When reusing layers from the base model, these layers can be “frozen,” meaning their weights will not be updated during training on the new task. This allows the model to retain the valuable features learned from the original task.
- Trainable Layers: These are the new or adjusted layers in the new model that will be trained on the new dataset. By fine-tuning these layers, the model can adapt to the specific needs of the new task.
How Does Transfer Learning Work?
Imagine you have a deep neural network that was trained to classify images of animals into categories like dogs, cats, and birds. Now, you want to adapt this model to classify a new set of images, say, different species of dogs. The process of transfer learning might look something like this:
- Reuse Pre-trained Layers: The lower layers of the original DNN, which have learned to detect edges, textures, and shapes, can be reused in the new model. These are general features that are useful across many different image classification tasks.
- Freeze Layers: You freeze the weights of these reused layers so that they are not modified during the training of the new model. This helps retain the useful features learned from the original task.
- Replace the Output Layer: The output layer of the original model, which was designed to classify into broad categories, is replaced with a new output layer that is tailored to the new task (e.g., classifying specific dog breeds).
- Fine-tune the New Model: Finally, you train the new output layer and perhaps some of the higher layers in the network on the new dataset. The lower layers, which were frozen, provide a solid foundation, while the higher layers adapt to the specific features of the new task.
When to Use Transfer Learning
Transfer learning is especially useful in the following scenarios:
- Limited Data: When you have a small dataset for the new task, transfer learning allows you to leverage the knowledge from a model trained on a much larger dataset.
- Similar Tasks: If the new task is closely related to the original task (e.g., both involve image classification), transfer learning can help improve accuracy and reduce training time.
- Resource Constraints: Training deep networks from scratch requires significant computational power and time. Transfer learning can help mitigate these demands by reusing pre-trained models. Check the Image Below

Conclusion
Transfer learning is a versatile and efficient approach in deep learning that allows models to be adapted for new tasks without starting from scratch. By reusing layers from a pre-trained model, freezing them, and fine-tuning the new layers, you can build powerful models that perform well even with limited data and resources.
Whether you’re working on image classification, natural language processing, or any other machine learning task, understanding and applying transfer learning can greatly enhance your projects and lead to faster, more accurate models.
By mastering this technique, you’re not just saving time—you’re standing on the shoulders of giants, leveraging the best of what has already been learned to solve new and exciting challenges.
From a fixed representation to three target classes
Suppose an animal-image backbone produces 512 features per image. A linear head for three dog breeds connects each feature to each output logit and gives each logit a bias. The head maps backbone features to logits .
Count three rows of 512 weights and one bias in each row. With the backbone frozen, these are the only trainable parameters in this example.
To see what a row computes, take a smaller feature vector (2, 1), a weight row (0.5, −1), and a bias of 0.25. Its logit is 1 − 1 + 0.25, or 0.25. The other rows score the other classes. During head training only those score functions adjust. A fixed representation with poor separation may need adaptation.
Frameworks and evaluation
PyTorch. Set requires_grad_(False) on the frozen backbone before building the optimizer. When unfreezing, add the newly trainable parameters to the optimizer or rebuild its parameter groups. Calling model.train() also switches child modules to training mode, so reapply evaluation mode to frozen BatchNorm layers when keeping their running statistics fixed.
Keras 3. Set base_model.trainable = False for head training. Recompile after changing trainable. When unfreezing a base model that contains BatchNormalization, the Keras guide shows invoking the base with training=False to keep its normalization statistics in inference mode.
Common mistakes. Follow the checkpoint’s preprocessing contract. Split by subject, source, or time when near-duplicates could cross a random split. Compare the frozen head, partial fine-tuning, and random initialization on the same target splits. A hypothetical rise from 81% to 85% macro recall is four percentage points. Check variation across seeds and inspect which classes gained before interpreting the change.
References
- Keras transfer learning and fine-tuning guide
- PyTorch computer vision transfer learning tutorial
- Yosinski and colleagues on feature transferability
Practice and FAQs
How many trainable parameters are in a three-class linear head on 512 features?
There are 1,536 weights and three biases, for 1,539 trainable parameters while the backbone stays frozen.
How do you compare transfer learning with training from scratch?
Train a randomly initialized baseline and a pretrained alternative on the same target training split. Use the same held-out targets, control evaluation choices, and report variability across runs.
Why can validation deteriorate when you unfreeze the backbone?
A large learning rate may rapidly change pretrained features. Test a lower rate on the newly trainable layers and use validation metrics to decide how much of the backbone to unfreeze.
Why can BatchNorm statistics change even when PyTorch parameters are frozen?
Running means and variances are buffers. A BatchNorm layer in training mode can update them during the forward pass, so keep those layers in evaluation mode when you want to retain the pretrained statistics.
