Probability, softmax and next-token prediction
Turning scores into probabilities, and probabilities into text
Chapter 4 of 21 · I. Foundations · about 30 minutes. Builds on: 1. Functions, parameters and gradient descent.
Probability distributions over a finite set
Section titled “Probability distributions over a finite set”A language model’s output is a probability distribution over its vocabulary: a vector with and . The network itself produces unconstrained real numbers (called logits), one per vocabulary entry. The softmax function turns them into probabilities:
Properties worth knowing:
- Adding the same constant to every logit changes nothing, since the constant cancels. Implementations subtract before exponentiating to avoid overflow.
- It is monotonic: the largest logit gets the largest probability.
- Scaling the logits by before softmax sharpens ( < 1) or flattens (T > 1) the distribution. is called temperature.
Cross-entropy: the training loss
Section titled “Cross-entropy: the training loss”If the true next token has index , the negative log-likelihood (also called cross-entropy loss for a one-hot target) is
It is 0 when the model gives the right token probability 1 and grows without bound as that probability approaches 0. Differentiate with respect to a logit :
That is the whole gradient: the predicted probability minus a one-hot vector for the target. Logits of wrong tokens are pushed down in proportion to how much probability they hold; the right token’s logit is pushed up. Play with it:
Information-theoretic reading
Section titled “Information-theoretic reading”The entropy of a distribution is log , the average surprise. The cross-entropy between the data distribution and the model is log . It decomposes as
so minimizing cross-entropy drives the model toward the data distribution, and the KL divergence measures the remaining gap. KL reappears in Chapter 17: preference-tuning methods use it to keep the tuned model close to a reference model.
Language modeling is next-token prediction
Section titled “Language modeling is next-token prediction”Any sequence of tokens , …, has a probability that factors exactly by the chain rule of probability:
A language model is a function that, given the prefix, outputs over the vocabulary. Training minimizes the average negative log-likelihood of the real next token at every position, over huge amounts of text. Next-token prediction is the main training signal in pretraining; a few models add small auxiliary objectives (Chapter 9).
Perplexity
Section titled “Perplexity”If the average per-token loss is ℓ (in nats), the perplexity is . A perplexity of 8 means the model is, on average, as uncertain as if it were choosing uniformly among 8 equally likely tokens. Lower is better, and it is only comparable between models using the same tokenizer and text. Bits per byte is a tokenizer-independent alternative.
Turning a distribution into text: decoding
Section titled “Turning a distribution into text: decoding”- Greedy: always take the argmax. Deterministic but repetitive.
- Sampling with temperature: sample from .
- Top-k : keep only the most likely tokens, renormalize, sample.
- Top-p (nucleus) : keep the smallest set of tokens whose cumulative probability reaches , renormalize, sample. The set size adapts to how confident the model is.
Notice that the training loss uses and the full distribution; sampling controls are applied only at generation time.
Check yourself
Section titled “Check yourself”Derive ∂L/∂z for softmax cross-entropy.
. The first term differentiates to ; the second to . Sum: .
A model assigns probability 0.25 to the correct token at every position. What are the loss and the perplexity?
Loss = −ln nats; perplexity = .
If temperature is raised, does the model get "smarter" or "more random"?
More random: the distribution flattens toward uniform. The model’s weights and its ranking of tokens are unchanged.
Papers
Section titled “Papers”- Why pure likelihood decoding degenerates and the case for nucleus sampling.
- Top-k sampling.
- Chapter 3 (probability and information theory).
- Chapter 5 (loss functions).