{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"softmax-and-its-gradient","title":"Softmax, its gradient, and the shift it cannot see","url":"https://addaly.com/learn/maths-you-need/softmax-and-its-gradient","position":46,"of":76,"minutes":9,"answer":"Softmax exponentiates and normalises, so only logit differences matter and a shared shift is invisible, and with cross-entropy its gradient is simply probability minus target.","keyIdea":"Softmax exponentiates and normalises, so only logit differences matter and a shared shift is invisible, and with cross-entropy its gradient is simply probability minus target, which is why training pushes hard on confident mistakes and barely at all on what is already right."},"body":{"format":"markdown","text":"## Turning scores into probabilities\n\nA classifier's last layer produces one number per class. They can be negative, they need not sum to anything, and they are called logits. Softmax turns them into probabilities:\n\n```\nsoftmax(z)_i = e^(z_i) / Σ_j e^(z_j)\n```\n\nCompute one. Logits `z = (2, 1, 0)`:\n\n```\ne^2 = 7.389,  e^1 = 2.718,  e^0 = 1.000,  sum = 11.107\np = (7.389, 2.718, 1.000) / 11.107 = (0.665, 0.245, 0.090)\n```\n\nThree properties follow from the exponential. Every output is positive, because `e^x` is. The outputs sum to 1, by construction. And larger logits give larger probabilities, monotonically.\n\n## Logits are log-probabilities up to a constant\n\nTake the log of the softmax:\n\n```\nlog p_i = z_i − log Σ_j e^(z_j)\n```\n\nThe second term is the same for every class. So the logit *is* the log-probability, shifted by a shared constant. Differences of logits are therefore logs of probability ratios: `z_1 − z_2 = 1` means `p_1 / p_2 = e = 2.718`, and in the example `p_1 / p_2 = 0.665 / 0.245 = 2.72`. A logit gap of 4.6 means one class is a hundred times more probable than the other. This is the fastest way to read a row of logits.\n\n## The shift it cannot see\n\nAdd the same number to every logit and nothing changes. `(102, 101, 100)` gives exactly the same probabilities as `(2, 1, 0)`, because `e^(z+c) = e^c × e^z` and the `e^c` cancels between numerator and denominator.\n\nTwo consequences. First, a model's logits have no absolute meaning; only their differences do, so a logit of 12 is not \"confident\" unless you know the others. Second, you may subtract anything you like before exponentiating, and the next lesson shows why every library subtracts the maximum.\n\n## Temperature\n\nDividing the logits by a temperature `T` before softmax is the same operation as scaling the gaps. With `T = 2` the example becomes `(1, 0.5, 0)` and the output `(0.506, 0.307, 0.186)`: flatter. With `T = 0.5` it becomes `(4, 2, 0)` and the output `(0.867, 0.117, 0.016)`: sharper. As `T` goes to zero the largest logit takes everything; as it goes to infinity the output goes uniform. The course `how-llms-work` covers what that does to generated text. The arithmetic is only this: temperature rescales the logit differences, and a difference of `d` at temperature `T` is a probability ratio of `e^(d/T)`.\n\n## The gradient, and why it is worth seeing\n\nPair softmax with cross-entropy, so the loss is `−log p_correct`, and differentiate with respect to the logits. Several cancellations happen and the result is remarkably clean:\n\n```\n∂L / ∂z_i = p_i − y_i\n```\n\nwhere `y` is the one-hot target. For the example, with the correct class first:\n\n```\ngradient = (0.665 − 1, 0.245 − 0, 0.090 − 0) = (−0.335, 0.245, 0.090)\n```\n\nThe correct logit is pushed up by 0.335, the others are pushed down by their own probability, and the three numbers sum to zero, as they always do. The size of the push is the size of the mistake. If the model had assigned 0.01 to the correct class the push would be 0.99, nearly the maximum; if it had assigned 0.999 the push would be 0.001. **Cross-entropy trains hard on what it gets wrong and barely at all on what it already knows.** That is the mechanism behind the shape of most loss curves: fast early, when everything is wrong, and slow late, when the remaining errors are few and the gradient on each is small.\n\nIt is also why the derivative has the same form as in linear regression, `prediction − target`, as the previous lesson promised. The exponential in the softmax and the log in the loss undo each other exactly.\n\n## Where it fails\n\n**Saturation.** When one logit exceeds the rest by 20, its probability is `1 − 2 × 10^-9`. If that class is wrong, the gradient on the correct logit is essentially 1 and on the wrong one essentially −1, which sounds healthy, but the *input* to the softmax is far from any region where small changes matter, and it can take many steps to climb back. Confident mistakes are slow to unlearn. Label smoothing and clipping of logits both exist to keep the model out of that region.\n\n**Forced choice.** Softmax insists the classes are exclusive and exhaustive. Add an irrelevant fourth class and every other probability shrinks to make room. If an example can belong to several classes at once, or to none, the right tool is one sigmoid per class, not a softmax.\n\n## Five lines of NumPy\n\n```python\nimport numpy as np\ndef softmax(z):\n    z = z - z.max()               # the shift it cannot see, used on purpose\n    e = np.exp(z)\n    return e / e.sum()\np = softmax(np.array([2.0, 1.0, 0.0]))\ngrad = p - np.array([1, 0, 0])   # (-0.335, 0.245, 0.090)\n```\n\nRun it once, change a logit, and watch the gradient move. The whole of classification training is that last line, repeated."},"check":{"question":"Two logits are 3.0 and 1.0, with the rest far below. The model then adds 50 to every logit. What happens to the probability of the first class?","options":[{"text":"Nothing: only differences between logits matter.","correct":true},{"text":"It rises to nearly 1, because a logit of 53 is enormous.","correct":false,"tempting":"Reads a logit as an absolute score; the shared 50 cancels between numerator and denominator, and the ratio stays e² ≈ 7.4."},{"text":"It becomes NaN, because exponentiating 53 overflows.","correct":false,"tempting":"e^53 is about 10^23, far below the float32 limit near 10^38; overflow needs logits above roughly 88, and the max-subtraction trick removes even that."},{"text":"It falls, because the shift dilutes every class equally.","correct":false,"tempting":"Invents a mechanism; a constant shift multiplies every exponential by the same factor, which the normalisation removes."}]},"prev":{"slug":"maximum-likelihood-and-where-losses-come-from","title":"Loss functions are not arbitrary: maximum likelihood","url":"https://addaly.com/learn/maths-you-need/maximum-likelihood-and-where-losses-come-from"},"next":{"slug":"log-sum-exp-and-stable-softmax","title":"The log-sum-exp trick, and why exp(1000) is not a number","url":"https://addaly.com/learn/maths-you-need/log-sum-exp-and-stable-softmax"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Softmax, its gradient, and the shift it cannot see', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/softmax-and-its-gradient (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Softmax, its gradient, and the shift it cannot see","courseTitle":"The Maths You Actually Need","module":{"index":5,"slug":"logs-information-and-loss","title":"Logarithms, information and the shape of a loss"},"answer":null,"keyIdea":"Softmax exponentiates and normalises, so only logit differences matter and a shared shift is invisible, and with cross-entropy its gradient is simply probability minus target, which is why training pushes hard on confident mistakes and barely at all on what is already right.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"turning-scores-into-probabilities","runs":[{"text":"Turning scores into probabilities"}]},{"t":"p","runs":[{"text":"A classifier’s last layer produces one number per class. They can be negative, they need not sum to anything, and they are called logits. Softmax turns them into probabilities:"}]},{"t":"code","lang":"","label":"Text","text":"softmax(z)_i = e^(z_i) / Σ_j e^(z_j)"},{"t":"p","runs":[{"text":"Compute one. Logits "},{"text":"z = (2, 1, 0)","code":true},{"text":":"}]},{"t":"code","lang":"","label":"Text","text":"e^2 = 7.389,  e^1 = 2.718,  e^0 = 1.000,  sum = 11.107\np = (7.389, 2.718, 1.000) / 11.107 = (0.665, 0.245, 0.090)"},{"t":"p","runs":[{"text":"Three properties follow from the exponential. Every output is positive, because "},{"text":"e^x","code":true},{"text":" is. The outputs sum to 1, by construction. And larger logits give larger probabilities, monotonically."}]},{"t":"h2","id":"logits-are-log-probabilities-up-to-a-constant","runs":[{"text":"Logits are log-probabilities up to a constant"}]},{"t":"p","runs":[{"text":"Take the log of the softmax:"}]},{"t":"code","lang":"","label":"Text","text":"log p_i = z_i − log Σ_j e^(z_j)"},{"t":"p","runs":[{"text":"The second term is the same for every class. So the logit "},{"text":"is","i":true},{"text":" the log-probability, shifted by a shared constant. Differences of logits are therefore logs of probability ratios: "},{"text":"z_1 − z_2 = 1","code":true},{"text":" means "},{"text":"p_1 / p_2 = e = 2.718","code":true},{"text":", and in the example "},{"text":"p_1 / p_2 = 0.665 / 0.245 = 2.72","code":true},{"text":". A logit gap of 4.6 means one class is a hundred times more probable than the other. This is the fastest way to read a row of logits."}]},{"t":"h2","id":"the-shift-it-cannot-see","runs":[{"text":"The shift it cannot see"}]},{"t":"p","runs":[{"text":"Add the same number to every logit and nothing changes. "},{"text":"(102, 101, 100)","code":true},{"text":" gives exactly the same probabilities as "},{"text":"(2, 1, 0)","code":true},{"text":", because "},{"text":"e^(z+c) = e^c × e^z","code":true},{"text":" and the "},{"text":"e^c","code":true},{"text":" cancels between numerator and denominator."}]},{"t":"p","runs":[{"text":"Two consequences. First, a model’s logits have no absolute meaning; only their differences do, so a logit of 12 is not “confident” unless you know the others. Second, you may subtract anything you like before exponentiating, and the next lesson shows why every library subtracts the maximum."}]},{"t":"h2","id":"temperature","runs":[{"text":"Temperature"}]},{"t":"p","runs":[{"text":"Dividing the logits by a temperature "},{"text":"T","code":true},{"text":" before softmax is the same operation as scaling the gaps. With "},{"text":"T = 2","code":true},{"text":" the example becomes "},{"text":"(1, 0.5, 0)","code":true},{"text":" and the output "},{"text":"(0.506, 0.307, 0.186)","code":true},{"text":": flatter. With "},{"text":"T = 0.5","code":true},{"text":" it becomes "},{"text":"(4, 2, 0)","code":true},{"text":" and the output "},{"text":"(0.867, 0.117, 0.016)","code":true},{"text":": sharper. As "},{"text":"T","code":true},{"text":" goes to zero the largest logit takes everything; as it goes to infinity the output goes uniform. The course "},{"text":"how-llms-work","code":true},{"text":" covers what that does to generated text. The arithmetic is only this: temperature rescales the logit differences, and a difference of "},{"text":"d","code":true},{"text":" at temperature "},{"text":"T","code":true},{"text":" is a probability ratio of "},{"text":"e^(d/T)","code":true},{"text":"."}]},{"t":"h2","id":"the-gradient-and-why-it-is-worth-seeing","runs":[{"text":"The gradient, and why it is worth seeing"}]},{"t":"p","runs":[{"text":"Pair softmax with cross-entropy, so the loss is "},{"text":"−log p_correct","code":true},{"text":", and differentiate with respect to the logits. Several cancellations happen and the result is remarkably clean:"}]},{"t":"code","lang":"","label":"Text","text":"∂L / ∂z_i = p_i − y_i"},{"t":"p","runs":[{"text":"where "},{"text":"y","code":true},{"text":" is the one-hot target. For the example, with the correct class first:"}]},{"t":"code","lang":"","label":"Text","text":"gradient = (0.665 − 1, 0.245 − 0, 0.090 − 0) = (−0.335, 0.245, 0.090)"},{"t":"p","runs":[{"text":"The correct logit is pushed up by 0.335, the others are pushed down by their own probability, and the three numbers sum to zero, as they always do. The size of the push is the size of the mistake. If the model had assigned 0.01 to the correct class the push would be 0.99, nearly the maximum; if it had assigned 0.999 the push would be 0.001. "},{"text":"Cross-entropy trains hard on what it gets wrong and barely at all on what it already knows.","b":true},{"text":" That is the mechanism behind the shape of most loss curves: fast early, when everything is wrong, and slow late, when the remaining errors are few and the gradient on each is small."}]},{"t":"p","runs":[{"text":"It is also why the derivative has the same form as in linear regression, "},{"text":"prediction − target","code":true},{"text":", as the previous lesson promised. The exponential in the softmax and the log in the loss undo each other exactly."}]},{"t":"h2","id":"where-it-fails","runs":[{"text":"Where it fails"}]},{"t":"p","runs":[{"text":"Saturation.","b":true},{"text":" When one logit exceeds the rest by 20, its probability is "},{"text":"1 − 2 × 10^-9","code":true},{"text":". If that class is wrong, the gradient on the correct logit is essentially 1 and on the wrong one essentially −1, which sounds healthy, but the "},{"text":"input","i":true},{"text":" to the softmax is far from any region where small changes matter, and it can take many steps to climb back. Confident mistakes are slow to unlearn. Label smoothing and clipping of logits both exist to keep the model out of that region."}]},{"t":"p","runs":[{"text":"Forced choice.","b":true},{"text":" Softmax insists the classes are exclusive and exhaustive. Add an irrelevant fourth class and every other probability shrinks to make room. If an example can belong to several classes at once, or to none, the right tool is one sigmoid per class, not a softmax."}]},{"t":"h2","id":"five-lines-of-numpy","runs":[{"text":"Five lines of NumPy"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\ndef softmax(z):\n    z = z - z.max()               # the shift it cannot see, used on purpose\n    e = np.exp(z)\n    return e / e.sum()\np = softmax(np.array([2.0, 1.0, 0.0]))\ngrad = p - np.array([1, 0, 0])   # (-0.335, 0.245, 0.090)"},{"t":"p","runs":[{"text":"Run it once, change a logit, and watch the gradient move. The whole of classification training is that last line, repeated."}]}],"check":{"question":[{"text":"Two logits are 3.0 and 1.0, with the rest far below. The model then adds 50 to every logit. What happens to the probability of the first class?"}],"options":[{"text":[{"text":"Nothing: only differences between logits matter."}],"correct":true,"tempting":null},{"text":[{"text":"It rises to nearly 1, because a logit of 53 is enormous."}],"correct":false,"tempting":[{"text":"Reads a logit as an absolute score; the shared 50 cancels between numerator and denominator, and the ratio stays e² ≈ 7.4."}]},{"text":[{"text":"It becomes NaN, because exponentiating 53 overflows."}],"correct":false,"tempting":[{"text":"e^53 is about 10^23, far below the float32 limit near 10^38; overflow needs logits above roughly 88, and the max-subtraction trick removes even that."}]},{"text":[{"text":"It falls, because the shift dilutes every class equally."}],"correct":false,"tempting":[{"text":"Invents a mechanism; a constant shift multiplies every exponential by the same factor, which the normalisation removes."}]}],"order":[2,1,0,3],"why":null},"prev":{"slug":"maximum-likelihood-and-where-losses-come-from","title":"Loss functions are not arbitrary: maximum likelihood","minutes":10},"next":{"slug":"log-sum-exp-and-stable-softmax","title":"The log-sum-exp trick, and why exp(1000) is not a number","minutes":9}}}