Concepts / Vocabularies and Token Probabilities

Vocabularies and Token Probabilities

Logits are the raw output of a model before probability conversion; larger logits correspond to higher probabilities, but the logits themselves are not probabilities.

  • Programming
Interactive lab

Try it: Text Vectorization: Tokens to Vectors

The text-to-tensor pipeline: standardize, split into tokens, build a frequency-ranked vocabulary with padding and [UNK], map tokens to integer ids, pad or truncate, then one-hot or multi-hot encode and look the ids up in an embedding table.

How it works

  1. Standardize: lowercase and delete punctuation.
  2. Split on whitespace into tokens.
  3. Build the vocabulary: 0 = padding, 1 = [UNK], then words by frequency (ties: first appearance), capped at max tokens.
  4. Map each token to its id (1 if it did not make the vocabulary), then pad with 0 or truncate to the sequence length.
  5. Encode: one-hot rows per position or one multi-hot row per text; embed: id k selects row k of the embedding table.

Default run (18 steps): Raw text (59 characters): "The movie was great. The plot was great, the acting wasn't!". A model cannot read strings, so it is turned into numbers step by step. … Embedding lookup: id k selects row k of a fixed 8 x 3 table, giving a (8, 3) dense matrix instead of (8, 8) sparse one-hots. Row 0 (padding) is looked up too; a trained Embedding layer would learn these rows.

Simplified: Bounded ASCII text (up to 200 characters, 40 tokens), whitespace splitting only, and a small FIXED embedding table (a real Embedding layer learns its rows). The multi-hot vector keeps the padding slot (always 0) so its positions line up with the ids.

Educational simulation

Loading the simulation…

Why Raw Model Scores Look Like Probabilities

A language model does not finish its work when it produces its raw output. That output is a list of numbers, one for each token in the model's vocabulary. The vocabulary is the full set of tokens the model can produce, and the raw output assigns one number to every entry in that set. It is tempting to read those numbers as the model's confidence about each token. The larger the number, the more the model seems to favor that token, and the numbers look like they might already be percentages that add up to a whole.

This article tests that belief, shows exactly where it breaks, and then introduces the standard tool used to turn raw output into something that can be treated as a probability. Keep the belief in mind as you read: the model outputs a vector of scores, the largest score wins, and so the scores must be the model's probability estimates. Part of this is true, and part of it is where the reasoning goes wrong.

What do you think happens?

A model with three tokens produces the logit vector [2, 1, 0]. Which token should receive the highest probability, and would you expect the three probabilities to add up to exactly one?

  • The first token gets the highest probability, and the three values add up to exactly one

  • The first token gets the highest probability, but the values do not add up to one

  • The last token gets the highest probability, and the values add up to one

Reveal answer
Answer:

The first token, with logit 2, gets the highest probability, but the raw logits do not add up to one. Their sum is 3.

A larger logit ranks a token higher, so the largest entry points at the most favored token. However, the logits are raw scores, not probabilities, and nothing in them forces a total of one. The sum of 2, 1, and 0 is 3, so the raw vector cannot be read as a probability distribution. The conversion step below is what fixes this.

Three Gaps Between Logits and Probabilities

Definition

Logits are the raw output of a model before probability conversion. Larger logits correspond to higher probabilities, but the logits themselves are not probabilities.

The first half of the common belief is partly true. A larger logit does rank a token higher, so the largest entry still points at the most favored token. The second half is where the reasoning goes wrong. The scores are raw outputs, and they have not yet been arranged into a form that probability rules require. The source identifies three properties that separate logits from probabilities. Each one is a property a probability must have, and logits do not automatically have it.

  • Logits do not represent probabilities. They are raw scores produced before any conversion.
  • Logits do not have to add up to one. Their total can be any number.
  • Logits can be negative, whereas probabilities must be non-negative.

Because of these three gaps, a raw logit vector cannot be used directly as a probability distribution. A probability distribution over the vocabulary needs every entry to be non-negative and the entries to sum to one. Raw logits meet neither requirement by default, even though their ordering is still informative.

Tracing Each Token Through the Vocabulary

To see the mapping concretely, imagine a tiny vocabulary of three tokens: cat, dog, and fish. The model produces the logit vector [2, 1, 0], meaning cat has logit 2, dog has logit 1, and fish has logit 0. Each token keeps its own logit, and after conversion each token also gets its own probability. The sequence of positions in the vector stays fixed, so each token's logit and probability can be traced across the whole vector.

catlogit 2, probability 0.665doglogit 1, probability 0.245fishlogit 0, probability 0.090
How does each token in the vocabulary line up with its own logit and its own probability?

Notice that the ordering survives the conversion. Cat has the largest logit and also the largest probability, which matches the source's statement that larger logits correspond to higher probabilities. What changes is the form of the numbers. The logits are 2, 1, and 0 with a total of 3, while the probabilities are about 0.665, 0.245, and 0.090 with a total of exactly one.

The Softmax Formula

A softmax layer is the component that converts a logit vector into probabilities. Suppose the model's vocabulary has N tokens, and the logit vector is written as x_1, x_2, up to x_N. The probability assigned to token i is given by the formula below, where the sum in the denominator runs over every token j from 1 to N.

Formula

Read the formula as a recipe. Take each logit x_i, apply the exponential to get $e^{x_i}$, and then divide by the total of all the transformed values. The numerator belongs to one token, while the denominator is shared by every token in the vocabulary.

Formula, step by step
  1. 1
    x_i is the raw logit for token i, taken from the logit vector of length N
  2. 2
    e^{x_i} transforms that logit into a positive number, which becomes the numerator
  3. 3
    The sum over j from 1 to N adds the transformed values of all N tokens, forming the denominator
  4. 4
    Dividing the numerator by the denominator gives the probability p_i for token i

Softmax Step by Step on a Logit Vector

Now apply the recipe to the logit vector [2, 1, 0] from the opening. The steps are the same for any vector, so this one works as a template for the process.

Logits2, 1, 0Exponentiate each7.389, 2.718, 1Sum the exponentials11.107Divide each by thesumnumerator over 11.107Probabilities0.665, 0.245, 0.090apply e^xadd togetheruse as denominatornormalize
What happens step by step when exponentials are applied to each logit and then normalized by their sum?
Worked example

Converting [2, 1, 0] into probabilities

A model with three tokens (cat, dog, fish) produces the logit vector [2, 1, 0]. Compute the probability of each token using softmax.

  1. 1
    Exponentiate each logit

    $e^{2} \approx 7.389$, $e^{1} \approx 2.718$, and $e^{0} = 1$. All three values are positive.

  2. 2
    Sum the exponentials

    The denominator is $7.389 + 2.718 + 1 \approx 11.107$.

  3. 3
    Divide each numerator by the denominator

    For cat, $7.389 / 11.107 \approx 0.665$. For dog, $2.718 / 11.107 \approx 0.245$. For fish, $1 / 11.107 \approx 0.090$.

  4. 4
    Check the two required properties

    Each value is non-negative, and $0.665 + 0.245 + 0.090 = 1.000$, so the result is a valid probability distribution.

Result

cat has probability about 0.665, dog about 0.245, and fish about 0.090. The highest logit still gives the highest probability, and the values now sum to one.

Why Negative Logits Still Work

A common surprise is that logits can be negative, while probabilities cannot. Softmax resolves this without any special rule. The exponential of any real number is positive, so a negative logit becomes a small positive number in the numerator. A negative logit therefore receives a small but nonzero probability, and the normalization step still makes the total equal one.

Worked example

Softmax on an all-negative logit vector

Apply softmax to the logit vector [-1, -2]. Both logits are negative. Do the results still form a valid probability distribution?

  1. 1
    Exponentiate each logit

    $e^{-1} \approx 0.368$ and $e^{-2} \approx 0.135$. Both are positive, even though the exponents are negative.

  2. 2
    Sum the exponentials

    The denominator is $0.368 + 0.135 \approx 0.503$.

  3. 3
    Divide each numerator by the denominator

    For the first token, $0.368 / 0.503 \approx 0.731$. For the second token, $0.135 / 0.503 \approx 0.269$.

  4. 4
    Check the total

    $0.731 + 0.269 = 1.000$, and both values are non-negative.

Result

The output is a valid pair of probabilities, about 0.731 and 0.269. The first logit, which is less negative, receives the larger probability. The sign of a logit does not decide whether it can be converted, because the conversion always produces non-negative values.

Notice the relative sizes. The logit -1 is larger than -2, and after exponentiation its value 0.368 is larger than 0.135. Exponentiation preserves ordering, so the larger logit still corresponds to the larger probability even when every logit is below zero.

Mistakes When Reading Logits

  • Common mistake

    Treating the largest logit as the probability of that token

    What it looks like

    A logit of 2 is read as meaning a 200 percent chance, or as a percentage of 2 percent.

    Why it fails

    A logit is a raw score. It ranks tokens, but it is not a probability and need not lie between zero and one.

    Do this instead

    Apply softmax first, then read the resulting value as the probability for that token.

  • Common mistake

    Assuming the raw logits should add up to one

    What it looks like

    The vector [2, 1, 0] is expected to total one because it looks like a set of percentages.

    Why it fails

    Logits need not sum to one. Their total here is 3.

    Do this instead

    Check the sum only after conversion. The softmax output for [2, 1, 0] sums to one.

  • Common mistake

    Assuming a negative logit cannot produce a probability

    What it looks like

    Seeing a logit of -1 and concluding that the token has zero or impossible probability.

    Why it fails

    The exponential of a negative number is positive, so the negative logit becomes a small positive numerator.

    Do this instead

    Exponentiate first. For -1, the numerator is about 0.368, which gives a small but nonzero probability after normalization.

  • Common mistake

    Judging a token's probability from its logit alone

    What it looks like

    Assuming that a logit of 1 always means about a 1 in 3 chance.

    Why it fails

    The probability depends on the whole vector, because the denominator sums over every token in the vocabulary.

    Do this instead

    Compare the logit against all others through the full softmax computation.

Where Sampling Fits After Softmax

Once a softmax layer has produced a probability for every token, the model still has to choose which token to output. This choice is where sampling strategies operate. The softmax layer's job ends when the distribution exists. Sampling begins with that distribution as its input.

The source explains that the right sampling strategy can make a model's responses better suited to a given application. One strategy can make the model's responses more creative, while another can make them more predictable. Many strategies have been introduced to nudge the model toward responses with specific attributes. You can also design your own, although the source notes that doing so typically requires access to the model's logits.

Practice on a New Logit Vector

MEDIUM

A model with three tokens (red, green, blue) produces the logit vector [1, 0, -1]. Apply the softmax formula to compute the probability of each token. Then check that each probability is non-negative and that the three probabilities sum to one. Which token receives the highest probability, and does the ordering of the logits match the ordering of the probabilities?

Hints
  • Start by computing e^1, e^0, and e^{-1} separately. The exponential of a negative logit is still positive.
  • Add the three exponentials to get the denominator before dividing any numerator.
  • Compare the ordering of the probabilities with the ordering of the logits. Softmax preserves the order of the values.

Key Takeaways

  • Raw logits are the model's output before probability conversion. A larger logit corresponds to a higher probability, but logits are not probabilities.
  • Logits need not sum to one and can be negative, while probabilities must be non-negative and sum to one.
  • A softmax layer computes p_i = e^{x_i} divided by the sum of e^{x_j} over all N tokens. Exponentials are always positive, so the outputs are non-negative and sum to one even when some logits are negative.
  • Sampling strategies operate after the softmax distribution has been produced, choosing the output token from those probabilities.

Related concepts

More concepts starting with V