Chapter 6-Transfer Learning
Chapter 6-Transfer Learning
Definition
Transfer learning in deep learning is a
technique that uses knowledge from a pre-
trained model to improve performance on a
new task. It's a popular approach because it
can reduce the amount of data, time, and
compute resources required to train a
model.
My Transfer Learning
Options in Transfer Learning
Fine-Tuning the Entire Re-cook paneer bhaji
Model with peas, adjusting all
spices.
Freezing Some Layers Keep the original
and Training Others gravy, just add peas
without changing it.
Use leftover paneer
Using the Model as a
Feature Extractor gravy as a base for a
new dish (soup or
curry).
Options in Transfer Learning
Fine-Tuning the Entire Update all the weights
Model at all layers.
o Domain Adaptation,
o Domain Confusion,
o One-shot Learning,
o Zero-shot Learning,
o Multitask Learning;
Domain Adaptation
Domain adaptation is a type of deep transfer learning that
aims to address domain shifts between source and target data.
In cases where data distributions differ significantly across
domains, domain adaptation helps align feature spaces
between the two, so a model trained on one domain can
perform well in another.
Key Idea of Domain Adaptation
o Domain adaptation techniques seek to reduce the domain
discrepancy by either:
▪ Learning domain-invariant features that perform well on both domains.
▪ Transforming source data to resemble the target domain (or vice versa).
▪ Adapting model parameters to better generalize on the target domain.
Example: Domain Adaptation for Sentiment Analysis in Different Industries
Let’s say we have a sentiment analysis model trained on movie reviews (source domain) and want to apply it
to predict customer sentiment in product reviews for electronics (target domain). In this case, there is a
significant domain shift since the language and keywords in movie reviews (like "plot" or "acting") differ
from those in electronics reviews (like "battery life" or "display").
Domain Adaptation Techniques
o Feature-Based Adaptation: A deep network learns domain-invariant features by aligning feature spaces across
domains. For example, techniques like Domain-Adversarial Neural Networks (DANN) use a gradient reversal
layer that encourages the model to learn features that are predictive of sentiment while being agnostic to the specific
domain.
o Data Transformation: Another approach is to modify the source data to make it look more like the target data
through style transfer or domain translation. Here, we might use a language model to rephrase movie reviews to
contain product-related vocabulary, enabling a smoother transfer of sentiment classification knowledge.
o Parameter Adaptation: Fine-tuning certain model layers on a small set of electronics reviews allows the model to
adapt its higher-level features to the target domain’s language patterns while retaining general sentiment detection
abilities from the movie reviews.
Other examples
o Medical Imaging: Models trained on MRI scans from one machine are adapted to perform well on scans from different machines or institutions.
o Text Classification Across Industries: Sentiment or topic classification models are adapted to perform well across industries, where vocabulary and
context vary widely.
o Autonomous Driving: Simulation data is adapted to real-world driving scenarios, reducing the model’s sensitivity to discrepancies in environmental
conditions.
Domain Confusion
Domain Confusion is a type of deep transfer learning technique used to make a
model invariant to differences between source and target domains. By using
domain confusion, the model learns to ignore domain-specific details and
focuses instead on features that generalize well across domains.
Domain confusion works by introducing a mechanism that encourages the model
to learn features common to both domains. This is typically achieved using
adversarial training, where:
A feature extractor learns shared representations from both source and
target data.
A domain discriminator is trained to differentiate between the source and
target domain features.
The feature extractor tries to "confuse" the domain discriminator by learning
features that are similar across domains.
This process allows the model to focus on domain-invariant features, improving
generalization to the target domain.
Example: Sentiment Analysis for Cross-Language Texts
a sentiment analysis model pre-trained in English (source domain) and
want to apply it to classify sentiments in Spanish (target domain).
Here’s how domain confusion helps:
o Feature Extraction: A neural network is trained to extract features from
English and Spanish texts. Ideally, the model will learn to focus on
sentiment-related features that apply to both languages.
o Domain Discriminator: The domain discriminator tries to determine
whether a given text feature representation originates from the English or
Spanish dataset. This encourages the feature extractor to learn language-
invariant features that convey sentiment without relying heavily on language-
specific elements.
Analysis
Domain confusion allows the model to adapt to Spanish by focusing on
language-agnostic indicators of sentiment, such as contextual patterns
around positive or negative expressions. This reduces the model’s
reliance on language-specific features, like syntax or vocabulary, and
helps it generalize to multiple languages without direct translation.
Applications Beyond Language
Domain confusion is widely applicable to scenarios with domain shifts,
such as:
Image Classification: Transferring models trained on synthetic images
(e.g., computer-generated) to real-world images.
One-shot Learning
One-shot learning is a type of deep transfer learning where
a model is trained to recognize new classes or tasks with very
few (often just one) examples. It’s useful in scenarios where
data for new tasks is scarce, but the model has been pre-
trained on similar tasks or classes.
Key Idea of One-shot Learning
The goal of one-shot learning is to enable the model to
generalize to new classes by leveraging prior knowledge
learned on a related task, typically with a metric-learning
approach, where the model learns a similarity measure
between examples.
Example: One-shot Learning for Facial Recognition
Scenario
Consider a facial recognition system deployed at a secure facility. The system is pre-trained on a
large dataset of faces but occasionally needs to add new personnel for access control. However,
collecting multiple images of each new person may be impractical. The system must learn to
recognize new individuals with only one or a few images of each.
One-shot learning can be achieved here using a Siamese Network:
Network Architecture: A Siamese network consists of two identical subnetworks that share
weights and learn to compare two inputs. During training, it learns to output a similarity score
between two images.
Training Phase: The model is pre-trained on pairs of images to distinguish between "same
person" and "different person" classes. This helps it learn a similarity function that works for facial
features.
One-shot Phase: When a new person’s image is introduced, the system compares this single
image against stored images in the database. By computing similarity scores, the model determines
whether the new face matches any existing records, essentially achieving identification with a single
example.
Advantages of One-shot Learning in This Context
Data Efficiency: Recognizes new individuals with minimal data.
Generalization: Effective for scenarios where class characteristics are similar (in this case, human
faces).
Scalability: As new faces are added, the model does not require retraining but simply compares
features, making it scalable for real-time use.
Zero-shot Learning
Zero-shot learning (ZSL) is a type of deep transfer learning where a
model learns to recognize objects or concepts it hasn’t seen during
training. This is achieved by transferring knowledge from known classes
to unseen ones through shared attributes or descriptions, enabling the
model to generalize to new categories without direct training data for
those classes.
How Zero-Shot Learning Works
Zero-shot learning typically relies on additional semantic information,
such as:
Attributes: Shared properties among classes, like color or shape.
Text Descriptions: Natural language descriptions or embeddings for each
class.
Hierarchical Relationships: Relationships in a hierarchy (e.g., species
taxonomy).
These semantic features help the model make educated guesses about new,
unseen classes based on learned relationships.
Example of Zero-Shot Learning in Image Classification
Suppose we train a model to recognize animals, but we only have images for a limited set of classes,
such as “dog,” “cat,” and “horse.” We want this model to identify a new animal—say, a “zebra”—even
though it has never seen zebra images in the training data.
Solution Using Zero-Shot Learning
Attribute-Based Approach: Each known animal class is associated with attributes, such as:
Dog: Attributes include "four legs," "furry," and "domestic animal."
Horse: Attributes include "four legs," "mane," and "large size."
Zebra (target class): We define the attributes as "four legs," "stripes," "wild animal."
Transfer Process: When the model is presented with an image of a zebra, it uses these semantic
attributes to compare the zebra's characteristics with those it learned from seen classes. Since “zebra”
shares the attribute of “four legs” with both horses and dogs but also has a unique attribute (“stripes”),
the model can predict that the new animal is most likely a zebra.
Embedding-Based Approach: Alternatively, natural language embeddings (like Word2Vec or
GloVe) can represent semantic relationships between “zebra” and other known classes, such as “horse”
or “animal.” The model can use these embeddings to infer that a zebra, while unseen, belongs to the
general category of quadrupedal mammals with specific characteristics.
Real-World Applications
Object Recognition in Robotics: Robots equipped with zero-shot learning can recognize new items
in warehouses (e.g., a specific type of electronic device) by leveraging attributes or descriptions, even
without specific training for that item.
Medical Diagnosis: For rare diseases with limited training data, models can use descriptions of
symptoms to identify new conditions based on similarity to known diseases.
Benefits and Limitations
Benefits: Zero-shot learning reduces the need for extensive labeled datasets, enabling recognition of
diverse or rare categories.
Limitations: Success depends on accurate, relevant semantic descriptors. If descriptors are insufficiently
detailed, the model might struggle with accurate predictions.
Multitask Learning
Multitask Learning (MTL) is a type of deep transfer learning where a
model is trained to perform multiple related tasks simultaneously. Rather
than learning each task independently, MTL leverages shared information
across tasks, enabling it to generalize better and improve performance on
each task. This approach is particularly beneficial when tasks are related, as
learning from one task can provide useful insights for others.
Key Characteristics of Multitask Learning
Shared Representation: In MTL, the model’s earlier layers (or parts) are
shared across all tasks, enabling these layers to learn representations that are
useful across different tasks.
Task-Specific Layers: In addition to shared layers, each task has its own
dedicated layers in the later stages of the model, allowing for task-specific
adaptations.
Regularization Effect: Learning multiple tasks in parallel can serve as a form
of regularization, reducing the risk of overfitting, especially when the dataset for
each individual task is limited.
Example: Multitask Learning in Autonomous Driving
In autonomous driving, the system must simultaneously perform multiple tasks to safely navigate the environment.
For instance, an autonomous vehicle needs to:
Detect and classify objects (e.g., pedestrians, vehicles, traffic signs).
Segment the road area to differentiate it from non-road regions.
Estimate depth to understand the distance of various objects.
Identify drivable paths.
Using MTL, a single deep learning model can be trained to perform all these tasks concurrently:
Shared Representation: The model’s initial layers might learn to recognize edges, shapes, and other basic visual
features. These representations are beneficial for all tasks, as they capture essential environmental cues.
Task-Specific Layers: Later layers are dedicated to each specific task. For example:
Object detection might use bounding box regression and classification layers.
Semantic segmentation uses layers that output a pixel-wise classification of road and non-road areas.
Depth estimation uses layers to predict distance information.
Path prediction has layers focused on generating a probable trajectory.
Analysis of Benefits
Improved Performance: The shared representation learned across tasks allows the model to generalize better and make more accurate predictions. For
instance, the information learned from depth estimation can enhance object detection by helping the model differentiate between nearby and distant objects.
Data Efficiency: MTL makes more effective use of data, as the model learns complementary information from multiple tasks even if data for each individual
task is limited.
Reduced Computation: With a single model handling multiple tasks, MTL reduces the computational cost and memory requirements compared to
training separate models for each task.
Joint
Learning
Task
Joint
Learning
Task
<Start>
I LOVE YOU
Classifier
<Start>
O O E
Go to School
<Start>
45
VGG 16 with multiple outputs
Task 1
Task 2
47
Single Sequential input and
Multiple Sequential outputs
46
Multi-Task Learning
Multi-Task Learning (MTL) is a type of machine learning technique where a model is trained to perform
multiple tasks simultaneously.
In deep learning, MTL refers to training a neural network to perform multiple tasks by sharing some of
the network’s layers and parameters across tasks.
the goal is to improve the generalization performance of the model by leveraging the information shared
across tasks.
By sharing some of the network’s parameters, the model can learn a more efficient and compact
representation of the data,
which can be beneficial when the tasks are related or have some commonalities.
Multitask Learning
There are different ways to implement MTL in deep learning, but the
most common approach is to use a shared feature extractor and
multiple task-specific heads.
The shared feature extractor is a part of the network that is shared across
tasks and is used to extract features from the input data.
The task-specific heads are used to make predictions for each task and are
typically connected to the shared feature extractor.
multi-task learning should be used when the tasks have some level of correlation.
In other words, multi-task learning improves performance when there are underlying
principles or information shared between tasks.
Zero shot and Few Shot Learning
With the amazing success of unsupervised learning methods and transfer learning, the NLP community
has built models which serve as a knowledge base for multiple NLP tasks.
However, we’re still dependent on annotated data for fine-tuning on a downstream task.
Often, getting labeled data is not handy and is a relatively expensive and time taking exercise.
What can we do if we don’t have any labeled data or have very less of it?
There is no single definition of zero and few shot methods. Rather, one can say that its definition is task
dependent.
Zero Shot Classification
train a model on some classes and predict for a new class, which the model has never seen before.
the class name needs to exist in the list of classes, but there are no training samples for this class.
From the task’s description, it is quite clear to us what is to be done [Link] have used our knowledge base to infer what translation
means.
Reading the self explanatory task explanation (happy-or-sad), we understand that it is a classification task. Our knowledge base also
helps us understand the sentence and infer that it is happy!
Zero-shot learning
Zero-shot learning in NLP refers to a model’s ability to
perform a task it has never explicitly been trained on by
leveraging generalized knowledge learned from other tasks.
The model can handle new tasks with no direct task-specific
examples, making it useful in situations where labeled data is
unavailable for certain tasks.
• Example: Zero-Shot Text Classification with GPT-3
Imagine you have a customer feedback dataset and you want to classify
the feedback into categories like "Product Issue," "Customer Service," and
"Other." However, you don't have labeled examples for this specific task.
Scenario:
You have the following customer feedback:
"The product stopped working after two weeks."
"The support team was very helpful!"
"I had trouble understanding how to set up the device."
Without any explicit training on this task, you can still use zero-shot
learning to classify these texts using a pre-trained model like GPT-3.
Steps for Zero-Shot Learning in this Scenario:
[Link] Input: Instead of fine-tuning the model for this specific classification task, you
simply describe the task using natural [Link] might prompt the model like this:
[Link] 1: "Classify the following customer feedback into one of these categories: 'Product
Issue', 'Customer Service', 'Other'."
[Link]: "The product stopped working after two weeks."
[Link] 2: "Classify the feedback: 'The support team was very helpful!' as 'Product Issue',
'Customer Service', or 'Other'."
Zero-Shot Output:
Even without task-specific training data, the pre-trained model can classify the texts
based on its knowledge of language patterns:
•The model hasn’t seen this specific dataset or task during training. However, it has
learned a general understanding of how to classify text based on descriptions or
categories from massive amounts of training data.
Let’s say you want to perform sentiment analysis but don’t have a specific
sentiment dataset.
• You can simply prompt the model like this:
•
Prompt: "What is the sentiment of the following sentence: 'I love the new
features of this app!'"
• Model Output: "Positive"
In this case, the model uses its generalized understanding of sentiment without
having been trained on the specific dataset or task.
Working of zero shot learning
This is exactly how zero shot classification works.
We have a pre trained model (eg. a language model) which serves as the knowledge base since it has been
trained on a huge amount of text from many websites.
For any type of task, we give relevant class descriptors and let the model infer what the task is.
Domain-specific pre-
When transfer reduces Using a natural image
training, domain
Negative Transfer performance on the classifier to detect
adaptation, careful
target task. anomalies in MRI scans.
feature selection.
Transferring a model
The theoretical limits to Domain re-training,
from urban vehicle
transfer effectiveness adversarial training,
Transfer Bounds detection to satellite
based on task similarity unsupervised domain
vehicle detection with
and data differences. adaptation.
scale differences.
Negative Transfer
Definition: Negative transfer occurs when the knowledge from a source task
or domain worsens the model’s performance on the target task. This often
happens when the source and target tasks are not sufficiently similar, leading to
detrimental model adaptations.
Example: Consider transferring a model trained on general object recognition
(like ImageNet) to a specialized medical task, such as detecting rare anomalies
in MRI scans. If the pre-trained model has learned features like color and
texture specific to natural images, these may not be helpful—and could even
mislead the model—when fine-tuned on grayscale medical images with
different textures and structures. As a result, the model may struggle to focus
on medical-specific patterns, reducing its accuracy on anomaly detection.
Strategies to Mitigate: Techniques like domain adaptation or domain-
specific pre-training (e.g., initializing with models pre-trained on medical
image data) can mitigate negative transfer by making the feature representations
closer to those needed for the target domain.
Transfer Bounds
Definition: Transfer bounds refer to the theoretical limits that indicate how
well a model’s performance on a source task can bound or predict its success on
the target task. These limits are informed by factors like the similarity between
the tasks, the size of the target dataset, and the degree of domain shift.
Example: Suppose we transfer a model from detecting vehicles in urban
images to a target task of detecting vehicles in satellite imagery. If the satellite
imagery is very different in scale, angle, and context, the model may not
perform well, indicating a transfer bound issue. Even if we fine-tune on a large
target dataset, the learned features may only provide limited utility due to the
fundamental differences in viewpoint and resolution. The transfer effectiveness
is bounded because the initial learned features from urban scenes do not align
well with the visual cues in satellite imagery.
Strategies to Address: Domain-specific re-training and unsupervised
domain adaptation techniques, such as adversarial training to bridge the
domain gap, can improve transfer success within these bounds by aligning
features across the source and target distributions.
Transfer learning Applications:
Transfer learning for
o NLP/
o Audio/
o Speech/
o Computer Vision.
Transfer learning for NLP
1. Sentiment Analysis
o Application: Understanding customer feedback, reviews, or social media sentiment.
o Transfer Learning Example: Fine-tuning a BERT model, initially pre-trained on large
general text corpora, to classify sentiment polarity (positive, neutral, or negative) on a custom
dataset of product reviews.
o Analysis: A sentiment analysis model without transfer learning might start with randomly
initialized weights, resulting in high error rates and longer training time. In contrast, BERT fine -
tuning transfers learned patterns like syntax and sentiment-related words, yielding higher
accuracy and faster convergence on a smaller dataset.
2. Text Summarization
o Application: Automated generation of news summaries, article highlights, and research paper
summaries.
o Transfer Learning Example: Using a pre-trained T5 (Text-to-Text Transfer Transformer)
model, originally trained on tasks like summarization, question answering, and translation.
Fine-tuning it on domain-specific data (e.g., legal documents) for summarization.
o Analysis: Pre-trained models understand context, entities, and general summarization
patterns. Fine-tuning the T5 model allows it to learn specialized vocabularies and phrasing.
Transfer learning achieves near state-of-the-art summarization, while a model trained from
scratch would likely underperform on nuanced topics.
Transfer learning for NLP
3. Named Entity Recognition (NER)
o Application: Extracting names of people, organizations, and locations from unstructured text in various fields like
finance or healthcare.
o Transfer Learning Example: Adapting a RoBERTa model, which has learned general language structure and named
entities in a pre-trained setting, for domain-specific NER, such as legal documents or clinical notes.
o Analysis: NER models trained from scratch might struggle to detect specialized entities unique to a domain. Fine -
tuning RoBERTa improves entity recognition accuracy, especially in contexts where specific terms have distinct
meanings.
4. Question Answering (QA)
o Application: Providing quick answers from knowledge bases, FAQs, or customer service databases.
o Transfer Learning Example: Adapting a DistilBERT model, pre-trained on general question-answering tasks, for
specific FAQs related to a product.
o Analysis: Pre-trained models have learned general answer extraction, sentence boundaries, and relevancy ranking.
Fine-tuning for product-specific QA reduces training time and improves the model’s ability to pinpoint relevant
information. This approach is particularly efficient for companies with high customer service demands.
5. Machine Translation
o Application: Translating content between languages, especially for specialized content like technical manuals or
academic papers.
o Transfer Learning Example: Fine-tuning a MarianMT model, pre-trained for multilingual translation, for a
language pair like English-Finnish focused on technical language.
o Analysis: Starting with a pre-trained multilingual model means that fundamental sentence structure and vocabulary
are already learned. Fine-tuning on technical content improves translation accuracy on specialized terms, compared to
a model that might be trained from scratch only on this niche content.
Transfer learning for Audio
1. Speech Recognition
o Problem: Recognize spoken words or phrases, often requiring large
amounts of labeled data.
o Solution: Pretrain a model on a large audio dataset (like LibriSpeech) and
fine-tune it on domain-specific audio data.
o Example: Use a pre-trained model on general spoken language datasets and
fine-tune it for a custom dataset in a specific accent or dialect, making the
model more accurate for that population without needing extensive labeled
data.
o Metrics: Word Error Rate (WER) and phoneme classification accuracy.
2. Speaker Identification
o Problem: Distinguish between different speakers based on audio clips.
o Solution: Pretrained models like wav2vec 2.0 can be used to generate
embeddings, and then fine-tuned with data from specific speakers to adapt
the model.
o Example: Using a large speech dataset pretrained model and fine-tuning it
on a smaller set of clips from target speakers.
o Metrics: Accuracy, F1-score for speaker identification.
Transfer learning for Audio
3. Environmental Sound Classification
o Problem: Classify sounds from various environments (e.g., urban sounds, wildlife
sounds).
o Solution: Transfer learning using a pretrained model like YAMNet or VGGish (trained on
datasets like AudioSet) can help classify sounds even in complex or noisy environments.
o Example: A model trained on large, generic sound datasets can be fine-tuned to classify
sounds in a specific habitat for ecological monitoring.
o Metrics: Precision, recall, and mean Average Precision (mAP).
4. Emotion Detection in Speech
o Problem: Detect emotional tones from voice recordings, which may have variations in
intonation, pitch, and tempo.
o Solution: Use pretrained models on general audio datasets to extract audio features, then
fine-tune on labeled emotional datasets like IEMOCAP for nuanced emotion classification.
o Example: Adapt a model for customer service applications, where customer emotions
can be detected in real-time to adjust responses.
o Metrics: Emotion recognition accuracy, confusion matrix.
5. Music Genre Classification
o Problem: Classify music into genres based on sound patterns and features.
o Solution: Using a model pretrained on large audio datasets (e.g., GTZAN music dataset)
and then fine-tuning on specific genres or regional music datasets.
o Example: Fine-tune on specific genre categories (e.g., subgenres within electronic or
jazz) for applications in music recommendation.
o Metrics: Classification accuracy, precision, recall per genre.
Transfer learning for Computer Vision
1. Medical Image Analysis
o Application: Transfer learning is especially useful for medical imaging tasks like
detecting tumors in MRI scans, pneumonia in chest X-rays, and skin lesions in
dermatology.
o Example: A model pre-trained on a dataset like ImageNet can be fine-tuned for
pneumonia detection using chest X-rays. The lower layers retain the ability to detect
simple shapes and patterns, while upper layers adapt to recognize pneumonia-specific
indicators like lung opacity.
o Analysis: Fine-tuning the model on a smaller dataset of annotated medical images often
yields high accuracy, with less training time compared to training from scratch. This is
valuable as medical image datasets are often small and require expert annotations.
2. Object Detection in Autonomous Vehicles
o Application: Self-driving cars need to recognize and interpret objects like pedestrians,
vehicles, and road signs accurately. Transfer learning enables models to generalize from
common object detection datasets (e.g., COCO) and specialize for automotive data.
o Example: A model pre-trained for general object detection is adapted for autonomous
driving by adding classes like "traffic light" and "stop sign" through fine-tuning with car-
specific datasets like KITTI or Waymo Open Dataset.
o Analysis: Using transfer learning accelerates the training process and improves real-time
detection accuracy under complex road conditions, enhancing the vehicle's responsiveness
and safety.
Transfer learning for Computer Vision
3. Satellite and Aerial Imagery Analysis
o Application: Tasks like land cover classification, detecting changes in urban areas, and monitoring deforestation can
benefit from transfer learning with computer vision models.
o Example: Pre-trained models fine-tuned on satellite imagery data, like the SpaceNet dataset, can detect roads,
buildings, and vegetation types effectively. Transfer learning helps in recognizing these features by leveraging the
spatial pattern recognition capabilities learned from other image datasets.
o Analysis: Such applications benefit from models pre-trained on diverse datasets, as they help generalize to varying
environmental and lighting conditions without requiring extensive labeled data.
4. Product Defect Detection in Manufacturing
o Application: Transfer learning aids in identifying defective products from high-resolution images in industrial
quality control.
o Example: A pre-trained convolutional neural network (CNN) model on ImageNet can be fine-tuned to classify
defects in electronics, like scratches on screens or irregularities in semiconductor wafers.
o Analysis: This approach is effective when defect samples are scarce, as the model can detect anomalies by focusing on
minute texture and pattern deviations from its pre-trained knowledge.
5. Facial Recognition for Security
o Application: Transfer learning is frequently applied in facial recognition systems for verification and identification in
security.
o Example: A model pre-trained on a large facial recognition dataset (like VGGFace) can be fine-tuned to recognize
employees within a specific company. The model learns to identify unique facial features more effectively, even under
challenging lighting and angles.
o Analysis: Pre-trained models adapt well to recognizing subtle facial features across different individuals, requiring
minimal additional data and allowing efficient implementation in real-world applications.
Thank You