Pretraining-Finetuning Paradigm�
Somak Aditya
Assistant Professor
Department of CSE, IIT Kharagpur
Slide Courtesy: Prof. Chris Manning, Prof. Li Fei Fei, Prof. Yunzhu Li, Prof. Ruohan Gao (Stanford Univ)
Pre-training
The idea of transfer learning
Examples of popular
pre-trained Language Models
What has made Transformers popular is that they can be combined with the idea of transfer learning.
Transformers have become the go-to model for building large pretrained language models which can be adapted for several tasks
The pretraining revolution
Pretraining has had a major, tangible impact on how well NLP systems work
Standard Supervised Deep Learning
Labeling a sizeable dataset needs a lot of effort / time.
Every new learning problem needs labeling of a new dataset
+
+
text + labels
neural network starting from random weights
(not) spam
Why is this an issue
Randomly initialized
Word Vectors
The training data for our downstream task (like question answering) must be sufficient to teach all contextual aspects of language, and train millions of parameters.
Authors train Large Deep Neural Nets (CNNs) on the Object detection “train2017 set” and evaluate bounding box AP on the “val2017 set”,
Abundance of domain-specific NLP Applications
Very difficult to get sufficient labeled data for each task
NLP: The Standard Pipeline
Prof. Somak Aditya, IIT KGP, SSAI 25
Language Modeling
(Pre-training)
Downstream Tasks
(Fine-Tuning)
Raw (unlabeled) Text
Samples and Labels
(Opt: Instructions)
Advantage: Achieves SOTA
Does it with much less fewer
labels
Slide Courtesy: Prof. Mitesh Khapra, IITM
Next word prediction
(unsupervised)
Standard supervised tasks
Sequence labeling tasks
Random Weights (W)
Transfer Learning with Language Modeling
Self-supervised training algorithm
We just train them to predict the next word!
"Self-supervised" because it just uses the next word as the label!
Prof. Somak Aditya, IIT KGP, SSAI 25
Intuition of language model training: loss
Prof. Somak Aditya, IIT KGP, SSAI 25
Other Pretraining Objectives
Prof. Somak Aditya, IIT KGP, SSAI 25
What does a model learn from pretraining?
Some claim: Apparently everything
Empirically: Syntax, Syntactic dependencies, Semantics, (and some) commonsense knowledge
Prof. Somak Aditya, IIT KGP, SSAI 25
To a Limited
extent
What the model does NOT learn from pretraining…
Prof. Somak Aditya, IIT KGP, SSAI 25
And many more types of reasoning ….
Big idea
Prof. Somak Aditya, IIT KGP, SSAI 25
Slide Courtesy: Stanford NLP Course
Pretraining Corpus: LLMs are mainly trained on the web
Prof. Somak Aditya, IIT KGP, SSAI 25
Slide Courtesy: Stanford NLP Course
The Pile: a pretraining corpus
Prof. Somak Aditya, IIT KGP, SSAI 25
academics web
books
dialog
Slide Courtesy: Stanford NLP Course
Filtering for quality and safety
Quality is subjective
Prof. Somak Aditya, IIT KGP, SSAI 25
Slide Courtesy: Stanford NLP Course
The Pretraining / Finetuning Paradigm
20
Pretraining can improve NLP applications by serving as parameter initialization.
(Transformer, LSTM, ++ )
Iroh
goes
to
make
tasty
tea
goes to
make tasty tea END
Step 1: Pretrain (on language modeling)
Lots of text; learn general things!
Step 2: Finetune (on your task)
Not many labels; adapt to the task!
(Transformer, LSTM, ++ )
☺/☹
… the movie was …
Pre-trained LSTMs: ELMo
Pretrained LSTMs: ELMo
Pre-trained 2-layer LSTMs, both forward and backward, over a large corpus
The LSTM hidden states that we get (for different words) after the pre-training represents world knowledge. Use these hidden state vectors for new tasks.
ELMo: Training a LM (2-layer LSTM)
A bidirectional LM
Predicting the next word
Predicting the previous word
The two LSTMs are trained independently
ElMo: Obtaining the final embeddings
Combining the two pre-trained LSTMs to get a pre-trained embedding for a word (which will be used for the downstream task). Used the representations of a word at different layers.
ELMo: Weighting of layers
Which layer to give more weightage to?
This depends on the particular downstream task
Using ELMo for a task (sequence tagging; NER)
Say, we are training a bi-RNN for sequence tagging / NER task.
Embeddings from the pre-trained ELMo are appended to the hidden states of the bi-RNN.
Pre-trained ELMo embeddings are frozen. The loss is used to update only the parameters of the smaller bi-RNN.
ELMo: Results
Possibly the first paper that showed large improvements over multiple tasks, using the same model.
Previous SOTA for different tasks were different models.
Demonstrated the power of knowledge transfer using pre-trained Language Models.
ElMo - How does the pretraining help with efficient fine-tuning?
X-axis: amount of training data Y-axis: performance
Using the pre-trained model is especially better when the amount of training data is small.
Pre-training
Part 2
Slides mostly borrowed from materials by Jay Alammar, Pawan Goyal, the Stanford NLP course materials and other online resources
Using Transformers for Pretraining
Pretraining for three types of architectures
The pre-training task
Choice of the Pre-training Task
Allowed in both encoder & decoder
A word attends only to previous words
Allowed in encoder only
Each word can attend to all words in the sequence
Pretraining Encoders
Final objective – get good contextual representations of a given sequence of words / tokens
Allowed in both encoder & decoder
Allowed in encoder only;
Predicting the next word is NOT a good pre-training task for encoders, since we cannot use the words coming afterwards
Solution: Use Masks
Input to the Transformer
Task – predict the masked words
Loss is computed over the masked words, and then backpropagated.
Which words should the model predict?
BERT: Bidirectional Encoder Representations from Transformers
This loss, used in BERT, is popularly called “Masked LM” loss.
BERT: another view
Compute loss at certain positions based on the actual word at those positions.
Standard Cross Entropy Loss, after Softmax to get a probability distribution on the whole vocabulary.
Next Sentence Prediction
Along with the “Masked LM” loss, BERT used another loss
Given 2 sentences the model predicts if they are a real pair of adjacent sentences from the training corpus or a pair of unrelated sentences.
BERT introduces two special tokens
[CLS] words of sentence1 [SEP] words of sentence2 [SEP]
And two more special embeddings
[1st segment] embedding added to all words of sentence1 [2nd segment] embedding added to all words of sentence2
BERT: Next Sentence Prediction
Two-class classification based on the final representation of the [CLS] token: continuation or not
Since we are using bi-directional attention, [CLS] will attend to all words in the sequence even though it is at the start.
BERT: Next Sentence Prediction
More Details about BERT
Vocabulary of around 31K words
Contextual Embeddings to represent words
We get from BERT:
Contextual representation of each word in the sequence
A single contextual representation of the whole sequence - the final representation of [CLS]
Static vs Contextual Embeddings
Static embeddings represent word types (dictionary entries)
Contextual embeddings represent word instances (one for each time the word occurs in any context/sentence)
Using BERT to create contextualized word embedding
Most commonly, we use the representation from the last encoder layer.
But there have been other experiments too.
Using BERT to create contextualized word embedding
We tried something similar with ElMO (pre-trained LSTMs)
Using BERT (already pre-trained) for different tasks
Sentence-level tasks
Using BERT (already pre-trained) for different tasks
Word-level tasks
Transfer Learning through Fine-tuning
Fine Tuning for Sequence Classification
Classifier head can be a fully connected layer (the classification layer) followed by softmax over the set of classes (to get probability of each class)
If 5 classes and 768-dimensional BERT, the number of additional parameters is of the order of 5 x 768 .. much smaller than the number of pre-trained parameters
Fine Tuning for Sequence Classification
These parameters are updated during fine-tuning, using task-specific training data
Pre-trained weights; Frozen or a small subset can be updated
Pairwise Sequence Classification
Pair-wise Sequence Classification
Sequence Labeling (e.g., NER, POS tagging)
We want to label each token.
Do not use the [CLS] embedding; rather, attach a predictor
head to the representation of individual tokens.
POS Tagging
These parameters need to be trained during fine-tuning
GPTs: Pretraining Decoders
Pretraining Decoders
It’s natural to pretrain decoders as language models
Causal self-attention : a token attends to only the past / previous tokens.
Pre-training task: next word / token prediction
Teacher forcing used during pre-training. Loss computed w.r.t. ground truth next-token and back-propagated.
Generative Pretrained Transformer (GPT)
Vocabulary larger than BERT
Comparison of GPT series
Multiple versions of GPT-2 were released. See next slide
Image Source: “Large Language Models: A Deep Dive”
Pretrained Decoders
GPT: Transformer-Decoder
GPT-2: Quick Summary
During inference (once the model has been pre-trained):
Start with the <start> token.
Decoders use self-attention over previous words. Linear layer + softmax gives a probability distribution over the vocabulary. Use any sampling strategy to select the next word (e.g., greedy or beam search).
Feed the chosen word as input to generate the next word …
Until some stopping criterion is satisfied.
GPT-2: Quick Summary
GPT-2: Quick Summary
The 2nd word can attend to the word generated previously
How to use Pretrained Decoder-only models?
We will feed the input tokens (context) as the first few tokens, say w1, w2, w3.
The subsequent output tokens will attend to the input tokens as well as previous output tokens (using causal / masked self-attention).
Input and output tokens are part of the same sequence in decoder-only models.
Decoder-only model can generate sequences using much lesser parameters than Encoder-Decoder models
Fine-tune GPT-2 for Machine Translation
Convert each training data-point to a sequence using a special token <to-fr>
Use the sequences to fine-tune GPT-2 (next token prediction task)
GPT-2: Summarization
In these examples, we are using a decoder-only architecture for sequence-to-sequence tasks
GPT: Beyond Language Modeling
Probabilistic framework: generate token probabilities
Classification problem can also be expressed in a probabilistic framework – generate one token (label) for a given input
How to format inputs?
The [EXTRACT] token attends to all input tokens
Its application is similar to the [CLS] token of BERT
GPT: How to format inputs?
GPT-2: Zero shot and in-context: Beginning
Claim: no fine-tuning needed for tasks
GPT-2
Zero-shot task performance of GPT-2 as a function of model size on many NLP tasks
Zero-shot performance improves as model size increases.
Cultural Moment: GPT-3
In-context learning: Terminology
Frozen LM: parameters are NOT updated at all
Prompt engineering
Prompting as the standard interface for GPT-3
Example shown for a sentiment classification task
Additional processing may be needed to map the token(s) generated by the LM to a class label