0% found this document useful (0 votes)
28 views

Unit 2

Uploaded by

vishalbobby680
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Unit 2

Uploaded by

vishalbobby680
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Transfer learning in the Transformers library is a central approach for tackling various natural language

processing (NLP) tasks. It involves leveraging pre-trained models that have been trained on large datasets to serve as
the starting point for a wide range of downstream tasks. The process of fine-tuning these pre-trained models on task-
specific data allows users to benefit from the knowledge encoded in the original model while adapting it to new, more
specialized objectives.

Role of Transfer Learning in Transformers

The Transformers library, developed by Hugging Face, provides numerous pre-trained models like BERT, GPT, and T5,
which are widely used for tasks such as text classification, translation, summarization, and question answering. These
models are typically trained on massive corpora, such as Wikipedia or the Common Crawl, allowing them to capture deep
contextual understanding of language. Transfer learning in this context typically works in two stages:

Pre-training: A model is trained on large-scale, general-purpose language data with objectives like masked language
modeling (in BERT) or next-word prediction (in GPT). This allows the model to learn universal language patterns,
relationships, and syntactic/semantic structures.

Fine-tuning: After pre-training, the model is further trained (fine-tuned) on a task-specific, often much smaller dataset. This
adapts the general language understanding to the particular task (e.g., sentiment analysis, entity recognition) without
requiring vast amounts of task-specific data.
Benefits of Transfer Learning in Transformers

Efficiency in Data Usage: Fine-tuning a pre-trained model often requires only a small dataset. This is because the pre-
trained model already captures a deep understanding of the language, reducing the need for extensive labeled task-
specific data.

Reduced Computational Costs: Pre-training large language models is computationally expensive and time-
consuming. However, with transfer learning, users can leverage these pre-trained models and focus only on fine-tuning,
which is more computationally feasible.

Improved Performance: Models pre-trained on large corpora often yield better performance than models trained from
scratch, especially on tasks where the labeled dataset is small. They provide a strong baseline due to their extensive
language understanding.

Generalization Across Tasks: Pre-trained models can generalize across a wide range of tasks. Once fine-tuned for a
specific task, they can adapt quickly to others with minimal effort due to the learned language representations.

Access to State-of-the-Art Models: The Transformers library provides access to top-performing models (BERT, GPT,
RoBERTa, etc.), giving practitioners the ability to achieve cutting-edge results without building models from scratch.

Dr. Ashaq Bhat 2


Challenges of Using Pre-Trained Models
Domain Mismatch: Pre-trained models might not perform optimally if the downstream task’s domain differs
significantly from the domain of the pre-training corpus. For instance, a model pre-trained on news articles might not
perform well on medical text unless fine-tuned carefully.
Fine-tuning Complexity: Fine-tuning large models can be tricky. Overfitting is a risk, especially if the fine-tuning
dataset is small. Careful hyperparameter tuning is needed to avoid this and ensure the model adapts well to the new
task.
Resource Requirements for Fine-tuning: While fine-tuning is more efficient than pre-training from scratch, large
models like GPT or BERT still require substantial computational resources (e.g., GPUs) to fine-tune effectively.
Catastrophic Forgetting: During fine-tuning, pre-trained models can forget some of the general knowledge they
acquired during pre-training. This can lead to reduced performance on broader language tasks or even the pre-trained
tasks if not fine-tuned correctly.
Model Size and Latency: Large transformer models are computationally intensive and require significant memory.
In production, this can lead to slower inference times and increased deployment costs.

Dr. Ashaq Bhat 3


Sentiment Analysis using TextBlob focuses on two key metrics: polarity and subjectivity. These metrics
help quantify the sentiment expressed in a piece of text, determining whether the sentiment is positive, negative, or
neutral, as well as how subjective the text is.
Polarity and Subjectivity Metrics
Polarity: Polarity measures the sentiment orientation of the text.
It ranges from -1 to +1:
-1: Very negative sentiment.
0: Neutral sentiment.
+1: Very positive sentiment.
Example: "I love this product!" will have a high positive polarity, while "I hate this service" will have a high
negative polarity.
Subjectivity: Subjectivity measures the degree of personal opinion in the text.
It ranges from 0 to 1:
0: Objective text (fact-based, neutral).
1: Highly subjective text (opinion-based).
Example: "The Earth orbits the Sun" is factual and would have low subjectivity, while "This is the best restaurant in
town" is subjective, with a high subjectivity score.

Dr. Ashaq Bhat 4


Example of Sentiment analysis

from textblob import TextBlob

# Example 1: Positive sentiment


text1 = "TextBlob is a wonderful library for NLP."
blob1 = TextBlob(text1)
print(f"Polarity: {blob1.sentiment.polarity}, Subjectivity: {blob1.sentiment.subjectivity}")

# Example 2: Negative sentiment


text2 = "I am really disappointed with the service."
blob2 = TextBlob(text2)
print(f"Polarity: {blob2.sentiment.polarity}, Subjectivity: {blob2.sentiment.subjectivity}")

# Example 3: Neutral sentiment


text3 = "This is a book."
blob3 = TextBlob(text3)
print(f"Polarity: {blob3.sentiment.polarity}, Subjectivity: {blob3.sentiment.subjectivity}")

# Example 4: Mixed sentiment


text4 = "The product is great but the packaging was terrible."
blob4 = TextBlob(text4)
print(f"Polarity: {blob4.sentiment.polarity}, Subjectivity: {blob4.sentiment.subjectivity}")

Dr. Ashaq Bhat 5


Analysis of Examples
Positive Sentiment:
Text: "TextBlob is a wonderful library for NLP."
Polarity: Positive (close to +1, e.g., 0.8).
Subjectivity: Subjective (e.g., 0.75), since "wonderful" expresses personal opinion.
Negative Sentiment:
Text: "I am really disappointed with the service."
Polarity: Negative (close to -1, e.g., -0.6).
Subjectivity: Highly subjective (e.g., 0.9), as "disappointed" is a personal feeling.
Neutral Sentiment:
Text: "This is a book."
Polarity: Neutral (0.0).
Subjectivity: Objective (0.0), since it is a factual statement.
Mixed Sentiment:
Text: "The product is great but the packaging was terrible."
Polarity: Slightly negative or neutral (e.g., 0.0 to -0.1), since it has both positive and negative components.
Subjectivity: Subjective (e.g., 0.6), as both "great" and "terrible" are opinions.

Benefits and Use Cases



Polarity helps classify texts for applications like customer feedback, where businesses can determine if reviews
are positive or negative.

Subjectivity is useful for identifying opinion-based content, which could help filter editorial content or social
media posts based on objectivity.

Dr. Ashaq Bhat 6


SpaCy is an open-source, advanced Natural Language Processing (NLP) library in Python that is widely used for building production-level
NLP applications. It is designed for speed, scalability, and ease of use, making it a go-to choice for tasks like text processing, named entity
recognition, and dependency parsing. SpaCy supports several languages and provides pre-trained models that are capable of handling various
NLP tasks out of the box.
Key Features of SpaCy:
Pre-trained Models: SpaCy provides pre-trained models for various NLP tasks, including part-of-speech (POS) tagging, named entity recognition
(NER), dependency parsing, and sentence segmentation.
Tokenization: It has an efficient tokenizer that breaks down the text into tokens (words, punctuation, etc.) while maintaining linguistic rules.
Named Entity Recognition (NER): SpaCy is capable of recognizing named entities such as persons, organizations, locations, and dates. It can be
customized for domain-specific entities as well.
Dependency Parsing: It provides dependency parsing to extract the syntactic structure of sentences, identifying relationships between words like
subject-verb-object.
Custom Pipelines: SpaCy allows developers to customize and extend its processing pipeline by adding or modifying components to suit specific
applications.
Integration with External Libraries: SpaCy integrates seamlessly with deep learning frameworks like TensorFlow and PyTorch, enabling tasks such
as text classification or sequence labeling with custom models.
Efficient for Production: SpaCy is built to be fast and memory-efficient, making it suitable for large-scale applications and deployment in production
environments.
Extensible Architecture: The modular nature of SpaCy allows you to extend its capabilities by building your own components or integrating external
data sources and knowledge bases.

Dr. Ashaq Bhat 7


Core Components of SpaCy:
Tokenization: Breaks down text into individual tokens (words, punctuation).
Lemmatization: Converts words to their base forms (e.g., "running" to "run").
Part-of-Speech Tagging: Assigns a grammatical role to each token (e.g., noun, verb).
Dependency Parsing: Analyzes the syntactic structure and relations between words.
Named Entity Recognition (NER): Detects and labels entities such as names, places, and organizations.
Sentence Segmentation: Splits text into individual sentences based on linguistic cues.
Vector Representation: SpaCy provides word vectors and similarity comparisons using pre-trained embeddings.

import spacy

# Load a pre-trained SpaCy model


nlp = spacy.load("en_core_web_sm")

# Process some text


doc = nlp("Apple is looking at buying a U.K. startup for $1 billion.")

# Tokenization
for token in doc:
print(token.text, token.lemma_, token.pos_, token.dep_)

# Named Entity Recognition (NER)


for ent in doc.ents:
print(ent.text, ent.label_)

# Dependency Parsing
for token in doc:
print(f'{token.text} --> {token.head.text} ({token.dep_})')

Dr. Ashaq Bhat 8


Customizing SpaCy is particularly beneficial in industries where domain-specific language, terminology, and structures are
essential for accurate Natural Language Processing (NLP). Many fields rely on fine-tuned models to handle specialized jargon, entities, and
patterns that general-purpose NLP models may not recognize.

Industries and Applications Benefiting from Customizing SpaCy

Healthcare & Medical Research:

Application: Extracting diseases, medications, symptoms, and medical procedures from unstructured text such as research papers or clinical notes.

Customization: Train a specialized Named Entity Recognition (NER) model to recognize medical entities like drug names, anatomical terms, or
clinical abbreviations.

Legal Industry: Application: Parsing legal documents, contracts, and regulations to extract parties, clauses, dates, and obligations.

Customization: Custom NER for legal entities such as court names, statute references, case citations, and legal terms.

Financial Services: Application: Processing financial reports, news, and regulatory filings to extract information about companies, stock prices,
transactions, and risk indicators.

Customization: Add custom components to identify financial terms, company names, stock tickers, and transaction types.

E-commerce: Application: Analyzing customer reviews, product descriptions, and advertisements to extract product features, customer sentiment,
and competitor information.

Customization: Fine-tune NER models to detect product names, features, and brands.

Scientific Research: Application: Extracting chemical compounds, biological entities, or gene names from research articles or patent filings.

Customization: Create custom pipelines for recognizing scientific terms, formulas, or references to experiments and results.

Dr. Ashaq Bhat 9


Customizing SpaCy's Named Entity Recognition (NER) and Other Components
To handle domain-specific terms and entities in SpaCy, several customization methods can be employed:
1. Custom Named Entity Recognition (NER)
Fine-tuning NER Models:
Data Collection: Gather domain-specific annotated datasets with entity labels relevant to your field (e.g., chemicals, legal
clauses, or medical conditions).
Training: Fine-tune SpaCy’s NER model using this custom dataset. This involves re-training the model to recognize new
entities or categories while preserving the general ones (like persons, organizations, dates).
Entity Ruler:For simpler cases, an Entity Ruler can be used to create custom rules that detect domain-specific terms
without training.
# Load a blank or pre-existing model
nlp = spacy.blank("en")

# Define new labels ruler = nlp.add_pipe("entity_ruler")


ner = nlp.create_pipe("ner") patterns = [{"label": "CHEMICAL", "pattern":
ner.add_label("DISEASE") "aspirin"}, {"label": "BIOLOGY", "pattern": "gene
ner.add_label("DRUG") expression"}]
nlp.add_pipe(ner, last=True) ruler.add_patterns(patterns)

# Train the model with custom data


optimizer = nlp.begin_training()
for itn in range(10):
for text, annotations in TRAIN_DATA:
nlp.update([text], [annotations], sgd=optimizer)

Dr. Ashaq Bhat 10


2. Custom Tokenizer
Special Tokenization Rules:
In domains like finance or biology, words or symbols might have specific meanings (e.g., stock tickers like "AAPL", gene
names, or chemical formulas). Customize SpaCy’s tokenizer to correctly handle punctuation or symbols as part of a token,
rather than splitting them.
Example: Allow $GOOG to remain as a single token instead of splitting it into $ and GOOG.

3. Customizing the Processing Pipeline


Pipeline Components:
SpaCy allows the addition of custom components to the processing pipeline for domain-specific tasks. For example, you can
add a custom component to extract relations between entities or to perform specific transformations before the text is
processed by other components like NER or POS tagging.
Using Knowledge Bases:
Integrate external knowledge bases to link extracted entities to real-world entities (e.g., linking gene names to gene databases
or drug names to pharmacological information).
The EntityLinker component can be used to map recognized entities to IDs in an external database, improving the usability of
extracted entities in specialized domains.

Dr. Ashaq Bhat 11


SpaCy’s Processing Pipeline for Extracting Structured Information
SpaCy processes unstructured text through a pipeline of components, each performing a specific task to convert the text into structured
information. The typical pipeline includes the following steps:
Tokenization:
SpaCy splits the text into individual tokens (words, punctuation marks, etc.) using its tokenizer.
The tokenizer handles different languages and punctuation rules, producing a sequence of tokens for further processing.
Tagging:
Each token is assigned a part-of-speech (POS) tag, which indicates its grammatical role (noun, verb, adjective, etc.). POS tagging helps
in understanding the structure and meaning of the sentence.
Dependency Parsing:
SpaCy uses a dependency parser to determine the syntactic relationships between words in a sentence, building a dependency tree. This
helps in extracting subject-verb-object relationships or identifying noun phrases.
Named Entity Recognition (NER):
The NER component identifies named entities (e.g., persons, organizations, locations) and assigns a label to them. In a customized
pipeline, it can also detect domain-specific entities, such as medical conditions or financial terms.
Entity Linking (optional):
If integrated, an EntityLinker can map the recognized entities to a knowledge base for disambiguation or linking to real-world entities
(e.g., Wikipedia articles or drug databases).
Custom Components:
Custom pipeline components can be added at any stage for specific tasks like filtering, normalization, relation extraction, or applying
rules based on business logic.
Output:
The final result of the pipeline is a Doc object, which contains structured information about the text, including tokenized words, entity
annotations, syntactic dependencies, and more. This structured information can then be used for further analysis, search, or extraction of
specific details.

Dr. Ashaq Bhat 12


import spacy

# Load a SpaCy model


nlp = spacy.load("en_core_web_sm")

# Process the text


doc = nlp("Google acquired YouTube for $1.65 billion in 2006.")

# Extract structured information


for ent in doc.ents:
print(ent.text, ent.label_)
# Output: Google ORG, YouTube ORG, $1.65 billion MONEY, 2006 DATE

# Extract dependency relations


for token in doc:
print(token.text, token.dep_, token.head.text)

Dr. Ashaq Bhat 13


Fine-tuning pre-trained Transformer models, like BERT or GPT, on a custom dataset for sentiment analysis is a
highly effective way to leverage the deep contextual knowledge these models capture from large-scale training on general corpora.
Fine-tuning allows adapting the model to a specific task (like classifying text sentiment as positive, negative, or neutral), yielding
better results than training from scratch. Here’s a breakdown of techniques and best practices for fine-tuning pre-trained
transformer models for sentiment analysis.
Steps for Fine-tuning Transformer Models
1. Select a Pre-trained Transformer Model
Start with a Transformer-based model suitable for text classification, such as BERT, RoBERTa, DistilBERT, or ALBERT. For sentiment
analysis, BERT (Bidirectional Encoder Representations from Transformers) and its variants (e.g., DistilBERT) are popular choices due to
their success in text classification tasks.
Hugging Face’s Transformers library provides a rich collection of pre-trained models that can be fine-tuned for sentiment analysis.
2. Prepare the Custom Dataset
Data Format: Organize your dataset with clear input (text) and output labels (e.g., sentiment scores or categories). For example, you could
have labels like positive, negative, and neutral.
Text Preprocessing: Minimal text preprocessing is needed since the Transformer models handle tokenization efficiently using a WordPiece
tokenizer or Byte-Pair Encoding (BPE). Common preprocessing steps include removing extraneous whitespaces, lowercasing (if the model
isn't case-sensitive), and handling special tokens.
Tokenization: Tokenize the text using the same tokenizer that the model was pre-trained with, ensuring that the input is in the format that
the model expects.

Dr. Ashaq Bhat 14


3. Define the Sentiment Classification Task
Binary Classification: If you are working with positive and negative sentiments, the model will output two labels.
Multi-Class Classification: For multiple sentiment categories (e.g., positive, negative, neutral), modify the model output
accordingly
4.Set Up the Training Process
Optimizer: Use an optimizer like AdamW (Adam with weight decay), which works well with Transformer models.
Learning Rate: Transformers benefit from lower learning rates during fine-tuning. Typically, a range of 1e-5 to 5e-5 is used.
Too high a learning rate can cause the model to "forget" the pre-trained knowledge.
Batch Size: Choose a batch size that is small enough to fit into memory, as Transformer models are computationally intensive
(commonly 16 to 32).
Scheduler: Use a learning rate scheduler (e.g., linear warm-up) that gradually increases the learning rate at the beginning and
decreases it later during training.
Loss Function: Use cross-entropy loss for classification tasks.
5. Fine-tune the Model
Epochs: Fine-tune for a small number of epochs, typically 3 to 5, since large pre-trained models are sensitive to overfitting.
Validation: Always monitor validation accuracy or loss after each epoch to avoid overfitting. Early stopping can be
implemented if the validation performance plateaus.
Data Augmentation: For sentiment analysis, you may benefit from techniques like back-translation or random word deletion
to expand the training dataset and make the model more robust.n: Use cross-entropy loss for classification tasks.
6. Evaluate the Model
After training, evaluate the model on a test set to check its performance. Metrics like accuracy, F1-score, precision, and recall
are important for sentiment analysis tasks.

Dr. Ashaq Bhat 15


Best Practices for Fine-tuning Transformer Models
Start with a Small Learning Rate:
Transformer models are sensitive to large updates in weights, so starting with a small learning rate (e.g., 1e-5 to 5e-5) is
crucial. Fine-tuning too aggressively can cause the model to lose the general linguistic knowledge learned during pre-training.
Use Pre-trained Tokenizer:
Always use the same tokenizer that was used for pre-training the model. This ensures that tokenization aligns with the
model’s internal representations, such as subwords or word pieces.
Gradient Accumulation for Large Datasets:
When memory constraints prevent large batch sizes, gradient accumulation allows for the accumulation of gradients over
multiple mini-batches, effectively simulating a larger batch size.
# Accumulate gradients over batches
for step, batch in enumerate(train_dataloader):
outputs = model(**batch)
loss = outputs.loss / accumulation_steps
loss.backward()

if (step + 1) % accumulation_steps == 0:
optimizer.step()
scheduler.step()
optimizer.zero_grad()
Use Early Stopping:
Implement early stopping to prevent overfitting, especially if the model starts to overfit after a few epochs. Track the
validation loss and stop training if it doesn’t improve for a few epochs.

Dr. Ashaq Bhat 16


Regularization:
Use techniques like dropout or weight decay (via AdamW) to avoid overfitting, especially with smaller datasets.
Experiment with Freezing Layers:
For some tasks, freezing the lower layers of the Transformer model (which capture general linguistic features) and fine-tuning
only the upper layers can reduce overfitting and improve performance.
# Freeze all layers except the last layer(s)
for param in model.base_model.parameters():
param.requires_grad = False
Balance Dataset:
Ensure your dataset is balanced in terms of sentiment labels (positive, negative, neutral). If there is significant class
imbalance, techniques like class weighting or oversampling of underrepresented classes can help.
Data Augmentation:
Use text augmentation techniques to increase the robustness of the model. For example, back-translation (translating the text
to another language and back) can generate variations of the original data.
Evaluating Fine-tuned Models
After training, evaluate the model using various performance metrics:
Accuracy: Measures how often the model is correct.
Precision and Recall: Important when dealing with imbalanced datasets.
F1-Score: Balances precision and recall, offering a single metric for model performance.
Confusion Matrix: Helps visualize where the model is making mistakes, such as misclassifying positive sentiment as
negative.

Dr. Ashaq Bhat 17


The Transformer architecture is a deep learning model introduced in the paper "Attention is All You Need" by
Vaswani et al. in 2017. It has become the foundation for many modern natural language processing (NLP) models,
such as BERT, GPT, and T5. Here's a breakdown of its key components and how it differs from Recurrent Neural
Networks (RNNs):
Key Features of Transformer Architecture:
Self-Attention Mechanism:
The core innovation in Transformers is the self-attention mechanism, which allows the model to weigh the importance of each word
in a sequence relative to others, regardless of their position. This helps the model focus on different parts of the input simultaneously,
capturing dependencies between words at different distances.
For each word in the input sequence, the self-attention mechanism computes attention scores with all other words and generates a
weighted sum of the input words.
Positional Encoding:
Since Transformers do not process inputs sequentially (like RNNs do), they need a way to represent the position of each word in the
sequence. This is done using positional encodings, which are added to the word embeddings to give the model a sense of order.
Feed-Forward Neural Networks:
After the self-attention operation, the model applies a fully connected feed-forward neural network to each position separately. These
layers increase the model’s capacity for capturing complex patterns.
Parallelization:
Unlike RNNs, Transformers do not rely on processing sequences in order, so the entire sequence can be processed in parallel. This
makes Transformers more efficient and scalable on modern hardware (GPUs/TPUs).
Multi-Head Attention:
Instead of applying one attention mechanism, the Transformer uses multi-head attention, which allows it to capture different types
of relationships and information in the sequence by applying attention multiple times in parallel.
Encoder-Decoder Structure:
The original Transformer model was designed as an encoder-decoder architecture for tasks like machine translation. The encoder
processes the input sequence and generates a representation, while the decoder takes this representation and generates the output
sequence.

Dr. Ashaq Bhat 18


Differences from Recurrent Neural Networks (RNNs):
Sequential vs. Non-Sequential Processing:
RNNs (and LSTMs/GRUs) process data sequentially, meaning they read one word at a time and update a
hidden state based on the previous word. This makes them slow to train and hard to parallelize.
Transformers, on the other hand, process the entire sequence in parallel, making them much faster to
train and better suited for long sequences.
Long-Range Dependencies:
RNNs struggle with long-range dependencies due to issues like the vanishing gradient problem, making
it hard for them to capture relationships between distant words in a sequence.
The self-attention mechanism in Transformers allows them to efficiently capture relationships between
words that are far apart in the input sequence.
Memory and Efficiency:
RNNs maintain a hidden state that is updated sequentially, which can be memory-inefficient for long
sequences.
In contrast, Transformers use attention and parallel computation, which significantly reduces memory
consumption during training and inference, though self-attention scales quadratically with sequence
length in terms of computation.
Training Time:
RNNs (especially LSTMs/GRUs) require significant time to train due to their sequential nature.
Transformers are faster to train because they can leverage parallelization more effectively and do not
suffer from the sequential processing bottleneck.

Dr. Ashaq Bhat 19


Dr. Ashaq Bhat 20
The self-attention mechanism is a key component of the Transformer architecture that allows it to efficiently
model relationships between different parts of a sequence. It computes a weighted representation of each input
token by attending to all other tokens in the sequence. Here's a detailed breakdown of how it works and the
advantages it offers over traditional sequence models like RNNs:

How Self-Attention Works

For each token (or word) in the input sequence, the self-attention mechanism performs the following steps:

Input Representation:

Each input token is represented as an embedding vector (typically a learned representation), which captures the
token's semantic meaning. These embeddings are combined with positional encodings to retain positional
information.

Generating Query, Key, and Value Vectors:

For each token, the Transformer computes three vectors: Query (Q), Key (K), and Value (V). These vectors are
derived by multiplying the input embedding with learned weight matrices: Q=XWQ​,K=XWK​,V=XWV​Where:

X is the input embedding for the token.

WQ​, WK​, and WV​are learned weight matrices for queries, keys, and values, respectively.

The Query represents the token we are focusing on, the Key represents the tokens we are comparing it against, and
the Value contains the token's actual information.

Dr. Ashaq Bhat 21


Attention Scores:
For each token in the sequence, the attention score between the token's Query and all other tokens' Keys is
⋅ This measures the similarity between the current
computed by taking the dot product: score(Qi​,Kj​)=Qi​Kj​
token i and token j.
Scaled Dot-Product Attention:
To stabilize gradients and prevent large attention scores, the dot products are scaled by the square root of the
⋅ ​
dimensionality of the Key vectors dk​: attention_score=dk​​Qi​Kj​
Softmax Normalization:
The attention scores are passed through a softmax function to convert them into a probability distribution,
⋅ ​) This gives us the weight assigned
ensuring that the weights sum to 1: attention_weightij​=softmax(dk​​Qi​Kj​
to each token j when focusing on token i.
Weighted Sum of Values:
For each token i, the weighted sum of the Value vectors of all other tokens (based on the attention weights)
is computed: outputi​=j∑​attention_weightij​Vj​⋅ This weighted sum gives a new representation for token i,
reflecting its relationship to all other tokens in the sequence.
Multi-Head Attention:
To capture different types of relationships between tokens, the Transformer applies multiple self-attention
mechanisms in parallel, known as multi-head attention. Each head learns different weight matrices for WQ​
, WK​, and WV​, allowing the model to focus on various aspects of the sequence simultaneously. The outputs
of all heads are concatenated and linearly transformed.

Dr. Ashaq Bhat 22


Dr. Ashaq Bhat 23
Model distillation (also known as knowledge distillation) is a technique in deep learning where a smaller, more
efficient model (called the student model) is trained to replicate the behavior of a larger, more complex model
(called the teacher model). The idea is to transfer the knowledge from the teacher model to the student model,
enabling the student to achieve similar performance while being much faster and requiring fewer computational
resources.
Key Components of Model Distillation
Teacher Model:
This is typically a large, pre-trained, and highly accurate model (e.g., a deep neural network with many layers).
The teacher model is usually complex and computationally expensive, but it has high performance on the task.
Student Model:
The student model is a smaller and more efficient model. It could have fewer layers, fewer parameters, or use
simpler architectures. The goal is to make the student model lightweight while maintaining high performance.
Knowledge Transfer:
Instead of directly training the student model on the original dataset (which could be difficult for the smaller
model to learn), the student model is trained to mimic the behavior of the teacher model. This is done by using
the soft predictions (also called soft targets) from the teacher, in addition to or instead of the hard labels from
the dataset.
Soft Predictions:
The teacher model outputs soft predictions, which are probability distributions over the output classes (obtained
using a softmax function). These distributions often contain more nuanced information than just the hard labels
(which only indicate the correct class).
For example, in a classification task, the teacher might give a prediction like [0.8, 0.15, 0.05] (indicating high
confidence in the correct class but also some information about similar classes), while the hard label would
simply be [1, 0, 0] (indicating only the correct class).

Dr. Ashaq Bhat 24


Distillation Loss:

During training, the student model is optimized using a distillation loss function, which
typically combines two terms:

Cross-entropy loss with hard labels (from the original dataset).

Cross-entropy loss with soft labels (from the teacher model’s soft predictions).

A temperature parameter T is used to soften the teacher model’s predictions, making


them more useful for training the student. This temperature controls the "sharpness" of the
probability distribution produced by the teacher. A higher temperature produces softer
probabilities, providing more information about the relative probabilities between classes.

The total loss function can be written as:

(Hard Labels) (Soft Labels)Loss = α⋅Cross-Entropy (Hard Labels) +

(1−α)⋅Cross-Entropy (Soft Labels)

Where α is a hyperparameter that controls the weight given to the hard and soft labels.

Dr. Ashaq Bhat 25


Advantages of Model Distillation
Model Compression:
Model distillation enables compressing a large model into a smaller one, which is useful when deploying models
in resource-constrained environments such as mobile devices, IoT devices, or embedded systems.
Inference Speed:
Smaller models require fewer computational resources (such as memory and processing power), which results in
faster inference times. This is important for real-time applications, such as speech recognition or video processing.
Retaining Performance:
Despite being smaller, the student model can often achieve performance close to the teacher model because it
learns from the teacher’s soft predictions, which provide additional information beyond the dataset's hard labels.
Generalization:
By learning from the soft predictions, the student model can often generalize better than if it were trained directly
on the hard labels alone. The teacher's soft predictions contain richer information about the decision boundary,
making it easier for the student to learn subtle patterns in the data.
Versatility:
The student model does not need to have the same architecture as the teacher model. For example, a large
transformer-based teacher can distill knowledge into a smaller convolutional neural network (CNN) or even a
simpler model like a decision tree.

Example of Model Distillation


One of the classic applications of model distillation is in compressing large models like BERT (a massive
transformer model used for natural language processing tasks). A small model like DistilBERT is trained using
model distillation, where it learns from BERT’s soft predictions. Despite being much smaller and faster,
DistilBERT retains a significant portion of BERT’s accuracy on NLP tasks.

Dr. Ashaq Bhat 26


Distillation Process: Step-by-Step
Train the Teacher Model:
First, a large, accurate teacher model is trained on the original dataset. This model is expected
to achieve high performance on the task.
Generate Soft Targets:
The teacher model’s outputs are collected as soft predictions (probabilities over the classes)
on the training data. These soft predictions will be used to guide the student model’s training.
Train the Student Model:
The student model is trained using both the original dataset’s hard labels and the soft targets
from the teacher. A combination of hard label loss and soft label loss (with temperature
scaling) is used during training.
Optimize the Student Model:
The student model is optimized to minimize the distillation loss, adjusting its parameters to
match the teacher’s outputs as closely as possible.

Dr. Ashaq Bhat 27


BERT (Bidirectional Encoder Representations from Transformers) and DistilBERT are both popular
transformer-based models for natural language processing tasks. While BERT is known for its high accuracy
and deep architecture, DistilBERT is a smaller, faster, and more efficient variant of BERT created through a
technique called knowledge distillation. Below is a detailed comparison between BERT and DistilBERT in
terms of architecture, training approach, performance, and the trade-offs between model size, processing
speed, and accuracy.

1. Architecture Comparison
BERT:
Layers: BERT has two main versions: BERT-Base (12 layers) and BERT-Large (24 layers), where each layer
is a Transformer encoder.
Hidden Units: In BERT-Base, each layer has 768 hidden units, while BERT-Large has 1024 hidden units.
Attention Heads: BERT-Base has 12 attention heads, and BERT-Large has 16 attention heads.
Parameters:
BERT-Base has around 110 million parameters.
BERT-Large has around 340 million parameters.
DistilBERT:
Layers: DistilBERT reduces the number of layers to 6, exactly half the number of BERT-Base.
Hidden Units: DistilBERT retains the same number of hidden units as BERT-Base (768 units).
Attention Heads: DistilBERT also retains the 12 attention heads from BERT-Base.
Parameters:
DistilBERT has approximately 66 million parameters, about 60% of BERT-Base's size.

Dr. Ashaq Bhat 28


2. Training Approach
BERT:
Pretraining: BERT is trained on two unsupervised tasks:
Masked Language Modeling (MLM): Randomly masks words in the input and trains the model to predict
the masked tokens.
Next Sentence Prediction (NSP): Given two sentences, the model predicts whether the second sentence
logically follows the first.
Fine-Tuning: After pretraining, BERT can be fine-tuned on various downstream tasks such as text
classification, question answering, and named entity recognition.
DistilBERT:
Knowledge Distillation: DistilBERT is trained using knowledge distillation from BERT. The key aspects of
this process include:
Soft Labeling: DistilBERT learns from the soft labels (the probability distribution over classes) generated by a
pre-trained BERT teacher model, rather than from the ground truth labels alone.
Masking Mechanism: Like BERT, DistilBERT also uses masked language modeling but omits the Next
Sentence Prediction (NSP) task, which simplifies the training process.
Cosine Embedding Loss: DistilBERT is trained to match the behavior of the teacher BERT model by
minimizing the cosine distance between the output embeddings of the teacher and student models.

Dr. Ashaq Bhat 29


3. Performance Comparison
Accuracy:
BERT:
Due to its larger size and more layers, BERT generally achieves higher accuracy on most NLP benchmarks compared to
DistilBERT.
On tasks like GLUE, SQuAD, and other text classification/understanding tasks, BERT sets high standards in terms of
performance.
DistilBERT:
DistilBERT achieves around 97% of BERT's accuracy on most tasks, meaning that the reduction in model size and depth leads
to only a minor loss in accuracy.
On benchmarks like GLUE and SQuAD v1.1, DistilBERT performs slightly worse than BERT but remains competitive,
especially given its size and speed advantages.
Speed and Efficiency:
BERT:
BERT is large and computationally expensive, with inference times that can be slow due to its deep architecture. This is
especially noticeable in real-time or resource-constrained applications.
Inference speed on BERT can be significantly slower, especially for the BERT-Large model, which has a high number of
parameters.
DistilBERT:
DistilBERT is approximately 60% faster than BERT-Base during inference due to its reduced depth (6 layers vs. 12 layers).
It uses 40% less memory and has a lighter footprint, making it ideal for applications where latency or computational resources
are limited (e.g., mobile apps, embedded systems).

Dr. Ashaq Bhat 30


4. Trade-offs: Model Size, Processing Speed, and Accuracy
Model Size:
BERT is larger and more complex, leading to higher accuracy, but it comes at the cost of more memory usage and slower
inference. DistilBERT is more compact with fewer parameters, making it easier to deploy on devices with limited resources.
Processing Speed:
BERT’s deeper architecture leads to slower inference speeds. DistilBERT, being only half as deep (6 layers vs. 12), achieves
significantly faster inference speeds (up to 60% faster) while using fewer computational resources.
Accuracy:
BERT achieves slightly higher accuracy on most tasks, especially those that benefit from a deeper architecture like question
answering and sentence entailment. However, DistilBERT performs very close to BERT (~97% of BERT’s accuracy), making
it a practical option in scenarios where efficiency is more important than marginal gains in accuracy.
Practical Use Cases
BERT:
Best suited for scenarios where maximum accuracy is crucial, and computational resources are not a primary concern. It is
ideal for research, complex NLP tasks, or when running on powerful servers where speed is not the bottleneck.
DistilBERT:
Ideal for real-time applications or situations where computational efficiency is a priority, such as deploying models on mobile
devices, embedded systems, or edge computing environments. It’s also useful in high-throughput environments like online
services that need to process large volumes of text quickly.

Dr. Ashaq Bhat 31


Significance of checkpoints in the context of Hugging Face's platform

1. Significance of Checkpoints in Hugging Face’s Platform


In the context of the Hugging Face platform, checkpoints refer to saved versions of a model’s parameters during or after
training. Checkpoints are critical in several aspects of model development and deployment:
Key Roles of Checkpoints:
Model Saving and Reusability:
Checkpoints allow developers to save their model at various stages of training. These saved models can later be reloaded,
fine-tuned, or deployed without the need to retrain from scratch. Hugging Face provides access to thousands of pre-
trained checkpoints for a variety of models (e.g., BERT, GPT, T5) through the Model Hub.
Pre-trained Models:
The Hugging Face Model Hub is a repository where users can find a wide range of pre-trained checkpoints for different
tasks like text classification, machine translation, summarization, etc. These checkpoints can be downloaded and fine-tuned
on new datasets. This enables transfer learning, where a model is adapted to new tasks with minimal training.
Fine-tuning:
Checkpoints provide the ability to fine-tune a pre-trained model on a specific task. For instance, a BERT checkpoint pre-
trained on a massive corpus can be fine-tuned on a smaller task-specific dataset (e.g., sentiment analysis) to improve
performance in a particular domain.
Checkpoint Resumption:
During long training processes, checkpoints allow users to resume training if it is interrupted. Instead of starting from
scratch, the model can be reloaded from the last saved checkpoint, preserving the progress made.
Versioning and Experimentation:
Hugging Face enables version control of checkpoints, allowing users to keep track of different versions of a model. This is
important for experimentation, as it allows developers to compare different models, hyperparameter settings, and
architectures without losing progress.
Deployment:
Checkpoints are used for deployment in production environments. Once a model is fine-tuned and evaluated, a checkpoint
containing the final model parameters can be deployed to inference engines. Hugging Face’s transformers library makes it
easy to load checkpoints and integrate them into applications for tasks like text generation, question answering, and more.

Dr. Ashaq Bhat 32


Nuance in Language
Nuance in language refers to subtle differences in meaning, tone, emotion, or implication within text or
speech. A nuanced understanding of language involves grasping these subtle variations, which are often
context-dependent. Language is inherently ambiguous, and the same word or phrase can carry different
meanings based on context, tone, or cultural implications.
Examples of Nuance:
Word Sense Disambiguation:
The word “bank” can refer to:
A financial institution: “I deposited money in the bank.”
The side of a river: “We sat by the bank of the river.”
The nuance here depends on the context in which the word "bank" is used.
Tone or Sarcasm:
Consider the phrase: “Oh, great! Another meeting.”
In a literal sense, this could mean excitement about a meeting.
However, in many contexts, it could be sarcastic, implying that the speaker is unhappy about the meeting. The nuance here is
conveyed through tone and context.
Politeness vs. Directness:
“Could you pass me the salt?” vs. “Give me the salt.”
Both sentences aim to achieve the same outcome, but the former is more polite, while the latter is direct. Understanding this
subtle difference is crucial for capturing the speaker's intent.
Cultural Nuance:
Idiomatic expressions like “it’s raining cats and dogs” or “kick the bucket” carry meanings that are far from their literal
interpretation. These expressions depend on cultural knowledge and context for proper understanding.
How Transformer Models Capture Nuance:
Transformer models, like BERT, GPT, and T5, have proven adept at capturing nuances in text because of the following key
mechanisms:
Self-Attention Mechanism:
Transformers rely on self-attention, which allows the model to weigh the importance of different words in a sentence relative to
each other. This mechanism enables the model to capture context-dependent meanings of words.
For example, in the sentence “She went to the bank to deposit money,” the model attends to the words “deposit” and “money” to
correctly infer that “bank” refers to a financial institution, not a riverbank.

Dr. Ashaq Bhat 33


Bidirectional Context:
Models like BERT are trained to consider both the left and right context of a word (i.e., they are bidirectional). This helps in
understanding nuances that depend on both prior and subsequent text.
Example: In the sentence “The painter stood on the bank to paint the landscape,” BERT will use both “stood on” and
“landscape” to understand that "bank" refers to the edge of a river, not a financial institution.
Pretraining on Large Corpora:
Transformer models are pre-trained on massive, diverse datasets, allowing them to learn subtle patterns in language. This
pretraining helps the model understand nuanced linguistic phenomena such as idioms, tone, and polysemy (words with
multiple meanings).
For example, pre-training on large amounts of text helps models learn that "kick the bucket" refers to dying rather than
interpreting it literally.
Masked Language Modeling (MLM):
In models like BERT, the masked language modeling task helps in learning nuanced word usage. During training, words are
randomly masked, and the model must predict them based on surrounding context. This forces the model to learn subtle
dependencies and relationships between words.
Example: If “The judge gave him a light sentence” is masked to “The judge gave him a [MASK] sentence,” BERT can predict
that "light" refers to leniency, not weight, based on the surrounding context.
Handling of Long-Range Dependencies:
Transformers, unlike RNNs, can capture long-range dependencies effectively due to their self-attention mechanism. This
enables the model to understand nuanced information spread across longer texts or paragraphs.
Example: In a news article, understanding that “the president” refers to “Barack Obama” mentioned several sentences earlier
helps capture the right contextual meaning when the two are distant in text.
Fine-Tuning on Task-Specific Data:
After pre-training, transformers can be fine-tuned on task-specific data (e.g., sentiment analysis, sarcasm detection). During
fine-tuning, the model learns task-specific nuances like tone (positive/negative sentiment) or detecting irony in text.

Dr. Ashaq Bhat 34

You might also like