freecoding.school100% FREE · NO SIGNUP
Tensor TownISSUE #85 of 120

top-k · top-p · repetition penalty

NeuraVSThe Overfit Ogre
Neura saysTop-k, top-p (nucleus), and repetition penalty refine which tokens sampling considers.

Raw sampling can pick absurd low-probability tokens. Top-k restricts choices to the k most likely; top-p (nucleus) keeps the smallest set whose probabilities sum to p (adapts to confidence). Repetition/frequency penalty discourages repeating tokens, avoiding loops. Together with temperature these are the decoding knobs — tune them per task to balance coherence, diversity, and avoiding repetition.

Power-ups you unlock

The Overfit Ogre attacks — common mistakes

Boss battleExplain how top-p adapts the candidate set to model confidence.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>top-k: keep top 40 tokens
top-p 0.9: keep smallest set summing to 90%
repetition penalty: stop "the the the"</pre></body></html>
▶ Open the interactive comic issue
‹ Inference · Greedy · Sampling · TemperatureSpeculative Decoding · Faster Inference ›