cd .. / course
Part 1 · Lesson 11/15

Three dials on the next-word distribution

Softmax hands you one honest distribution over the next word. Before picking, you can reshape it with three dials — temperature, top-k, and top-p — without ever touching the logits underneath. Every bar below is recomputed live from the same eight scores. (Last page produced vocab-logits over the 7 sentence tokens; here we swap to a richer 8-candidate set for "…it was ___" so the dials have something visible to reshape — same idea, a distribution over next words.)

⏱ ~5 min 🎚 three live dials math: some

Last page turned the final vector into vocab-logits and softmaxed them into probabilities. That's one fixed distribution — but it's not the end of the story. Between "here are the probabilities" and "here's the word," there's a reshaping step, and it's where creativity versus focus gets dialed.

Temperature divides every logit by T before softmax: below 1 sharpens toward the leader (safer, more deterministic), above 1 flattens toward the field (riskier, more surprising). Top-k keeps only the k best candidates and zeroes the rest. Top-p keeps the smallest set whose probabilities add up to at least p.

The division of labor: top-k and top-p decide which tokens are eligible; temperature decides how peaky the choice among them is. Same softmax underneath — the model's scores never move.

Reshape the distribution · computed live
candidates for “…it was ___” — eight next-token scores, reshaped by the dials below
temperature T1.0
z′ = z / T · <1 sharpens, >1 flattens
top-k8 · off
keep the k highest, zero the rest
top-p1.00 · off
keep smallest nucleus with Σ ≥ p
p = softmax(logits / T)  →  keep top-k or the top-p nucleus  →  renormalize survivors
eligible tokens, renormalized Σ eligible = 100.0%

TRY → Pull temperature to 0.5 and watch tired shoot toward 79%; push it to 2.0 and the field flattens. Set top-k = 3 — five bars go dark and the survivors regrow. Now set top-p = 0.9 instead: it keeps four tokens, not three, because it counts probability mass, not candidates.

Show me the actual numbers

The eight raw logits (fixed): tired 3.2 · sleepy 2.4 · hungry 1.6 · resting 1.1 · scared 0.7 · purring 0.3 · gone -0.2 · blue -1.0. At T = 1, softmax gives:

tokenlogit zp (T=1)cumulative
tired3.251.0%0.510
sleepy2.422.9%0.740
hungry1.610.3%0.843
resting1.16.3%0.906
scared0.74.2%0.947
purring0.32.8%0.975
gone-0.21.7%0.992
blue-1.00.8%1.000

top-k = 3 keeps {tired, sleepy, hungry} and renormalizes them to 60.6% / 27.2% / 12.2% — a hard cap on the count of survivors, always exactly three.

top-p = 0.9 walks the cumulative column: 0.510 → 0.740 → 0.843 → 0.906. It stops the moment the running sum crosses 0.9 — which happens after adding resting. So it keeps {tired, sleepy, hungry, resting} — one more token than k=3. That extra token is the whole point: top-p caps probability mass, so the number of candidates floats with how confident the model is. Here the model isn't confident enough for three tokens to cover 90%, so a fourth is pulled in.

Temperature reshapes without cutting: T = 0.5 sharpens tired to ~79%; T = 2 flattens it to ~30%. Every one of these — temperature, top-k, top-p — changes the distribution you sample from. None of them changes the logits. The model's scores are fixed the instant the forward pass ends; the dials only reshape how you draw a word from them. (Illustrative candidates — the mechanism is the real thing.)

MythTemperature makes the model smarter or dumber — it changes what the model knows.
RealityThe logits are fixed. Temperature, top-k, and top-p only reshape the distribution you sample from — more peaky and safe, or flatter and more surprising. The model's knowledge is untouched; you're only changing how you draw from it.
Takeaway

Three dials, one distribution: temperature sets how peaky, top-k caps the candidate count, top-p caps the probability mass. The logits never move — you only reshape how you sample from them.

You set top-p = 0.9. What does that do?
Why: top-p (nucleus) caps probability mass, so the number of eligible tokens floats with the model's confidence — unlike top-k, which caps the count. Here 0.9 pulls in four tokens because three only reach 0.843.