Advanced Mathematics

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 ziz_i to pi=ezi/∑jezjp_i = e^{z_i}/\sum_j e^{z_j}: 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 TT makes the choice sharper (T<1T < 1) or flatter (T>1T > 1). 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 (2,1,0)(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=2T = 2. Use e2≈7.389e^2 \approx 7.389, e≈2.718e \approx 2.718, e0.5≈1.649e^{0.5} \approx 1.649.
Show the worked answer
Weights 7.389,2.718,17.389, 2.718, 1, sum 11.10711.107: p≈(0.665,0.245,0.090)p \approx (0.665, 0.245, 0.090). Loss =−ln⁡0.245≈1.41= -\ln 0.245 \approx 1.41 nats. At T=2T = 2 the logits become (1,0.5,0)(1, 0.5, 0), weights 2.718,1.649,12.718, 1.649, 1, sum 5.3675.367: p≈(0.506,0.307,0.186)p \approx (0.506, 0.307, 0.186), flatter than before.

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.

On the way to

Recommended