GGiriBioAI

/ module 07 · Inside the model

Sampling & Decoding

A model does not output a word. It outputs a score for every token in its vocabulary. Decoding is the step that turns those scores into one chosen token, and the knobs you set here decide whether answers feel precise, creative or unhinged.

By the end you can
  • Explain logits and how softmax turns them into probabilities
  • Predict the effect of temperature, top-k and top-p
  • Say when greedy, sampling or beam search fits best

Try this first: Click the 'greedy' preset, sample 10 times, then try 'wild'.

about 12 min hover ? for help

concept

From logits to a token

The last layer produces logits. A softmax turns them into probabilities. Before sampling, we can reshape that distribution: temperature sharpens or flattens it, top-k and top-p cut off the unlikely tail, and a repetition penalty discourages words already used.

logits
Raw, unnormalized scores the model gives every token in the vocabulary.
temperature
Sampling randomness. Low for facts, higher for brainstorming. 0 = greedy.
top-p (nucleus)
Keep the smallest set of tokens whose probabilities add up to p, then sample from those.
Live lab · Next-token sampler
The scientist looked into the microscope and saw a ▍
presets:
cell
40.2%
bacterium
22.1%
virus
13.4%
sample
11.0%
pattern
6.6%
crystal
4.5%
cat
1.1%
galaxy
0.7%
dragon
0.4%
sandwich
0.2%
candidates kept: 10entropy: 2.38 bits
Ten independent samples with these settings

Press the button. Greedy settings give the same word ten times; wild settings give a mix, including odd choices.

search

Greedy is not always best

Greedy decoding grabs the single most likely token every step. Beam search keeps several partial answers alive and picks the sequence with the best total probability. In this two-step example, greedy picks "a" first (50%) but then has no strong follow-up, while "the dog" wins overall.

Live lab · Greedy vs beam search
Greedy (beam width 1)
step 1: a (50%)
step 2: bird (40%)
"a bird" = 20%
Beam search (all paths scored)
the dog36%
a bird20%
a dog15%
a cat15%

Chat models usually sample (temperature + top-p) because beam search tends to sound bland and repetitive in open-ended text. Beam search remains common in translation and speech recognition.

top-k (sampling)
Only the k most likely next tokens are allowed.
beam search
Keeps several candidate continuations at once and picks the best overall sequence.
repetition penalty
Lowers the odds of tokens that were already generated, to avoid loops.

Check yourself

Tick each one you could explain to a friend. If you can't yet, scroll back up and replay the lab.

Finished exploring?
Progress is saved only in this browser.