Skip to content

Probability for Language Models: Softmax, Temperature, and Sampling

6 min reading

Outcome: Convert scores into a probability distribution, trace temperature scaling and separate sampling choices from factual confidence.

Page details

GenAI foundations · Chapter 4 · Sources

Published · Last edited: · ~6 min reading · ~17 min with practice

20px

Know first: Vectors and Similarity: Dot Products, Cosine, and Why Embeddings Work

GenAI foundations glossary

Hypothetical scenario. Priya runs Northwind's policy prompt twice and sees different wording. Dev proposes lowering temperature. That can change how a token is selected, but it cannot repair a missing policy passage or prove that the selected answer is correct.

Start with the distribution

A discrete probability distribution assigns a nonnegative probability to each possible outcome, with all probabilities summing to one. For the next-token problem, the outcomes are vocabulary tokens. The distribution describes one prediction step conditioned on the current sequence; a complete generated answer requires many such steps.

A model's raw output scores are often called logits. A logit is not already a probability: it may be negative or greater than one. Softmax turns a list of logits into positive normalized weights by exponentiating each score and dividing by the sum of all exponentials. Larger scores get larger shares, but every share is interpreted relative to the other candidates.

For logits zᵢ, the rule is pᵢ = exp(zᵢ) / Σ exp(zⱼ). The exponential is the function e raised to a value. No memorized constant is necessary to understand the mechanism: turn scores into positive weights, sum those weights, then divide each weight by the same total.

Logits2 · 1 · 0Exponentiate7.389 · 2.718 · 1Normalize0.665 · 0.245 · 0.090
Logits: 2 · 1 · 0 → Exponentiate: 7.389 · 2.718 · 1 → Normalize: 0.665 · 0.245 · 0.090. Illustrative conceptual flow.

Normalize three chosen scores

Illustrative calculation. Choose three logits (2, 1, 0). Their exponentials are approximately (7.389, 2.718, 1). The sum is 7.389 + 2.718 + 1 = 11.107. Divide each weight by that sum: 7.389 / 11.107 ≈ 0.665, 2.718 / 11.107 ≈ 0.245 and 1 / 11.107 ≈ 0.090.

The rounded probabilities sum to 0.665 + 0.245 + 0.090 = 1.000. The first candidate is most probable, but the other two still have probability mass. The values are chosen to expose the calculation; they are not a report of what a named model predicted for a real Northwind request.

Adding the same constant to every logit does not change this distribution. Each exponential gains the same multiplicative factor, which cancels between numerator and denominator. Implementations can exploit that property to subtract the largest logit before exponentiating, avoiding unnecessarily large intermediate numbers while preserving the mathematical result.

Temperature reshapes the shares

For positive temperature T, divide logits by T before applying softmax. With T = 0.5, the chosen scores become (4, 2, 0). Their exponentials are approximately (54.598, 7.389, 1), summing to 62.987. The resulting probabilities are about (0.867, 0.117, 0.016). The largest candidate now receives a greater share.

Higher positive temperature flattens relative differences; lower positive temperature sharpens them. It changes selection probabilities rather than adding evidence to the context. If the best-supported token was assigned a low score because the prompt omitted the relevant policy, sharpening the distribution can make the wrong continuation more consistent.

Temperature zero is commonly exposed as a greedy or deterministic-decoding setting, rather than evaluated by literally dividing logits by zero. The precise interface behavior must be checked. Ties, numerical differences, implementation choices and version changes can still affect outputs. A zero setting alone is not a portable guarantee of identical end-to-end responses.

Choosing a token is another step

Greedy selection takes a highest-scoring candidate. Sampling draws according to the configured distribution. For the initial toy probabilities, intervals from 0 to 0.665, 0.665 to 0.910 and 0.910 to 1 can represent the three candidates. An illustrative uniform draw of 0.80 falls in the second interval, so sampling chooses the second candidate even though it is not the most probable.

In the wild. Hugging Face's generation configuration documents sampling controls, temperature and greedy-decoding options separately. That distinction helps locate the decision: the application chooses a decoding policy around model scores. The guide's interface options do not establish that one temperature is universally best for support answers.

Other controls can truncate or restrict the candidate set. Their combination changes the distribution actually sampled. Record the complete decoding configuration when comparing runs, rather than attributing every difference to the model or to temperature alone.

Probable text and verified policy are different

Misconception: a high token probability proves that a factual answer is correct. It indicates a preferred continuation under the current model, context and decoding procedure. It says nothing by itself about whether the source policy is current, whether the answer cites the right paragraph or whether the caller may see the evidence.

Use softmax and temperature reasoning to understand output variation, debug configuration and design controlled comparisons. Keep the model, input, tokenizer and decoding rules fixed when isolating one factor. Then assess answers using a task rubric and source evidence; predictable wording is not equivalent to a correct decision.

Check yourself

What is the probability of the second candidate for logits (2, 1, 0)?

About 2.718 / 11.107 = 0.245. Its raw logit 1 is not itself a probability.

Why does a draw of 0.80 choose the second token in the example?

The cumulative first interval ends at 0.665 and the second at 0.910. The draw lies inside the second interval.

Does setting temperature to zero repair an outdated policy answer?

No. It changes decoding behavior, not the supplied evidence. Check the current source and evaluate the answer; also verify the interface’s actual zero-temperature semantics.

Go deeper