Softmax and Language Models
Softmax turns a list of scores into probabilities; a language model uses it to predict the next token, trained by cross-entropy.
Needs a step first: 39
Softmax maps scores to : all positive, summing to 1, and keeping the order of the scores. It is the Boltzmann distribution with the scores as negative energies, and dividing them by a temperature makes the choice sharper () or flatter (). A language model reads the text so far, produces one score per word piece in its vocabulary, and softmax gives the probability of each next token; generating text is sampling one, appending it and repeating, a Markov chain on growing context. Training minimises the cross-entropy of the true next token, and attention uses the same softmax to weigh tokens.
Where it shows up: Softmax is inside every attention head, and it is the output layer of every language model.
How to hold it: In your head
See softmax as turning scores into probabilities that keep their order, and temperature as the dial between always taking the top score and choosing almost at random.
What knowing it looks like
- Compute the softmax of three logits and the cross-entropy loss for a given correct token.
- Show what raising or lowering the temperature does to the probabilities.
- Explain why subtracting the largest logit before exponentiating changes nothing but avoids overflow.
Test yourself
A model gives logits for three possible next tokens. Find the softmax probabilities, the cross-entropy loss in nats if the true next token is the second one, and the probabilities at temperature . Use , , .
Show the worked answer
Needs a step first: 39
Learn it with an AI
Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.
Be my maths tutor. I'm learning "Softmax and Language Models" (Frontier tier): Softmax turns a list of scores into probabilities; a language model uses it to predict the next token, trained by cross-entropy.
I already know: Cross-Entropy and KL Divergence, Neural Networks, Markov Chains.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: A model gives logits $(2, 1, 0)$ for three possible next tokens. Find the softmax probabilities, the cross-entropy loss in nats if the true next token is the second one, and the probabilities at temperature $T = 2$. Use $e^2 \approx 7.389$, $e \approx 2.718$, $e^{0.5} \approx 1.649$.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.Needs first
- Cross-Entropy and KL DivergenceCross-entropy measures how many bits a model needs to describe data really drawn from ; KL divergence is the excess over the minimum.Undergraduate II · Optimisation & Information
- Neural NetworksLayers of simple units, each a weighted sum passed through a nonlinear function, composed into a function that can fit almost anything.Frontier · Machine Learning
- Markov ChainsA random process hopping between states, where the next step depends only on the current state and not on how it got there.Undergraduate II · Probability & Statistics
Leads to
On the way to
Recommended
- Neural Networks: Zero to HeroAndrej Karpathy · VideosFree lectures that build backpropagation, a language model and a small GPT from scratch in Python, explaining every line.
- Understanding Deep LearningSimon J. D. Prince (MIT Press) · BookA clear, well-illustrated 2023 textbook covering networks, training, transformers, diffusion models and reinforcement learning, free as a PDF with Python notebooks.
- 动手学深度学习阿斯顿·张、李沐等 · Book · 中文The Chinese edition of Dive into Deep Learning: a free textbook in which every model, from linear regression to transformers, runs as code beside the maths.
- 跟李沐学AI李沐 · Videos · 中文Mu Li's own Bilibili channel: the video course for Dive into Deep Learning and paragraph-by-paragraph readings of key papers, including the Transformer paper.