Concepts / Logits and Softmax Transformation

Logits and Softmax Transformation

A logit vector is produced by a language model for each input, and each logit corresponds to one token in the vocabulary.

  • 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…

Reading a Model's Raw Output

When a language model is asked what should come next, its first output is a list of numbers. A common first reaction is to read each number as the chance that a particular token comes next. The numbers look like scores that rank the options, and a larger number seems to mean a more likely token. Part of that intuition is correct. The belief that the raw numbers already are probabilities, however, is a misconception. The raw numbers are called logits, and they are a different kind of quantity.

What do you think happens?

A model produces three numbers for three candidate tokens: 2.0, 1.0, and -1.0. What kind of numbers are these? Are they probabilities?

  • They are probabilities, because they rank the candidate tokens

  • They are logits, raw scores that are not yet probabilities

  • They are percentages out of 100

Reveal answer
Answer:

They are logits, raw scores that are not yet probabilities.

They rank the tokens, since a larger number signals a stronger lean toward that token. But one value is negative, and the three values do not add up to one. Probabilities must be non-negative and must sum to one, so these raw values cannot be read directly as probabilities.

Mapping Each Position to a Token

For each input the model processes, it produces a logit vector. This is simply an ordered list of numbers. Every position in that list corresponds to one token in the model's vocabulary. If the vocabulary has N tokens, the logit vector has N entries, and each entry is the model's raw score for the token at that same position. The vector is therefore a complete snapshot of the model's current leaning across the whole vocabulary, taken at one moment for one input.

Position 1logit 2.0Token 1vocabulary entryPosition 2logit 1.0Token 2vocabulary entryPosition 3logit -1.0Token 3vocabulary entryscore forscore forscore for
How does each position in the logit vector line up with one specific token, and what does a given index refer to?

Because the position of each number is tied to a specific token, the order of the list matters. Reading logit number 2 means reading the score for vocabulary token 2, not some generic value. Keeping that correspondence in mind makes the vector easy to interpret, because each number answers the question of how strongly the model leans toward one particular token. Shuffling the numbers without shuffling the tokens would attach every score to the wrong word.

Why Logits Are Not Probabilities

Logits share one feature with probabilities. A larger logit corresponds to a higher probability once the scores are converted. The resemblance ends there, because logits differ from probabilities in two specific ways. First, logits can be negative, while probabilities must be non-negative. Second, logits do not sum to one, while the probabilities across the vocabulary must total one. These two differences explain why the raw output must be transformed before it can be read as a distribution of likelihoods. The job of that transformation is performed by a softmax layer.

PropertyLogitsProbabilities
Can the value be negative?YesNo, must be non-negative
Do the values sum to one?Not required, and usually not trueYes, they must total one
Does a larger value mean a higher likelihood?Yes, in ranking termsYes, and the values give the likelihood directly
Can it be read directly as a chance?No, it must be transformed firstYes

Logits and probabilities compare on the same ranking idea but differ in allowed values and total

From Logits to Probabilities with Softmax

To convert logits to probabilities, a softmax layer is often used. Suppose the model has a vocabulary of N tokens and the logit vector is the list of values x_1 through x_N. The probability for token i, written as p_i, is computed from the logit at position i. The softmax step has two parts. It first exponentiates every logit, and then it divides each exponentiated value by the sum of all the exponentiated values.

Formula

Each part of the formula solves one of the two problems from the previous section. The exponential step turns every logit into a positive number, so a negative logit no longer produces a negative result. Dividing each result by the total of all of them forces the outputs to sum to one. Both properties are exactly what probabilities require. The formula reads the same way for every token, so the same two operations apply to the whole vocabulary.

Logit vectorraw scores x_1 to x_NExponentiate eachlogitevery result is positiveSum all exponentiatedvaluesthe normalizing totalDivide each by thesumone result per positionProbabilities p_inon-negative, sum to onestep 1step 2step 3step 4
What sequence of operations turns the raw logit vector into probabilities, and in what order do they happen?

Working Through the Three-Token Case

Worked example

Softmax on logits 2.0, 1.0, and -1.0

Convert the logit vector 2.0, 1.0, -1.0 into probabilities for three tokens. Check that the results are non-negative and sum to one.

  1. 1
    Exponentiate each logit

    Compute e raised to each logit. The value for 2.0 is about 7.389, the value for 1.0 is about 2.718, and the value for -1.0 is about 0.368. Notice that the negative logit has become a positive number.

  2. 2
    Sum the exponentiated values

    Add the three results: 7.389 plus 2.718 plus 0.368 gives about 10.475. This total is the normalizing denominator for every token.

  3. 3
    Divide each value by the sum

    For the first token, 7.389 divided by 10.475 is about 0.705. For the second token, 2.718 divided by 10.475 is about 0.259. For the third token, 0.368 divided by 10.475 is about 0.035.

  4. 4
    Check the two required properties

    All three results are non-negative. They add to about 0.999, and the small gap from exactly one comes from rounding each value to three decimal places.

Result

The probabilities are about 0.705, 0.259, and 0.035. The token with the largest logit, 2.0, receives the largest probability.

Token 1 logit2.0Token 1 probability0.705Token 2 logit1.0Token 2 probability0.259Token 3 logit-1.0Token 3 probability0.035softmaxsoftmaxsoftmax
What does the same vocabulary look like before softmax, with raw possibly negative scores, and after softmax, with non-negative values summing to one?

Read the diagram as a change in the values, not in the tokens. Each token keeps its position and its identity. The negative score of -1.0 becomes a small positive probability, and the three values now total one. The ranking survives the transformation, because 2.0 was the largest logit and 0.705 is the largest probability.

Common Misreadings of Logits

The following errors come up often when learners first meet raw model output. Each one follows from treating logits as if they were already probabilities.

  • Common mistake

    Reading a logit directly as the chance of a token

    What it looks like

    Seeing a logit of 2.0 and concluding the token has a 200 percent chance.

    Why it fails

    Logits are raw scores and are not yet probabilities, so they do not express a chance on their own.

    Do this instead

    Apply softmax to the whole logit vector first, then read the resulting probability for that position.

  • Common mistake

    Assuming a negative logit means impossible

    What it looks like

    Dismissing the token with logit -1.0 as having zero chance.

    Why it fails

    A negative logit is still a valid raw score. The exponential step in softmax turns it into a positive number.

    Do this instead

    Keep the token in the calculation and let softmax determine its share of the total.

  • Common mistake

    Expecting logits to add up to one

    What it looks like

    Checking 2.0 plus 1.0 plus -1.0 and expecting a total of one.

    Why it fails

    The source states that logits do not sum to one. Only probabilities are required to sum to one.

    Do this instead

    Check the sum only after the softmax division has produced the probabilities.

  • Common mistake

    Losing the position-to-token correspondence

    What it looks like

    Sorting the logit vector by size and then reading the result as if the tokens were still in their original order.

    Why it fails

    Each position is tied to one specific vocabulary token, so reordering the numbers attaches the scores to the wrong tokens.

    Do this instead

    Keep track of which position belongs to which token, or sort the tokens along with their scores.

Trying a Second Logit Vector

MEDIUM

A model's vocabulary has three tokens, and the logit vector is 1.0, 0.0, -1.0. Apply the softmax steps to find the three probabilities. Then state which token receives the highest probability and confirm that your three values are non-negative and sum to one.

Hints
  • Start by computing e raised to 1.0, e raised to 0.0, and e raised to -1.0 separately.
  • The value for 0.0 is the same as e raised to zero, which equals one.
  • Add the three exponentiated values to get the single total you will divide by.
  • The highest probability should belong to the position that had the highest logit.

Pulling the Steps Together

Across the example, the logit vector kept its meaning at every stage. Each position stayed tied to one vocabulary token, the raw values were allowed to be negative and to total any amount, and softmax then converted them into a valid distribution. Only after that final conversion can the numbers be read as the model's probabilities for each token.

Key Takeaways

  • A logit vector is the raw ordered output a language model produces for one input, with one entry for each token in the vocabulary.
  • Logits are not probabilities: a larger logit ranks higher, but logits can be negative and do not sum to one.
  • The softmax layer converts logits to probabilities by exponentiating each logit and dividing by the sum of all exponentiated logits.
  • The exponential step makes every value positive and the division forces the total to one, which are the two properties probabilities require.

Related concepts

More concepts starting with L