19

Predicting the next word

How can something that only predicts words write an essay?

11 minExecutive path

Predicting the next word

With voice. You can pause and jump to any step.

Lesson complete

Subtitles

Type “the sky is” and stop. What word comes next? You probably thought of “blue.” Maybe “gray.”

Voice

No voice: the lesson advances on its own with subtitles.

The voice for this lesson is being recorded: for now it advances on its own with subtitles.

0:00 / 0:00

What you will achieve

  • Explain how a language model writes: it gives a probability to every word that can come next, picks one, adds it to the context, and repeats.
  • Compute probabilities from counts and with softmax, and predict what temperature, top-k, and top-p do to them.
  • Answer honestly “how can something that only predicts write an essay?”, including what that answer does not guarantee.

Key ideas

  1. A language model gives, for a context, a probability to every word (or piece) that can come next.
  2. The simplest model counts: probability = count ÷ total. Neural networks give scores, and softmax turns them into probabilities that add up to one.
  3. It writes in a loop: pick a word, add it to the context, and compute again. That is autoregression.
  4. Always taking the most likely word (greedy) falls into repetition; sampling gives a different text on every run. Temperature sharpens (low) or flattens (high) the probabilities; top-k and top-p cut off rare words.
  5. Predicting well demands capturing a lot, but the goal is the likely, not the true. From GPT-1 to GPT-3 the objective is the same; the scale changes.

What many people believe

Common belief Since it only predicts the next word, a language model is an autocomplete that copies phrases it has already seen.

What is true It computes a fresh distribution for every context and can write sentences it never read (even our eight-sentence model does). Getting it right at scale demands capturing grammar, facts, and patterns of reasoning. It does optimize for the likely, though, not the true.

Check yourself

Four questions. No grade, no account: they just show whether the idea stuck.

ConceptWhat does a language model compute at each step of writing?
ConceptIf you turn the temperature way up before the softmax, what happens to the distribution?
CalculationA network gives two scores: 0.7 and −0.7. As in the lesson, the exponential turns them into 2 and 0.5. What probability does softmax give the first word?
ApplicationA team uses a model to extract figures from contracts and gets different results on each run. What should be checked first?

Under the hoodFor those who want the math and the code. You do not need it to follow the course.

The objective: predict the next piece

By the chain rule, the probability of a text x1,…,xnx_1,\dots,x_n factors into next-piece predictions:

P(x1,…,xn)=∏t=1nP(xt∣x1,…,xt−1)P(x_1,\dots,x_n)=\prod_{t=1}^{n}P(x_t\mid x_1,\dots,x_{t-1})

A language model is any model of P(xt∣x<t)P(x_t\mid x_{<t}). GPT-1 is trained by maximizing ∑tln⁡Pθ(xt∣xt−k,…,xt−1)\sum_t \ln P_\theta(x_t\mid x_{t-k},\dots,x_{t-1}) over a corpus, the “standard language modeling objective” (equation 1 of the paper), where kk is the size of the context window. It is equivalent to minimizing the cross-entropy L=−1n∑tln⁡pθ(xt∣x<t)L=-\tfrac1n\sum_t \ln p_\theta(x_t\mid x_{<t}). Generating is the reverse trip: xt∼P(⋅∣x<t)x_t\sim P(\cdot\mid x_{<t}), one piece at a time (autoregression).

Counting: the n-gram model

With a window of n−1n-1 pieces, the maximum-likelihood estimate is the relative frequency:

P(w∣c)=N(c,w)N(c)P(w\mid c)=\frac{N(c,w)}{N(c)}

where N(c,w)N(c,w) counts how many times ww followed context cc. In the lesson (n=2n=2, bigrams), “is” appears N=8N=8 times and N(is,blue)=4N(\text{is},\text{blue})=4, so P=4/8=0.5P=4/8=0.5. Shannon (1948) already generated text this way: his “second-order word approximation” picks each word according to the transition frequencies from the previous one. The limit is sparsity: with a vocabulary of size VV there are up to V n−1V^{\,n-1} contexts, almost all rare or never seen, and the table does not generalize. Bengio et al. (2003) proposed replacing it with a network that learns a vector per word: if “dog” and “cat” have similar vectors, what is learned from one sentence carries over to the other.

Softmax and temperature

A network produces a score ziz_i (a logit) for every piece ii of the vocabulary. The probability is

pi=ezi/T∑jezj/Tp_i=\frac{e^{z_i/T}}{\sum_j e^{z_j/T}}

With T=1T=1 it is the ordinary softmax. Adding the same constant to every score changes nothing; that is why the lesson’s scores, zi=ln⁡ci−ln⁡2z_i=\ln c_i-\ln 2 (counts 4,2,1,14,2,1,1; rounded on screen to 0.7, 0, −0.7, −0.70.7,\,0,\,-0.7,\,-0.7), give exactly count ÷ total. In general, with zi=ln⁡ciz_i=\ln c_i we get pi=ci1/T/∑jcj1/Tp_i=c_i^{1/T}/\sum_j c_j^{1/T}: with T=0.5T=0.5, blue gets 16/22≈0.72716/22\approx0.727; with T=2T=2, 2/(2+2+1+1)≈0.3692/(2+\sqrt2+1+1)\approx0.369; with T=0.25T=0.25, 256/274≈0.934256/274\approx0.934. As T→0T\to0 all the mass goes to the highest-scoring piece (greedy); as T→∞T\to\infty the distribution tends to uniform. The name comes from statistical physics: the Boltzmann distribution has this same form, with the temperature dividing. It plays this role in, for example, knowledge distillation (Hinton et al., 2015).

Top-k and top-p

After the softmax one can truncate. Top-k (Fan et al., 2018, who sample among the k=10k=10 most likely) keeps the kk most likely pieces. Top-p, or nucleus sampling (Holtzman et al., 2020), keeps the smallest set V(p)V^{(p)} whose probability adds up to at least pp:

∑x∈V(p)P(x∣x<t)≥p\sum_{x\in V^{(p)}}P(x\mid x_{<t})\ge p

Then it shares the probability out again, qi=pi/∑j∈V(p)pjq_i=p_i/\sum_{j\in V^{(p)}}p_j, and samples with qq. Holtzman et al. observed that decoding methods that maximize probability produce text that is “bland, incoherent, or gets stuck in repetitive loops” (the greedy loop in the lesson), and that truncating the unreliable tail, without going all the way to greedy, yields text closer to human writing.

In code

The lesson’s model: count pairs, softmax with temperature, top-p, and greedy writing, with the output it prints:

import math
from collections import Counter, defaultdict

text = ("the sky is blue . the sea is blue . the sky is gray . the sun is big . "
        "at night the sky is dark . the sky is blue and the sun shines . "
        "the sea is gray . today the sky is blue .")
pieces = text.split()

# 1. Count which piece follows each piece (a bigram model)
table = defaultdict(Counter)
for a, b in zip(pieces, pieces[1:]):
    table[a][b] += 1

def softmax(scores, T=1.0):
    z = [s / T for s in scores]
    m = max(z)
    e = [math.exp(v - m) for v in z]    # subtracting the max does not change the result
    return [v / sum(e) for v in e]

def distribution(context, T=1.0, top_p=1.0):
    words = [w for w, _ in table[context].most_common()]
    scores = [math.log(c) for _, c in table[context].most_common()]   # score = ln(count)
    p = softmax(scores, T)
    nucleus, acc = [], 0.0               # top-p: the most likely words until they reach top_p
    for w, q in zip(words, p):
        nucleus.append((w, q)); acc += q
        if acc >= top_p - 1e-9: break
    total = sum(q for _, q in nucleus)
    return [(w, round(q / total, 3)) for w, q in nucleus]   # share it out again

print(dict(table["is"]))
for T in (1.0, 0.5, 2.0):
    print(T, distribution("is", T))
print(distribution("is", top_p=0.7))

# 2. Write: always take the most likely piece (greedy) and repeat
written = ["the", "sky", "is"]
for _ in range(9):
    written.append(table[written[-1]].most_common(1)[0][0])
print(" ".join(written))
# {'blue': 4, 'gray': 2, 'big': 1, 'dark': 1}
# 1.0 [('blue', 0.5), ('gray', 0.25), ('big', 0.125), ('dark', 0.125)]
# 0.5 [('blue', 0.727), ('gray', 0.182), ('big', 0.045), ('dark', 0.045)]
# 2.0 [('blue', 0.369), ('gray', 0.261), ('big', 0.185), ('dark', 0.185)]
# [('blue', 0.667), ('gray', 0.333)]
# the sky is blue . the sky is blue . the sky

The text was written for the lesson, and its Spanish and English versions are parallel word for word, so the counts match in both languages. The scene uses this same model (lm.ts), and verify-data.ts checks it; both live in the lesson folder.

Scale: GPT-1, GPT-2, and GPT-3

All three are decoder-only transformers trained with the objective above. GPT-1 (2018) read BooksCorpus, over 7,000 unpublished books; its size, 117 million parameters, appears in table 2 of the GPT-2 paper (“the smallest model is equivalent to the original GPT”). GPT-2 (2019) reached 1,542 million and read WebText, slightly over 8 million documents (40 GB of text). GPT-3 (2020) has 175,000 million (175 billion). A curiosity: the GPT-3 paper counts 125 million for a model with the same shape as GPT-1 (12 layers of 768). The gap fits the embedding table, which grows with the vocabulary: GPT-1 used 40,000 BPE merges and GPT-2 expanded the vocabulary to 50,257 pieces.

What comes next

We said “words,” but the model works with pieces: tokens (lesson 20). The probability is computed by a network that looks at the whole context with attention (lessons 21 and 22), and what it means to train it on almost the whole internet is the subject of lesson 23.

Decision lens

What this idea means if your job is to decide, not to code.

  • The output is probabilistic: the same question can get different answers. For tasks with one right answer (extracting data, classifying) aim for low variation; for ideas and drafts, variation helps.
  • Lowering the temperature makes the answer more repeatable, not more truthful: a likely error stays likely. No parameter replaces checking against sources.
  • Ask which sampling parameters (temperature, top-p) your application uses and whether you can set them; not every product exposes them.
  • A test with a single run is not evidence. Repeat each case several times with the real configuration and measure how much it varies before deciding.

Terms in this lesson

Full transcript
CommentsThis lesson’s public conversation: questions, facts to check, ideas.

Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub

Sources