Part II · Ch. 9 — The Prediction Game

Part II · Chapter 9 of 21

The Prediction Game

One job: guess the next token


One job: guess the next token

A language model predicts what comes next. That compact job description leaves a lot to explain. The context arrives as embeddings, and the model produces one score per vocabulary token.

A left-to-right pipeline. Top left: five context chips reading The, cat, sat, on, the, with an empty dashed slot at the end, labeled 'context — token IDs (Ch. 7)'. An arrow feeds into a large box labeled 'the model — one enormous function' with a tag 'billions of dials θ'. An arrow leads to a small three-bar chart with bars mat (), couch (), keyboard () at heights 0.665, 0.245, 0.090, noted 'one bar per token — all V ≈ 50,000 of them'. An arrow goes down to a die icon labeled 'sample', then left to a chip reading mat. A long curving arrow labeled 'append, and play again' loops from the mat chip back up to the empty dashed slot in the context row.
Predict, sample, append, repeat.
$$p(\text{next} \mid \text{context})$$

In words: the probability of each possible next token, given all the tokens so far. That conditional bar "$\mid$" is Chapter 6's "given"; read it as "the next token, given the context."

Autoregressive generation follows this loop.

An early choice changes every later prediction. Here we take the scores as given; Chapters 10 and 11 build the part that computes them.

The box's raw scores: logits

A logit is an unrestricted score. The vector $\mathbf{z}$ collects all the logits. For hand calculation, pretend our vocabulary contains only mat, couch, keyboard.

$$z_{\text{mat}} = 2.0, \qquad z_{\text{couch}} = 1.0, \qquad z_{\text{keyboard}} = 0.0$$

In words: the box rates mat highest at 2.0, couch in the middle at 1.0, and keyboard lowest at 0.0. In Chapter 6 these same three words carried 0.60 / 0.25 / 0.15 from counting storybooks. A model's learned opinion need not match that shelf of storybooks: same three words, different source, different numbers.

Negative logits are legal too. Scores need not total one, so they are not yet probabilities.

Softmax: scores become shares of belief

Softmax converts the scores. First exponentiate; then normalize by the total.

Three panels sharing one color per word: mat, couch, keyboard. Left panel 'logits z — any numbers at all': lollipop stems at 2.0, 1.0, 0.0, with keyboard's dot sitting on the zero line, and a note 'negative scores would be legal too'. Middle panel 'after e^z — all positive now': bars at 7.39, 2.72, 1.00. Right panel 'after ÷ 11.11 — shares of belief': bars at 0.665, 0.245, 0.090 with a note 'now they total 1.000'.
Exponentiate, then divide by the total.

One three-candidate prediction

$$p(\text{mat}) = \frac{e^{2.0}}{e^{2.0} + e^{1.0} + e^{0.0}} = \frac{7.39}{7.39 + 2.72 + 1} = \frac{7.39}{11.11} = 0.665$$

In words: exponentiate mat's score, then divide by the sum of everyone's exponentiated scores, and mat gets 66.5 percent of the belief. The other two follow the same recipe: $p(\text{couch}) = 2.72/11.11 = 0.245$ and $p(\text{keyboard}) = 1/11.11 = 0.090$. The three add to $0.665 + 0.245 + 0.090 = 1.000$, so this is a legal distribution, the kind Chapter 6 drew as bars.

$$\mathrm{softmax}(\mathbf{z})_i = \frac{e^{z_i}}{\sum_j e^{z_j}}$$

In words: candidate $i$'s probability is $e$ to its own score, divided by the sum of $e$ to everyone's score. That $\sum_j$ is Chapter 6's add-them-all-up, and the $e$ is Chapter 4's exponential.

Follow every candidate through softmax
CandidateScoreExponentialShare
mat27.390.665
couch12.720.245
keyboard010.090

A score of −1 would contribute $e^{-1}\approx0.37$, still positive. Adding the same constant to all scores leaves the shares unchanged:

A one-point gap gives a probability ratio of e, about 2.72. Raising just one score changes the shared denominator too; its normalized probability does not itself multiply by e.

Temperature: the promised knob

Temperature T rescales the score gaps before conversion:

Three bar panels sharing a 0-to-1 vertical scale, same three words. Left 'T = 0.5 — crystalline': heights 0.867, 0.117, 0.016. Middle 'T = 1 — as scored': heights 0.665, 0.245, 0.090, with a note 'same logits every time: 2.0 · 1.0 · 0.0'. Right 'T = 2 — molten': heights 0.506, 0.307, 0.186.
Temperature rescales the score gaps.
$$\mathrm{softmax}(\mathbf{z}/T)_i = \frac{e^{z_i/T}}{\sum_j e^{z_j/T}}$$

In words: shrink or stretch all the scores by the same factor $T$, then convert to probabilities as before. Everything downstream is untouched; only the logits going in are rescaled.

Cold and hot on the same scores

At T=0.5, divide [2,1,0] by one half to get [4,2,0]. Since $e^4=(e^2)^2\approx54.6$, the exponential total is about 63.0.

$$p(\text{mat}) = \frac{e^{4.0}}{e^{4.0} + e^{2.0} + e^{0.0}} = \frac{54.6}{63.0} = 0.867 \qquad (T = 0.5)$$

In words: at half temperature, mat's share climbs from 0.665 to 0.867; $p(\text{couch}) = 0.117$ and $p(\text{keyboard}) = 0.016$ get squeezed. The gaps between the scores grew, so the exponentials pulled apart and the distribution sharpened onto its favorite. A cold setting concentrates belief. It does not make the favorite correct.

At T=2, scores become [1,0.5,0]. The exponentials are about 2.72, 1.65, 1, totaling 5.37. Divide to get 0.506, 0.307, 0.186. Their displayed total is 0.999 because of rounding.

Temperature changes choice, not stored weights
SettingBehavior
T below 1Sharpen toward the favorite.
T=1Use the original score gaps.
T above 1Flatten toward a tie.
T approaches zeroConcentrate on maximum-score tokens; tied maxima remain tied.
T grows very largeApproach equal shares across this finite vocabulary.

Play the game

Sampling rolls the reshaped die. Greedy selection takes a maximum directly; it avoids dividing by T=0.

Other sampling filters may keep only the k highest candidates (top-k) or the smallest leading set reaching a chosen total probability (top-p). The score-to-share calculation remains the foundation.

Autocomplete: true and misleading

Two panels. Left, 'your phone's autocomplete': a phone outline showing 'the cat sat on the' with three suggestion chips mat, the, couch, and a note 'counts which word followed your last one or two words'. Right, 'an LLM': the text 'the wet cat sat on the' with 'wet' in and underlined, feeding down through a stack of four layer boxes tagged 'billions of dials θ' into a wide strip of about forty faint bars with one tall bar labeled 'towel' and two mid bars, noted 'fresh scores over ~50,000 tokens, recomputed from the whole context'. A dashed arc runs from the underlined 'wet' down the margin to the towel bar, labeled 'one early word bends the whole answer'.
Earlier context can change the next guess.

Compare a simple word-count predictor with a learned context model. A count table estimates what followed a short phrase; an LLM computes scores through many learned layers. The phone drawing illustrates the simpler method, not every modern keyboard.

Predicting across many kinds of text pressures the model to learn grammar, facts, style and useful reasoning patterns. A narrow training objective can require rich internal structure.

A hallucination exposes the gap between a plausible continuation and a verified fact.

See it move

Watch (1:46): what to notice. The sentence arrives chip by chip and stops at a “?”, the true word mat is picked out in mint, the grade $-\ln p(\text{mat})$ lands at 0.15 in red, a confident-wrong round costs far more than a confident-right one, and billions of rounds pull the red curve down.

Why the LLM cares

We have described what goes in and what comes out: context in, scores out, probabilities, then a choice. Attention and the transformer explain how the scores depend on context.

Training will evaluate the probability of the correct token: a correct answer assigned p=0.1 incurs about 2.3 units of loss. Part III shows how that score changes the parameters $\theta$, “theta.”

What you now know

  • Generation repeatedly predicts, chooses, and appends.
  • Logits are scores; softmax converts them into probabilities.
  • Only score gaps affect softmax.
  • Temperature changes those gaps before sampling.
  • Predicting likely text requires learned structure but does not guarantee truth.

Where we're headed

How does “wet” affect a later prediction? Next we'll build attention, using dot products to decide which token information to mix.