Softmax and Logits
Softmax turns a list of raw scores into probabilities: exponentiate each score so it's positive, then divide by the total so they add to 1. Only the differences between scores matter, a temperature controls how confident the result looks, and paired with cross-entropy its gradient is simply prediction minus truth, .
What the symbols mean
- Logit
The raw score the network gives class i. Any real number, negative included.
- Probability
The model's probability for class i. Always positive, and all K of them add to 1.
- Number of classes
How many options the model chooses between.
What the symbols mean
- Temperature
Divides every logit. Small T sharpens toward the top class; large T flattens toward uniform.
What the symbols mean
- Cross-entropy loss
L = −ln p_c, minus the log of the probability given to the correct class c.
- Target
1 for the correct class and 0 for every other class (a one-hot vector).
Explanation
Softmax is simply the function that turns a list of raw scores into a list of probabilities. A classifier's last layer produces one number per class, called a logit: "cat" might get 2.0, "dog" 1.0 and "fox" 0.1. Those can be any real numbers, negative included, and they don't add up to anything in particular. Softmax fixes both problems in two moves: (make every score positive while keeping their order) and divide by the sum (scale them so they add to 1). The name is honest, too: it's a soft version of picking the max. A hard max would give the top class probability 1 and everyone else 0; softmax gives most of the probability to the top score but keeps some for the others. That's the whole function, exponentiate and then normalize, so don't let the sigma notation scare you.
Why the exponential? You could imagine just dividing each score by the total. That breaks the moment a score is negative or the scores add to 0. The exponential is always positive, and it does something nicer: it turns differences of logits into ratios of probabilities. Divide two softmax outputs and the shared denominator cancels:
So a logit that's 1 higher means odds times better, and a logit that's 2 higher means about times better. That's where the word logit comes from: with two classes, , the log of the odds, the same log-odds you met in logistic regression. In fact two-class softmax is the sigmoid from Activation Functions. Divide the top and bottom by :
Only differences matter. Add the same constant to every logit and nothing changes, because the factor comes out of the top and the bottom and cancels:
This matters in real code. overflows a 64-bit float (the largest one is about , which is ), so every serious softmax subtracts the largest logit first. The scores become , every exponential is at most 1, and the answer is exactly the same.
Temperature. Dividing every logit by a number before the softmax changes how sure the model sounds. Think about what that does to the gaps: dividing by doubles every gap, and since gaps turn into ratios through , the top class pulls far ahead. As the distribution collapses onto the top class, the hard max. Dividing by a large shrinks every gap toward 0, and as every class gets . Language models use exactly this knob when they sample the next word: low temperature for predictable text, high temperature for variety. There's a picture further down where you can slide and watch the bars reshape.
A common mistake. Since adding a constant to every logit changes nothing, it's tempting to think multiplying every logit by a constant changes nothing either. It does change things: multiplying by 2 doubles every gap, which is the same as setting , and the distribution gets sharper. Only shifts are free. The other classic slip is computing "softmax" of as by dividing by the sum of the raw scores. Softmax exponentiates first, and the real answer is about .
The gradient is prediction minus truth. Softmax is almost always followed by the cross-entropy loss , minus the log of the probability given to the correct class (you'll see why this is the right loss in Maximum Likelihood and Cross-Entropy). Write out with the softmax formula and use :
Now differentiate with respect to one logit . The first term gives if and otherwise, which is exactly . The second term, by the chain rule, gives . Put them together:
Think about how clean that is. All the exponentials and logs cancel, and what's left is "what the model said minus what was true". If the model gave the correct class 0.7, its gradient is , and since gradient descent steps against the gradient, that logit goes up. Every wrong class gets pushed down by exactly its own probability. This one subtraction is where backpropagation starts in every classifier (Backpropagation Through One Neuron carries it further back).
See it
Worked examples
A classifier outputs the logits for cat, dog and fox. Find the softmax probabilities, then check that they add to 1.
- 1
Softmax has two moves: exponentiate each logit, then divide each one by the total. Start with the first move, one logit at a time.
For the same logits , the correct answer is actually dog. Find the cross-entropy loss and its gradient with respect to each logit. Then take one gradient descent step on the logits with learning rate and see what happens to the dog probability.
- 1
Write the target as a one-hot vector. Dog is the second class, so .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A classifier's logits for three classes are . What probability does softmax give the first class? Give 4 decimal places.
A program computes the softmax of the logits and gets NaN, because overflows. What is the correct probability of the first class? Give 4 decimal places.
An image model's logit for "cat" is 1.5 higher than its logit for "dog". How many times more probable does it think cat is than dog? Give 4 decimal places.
A language model is choosing the next word between two candidates with logits 4.0 and 2.0. With sampling temperature , what is the probability that it picks the first word? Give 4 decimal places.
A 3-class model outputs , and the correct class is the third one. Find the gradient of the cross-entropy loss with respect to the logits .
What happens to a softmax distribution if every logit is multiplied by 3?
The last layer of a small network computes logits from a single hidden value , with weights and no biases. The correct class is the second one. Using and the chain rule, take one gradient descent step with . What is the new value of ? Give 4 decimal places.
The hardest one here. Get it, and you've got this lesson.
A classifier outputs the logits . Its developers want the top class to get probability exactly by changing the temperature . What should they use? Give 4 decimal places.
Where it's used
Every image classifier, spam filter and language model ends in a softmax. When a chatbot writes, it computes one logit for each of tens of thousands of possible next tokens, divides them by the temperature, applies softmax and samples from the result; "temperature 0" in an API means always taking the top token. The gradient is also why the output layer is cheap to train: the error signal for each class is a single subtraction.