Muhammad Hanzla Independent AI Alignment & Safety Researcher

Courses/Field Foundations/The Forward Pass/Choosing the Next Word

Field Foundations · Part 1 — The Forward Pass

Output: Logits →
Softmax → Sampling

Module 5 — the last step: how a final vector becomes an actual chosen word. This closes the machine.
2 short math sidebars (softmax · temperature) ~20–30 min read + your own notes Free · last updated
The one-sentence version: After the last block, the model turns each word's final vector into a score for every word in its vocabulary (logits), softmax turns those scores into a probability wheel, and a sampling strategy spins that wheel to pick the next word — which is exactly why the same prompt can give different answers.
🔌 One wording note
"word" = "token." The model scores and picks tokens, not always whole words. We say "word" because it reads easier, and for common words 1 token = 1 word. Mentally substitute "token" throughout.

Section 0

Why we're here (the last step that makes a word)

Look how far you've come — you hold the whole engine now:

text → tokens (M1) → embedding vectors (M2)
→ attention mixes in context (M3) → stacked blocks refine it on the residual stream (M4)
→ a final, deeply-context-aware vector for each token

After the final block, the last token sits there holding a rich vector — everything the model figured out about "what should come next." But a vector is a list of numbers. The model's actual job was to produce a word.

This module is the bridge from that final vector to an actual chosen token. It's the shortest hop in Part 1, but it answers two things you've probably wondered about:

By the end you'll be able to explain
what logits are + how the final vector becomes one score per token · what softmax does (scores → a distribution summing to 1) · greedy vs sampling · what temperature does (and why low = safe, high = creative) · what top-k and top-p trim away · why the same prompt can give different answers — and when it won't.

Section 1

From the final vector to logits — the scoreboard

The model has a fixed vocabulary — every token it knows (M1: built by BPE, often ~50,000 for GPT-2, ~100k+ for newer models). To pick the next token, the model has to weigh in on all of them at once.

So it takes the final vector for the current position and produces one number for every token in the vocabulary. That long list of numbers is called the logits.

What a logit is
A logit is a raw, unbounded score — "how much do I like this token as the next one." Higher = more preferred. They are not yet probabilities: they can be negative, can be large, don't add up to anything tidy. Think raw scoreboard, not percentages. A 50,000-token vocabulary → a list of 50,000 scores for this one position.
catlogit 8.2
doglogit 6.1
thelogit 3.0
bananalogit -1.4
xylophonelogit -4.0
the logits = one brightness per word in the whole vocabulary (5 of ~50,000 shown)
Analogy — the contestant and the giant scoreboard
Picture a game show with 50,000 buzzers, one per word. The model lights up each buzzer with a brightness = how much it wants that word next. cat glows bright, the glows medium, xylophone is nearly dark. That wall of brightness levels is the logits. Now the model has to actually pick one.

Section 2

Softmax — turning scores into a probability wheel

Raw scores are awkward to pick from. You can't "roll the dice" on a list like [8.2, -1.4, 3.0, …] — what's the chance of each? You need them as probabilities: every token gets a clean percentage, and they all add up to 100%.

The function that does this is softmax. Feed it the logits, and out comes a probability distribution — one probability per token, all between 0 and 1, summing to exactly 1.

the probability wheel
slice size = probability
cat ~46% dog ~27% the ~20% banana ~5% … ~2%

Same ranking as the scoreboard — now as slices of one wheel. cat gets a fat slice; xylophone a sliver you'd almost never land on. Pick the next token = spin the wheel.

Two properties worth holding:

  1. It keeps the order. The biggest logit becomes the biggest slice. Softmax never reshuffles who's ahead — it converts the gaps into proportions.
  2. It exaggerates the lead, gently. A token a bit ahead in logits ends up with a more-than-proportional slice. Big leads become near-certainties; small gaps stay genuinely uncertain.
Key takeaway
Logits = raw scores (any range, no meaning on their own). Softmax → probabilities (0–1, sum to 1, ready to pick from). Same ranking, now as slices of one wheel.

Section 3

Greedy vs sampling — two ways to pick

You've got the wheel. How do you choose the next token?

Greedy — take the biggest slice
Skip the spin. Pick the single highest-probability token, every time. Deterministic — same wheel → same pick. Safe for short facts, but over a paragraph it gets flat and repetitive (loops, always the most-obvious word).
Sampling — spin the weighted wheel
Pick randomly, weighted by slice size. The fat slice usually wins, but sometimes a medium slice gets picked — and that bit of chance is what makes writing feel alive, varied, human.
Analogy
Greedy = a contestant who always buzzes the brightest buzzer. Reliable, but predictable and boring. Sampling = the contestant spins the weighted wheel — favorite usually wins, but there's room for a surprise. Most real generation uses sampling, then shapes the wheel (next two sections) to control how wild the spin is.
Key contrast — the root of "why different answers?"
Greedy = deterministic (no randomness; biggest slice every time). Sampling = random, weighted (spin; bigger slice = better odds, not a guarantee). Hold that for §6.

Section 4

Temperature — the creativity dial

Here's the famous knob. Temperature reshapes the wheel before you spin it — it decides how sharp or how flat the slices are.

low T (0.2)
sharpened — leader dominates
safe · focused · dull
T = 1.0
the "true" wheel
untouched distribution
high T (1.2)
flattened — long-shots get a chance
creative · risks nonsense
Analogy — the dial on the spinner
Low temperature squeezes the wheel so the favorite owns almost all of it (boring but safe). High temperature spreads it so even sliver-tokens get landable slices (creative but risky). You're not changing which token leads — you're changing how much the leader dominates.
Trade-off
Low T = coherent but dull/repetitive. High T = creative but risks nonsense. No universally "right" value — it depends what you want. Code generation likes low T (one right answer); brainstorming likes higher T (variety). Temperature is the coherence ↔ creativity slider.

Section 5

Top-k and top-p — trimming the wheel before you spin

Temperature reshapes the whole wheel, but never fully removes the junk — at high temperature some genuinely terrible token still has a sliver, and once in a while you land on it and derail the sentence. Top-k and top-p fix that by chopping the bad slices off entirely before the spin.

Top-k — keep the k biggest slices

Pick a number k (say 40). Keep only the 40 highest-probability tokens, throw the rest away, rebuild the wheel from just those, spin. The other 49,960 can't be picked at all. Simple: it caps how far down the list a surprise can come from.

Limit of top-k
k is fixed, but how many tokens are "reasonable" changes by context. After "The capital of France is" basically one token is right (Paris) — keeping 40 is too many. After "My favorite hobby is" hundreds are good — 40 is too few. A fixed k can't adapt.

Top-p (nucleus) — keep the smallest set that adds up to p

Instead of a fixed count, pick a probability mass p (say 0.9). Starting from the biggest slice, keep adding tokens until their slices sum to 90% — then stop and discard the rest. Spin from that "nucleus." The clever part: the set resizes itself.

"The capital of France is ___"  →  model is confident
Paris 95% Lyon 1% Nice 0.5% … everything else
nucleus = basically just Paris (one slice already covers 90%). Adapts to "confident."
"My favorite hobby is ___"  →  model is wide open
readinghikinggaming cookingpaintingrunning … ~hundreds, until they sum to 90%
nucleus = a big set (no single token dominates). Adapts to "open."
Analogy
Top-k = "keep the top 40 slices, always." Top-p = "keep just enough top slices to cover 90% of the wheel — could be 1 slice, could be 300." Top-p is more popular because it adapts to how sure the model is. Many systems combine top-p and temperature.
Key takeaway
Temperature reshapes the wheel (sharp vs flat). Top-k / top-p trim the wheel (cut the unlikely tail) — top-k by a fixed count, top-p by an adaptive probability mass. Often used together: trim the junk, then shape what's left.

Section 6

Why the same prompt gives different answers

Now you can answer the question the whole module was pointed at.

If decoding uses sampling (spinning the weighted wheel), there's real randomness in every pick. Same prompt → same wheel → but a different spin → a different token → and from there the sentences diverge. That's why an AI can answer the same question two different ways.

And you can now predict exactly when it won't differ:

"A language model doesn't 'know' its answer — it builds a probability wheel for the next token and spins it. Determinism vs creativity is just whether you spin, and how sharp you made the wheel first."

Section 7

The handoff — you've closed the machine

Trace it end to end, because you can now do the whole journey:

"The cat sat on the"
→ tokens (M1)
→ embedding vectors (M2)
→ attention mixes in context (M3)
→ stacked blocks refine on the residual stream (M4)
→ final vector for the last position
→ LOGITS: a raw score for every vocabulary token (§1)
→ SOFTMAX: scores → a probability wheel (§2)
→ SAMPLING: shape it (temperature), trim it (top-k/top-p), spin it (§3–5)
→ next token: "mat"
→ append "mat" to the text, and do the whole thing again for the next token.

That last line is the secret of the whole machine: it produces one token at a time, then feeds the result back in and runs again. A whole paragraph is this loop, over and over.

That is the complete LLM machine — prompt in, word out. Part 1 is done.


🧠 Recap card

The whole module in six lines
  1. After the final block, the model turns each position's final vector into logits — one raw score per vocabulary token (any range, no meaning alone). It's a comparison of the final vector against every token (the "unembedding").
  2. Softmax turns logits into a probability distribution — every token 0–1, all summing to 1. Picture a weighted spinner: slice size = probability. Keeps the ranking, gives leaders an outsized slice (exponentiate, then normalize).
  3. Greedy = always take the biggest slice (deterministic, but flat/repetitive). Sampling = spin the weighted wheel (random, weighted — lively, human). Most generation samples.
  4. Temperature reshapes the wheel before the spin: low = sharp (safe, dull; T=0 is greedy), high = flat (creative, risks nonsense). It divides the logits before softmax — stretching or squishing the gaps.
  5. Top-k keeps the k biggest slices (fixed count); top-p / nucleus keeps the smallest set summing to p (adaptive — 1 token when confident, hundreds when open). They trim the junk tail; often combined with temperature.
  6. Same prompt → different answers because sampling is random. Greedy / T=0 → same answer every time; sampling with T>0 → varied.

✅ Now go and use it

Read it, write your own notes, then talk it through

Step 1 — talk it through. This copies a prompt with the whole module already inside it. Paste it into any AI assistant: it asks you questions one at a time, answers the ones you wrote down, and explains why a wrong answer was tempting. It will not test you.

Step 2 — only when you feel ready. The closed-book gate. It carries this module's own questions and tells the assistant to score you strictly and explain what you misunderstood. Write your own notes first — the notes are for encoding, not for shrinking the test, and the questions cover the whole module, not just what you wrote down.

Download the .md
a read b your own notes c discuss d scored check e Quiz Me on all five

Any assistant works — Claude, ChatGPT, Gemini, an open-source model you run yourself. Free tiers are fine. You can dictate your answers instead of typing.

← back to the index Free · no account · no tracking