PART 1 · VISUAL CONCEPTS · 03 / 05

Logits, Temperature & Sampling

How does an LLM turn raw neural scores into the next word—and why do Temperature, Top-K, and Top-P reshape its creativity?

Interactive Lab 01 · Probability Shaping

Logits → Temperature → Top-K & Top-P Nucleus Filter

Balanced
Presets:
Temperature (T) 0.80
Top-K Cap 10 (All)
Top-P (Nucleus) 0.90
Entropy (H) 0.00 bits Shannon uncertainty
Effective Choices (2^H) 1.00 Perplexity-equivalent pool
Surviving Pool 4 / 10 tokens After Top-K & Top-P
Top-1 Probability 99.8% "Paris"
Prompt: "The capital of France is..."
Click any row to inspect math
# Token Raw z z / T Probability Distribution Final P Status / Σp
Inspecting #1 "Paris" KEPT IN NUCLEUS
1. Raw Logit z_i 11.40
2. Scaled z_i / T 14.25
3. Pre-Filter Softmax 99.8%
4. Final P(w) 100.0%
Interactive Lab 02 · Sampling & Autoregressive Loop

Inverse-Transform CDF Roulette & 4-Token Rollout

0 Draws
Inverse-Transform CDF Ruler [0.00 → 1.00]
u=0.00
0.00 0.25 0.50 0.75 1.00
Click "Sample 1 Token" to drop a uniform random needle u ~ U(0, 1) Ready
4-Step Autoregressive Trajectory
Prompt: "The capital of France is..."
Click "Generate 4-Token Continuation" to run Prompt → Sample → Append → Re-score using your live T / Top-K / Top-P settings.
Set T = 0.00 and click twice: you get the exact same path every time. Raise T = 1.20 to watch each sampled token steer the entire downstream branch.
Mental Model · Pure Standard-Library Python

Exact Sampling Loop in ~24 Lines

sample_next_token.py

      

Temperature = Contrast Knob

Dividing by T < 1 widens logit gaps (confident/repetitive); T > 1 shrinks gaps (flat/inventive).

Why Top-P Beats Static Top-K

On "capital of France is...", Top-P keeps just 1 token (Paris), while on "a brave...", Top-P dynamically opens up to 6+ options.

Why T = 0 Isn't 100% Deterministic

GPU floating-point batching isn't strictly associative—microscopic rounding differences can flip near-tied top logits across runs.