The mathematical chain of LLM training
From logits to next-token probabilities, loss, and gradients
This page appears at more than one point in the learning path : The mathematical chain of LLM training.
-
08 · Score observed tokens Optional Stage 8/9
Evaluate the probability assigned to a known target, build a sequence likelihood, and connect negative log-likelihood to cross-entropy without selecting a token or changing parameters.
-
09 · Learn and update parameters Optional Stage 9/9
Connect observed targets to the aggregated loss, gradients, and parameter updates, then to the repeated training loop and validation loss; understand teacher forcing conceptually without making shifted-example preparation a stage of the Theory path.
An autoregressive model produces logits for the next token. Softmax turns them into probabilities; the observed target defines a likelihood and a loss; differentiating that loss gives gradients used to update the parameters. These concepts are connected, but they are not interchangeable.
Practice: Mathematical foundations exercises to work through the logits → loss → gradients chain.
The chain at a glance
- Logits — unnormalized real-valued scores produced by the model.
- Probabilities — a distribution over the vocabulary obtained with softmax.
- Likelihood — the probability assigned to the observed target tokens, or its logarithm.
- Cross-entropy — the probabilistic cost assigned to an observed target token by the predicted distribution; compare Shannon entropy, which describes uncertainty in the predicted distribution.
- Loss function — the scalar objective aggregated over selected tokens and examples.
- Gradients — derivatives describing how the loss locally changes with logits and parameters.
What these equations imply — and what they do not
- “Logits are probabilities.” False: logits can be negative, positive, and unbounded. Adding the same constant to every logit leaves softmax unchanged.
- “A high probability proves the model is right.” False: it is the model’s relative confidence, not a guarantee of truth or correct calibration.
- “Cross-entropy and loss always mean exactly the same thing.” False: cross-entropy may be a per-token loss; the training objective may average it, add regularization, or combine multiple objectives.
- “Maximizing likelihood differs from minimizing loss.” For fixed data and conventions, negative log-likelihood is the negative log-likelihood; minimizing one is equivalent to maximizing the other.
- “A negative gradient means a parameter is bad.” False: its sign describes a local change with respect to one coordinate under a given convention, not an intrinsic quality.
How to use this path
Each concept page derives one step, gives a calculable example, and states its assumptions. The links do not imply mastery: derivations should be reproduced and tested against edge cases.
- Start with logits, then probabilities.
- Connect observed probabilities to likelihood and cross-entropy.
- Distinguish per-token loss from the aggregated loss function.
- Finish with gradients, the chain rule, and numerical verification.
- For a detailed probabilistic treatment — autoregressive factorization, maximum likelihood, soft targets, and KL divergence — see the mathematics of language modeling.
Foundational references
- Goodfellow, Bengio & Courville, Deep Learning, Chapter 6 — Deep Feedforward Networks, for cost functions and optimization.
- Bishop, Pattern Recognition and Machine Learning, for probabilities, likelihood, and estimation.
- PyTorch — CrossEntropyLoss, which specifies logits, targets, and reduction conventions.
Canonical lesson: LLM overview. This mathematical synthesis connects several concepts in the learning route; it is a cross-reference, not a prerequisite.