Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed

NLP with Deep Learning · Lecture 9 of 23 · 1:18:46

Lecture 9: Pretraining

Stanford CS224N NLP with Deep Learning | 2023 | Lecture 9 - Pretraining on YouTube

Study guide

What this lecture covers

This lecture asks how a model can build reusable representations of language before it ever sees a labeled task. It starts with a practical problem: fixed word vocabularies cannot handle new words, typos, or richly inflected languages, which motivates splitting text into subword pieces. It then builds the case for pretraining as a general recipe: mask or predict part of a naturally occurring text and force a network to reconstruct it, which teaches it syntax, co-reference, world knowledge, and sentiment along the way.

The lecture sits right after the Transformers lecture in CS224N and works through the three architecture families introduced there: encoders, encoder-decoders, and decoders. By the end you should be able to explain how BERT, T5, and GPT differ in what they pretrain on and what they are good for, and understand why decoder-only models scaled up into the in-context learning and chain-of-thought behaviors seen in GPT-3.

Key ideas

  • Subword tokenization: vocabularies are built from frequently co-occurring character sequences rather than whole words, so rare or novel words split into known pieces instead of becoming an unknown token.
  • Pretrain-then-finetune paradigm: a model first learns general parameters from large unlabeled text, then those parameters are adapted with a smaller labeled dataset for a specific task.
  • Pretraining is language modeling: nearly all pretraining objectives are versions of predicting missing or upcoming words in real text, which requires no manual labels.
  • Masked language modeling (BERT): an encoder is trained by masking roughly 15% of subword tokens and predicting the originals from bidirectional context.
  • Span corruption (T5): an encoder-decoder masks contiguous spans in the input and generates the missing spans as output, combining bidirectional input context with generation.
  • Decoder-only pretraining (GPT): a decoder is trained purely to predict the next token, which scales into large models used directly for generation.
  • In-context learning: sufficiently large decoder-only models can perform new tasks from examples placed in the prompt, without any parameter updates.
  • Parameter-efficient fine-tuning: techniques like prefix tuning and low-rank weight updates adapt a pretrained model by training a small number of new parameters instead of all of them.

Walkthrough

Subword tokenization and the limits of fixed vocabularies (2:06)

The lecture opens by revisiting how word2vec-style models assumed a finite vocabulary learned from training data, which breaks down for typos, novel derived words, and languages with heavy morphology (the example given is a Swahili verb with over 300 conjugations). Mapping every unfamiliar string to a single unknown token throws away information. The fix is a subword algorithm that starts from individual characters and iteratively merges the most frequently co-occurring adjacent pairs into new vocabulary entries, so common words stay whole while rare words split into meaningful pieces. This subword approach, originally built for machine translation, underlies tokenization in essentially all modern language models.

From static word embeddings to contextual pretraining (14:20)

The lecture contrasts word2vec, which gives a word like "record" one fixed vector regardless of meaning, with the goal of contextual representations that distinguish "record" the verb from "record" the noun based on surrounding words. Earlier in the course, only word embeddings were pretrained while the rest of the network was randomly initialized; the shift now is to pretrain the entire network, including the Transformer stack, so that fine-tuning for a downstream task needs far less labeled data and starts from far better parameters.

Reconstructing masked input as a general training signal (19:25)

Pretraining is framed as reconstructing an input sequence after part of it is hidden. Worked examples (predicting a missing city name, a missing verb, a missing pronoun's referent, a missing word tied to sentiment) show how this single objective implicitly teaches syntax, co-reference, semantics, and sentiment. The lecture explains why starting fine-tuning from pretrained parameters, rather than random ones, tends to land in a better region after gradient descent, and why the huge scale of unlabeled text available for pretraining makes it more valuable than adding more labeled fine-tuning data.

Pretraining encoders: masked language modeling and BERT (36:37)

Because encoders see bidirectional context, ordinary next-word prediction is trivial for them, so the lecture introduces masked language modeling: replace about 15% of subword tokens (via masking, random substitution, or leaving them unchanged) and predict the true tokens from the surrounding context. This is the objective behind BERT, which also experimented with a next-sentence-prediction task using segment embeddings; the lecture notes that later work (RoBERTa) showed next-sentence prediction was unnecessary and that training on more data helped more. The section also covers using BERT's output vectors for classification tasks, BERT's scale (110M and 340M parameters), and lightweight fine-tuning methods such as prefix tuning and low-rank adaptation that freeze most pretrained parameters.

Pretraining encoder-decoders: span corruption and T5 (58:55)

For encoder-decoder models, the lecture describes span corruption: mask out spans of the input, feed the corrupted sequence to the encoder, and have the decoder generate the missing spans in sequence. This was shown to outperform plain language-modeling-style pretraining for encoder-decoders and is used in T5. The lecture also highlights a striking property of models pretrained with salient-span masking: after fine-tuning on some trivia questions, they can answer new trivia questions correctly some of the time, implying they store and retrieve factual knowledge learned during pretraining, though their answers are fluent even when wrong.

Pretraining decoders: GPT, scale, and in-context learning (1:03:00)

The final and longest section traces decoder-only pretraining through GPT (117M parameters), GPT-2 (1.5B parameters, notable for coherent long-form generation), and GPT-3 (175B parameters, trained on 300 billion words). GPT-3 introduces in-context learning: given a prompt containing examples of a pattern (such as word translations or arithmetic), the model continues the pattern for a new input without any gradient updates. The lecture also introduces chain-of-thought prompting, where prompts include worked reasoning steps, which makes models more likely to reach correct answers on multi-step problems. It closes by noting these are emergent, still poorly understood behaviors, and that pretrained models also learn and can amplify biases present in their training text.

Before you watch

  • Come from the CS224N Transformers lecture (self-attention, encoders, decoders, and encoder-decoders), since this lecture assumes that architecture vocabulary.
  • Review how language modeling and cross-entropy loss work, since pretraining objectives are framed as variants of language modeling.
  • Recall word2vec's fixed, context-independent embeddings, since the lecture uses them as the baseline pretraining improves on.

Check your understanding

  1. Why can't an encoder be pretrained with a plain next-word-prediction objective the way a decoder can?
  2. What problem does subword tokenization solve that a fixed word-level vocabulary cannot?
  3. How does span corruption in T5 differ from masked language modeling in BERT?
  4. What does in-context learning let a large decoder-only model do that fine-tuning does not require?
  5. Why might starting fine-tuning from pretrained parameters lead to a better outcome than training the same architecture from random initialization?

Vocabulary

pretraining (noun)
Training a model on large amounts of unlabeled text before it is adapted for a specific task.
Pretraining teaches the model general language patterns before fine-tuning.
subword tokenization (noun)
Splitting text into pieces smaller than whole words, so rare words can be built from known parts.
Subword tokenization lets the model handle words it has never seen whole.
vocabulary (noun)
The full set of tokens a model recognizes and can process.
A fixed vocabulary struggles with new or misspelled words.
morphology (noun)
The structure of words and how they are built from smaller meaningful parts.
Heavy morphology in some languages creates many word forms from one root.
unknown token (noun)
A placeholder symbol used for words a model does not recognize.
Without subwords, rare words are simply replaced with an unknown token.
contextual representation (noun)
A word representation that changes depending on the surrounding words.
A contextual representation gives 'record' different vectors as a noun and a verb.
downstream task (noun)
A specific application that a pretrained model is later adapted to perform.
Sentiment analysis is a common downstream task for pretrained models.
masked language modeling (noun)
A training method where some words are hidden and the model must predict them from context.
Masked language modeling is the training objective behind BERT.
co-reference (noun)
The relationship between different words or phrases that refer to the same thing.
Predicting a missing pronoun requires understanding co-reference.
segment embedding (noun)
A vector added to input tokens to mark which sentence or segment they belong to.
BERT used segment embeddings to distinguish two input sentences.
prefix tuning (noun)
A lightweight fine-tuning method that trains a small set of new input vectors instead of the whole model.
Prefix tuning adapts a pretrained model with very few new parameters.
low-rank adaptation (noun)
A fine-tuning technique that updates a model using small additional matrices instead of all its weights.
Low-rank adaptation reduces the cost of fine-tuning a large model.
span corruption (noun)
A pretraining method that hides continuous chunks of text for the model to reconstruct.
Span corruption is the pretraining objective used in T5.
salient-span masking (noun)
A masking method that hides important, information-rich spans like names or dates.
Salient-span masking helps a model learn to store factual knowledge.
in-context learning (noun)
A model's ability to perform a new task just from examples in the prompt, without training.
GPT-3 introduced strong in-context learning abilities.
chain-of-thought prompting (noun)
Giving a model worked-out reasoning steps in the prompt to improve multi-step answers.
Chain-of-thought prompting helps models solve math word problems.
emergent behavior (noun)
A new ability that appears only once a model reaches a certain scale.
In-context learning is considered an emergent behavior of large models.
bias (noun)
An unfair or skewed pattern learned from the training data.
Pretrained models can learn and amplify bias present in their training text.
fine-tune (verb)
To further train a pretrained model on a smaller, specific dataset.
The pretrained model is fine-tuned on a labeled task afterward.
randomly initialized (adjective)
Starting with random values before any training has happened.
Earlier models had a randomly initialized network besides the embeddings.
bidirectional (adjective)
Looking at context from both directions, before and after a word.
BERT uses bidirectional context to predict masked words.
referent (noun)
The thing or person a word or pronoun actually refers to.
Predicting a pronoun's referent requires understanding the sentence.
trivia question (noun)
A question about a specific fact, often used to test knowledge.
The model answers new trivia questions after fine-tuning on some.
fluent (adjective)
Smooth and natural-sounding, even if not necessarily correct.
The model's answers are fluent even when they are wrong.
coherent (adjective)
Logical and easy to follow, fitting together well.
GPT-2 was notable for generating coherent long-form text.
poorly understood (adjective)
Not yet clearly explained by current research.
Emergent behaviors are still poorly understood by researchers.
amplify (verb)
To make something stronger or more noticeable.
A model can amplify small biases found in its training data.
novel (adjective)
New and not seen before.
Subword tokenization handles novel words that were never in training.
iteratively (adverb)
By repeating a process step by step, improving each time.
The subword algorithm iteratively merges the most common pairs.

From the YouTube description

For more information about Stanford's Artificial Intelligence professional and graduate programs, visit: https://stanford.io/ai

This lecture covers:
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

To learn more about this course visit: https://online.stanford.edu/courses/c...
To follow along with the course schedule and syllabus visit: http://web.stanford.edu/class/cs224n/

John Hewitt
https://nlp.stanford.edu/~johnhew/

Professor Christopher Manning
Thomas M. Siebel Professor in Machine Learning, Professor of Linguistics and of Computer Science
Director, Stanford Artificial Intelligence Laboratory (SAIL)

#naturallanguageprocessing #deeplearning

← Lecture 8: Self-Attention and the Transformer · Lecture 11: Natural Language Generation →