0% found this document useful (0 votes)
124 views54 pages

cs224n 2023 Lecture9 Pretraining

The document discusses pretraining of deep learning models for natural language processing. It covers subword modeling, motivating pretraining from word embeddings, three ways to pretrain models including encoders, encoder-decoders and decoders, and what pretraining may teach models.

Uploaded by

Cassin Thangam
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)
124 views54 pages

cs224n 2023 Lecture9 Pretraining

The document discusses pretraining of deep learning models for natural language processing. It covers subword modeling, motivating pretraining from word embeddings, three ways to pretrain models including encoders, encoder-decoders and decoders, and what pretraining may teach models.

Uploaded by

Cassin Thangam
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/ 54

Natural Language Processing

with Deep Learning


CS224N/Ling284

John Hewitt
Lecture 9: Pretraining
Adapted from slides by Anna Goldie, John Hewitt
Lecture Plan
1. A brief note on subword modeling
2. Motivating model pretraining from word embeddings
3. Model pretraining three ways
1. Decoders
2. Encoders
3. Encoder-Decoders
4. Interlude: what do we think pretraining is teaching?
5. Very large models and in-context learning

Reminders:
Assignment 5 is out on Thursday! It covers lecture 8 and lecture 9(Today)!
It has ~pedagogically relevant math~
2
Word structure and subword models
Let’s take a look at the assumptions we’ve made about a language’s vocabulary.

We assume a fixed vocab of tens of thousands of words, built from the training set.
All novel words seen at test time are mapped to a single UNK.

word vocab mapping embedding


Common hat → pizza (index)
words
learn → tasty (index)
Variations taaaaasty → UNK (index)
misspellings laern → UNK (index)
novel items Transformerify → UNK (index)

3
Word structure and subword models
Finite vocabulary assumptions make even less sense in many languages.
• Many languages exhibit complex morphology, or word structure.
• The effect is more word types, each occurring fewer times.

Example: Swahili verbs can have


hundreds of conjugations, each
encoding a wide variety of
information. (Tense, mood,
definiteness, negation, information
about the object, ++)

Here’s a small fraction of the


conjugations for ambia – to tell.

4 [Wiktionary]
The byte-pair encoding algorithm
Subword modeling in NLP encompasses a wide range of methods for reasoning about
structure below the word level. (Parts of words, characters, bytes.)
• The dominant modern paradigm is to learn a vocabulary of parts of words (subword tokens).
• At training and testing time, each word is split into a sequence of known subwords.

Byte-pair encoding is a simple, effective strategy for defining a subword vocabulary.


1. Start with a vocabulary containing only characters and an “end-of-word” symbol.
2. Using a corpus of text, find the most common adjacent characters “a,b”; add “ab” as a subword.
3. Replace instances of the character pair with the new subword; repeat until desired vocab size.

Originally used in NLP for machine translation; now a similar method (WordPiece) is used in pretrained
models.

5 [Sennrich et al., 2016, Wu et al., 2016]


Word structure and subword models
Common words end up being a part of the subword vocabulary, while rarer words are split
into (sometimes intuitive, sometimes not) components.

In the worst case, words are split into as many subwords as they have characters.

word vocab mapping embedding


Common hat → hat
words learn → learn
Variations taaaaasty → taa## aaa## sty
misspellings laern → la## ern##
novel items Transformerify → Transformer## ify

6
Outline
1. A brief note on subword modeling
2. Motivating model pretraining from word embeddings
3. Model pretraining three ways
1. Encoders
2. Encoder-Decoders
3. Decoders
4. What do we think pretraining is teaching?

7
Motivating word meaning and context
Recall the adage we mentioned at the beginning of the course:

“You shall know a word by the company it keeps” (J. R. Firth 1957: 11)

This quote is a summary of distributional semantics, and motivated word2vec. But:

“… the complete meaning of a word is always contextual,


and no study of meaning apart from a complete context
can be taken seriously.” (J. R. Firth 1935)

Consider I record the record: the two instances of record mean different things.

8 [Thanks to Yoav Goldberg on Twitter for pointing out the 1935 Firth quote.]
Where we were: pretrained word embeddings
Circa 2017:
• Start with pretrained word embeddings (no ෝ
𝒚
context!)
• Learn how to incorporate context in an LSTM Not pretrained
or Transformer while training on the task.

Some issues to think about: pretrained


(word embeddings)
• The training data we have for our
… the movie was …
downstream task (like question answering)
must be sufficient to teach all contextual
[Recall, movie gets the same word embedding,
aspects of language. no matter what sentence it shows up in]
• Most of the parameters in our network are
randomly initialized!

9
Where we’re going: pretraining whole models
In modern NLP:
• All (or almost all) parameters in NLP ෝ
𝒚
networks are initialized via pretraining.
• Pretraining methods hide parts of the input
from the model, and train the model to Pretrained jointly
reconstruct those parts.

• This has been exceptionally effective at


building strong: … the movie was …

• representations of language
• parameter initializations for strong NLP [This model has learned how to represent
models. entire sentences through pretraining]
• Probability distributions over language that
we can sample from
10
What can we learn from reconstructing the input?

Stanford University is located in __________, California.

11
What can we learn from reconstructing the input?

I put ___ fork down on the table.

12
What can we learn from reconstructing the input?

The woman walked across the street,


checking for traffic over ___ shoulder.

13
What can we learn from reconstructing the input?

I went to the ocean to see the fish, turtles, seals, and _____.

14
What can we learn from reconstructing the input?

Overall, the value I got from the two hours watching


it was the sum total of the popcorn and the drink.
The movie was ___.

15
What can we learn from reconstructing the input?

Iroh went into the kitchen to make some tea.


Standing next to Iroh, Zuko pondered his destiny.
Zuko left the ______.

16
What can we learn from reconstructing the input?

I was thinking about the sequence that goes


1, 1, 2, 3, 5, 8, 13, 21, ____

17
Pretraining through language modeling [Dai and Le, 2015]
Recall the language modeling task:
• Model 𝑝𝜃 𝑤𝑡 𝑤1:𝑡−1 ), the probability
distribution over words given their past goes to make tasty tea END
contexts.
• There’s lots of data for this! (In English.) Decoder
(Transformer, LSTM, ++ )
Pretraining through language modeling:
• Train a neural network to perform language
modeling on a large amount of text. Iroh goes to make tasty tea
• Save the network parameters.

18
The Pretraining / Finetuning Paradigm
Pretraining can improve NLP applications by serving as parameter initialization.

Step 1: Pretrain (on language modeling) Step 2: Finetune (on your task)
Lots of text; learn general things! Not many labels; adapt to the task!
goes to make tasty tea END ☺/

(Transformer, LSTM, ++ ) (Transformer, LSTM, ++ )

Iroh goes to make tasty tea … the movie was …

19
Stochastic gradient descent and pretrain/finetune
Why should pretraining and finetuning help, from a “training neural nets” perspective?

• Consider, provides parameters 𝜃෠ by approximating min ℒpretrain 𝜃 .


𝜃
• (The pretraining loss.)

• Then, finetuning approximates min ℒfinetune 𝜃 , starting at 𝜃.
𝜃
• (The finetuning loss)
• The pretraining may matter because stochastic gradient descent sticks (relatively)
close to 𝜃෠ during finetuning.
• So, maybe the finetuning local minima near 𝜃෠ tend to generalize well!
• And/or, maybe the gradients of finetuning loss near 𝜃෠ propagate nicely!

20
Lecture Plan
1. A brief note on subword modeling
2. Motivating model pretraining from word embeddings
3. Model pretraining three ways
1. Encoders
2. Encoder-Decoders
3. Decoders
4. What do we think pretraining is teaching?

21
Pretraining for three types of architectures
The neural architecture influences the type of pretraining, and natural use cases.

• Gets bidirectional context – can condition on future!


Encoders
• How do we train them to build strong representations?

Encoder- • Good parts of decoders and encoders?


Decoders • What’s the best way to pretrain them?

• Language models! What we’ve seen so far.


Decoders
• Nice to generate from; can’t condition on future words

22
Pretraining for three types of architectures
The neural architecture influences the type of pretraining, and natural use cases.

• Gets bidirectional context – can condition on future!


Encoders
• How do we train them to build strong representations?

Encoder- • Good parts of decoders and encoders?


Decoders • What’s the best way to pretrain them?

• Language models! What we’ve seen so far.


Decoders
• Nice to generate from; can’t condition on future words

23
Pretraining encoders: what pretraining objective to use?
So far, we’ve looked at language model pretraining. But encoders get bidirectional
context, so we can’t do language modeling!

Idea: replace some fraction of words in the


went store
input with a special [MASK] token; predict
these words. 𝐴, 𝑏
ℎ1 , … , ℎ 𝑇
ℎ1 , … , ℎ 𝑇 = Encoder 𝑤1 , … , 𝑤𝑇
𝑦𝑖 ∼ 𝐴𝑤𝑖 + 𝑏

Only add loss terms from words that are


“masked out.” If 𝑥෤ is the masked version of 𝑥, I [M] to the [M]
we’re learning 𝑝𝜃 (𝑥|𝑥).
෤ Called Masked LM.
[Devlin et al., 2018]

24
BERT: Bidirectional Encoder Representations from Transformers
Devlin et al., 2018 proposed the “Masked LM” objective and released the weights of a
pretrained Transformer, a model they labeled BERT.

Some more details about Masked LM for BERT:


• Predict a random 15% of (sub)word tokens. [Predict these!] went to store
• Replace input word with [MASK] 80% of the time
• Replace input word with a random token 10% of Transformer
the time Encoder
• Leave input word unchanged 10% of the time (but
still predict it!)
• Why? Doesn’t let the model get complacent and not
I pizza to the [M]
build strong representations of non-masked words.
(No masks are seen at fine-tuning time!)
[Replaced] [Not replaced] [Masked]

25 [Devlin et al., 2018]


BERT: Bidirectional Encoder Representations from Transformers

• The pretraining input to BERT was two separate contiguous chunks of text:

• BERT was trained to predict whether one chunk follows the other or is randomly
sampled.
• Later work has argued this “next sentence prediction” is not necessary.

26 [Devlin et al., 2018, Liu et al., 2019]


BERT: Bidirectional Encoder Representations from Transformers
Details about BERT
• Two models were released:
• BERT-base: 12 layers, 768-dim hidden states, 12 attention heads, 110 million params.
• BERT-large: 24 layers, 1024-dim hidden states, 16 attention heads, 340 million params.
• Trained on:
• BooksCorpus (800 million words)
• English Wikipedia (2,500 million words)
• Pretraining is expensive and impractical on a single GPU.
• BERT was pretrained with 64 TPU chips for a total of 4 days.
• (TPUs are special tensor operation acceleration hardware)
• Finetuning is practical and common on a single GPU
• “Pretrain once, finetune many times.”

27 [Devlin et al., 2018]


BERT: Bidirectional Encoder Representations from Transformers
BERT was massively popular and hugely versatile; finetuning BERT led to new state-of-
the-art results on a broad range of tasks.
• QQP: Quora Question Pairs (detect paraphrase • CoLA: corpus of linguistic acceptability (detect
questions) whether sentences are grammatical.)
• QNLI: natural language inference over question • STS-B: semantic textual similarity
answering data • MRPC: microsoft paraphrase corpus
• SST-2: sentiment analysis • RTE: a small natural language inference corpus

28 [Devlin et al., 2018]


Limitations of pretrained encoders
Those results looked great! Why not used pretrained encoders for everything?

If your task involves generating sequences, consider using a pretrained decoder; BERT and other
pretrained encoders don’t naturally lead to nice autoregressive (1-word-at-a-time) generation
methods.

make/brew/craft goes to make tasty tea END

Pretrained Encoder Pretrained Decoder

Iroh goes to [MASK] tasty tea Iroh goes to make tasty tea

29
Extensions of BERT
You’ll see a lot of BERT variants like RoBERTa, SpanBERT, +++
Some generally accepted improvements to the BERT pretraining formula:
• RoBERTa: mainly just train BERT for longer and remove next sentence prediction!
• SpanBERT: masking contiguous spans of words makes a harder, more useful pretraining task

It’s bly irr## esi## sti## bly

BERT SpanBERT

It’ [MASK] [MASK] [MASK] [MASK] good


[MASK] irr## esi## sti## [MASK] good

30 [Liu et al., 2019; Joshi et al., 2020]


Extensions of BERT
A takeaway from the RoBERTa paper: more compute, more data can improve pretraining
even when not changing the underlying Transformer encoder.

31 [Liu et al., 2019; Joshi et al., 2020]


Full Finetuning vs. Parameter-Efficient Finetuning
Finetuning every parameter in a pretrained model works well, but is memory-intensive.
But lightweight finetuning methods adapt pretrained models in a constrained way.
Leads to less overfitting and/or more efficient finetuning and inference.

Full Finetuning Lightweight Finetuning


Adapt all parameters Train a few existing or new parameters

☺/ ☺/

(Transformer, LSTM, ++ ) (Transformer, LSTM, ++ )

… the movie was … … the movie was …


32 [Liu et al., 2019; Joshi et al., 2020]
Parameter-Efficient Finetuning: Prefix-Tuning, Prompt tuning
Prefix-Tuning adds a prefix of parameters, and freezes all pretrained parameters.
The prefix is processed by the model just like real words would be.
Advantage: each element of a batch at inference could run a different tuned model.

☺/

(Transformer, LSTM, ++ )

… the movie was …


Learnable prefix
parameters
33 [Li and Liang, 2021; Lester et al., 2021]
Parameter-Efficient Finetuning: Low-Rank Adaptation
Low-Rank Adaptation Learns a low-rank “diff” between the pretrained and finetuned
weight matrices.
Easier to learn than prefix-tuning.

𝐵 ∈ ℝ𝑘×𝑑
☺/
𝑊 ∈ ℝ𝑑×𝑑
(Transformer, LSTM, ++ )
𝐴 ∈ ℝ𝑑×𝑘

𝑊 + 𝐴𝐵
… the movie was …

34 [Hu et al., 2021]


Pretraining for three types of architectures
The neural architecture influences the type of pretraining, and natural use cases.

• Gets bidirectional context – can condition on future!


Encoders
• How do we train them to build strong representations?

Encoder- • Good parts of decoders and encoders?


Decoders • What’s the best way to pretrain them?

• Language models! What we’ve seen so far.


Decoders
• Nice to generate from; can’t condition on future words

35
Pretraining encoder-decoders: what pretraining objective to use?
For encoder-decoders, we could do something like language modeling, but where a
prefix of every input is provided to the encoder and is not predicted.
𝑤𝑇+2 , … ,
ℎ1 , … , ℎ 𝑇 = Encoder 𝑤1 , … , 𝑤𝑇
ℎ 𝑇+1 , … , ℎ2 = 𝐷𝑒𝑐𝑜𝑑𝑒𝑟 𝑤1 , … , 𝑤𝑇 , ℎ1 , … , ℎ 𝑇
𝑦𝑖 ∼ 𝐴ℎ𝑖 + 𝑏, 𝑖 > 𝑇

The encoder portion benefits from


bidirectional context; the decoder portion is 𝑤𝑇+1 , … , 𝑤2𝑇
used to train the whole model through
language modeling.
𝑤1 , … , 𝑤𝑇
[Raffel et al., 2018]

36
Pretraining encoder-decoders: what pretraining objective to use?
What Raffel et al., 2018 found to work best was span corruption. Their model: T5.

Replace different-length spans from the input


with unique placeholders; decode out the
spans that were removed!

This is implemented in text


preprocessing: it’s still an objective
that looks like language modeling at
the decoder side.

37
Pretraining encoder-decoders: what pretraining objective to use?
Raffel et al., 2018 found encoder-decoders to work better than decoders for their tasks,
and span corruption (denoising) to work better than language modeling.
Pretraining encoder-decoders: what pretraining objective to use?

A fascinating property
of T5: it can be
finetuned to answer a
wide range of
questions, retrieving
knowledge from its
parameters.

NQ: Natural Questions


WQ: WebQuestions 220 million params
770 million params
TQA: Trivia QA 3 billion params
11 billion params
All “open-domain”
versions
[Raffel et al., 2018]
Pretraining for three types of architectures
The neural architecture influences the type of pretraining, and natural use cases.

• Gets bidirectional context – can condition on future!


Encoders
• How do we train them to build strong representations?

Encoder- • Good parts of decoders and encoders?


Decoders • What’s the best way to pretrain them?

• Language models! What we’ve seen so far.


Decoders
• Nice to generate from; can’t condition on future words
• All the biggest pretrained models are Decoders.
40
Pretraining decoders
When using language model pretrained decoders, we can ignore
that they were trained to model 𝑝 𝑤𝑡 𝑤1:𝑡−1 ). ☺/

We can finetune them by training a classifier Linear 𝐴, 𝑏


on the last word’s hidden state.
ℎ1 , … , ℎ 𝑇
ℎ1 , … , ℎ 𝑇 = Decoder 𝑤1 , … , 𝑤𝑇
𝑦 ∼ 𝐴ℎ 𝑇 + 𝑏
Where 𝐴 and 𝑏 are randomly initialized and
specified by the downstream task. 𝑤1 , … , 𝑤𝑇

Gradients backpropagate through the whole [Note how the linear layer hasn’t been
network. pretrained and must be learned from scratch.]

41
Pretraining decoders
It’s natural to pretrain decoders as language models and then
use them as generators, finetuning their 𝑝𝜃 𝑤𝑡 𝑤1:𝑡−1 )!

This is helpful in tasks where the output is a 𝑤2 𝑤3 𝑤4 𝑤5 𝑤6


sequence with a vocabulary like that at 𝐴, 𝑏
pretraining time!
• Dialogue (context=dialogue history) ℎ1 , … , ℎ 𝑇
• Summarization (context=document)

ℎ1 , … , ℎ 𝑇 = Decoder 𝑤1 , … , 𝑤𝑇
𝑤𝑡 ∼ 𝐴ℎ𝑡−1 + 𝑏 𝑤1 𝑤2 𝑤3 𝑤4 𝑤5

[Note how the linear layer has been pretrained.]


Where 𝐴, 𝑏 were pretrained in the language
model!
42
Generative Pretrained Transformer (GPT) [Radford et al., 2018]
2018’s GPT was a big success in pretraining a decoder!
• Transformer decoder with 12 layers, 117M parameters.
• 768-dimensional hidden states, 3072-dimensional feed-forward hidden layers.
• Byte-pair encoding with 40,000 merges
• Trained on BooksCorpus: over 7000 unique books.
• Contains long spans of contiguous text, for learning long-distance dependencies.
• The acronym “GPT” never showed up in the original paper; it could stand for
“Generative PreTraining” or “Generative Pretrained Transformer”

43 [Devlin et al., 2018]


Generative Pretrained Transformer (GPT) [Radford et al., 2018]
How do we format inputs to our decoder for finetuning tasks?

Natural Language Inference: Label pairs of sentences as entailing/contradictory/neutral


Premise: The man is in the doorway
entailment
Hypothesis: The person is near the door

Radford et al., 2018 evaluate on natural language inference.


Here’s roughly how the input was formatted, as a sequence of tokens for the decoder.

[START] The man is in the doorway [DELIM] The person is near the door [EXTRACT]

The linear classifier is applied to the representation of the [EXTRACT] token.

44
Generative Pretrained Transformer (GPT) [Radford et al., 2018]
GPT results on various natural language inference datasets.

45
Increasingly convincing generations (GPT2) [Radford et al., 2018]
We mentioned how pretrained decoders can be used in their capacities as language models.
GPT-2, a larger version (1.5B) of GPT trained on more data, was shown to produce relatively
convincing samples of natural language.
GPT-3, In-context learning, and very large models
So far, we’ve interacted with pretrained models in two ways:
• Sample from the distributions they define (maybe providing a prompt)
• Fine-tune them on a task we care about, and take their predictions.

Very large language models seem to perform some kind of learning without gradient
steps simply from examples you provide within their contexts.

GPT-3 is the canonical example of this. The largest T5 model had 11 billion parameters.
GPT-3 has 175 billion parameters.

47
GPT-3, In-context learning, and very large models
Very large language models seem to perform some kind of learning without gradient
steps simply from examples you provide within their contexts.

The in-context examples seem to specify the task to be performed, and the conditional
distribution mocks performing the task to a certain extent.
Input (prefix within a single Transformer decoder context):
“ thanks -> merci
hello -> bonjour
mint -> menthe
otter -> ”
Output (conditional generations):
loutre…”
48
GPT-3, In-context learning, and very large models
Very large language models seem to perform some kind of learning without gradient
steps simply from examples you provide within their contexts.

49
Scaling Efficiency: how do we best use our compute
GPT-3 was 175B parameters and trained on 300B tokens of text.
Roughly, the cost of training a large transformer scales as parameters*tokens
Did OpenAI strike the right parameter-token data to get the best model? No.

This 70B parameter model is better than the much larger other models!
50
The prefix as task specification and scratch pad: chain-of-thought

51
[Wei et al., 2023]
Outline
1. A brief note on subword modeling
2. Motivating model pretraining from word embeddings
3. Model pretraining three ways
1. Encoders
2. Encoder-Decoders
3. Decoders
4. What do we think pretraining is teaching?

52
What kinds of things does pretraining teach?
There’s increasing evidence that pretrained models learn a wide variety of things about
the statistical properties of language. Taking our examples from the start of class:
• Stanford University is located in __________, California. [Trivia]
• I put ___ fork down on the table. [syntax]
• The woman walked across the street, checking for traffic over ___ shoulder. [coreference]
• I went to the ocean to see the fish, turtles, seals, and _____. [lexical semantics/topic]
• Overall, the value I got from the two hours watching it was the sum total of the popcorn
and the drink. The movie was ___. [sentiment]
• Iroh went into the kitchen to make some tea. Standing next to Iroh, Zuko pondered his
destiny. Zuko left the ______. [some reasoning – this is harder]
• I was thinking about the sequence that goes 1, 1, 2, 3, 5, 8, 13, 21, ____ [some basic
arithmetic; they don’t learn the Fibonnaci sequence]
• Models also learn – and can exacerbate racism, sexism, all manner of bad biases.
• More on all this in the interpretability lecture!
53
Parting remarks
These models are still not well-understood.
“Small” models like BERT have become general tools in a wide range of settings.
More on this in later lectures!
Assignment 5 out Thursday! Tuesday’s and today’s lectures in its subject matter.

54

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy