Concepts / Softmax and Converting Logits to Probabilities

Softmax and Converting Logits to Probabilities

Softmax is the step that converts a list of logits into a normalized probability distribution, one value per token, where the values are non-negative and sum to 1.

  • Programming

Why the Top Token Does Not Always Win

A language model does not produce text by looking up a final answer. At each step it produces a score for every token in its vocabulary, and those scores are then turned into probabilities. This article answers the question of what happens next: how the model chooses one token from that probability distribution, and what knob we can turn to change the choice.

What do you think happens?

Suppose the simplest rule is used: always take the token with the highest probability. If you run that rule 1,000 times on the same input, how many distinct tokens do you get back?

  • Many different tokens, roughly in proportion to their probabilities

  • Only one token, the same one every time

  • Two tokens, the top one and a runner-up

Reveal answer
Answer:

Only one token, the same one every time.

The always-take-the-highest-probability rule is deterministic. The same input always yields the same continuation. Sampling, by contrast, makes a random draw, so the top token wins most of the time but not every time.

From Raw Scores to a Probability Distribution

The raw scores a model produces are called logits. They are not probabilities yet: they can be any numbers, and they do not add up to 1. Softmax is the step that converts a list of logits into a normalized probability distribution, one value per token, where the values are non-negative and sum to 1. The softmax is applied along the vocabulary dimension, so every token in the vocabulary receives its own probability in the same pass.

forwardlogit 4.0 -> probability0.87backwardlogit 2.0 -> probability0.12pizzalogit 0.0 -> probability0.02
How does each raw logit become a probability, and how are the values normalized so they sum to 1? This toy example uses three illustrative tokens.

The diagram uses made-up logits to show the mechanism; it is not output from the source. The arithmetic behind the probabilities works in two stages. First, each logit is exponentiated, which makes every value positive and enlarges the gaps between the scores. Second, each exponentiated value is divided by the sum of all the exponentiated values, which normalizes them so they add up to 1. Because a larger logit produces a disproportionately larger exponentiated value, the top-scoring token captures most of the probability mass.

Tracing a Temperature Scaling Step

Temperature scaling changes the distribution without changing the logits themselves. The model's raw scores stay the same; the scaling happens between those scores and the softmax step. The helper divides every logit by the temperature T, which must be a number greater than 0, and then applies the softmax. Because the division happens first, every logit is rescaled by the same factor before any exponentiation takes place.

Worked example

Tracing logits at temperature 0.5

Using the illustrative logits forward 4.0, backward 2.0, and pizza 0.0, trace what happens when the temperature is 0.5.

  1. 1
    Identify the logits

    The raw scores are 4.0, 2.0, and 0.0. These are unchanged by temperature scaling.

  2. 2
    Divide by the temperature

    Dividing each logit by 0.5 gives 8.0, 4.0, and 0.0. Every logit is multiplied in effect by the same factor, so the gaps between them grow.

  3. 3
    Apply softmax

    The scaled values are exponentiated and normalized. The gap between forward and the rest is now much larger than it was at temperature 1.

  4. 4
    Read the probabilities

    The top token's probability rises to about 0.98, while backward and pizza fall to very small values. The distribution is sharper.

Result

At temperature 0.5 the most likely token dominates. Sampling will pick forward almost every time, and the distribution is more peaked than at temperature 1.

The same trace at temperature 1 leaves the logits unchanged, since dividing by 1 does nothing. In that case the probabilities are the ordinary softmax probabilities, and sampling behaves exactly as it would without any temperature scaling. Every other temperature changes the distribution.

Sampling Versus Always Taking the Top Token

Once the probabilities exist, the sampler must choose one token. The deterministic rule always takes the highest-probability token. The probabilistic rule draws a token at random, with a chance proportional to its probability. The difference matters: under the probabilistic rule, a most-likely token is chosen most often but not always.

always forwardsame output every runforward most drawschosen with its probabilitydeterministicsame input, samecontinuationother tokenssometimesrandom draw can pick them
Why does the top token get picked most often under sampling but not every time, while the greedy rule always picks it?

The source makes this concrete by repeating the random draw 1,000 times and counting how often each token appears. The most likely token, forward, appears in a clear majority of those draws, while several other tokens also appear at least occasionally. A single sampled call can therefore return a different token than the argmax choice, even though the top token still wins most of the time.

What the Temperature Knob Does to Token Choice

The core rule is direct. Temperatures greater than 1 produce more uniformly distributed token probabilities. Temperatures smaller than 1 produce more confident distributions, which are sharper or peakier. A temperature of 1 is the baseline and leaves the distribution unchanged.

forward 0.87temperature 1forward 0.47temperature 5backward 0.12temperature 1backward 0.32temperature 5pizza 0.02temperature 1pizza 0.21temperature 5
When the logits are divided by 5, how do the bar heights for each token change relative to one another?

The values in the diagram are illustrative, computed from the made-up logits used earlier, not taken from the source. The pattern is what matters: at temperature 5 the top token is still the most likely choice, but the other tokens receive enough weight to be selected much more often.

The effect is not symmetric in what it produces. A very small temperature such as 0.1 makes the draw select the most likely token almost every time, bringing the behavior close to argmax. A large temperature such as 5 adds variety. The source reports that at temperature 5, a continuation such as every effort moves you pizza appeared about 4 percent of the time. Large temperatures add variety at the cost of more nonsensical text.

Checking the Order of Operations When Tracing

The scaling happens before the softmax. That ordering determines where to look when a result seems wrong. A mistake in the scaling step shows up as a distorted distribution even when the softmax call itself is correct, so checking only the softmax will not find the problem.

  1. 1
    Check the logits: confirm they are the raw scores the model produced and that they have not already been modified.
  2. 2
    Check the temperature: confirm which value is active at this stage and that it is greater than 0.
  3. 3
    Check the division: confirm each logit was divided by the temperature, and that the same factor was applied to every logit.
  4. 4
    Check the softmax: confirm the exponentiation and normalization produce values that sum to 1.

When you trace a program through this pipeline, track three things at each stage: which temperature is active, whether the probability of the most likely token rises or falls relative to the unscaled case, and how many tokens receive any meaningful probability at all. A departure from the expected direction usually points to a step earlier in the list than the one you were examining.

Mistakes Beginners Make with Temperature

  • Common mistake

    Assuming a temperature below 1 makes the model more random

    Why it fails

    Temperatures smaller than 1 produce more confident, peakier distributions. The more random behavior comes from temperatures above 1.

    Do this instead

    Remember the rule: below 1 sharpens the distribution, above 1 flattens it.

  • Common mistake

    Thinking temperature changes the logits themselves

    Why it fails

    The raw scores the model produced stay the same. Temperature scaling happens between those scores and the softmax step.

    Do this instead

    Trace the logits first, then the division by the temperature, then the softmax.

  • Common mistake

    Believing temperature 1 is a special, non-default setting that changes results

    Why it fails

    A temperature of 1 divides by 1, which leaves the logits unchanged, so sampling behaves as it would without any temperature scaling.

    Do this instead

    Treat temperature 1 as the baseline.

  • Common mistake

    Expecting the sampler to always return the top token because it has the highest probability

    Why it fails

    The draw is random, with a chance proportional to probability. The top token wins most of the time, not every time.

    Do this instead

    Use the deterministic argmax rule if you want the same output every run.

  • Common mistake

    Using a temperature of 0 in code, which breaks the requirement that the temperature be greater than 0

Predicting Sharper or Flatter Distributions

MEDIUM

Using the illustrative logits forward 4.0, backward 2.0, and pizza 0.0, predict before calculating whether a temperature of 5 produces a sharper or a more uniform distribution than a temperature of 1. Then say which token you expect to be most likely at both temperatures.

Hints
  • Divide each logit by 5 first. What happens to the gaps between 4.0, 2.0, and 0.0?
  • A temperature above 1 flattens the distribution, so compare the top token's share of probability before and after.
  • The most likely token stays the same; only the distribution's shape changes.

What do you think happens?

Will the probability of the top token be higher or lower at temperature 5 than at temperature 1?

  • Higher, because the top token is the largest logit

  • Lower, because the distribution flattens

Reveal answer
Answer:

Lower, because the distribution flattens.

At temperature 5 the logits are divided by 5, shrinking the gaps between them. The top token keeps the highest probability, but its share drops and the other tokens gain weight.

Key Takeaways

  • A language model produces a score for every token, and softmax converts those logits into a normalized probability distribution that sums to 1.
  • Sampling picks the next token with a chance proportional to its probability, so the most likely token is chosen most often but not always, unlike the deterministic always-take-the-top-token rule.
  • Temperature divides every logit by the same value greater than 0 before softmax; a temperature of 1 leaves the distribution unchanged, above 1 flattens it, and below 1 sharpens it.
  • Very small temperatures bring sampling close to argmax, while large temperatures add variety at the cost of more nonsensical text.
  • When tracing code, check the logits, the temperature, the division, and then the softmax, in that order, because scaling errors appear as distorted distributions.

Related concepts

More concepts starting with S