Skip to content

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 pp with pi≥0p_{i} \ge 0 and ∑pi=1\sum p_{i} = 1. The network itself produces unconstrained real numbers zz (called logits), one per vocabulary entry. The softmax function turns them into probabilities:

pi=ezi∑jezjp_i=\frac{e^{z_i}}{\sum_j e^{z_j}}

Properties worth knowing:

  • Adding the same constant to every logit changes nothing, since the constant cancels. Implementations subtract max⁡(z)\max(z) before exponentiating to avoid overflow.
  • It is monotonic: the largest logit gets the largest probability.
  • Scaling the logits by 1/T1 / T before softmax sharpens (TT < 1) or flattens (T > 1) the distribution. TT is called temperature.

If the true next token has index tt, the negative log-likelihood (also called cross-entropy loss for a one-hot target) is

L=−log⁡pt=−zt+log⁡∑jezjL=-\log p_t=-z_t+\log\sum_j e^{z_j}

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 ziz_{i}:

∂L∂zi=pi−1[i=t]\frac{\partial L}{\partial z_i}=p_i-\mathbb 1[i=t]

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:

The entropy of a distribution qq is H(q)=−∑qiH(q) = -\sum q_{i} log qiq_{i}, the average surprise. The cross-entropy between the data distribution qq and the model pp is H(q,p)=−∑qiH(q, p) = -\sum q_{i} log pip_{i}. It decomposes as

H(q,p)=H(q)+KL(q ∥ p),KL(q∥p)=∑iqilog⁡qipi ≥0H(q,p)=H(q)+\mathrm{KL}(q\,\Vert\,p),\qquad\mathrm{KL}(q\Vert p)=\sum_i q_i\log\frac{q_i}{p_i}\ \ge 0

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 x1x_{1}, …, xnx_{n} has a probability that factors exactly by the chain rule of probability:

P(x1,…,xn)=∏t=1nP(xt∣x1,…,xt−1)P(x_1,\dots,x_n)=\prod_{t=1}^{n}P\bigl(x_t\mid x_1,\dots,x_{t-1}\bigr)

A language model is a function that, given the prefix, outputs P(xt∣prefix)P(x_{t} | prefix) 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).

If the average per-token loss is ℓ (in nats), the perplexity is exp⁡(ℓ)\exp(ℓ). 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 softmax⁡(z/T)\operatorname{softmax}(z / T).
  • Top-k : keep only the kk most likely tokens, renormalize, sample.
  • Top-p (nucleus) : keep the smallest set of tokens whose cumulative probability reaches pp, renormalize, sample. The set size adapts to how confident the model is.

Notice that the training loss uses T=1T = 1 and the full distribution; sampling controls are applied only at generation time.

Derive ∂L/∂z for softmax cross-entropy.

L=−zt+log⁡∑jezjL=-z_t+\log\sum_j e^{z_j}. The first term differentiates to −1[i=t]-\mathbb 1[i=t]; the second to ezi/∑jezj=pie^{z_i}/\sum_j e^{z_j}=p_i. Sum: pi−1[i=t]p_i-\mathbb 1[i=t].

A model assigns probability 0.25 to the correct token at every position. What are the loss and the perplexity?

Loss = −ln 0.25≈1.3860.25 \approx 1.386 nats; perplexity = e1.386=4e^{1.386} = 4.

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.

  • Why pure likelihood decoding degenerates and the case for nucleus sampling.
  • Top-k sampling.
  • Chapter 3 (probability and information theory).
  • Chapter 5 (loss functions).