Lensa ML

Temperature vs top-p: reshape every bar or cut the tail

A language model writes one token at a time, where a token is a word or part of a word. For each step it gives every candidate token a probability, then draws one at random. Temperature and top-p both change those probabilities before the draw, in different ways.

2 min read · Lensa ML
T = 1baseT = 0.5all reshapedtop-p 0.98 keptlittle25.644.227.5young18.924.320.4small12.710.913.7king10.47.311.2girl8.54.99.2man7.03.37.5beautiful5.72.26.1dragon4.21.24.5princess3.81.0cutboy3.10.7cut
Values in %. White ticks mark each T = 1 length; dashed boxes mark cut tokens.
  1. The bars come from the explainer's story prompt, Once upon a time, there was a ___, with ten candidate tokens. The model gives each a raw score called a logit, from 4.4 for little down to 2.3 for boy. Softmax turns logits into probabilities that add up to 1: it raises e to each logit and divides by the total. Temperature T is a number that divides every logit before softmax. At T = 1 nothing changes, so little gets 25.6% and boy 3.1%.
  2. At T = 0.5 the gaps between logits double, so little rises to 44.2% and boy falls to 0.7%. Every bar moves, and none drops to zero. The ranking never changes. A T above 1 does the opposite and flattens the bars toward equal.
  3. Top-p, also called nucleus sampling, leaves the shape alone and cuts the tail. It sorts tokens from most to least likely and keeps adding them until their total reaches p. With p = 0.9, the first seven tokens cover 88.8% and the eighth, dragon, takes the total to 93.0%, so eight stay. Princess and boy are cut, and the kept bars are divided by 0.930 so they add up to 1 again: little becomes 27.5%. On the explainer's factual prompt, Paris alone has 96.2%, so the same p keeps one token.

Takeaway

Temperature sets how strongly the model favors its top choices. Top-p sets how many choices stay in play, and adapts that number to each step. Lower the temperature for focused, repeatable text such as code or data extraction. Use top-p, often 0.9 to 0.95, to keep variety while dropping the long tail of unlikely tokens. Some API providers recommend adjusting one of the two, not both.

Go deeper · interactive

Sampling: Temperature, Top-k & Top-p

On step 5, drag the p slider and switch prompts to watch the nucleus grow and shrink.

Open the explainer

More bites

References