Basics of language models
Overview
Mohit Iyyer explains how language models assign probabilities to text by multiplying next-word conditional probabilities, then uses count-based n-gram models to show the tradeoffs between context, sparsity, and storage. He connects maximum likelihood estimation to log probability, negative log likelihood, surprisal, and perplexity, explaining why models are evaluated on held-out text and why perplexity depends on vocabulary and tokenization.
Key takeaways
- The chain rule lets a next-word model assign a probability to an entire text by multiplying each word’s conditional probability given its preceding context.
- N-gram models reduce count sparsity by limiting context, but their storage can grow as V^n, where V is vocabulary size and n is the n-gram length.
- Unseen n-grams can make a whole sentence probability zero; add-one smoothing avoids zero counts but can assign too much probability to implausible continuations.
- Maximum likelihood estimation selects model probabilities that maximize the training data’s likelihood, while held-out evaluation checks whether those probabilities generalize.
- Average negative log likelihood measures surprisal per token, and perplexity translates it into an intuitive effective number of next-word choices.
- Perplexity depends on vocabulary and tokenization, so scores from models using different prediction units should not be compared as if they shared the same scale.
Chapters
- A language model assigns a probability to a text, allowing a translation system to rank candidates such as “students opened their books” above an ungrammatical word order.
- The lecture introduces n-gram models, text probability, and perplexity as foundations for later neural language models.
- Course logistics include a homework assignment and a project-group form due September 14.
- The probability of a sequence such as W1, W2, W3 is the product of conditional probabilities for each word given its preceding words.
- A next-word predictor can therefore score an entire text by multiplying its successive next-word probabilities.
- Each prefix represents the observed text so far, and the model estimates a distribution over possible continuations.
- A count-based model could estimate P(Claude | “My favorite LLM is”) by dividing the count of the full prefix followed by Claude by the count of the prefix.
- Storing counts for every possible prefix is infeasible, especially for long prefixes that may appear only once or not at all.
- Exact counts do not transfer information between similar prefixes, and new terms introduced after training expose data staleness.
- An n-gram model approximates next-word probability by dropping earlier context, using only a fixed number of recent words.
- For “My favorite LLM is Claude,” a shorter-context estimate might use P(Claude | “is”) instead of the full prefix.
- The reduced prefix appears more often, improving count availability while discarding potentially useful meaning and grammatical constraints.
- Using “LLM is” rather than only “is” provides more context for predicting Claude, but still ignores earlier words such as “favorite.”
- Iyyer notes that context can keep helping across hundreds of words, with aggregate gains reported around 1,000 words.
- Extending n-gram context increases the number of possible prefixes and makes storage grow exponentially.
- A bigram model stores counts for word pairs, while a trigram model stores counts for three-word sequences.
- With vocabulary size V, storing all possible n-grams has a worst-case cost proportional to V^n.
- Pre-neural language-model efforts scaled n-grams, but storage constraints kept even large models to roughly 10-grams.
- Start-of-sequence and end-of-sequence markers delimit sentences and help define well-formed probability distributions.
- In a three-sentence example based on “I am Sam” and “I do not like green eggs and ham,” the vocabulary contains 12 types, including boundary markers.
- A type is a unique vocabulary item, while each occurrence in the data is a token; “am” is one type but appears as two tokens.
- Character-level models reduce vocabulary size, but require longer sequences and more context to make useful predictions.
- In the sample corpus, P(I | start-of-sequence) is 2/3 because two of three sentences begin with “I.”
- P(Ham | start-of-sequence) is zero when no training sentence begins with “Ham.”
- A sentence such as “Ham is Sam” consequently receives zero probability if any required n-gram was unseen.
- Smoothing reallocates some probability from observed n-grams to unseen ones so test sequences do not automatically receive zero probability.
- Add-one smoothing adds 1 to every n-gram count and adjusts the denominator to normalize the distribution.
- Uniformly adding one can overvalue implausible or ungrammatical continuations and flatten meaningful differences between observed counts.
- Counting n-grams and normalizing their counts is an instance of maximum likelihood estimation.
- The model chooses probabilities that maximize the likelihood of the observed training sentences.
- The same objective carries over to neural language models: maximize the product of next-word probabilities across the training data.
- Multiplying many probabilities below 1 can cause numerical underflow for long sequences.
- Taking logs converts the sequence probability product into a sum of log probabilities while preserving rankings because logarithms are monotonic.
- Dividing by the number of tokens gives average log probability, making comparisons across documents of different lengths more meaningful.
- Shannon’s information-theoretic view links probability to code length: common events need fewer bits to encode than rare events.
- Surprisal is the negative log base-2 probability; an event with probability 1/8 has surprisal of 3 bits.
- Negative log likelihood, or cross-entropy loss, is average surprisal per token and can be expressed in bits or, with natural logs, nats.
- Training minimizes NLL, which is equivalent to maximizing the probability assigned to the training data.
- Perplexity exponentiates average NLL: it is exp(NLL) with natural logs or 2^NLL with base-2 logs.
- A model choosing among K equally likely next words has perplexity K; lower perplexity means fewer effectively plausible choices at each position.
- Perplexity depends on vocabulary size and tokenization, so raw scores from word-level and character-level models are not directly comparable.
- Training and test perplexity should both be monitored; rising test perplexity can indicate overfitting, while the goal is to generalize to new text from the training distribution.
Transcript excerpt
0:07 Okay. Um, hi everyone. So, today we are going to cover some fundamental uh aspects of language models. And so we're going to talk about um engram models, probability of texts uh and uh perplexity. So I want to just emphasize throughout this lecture things that um we'll see over and over again in this class even as we transition from these simple models that we'll talk about today to more complicated uh neural network models that um we'll start seeing um after Labor Day. So next week um before I start some logistics uh things so your homework is posted um and the form to
1:00 fill out your project groups also posted. So please make use of the Piaza search for teammates thread if you need to find teammates um and submit that form uh by September 14th. The homework again is not intended to be uh very difficult. Um more just give you an idea of if you're not familiar with any of the concepts on that homework, you might want to do some self-study on those topics outside of class. Um okay, any questions about uh logistics uh stuff? Okay, great. So, uh, today we're going to be talking about, um, language models. Um,
1:50 okay. Language models. Um, and so there's um, you know, like this basic definition of a language model is um something that assigns a probability to a given text. So last class we saw briefly when we were talking about the um IBM like statistical machine translation system that there was a model of English that would basically tell you how plausible of an English sentence is this candidate translation.
2:40 um you can kind of generalize from that and think about any model that scores a piece of text and assigns it to a
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Mohit Iyyer.