Ten problems on softmax: its partial derivatives, the Jacobian diag(s) − ssᵀ and its rank, the s − y gradient of cross-entropy, log-softmax, soft labels, temperature and the batched version, with worked solutions and the mistakes that produce the wrong gradient.
Before you start
Softmax turns a vector of scores into probabilities, and cross-entropy compares those probabilities with a target. The gradient of the pair with respect to the scores is the probabilities minus the target: short enough to memorise, and easy to misuse. These ten problems derive it three ways, work out the full Jacobian of the softmax on the way, and extend the result to batches, soft labels and temperature. The five mistakes at the end are the places the derivation goes wrong: a Jacobian treated as diagonal, a chain rule stopped one step early, a missing ,1/N, a formula used outside its hypothesis, and logits read as if they were unique.
The conventions are those of the previous pages: vectors are columns, Jacobians are in numerator layout (∂y/∂x is m×n for ,y∈Rm,),x∈Rn), the gradient of a scalar is the transpose of its row derivative, a matrix gradient has the shape of the matrix, and for a scalar L of u with u a function of ,x,.∇xL=(∂u/∂x)⊤∇uL.
The logits are ,z∈Rn, one per class, and s=softmax(z) has entries .si=ezi/∑kezk. Every si>0 and .∑isi=1.
1 is the all-ones vector, so 1⊤v=∑ivi and .1⊤s=s⊤1=1.
δij is the Kronecker delta: 1 if i=j and 0 otherwise, so δij is entry (i,j) of ,I, the identity matrix.
The cross-entropy loss is L=−∑iyilogsi (natural log). Unless a problem says otherwise, y is one-hot: one entry is ,1, at the target class, and the rest are .0.
Batched: Z∈RN×n has one example per row, S=softmax(Z) is taken row by row, Y holds the one-hot targets as rows, and the loss is the mean L=N1∑kLk of the per-row losses.
diag(v) and the elementwise product ⊙ are as on the previous page; y/s is elementwise division, .(y/s)i=yi/si.
These results are the backward pass of the last layer of every classifier trained with cross-entropy.
Show that softmax(z+c1)=softmax(z) for any scalar .c. Why does an implementation subtract maxkzk first?
··
Compute ,∂si/∂zj, treating i=j and i=j separately, then write both cases as one formula.
··
Write ∂s/∂z as a matrix. Show it is symmetric and that .(∂s/∂z)1=0.
···
What is the rank of ∂s/∂z when every ?si>0? Describe its null space, and say what that means for how the logits determine the probabilities.
··
Let L=−∑iyilogsi with y one-hot. Compute .∇zL.
··
Let ,ℓ=logsoftmax(z), i.e. .ℓi=zi−log∑kezk. Compute .∂ℓ/∂z. Use it to redo Problem 5 in one line.
···
Batched: ,Z∈RN×n,S=softmax(Z) row-wise, Y one-hot rows, .L=−N1∑k,iYkilogSki. Compute ∇ZL and state its shape.
··
Repeat Problem 5 for soft labels y with ,yi≥0,.∑iyi=1. What changes if ?∑iyi=1?
···
With temperature, .s=softmax(z/T). Compute .∂s/∂z. What happens as T→∞ and as ?T→0+?
···
Compute ∇sL for ,L=−∑iyilogsi, then apply the chain rule through the Jacobian of Problem 3 and show you recover the answer to Problem 5.
Answers
;softmax(z+c1)=softmax(z); subtracting maxkzk keeps every exponent ,≤0, so nothing overflows and the largest term is exactly 1
∂si/∂zj=si(δij−sj)
;∂s/∂z=diag(s)−ss⊤; symmetric because both terms are; (diag(s)−ss⊤)1=s−s(s⊤1)=s−s=0
;rank=n−1; null space :span{1}: logits are determined by the probabilities only up to a common shift, which is Problem 1 again
∇zL=s−y
;∂ℓ/∂z=I−1s⊤; then ∇zL=−(∂ℓ/∂z)⊤y=−(y−s1⊤y)=s−y
,∇ZL=N1(S−Y),N×n
,∇zL=(∑iyi)s−y, which is s−y when the labels sum to 1
∂s/∂z=T1(diag(s)−ss⊤) with ;s=softmax(z/T); as ,T→∞,s→1/n and the Jacobian ;→0; as ,T→0+, when the largest logit is unique the softmax approaches a hard argmax and the Jacobian ,→0, while at a tie for the largest logit entries of the Jacobian grow without bound
,∇zL=J⊤∇sL=s−y, the same as Problem 5
Worked solutions
Problem 1
Show that softmax(z+c1)=softmax(z) for any scalar .c. Why does an implementation subtract maxkzk first?
.softmax(z+c1)i=∑kezk+cezi+c.Adding c1 adds the same c to every logit: the one in the numerator and every one in the sum.
.=ec∑kezkecezi.,ea+b=eaeb, and ec is the same factor in every term of the sum, so it comes out of the sum.
.=∑kezkezi=si.,ec>0, so it cancels. This holds for every i and every real .c.
Take .c=−maxkzk. Then every exponent zk−maxkzk is ,≤0, and it is 0 for the largest logit.Step 3 allows any ,c, so choose the one that makes the exponentials safe. Unshifted, ezk overflows to infinity once zk exceeds about 709 in 64-bit floats (about 88 in 32-bit), and the ratio becomes ,∞/∞, which is NaN.
;softmax(z+c1)=softmax(z); subtracting maxkzk keeps every exponent ,≤0, so nothing overflows and the largest term is exactly 1Every shifted exponential lies in ,(0,1], and because one of them is exactly 1 the denominator is at least :1: it cannot overflow, and it cannot underflow to 0 however negative the other logits are.
Problem 2
Compute ,∂si/∂zj, treating i=j and i=j separately, then write both cases as one formula.
Let ,Σ=∑kezk, so .si=ezi/Σ.Naming the denominator separates the two places z appears: zi in the numerator, and every zk in .Σ.
.∂Σ/∂zj=ezj.Only the j-th term of Σ depends on ,zj, and et is its own derivative.
:i=j:.∂zi∂si=Σ2eziΣ−eziezi=si−si2.Both the numerator and the denominator contain ,zi, so each contributes a term; then ezi/Σ=si in both.
:i=j:.∂zj∂si=−Σ2eziezj=−sisj.The numerator ezi does not contain ,zj, so only the denominator contributes, through step 2.
si−si2=si(1−si) and .−sisj=si(0−sj).Factoring out si leaves two expressions that differ only in the 1 or 0 in front of ,sj, and that 1 or 0 is .δij.
∂si/∂zj=si(δij−sj)δij is 1 exactly in the case .i=j. Sanity check: summing over i gives ,sj−sj∑isi=0, as it must, because ∑isi=1 for every ,z, so no change in zj can change the total.
Problem 3
Write ∂s/∂z as a matrix. Show it is symmetric and that .(∂s/∂z)1=0.
Entry (i,j) of ∂s/∂z is .∂si/∂zj=siδij−sisj.Numerator layout puts si on the rows and zj on the columns; the value is Problem 2, multiplied out.
siδij is entry (i,j) of ,diag(s), and sisj is entry (i,j) of the outer product .ss⊤.diag(s) has si at (i,i) and zeros elsewhere; ,(ab⊤)ij=aibj, here with .a=b=s.
Let ,J=∂s/∂z=diag(s)−ss⊤,.n×n.Steps 1 and 2 agree entry by entry.
.J⊤=diag(s)⊤−(ss⊤)⊤=diag(s)−ss⊤=J.A diagonal matrix is its own transpose, and .(ss⊤)⊤=(s⊤)⊤s⊤=ss⊤.
.J1=diag(s)1−s(s⊤1)=s−s.;diag(s)1=s⊙1=s; associativity lets s⊤1 be computed first, and it is .∑isi=1.
;∂s/∂z=diag(s)−ss⊤; symmetric because both terms are; (diag(s)−ss⊤)1=s−s(s⊤1)=s−s=0J1=0 is Problem 1 in derivative form: moving z along 1 does not change ,s, so the directional derivative in that direction is zero.
Problem 4
What is the rank of ∂s/∂z when every ?si>0? Describe its null space, and say what that means for how the logits determine the probabilities.
Let J=diag(s)−ss⊤ (Problem 3), and for any v∈Rn let .vˉ=∑isivi. Then .v⊤Jv=∑isivi2−vˉ2.,v⊤diag(s)v=∑isivi2, and v⊤ss⊤v=(s⊤v)2=vˉ2 by associativity.
,v⊤Jv=∑isi(vi−vˉ)2=Vars(v), the variance of the entries of v when entry i is drawn with probability .si.Expanding the square gives ,∑isivi2−2vˉ2+vˉ2, because ∑isivi=vˉ and .∑isi=1.
,Vars(v)≥0, with equality if and only if vi=vˉ for every ,i, that is, v is a multiple of .1.Each term is ,≥0, and because every si>0 the sum is 0 only if every vi−vˉ is .0. Conversely, v=a1 gives .vˉ=a.
If Jv=0 then ,v⊤Jv=0, so ;v∈span{1}; and .J1=0. So the null space is exactly ,span{1}, and .rankJ=n−1.Multiply by v⊤ and apply step 3; Problem 3 gives the reverse inclusion; then rank–nullity for an n×n matrix.
If ,softmax(z)=softmax(z′), then ,z′=z+(logΣ′−logΣ)1, where Σ=∑kezk and .Σ′=∑kezk′.Taking logs, ,logsi=zi−logΣ=zi′−logΣ′, and the correction is the same for every .i. Step 4 is about first-order changes; this covers finite ones.
;rank=n−1; null space :span{1}: logits are determined by the probabilities only up to a common shift, which is Problem 1 againOnly the differences zi−zj=log(si/sj) are fixed by .s. Because J is symmetric, its column space is the orthogonal complement of its null space: every first-order change Jdz in s has entries summing to zero.
Problem 5
Let L=−∑iyilogsi with y one-hot. Compute .∇zL.
Let c be the index with .yc=1. Then .L=−logsc.Every other term of the sum has .yi=0.
With ,Σ=∑kezk,,logsc=zc−logΣ, so .L=−zc+logΣ.The log of a quotient is a difference, and .logezc=zc. This form never divides by .sc.
.∂L/∂zj=−δjc+ezj/Σ=−δjc+sj.zc contains zj only when ;j=c; the derivative of logΣ is ,(1/Σ)∂Σ/∂zj, and only the j-th term of Σ depends on .zj.
.δjc=yj.y is 1 at c and 0 elsewhere, which is the definition of δjc as a function of .j.
∇zL=s−yEntry j of the gradient is ,∂L/∂zj=sj−yj, and the result is ,n×1, the shape of .z. Sanity check: its entries sum to ,1−1=0, as they must, because L depends on z only through ,s, which ignores a shift along .1.
Problem 6
Let ,ℓ=logsoftmax(z), i.e. .ℓi=zi−log∑kezk. Compute .∂ℓ/∂z. Use it to redo Problem 5 in one line.
With ,Σ=∑kezk,.∂ℓi/∂zj=δij−ezj/Σ=δij−sj.,∂zi/∂zj=δij, and ∂logΣ/∂zj=sj as in Problem 5, step 3; the second term is the same for every .i.
δij is entry (i,j) of ,I, and sj is entry (i,j) of .1s⊤.:(1s⊤)ij=1⋅sj: every row of 1s⊤ is ,s⊤, matching a term that does not depend on .i.
,∂ℓ/∂z=I−1s⊤,.n×n.Step 1 entry by entry. It is not symmetric: entry (i,j) subtracts sj and entry (j,i) subtracts .si. Multiplying by diag(s) on the left gives ,diag(s)−ss⊤, Problem 3, because s=eℓ elementwise has Jacobian diag(s) with respect to .ℓ.
,L=−y⊤ℓ, so .∇zL=(∂ℓ/∂z)⊤∇ℓL=−(∂ℓ/∂z)⊤y.L=−∑iyiℓi is linear in ℓ with gradient ;−y; then the chain rule for gradients.
.(I−1s⊤)⊤y=y−s1⊤y.,(1s⊤)⊤=s1⊤, and associativity lets 1⊤y be computed first.
;∂ℓ/∂z=I−1s⊤; then ∇zL=−(∂ℓ/∂z)⊤y=−(y−s1⊤y)=s−y1⊤y=∑iyi=1 for a one-hot .y. This is why cross-entropy is usually computed through log-softmax: it is evaluated from z directly, as ,z−logΣ1, so logsi never has to be taken of an si that has underflowed to .0.
Problem 7
Batched: ,Z∈RN×n,S=softmax(Z) row-wise, Y one-hot rows, .L=−N1∑k,iYkilogSki. Compute ∇ZL and state its shape.
L=N1∑kLk with .Lk=−∑iYkilogSki.Group the double sum by rows; Lk is the loss of example .k.
Lk depends on Z only through row .k.The softmax is taken row by row, so row k of S is computed from row k of Z alone, and Lk uses only row k of S and .Y.
.∂L/∂Zkj=N1∂Lk/∂Zkj=N1(Skj−Ykj).By step 2, only the k-th term of the mean contains ;Zkj; the value is Problem 5 applied to row ,k, whose softmax is row k of .S.
,∇ZL=N1(S−Y),N×nEntry (k,j) of a matrix gradient is ,∂L/∂Zkj, and step 3 fills every entry. The shape is that of .Z.
Problem 8
Repeat Problem 5 for soft labels y with ,yi≥0,.∑iyi=1. What changes if ?∑iyi=1?
With ,Σ=∑kezk,.L=−∑iyi(zi−logΣ)=−∑iyizi+(∑iyi)logΣ.,logsi=zi−logΣ, and logΣ does not depend on ,i, so it leaves the sum with coefficient .∑iyi. Nothing about y has been assumed yet.
.∂L/∂zj=−yj+(∑iyi)sj.Only the j-th term of ∑iyizi contains ,zj, and ∂logΣ/∂zj=sj (Problem 5, step 3).
,∇zL=(∑iyi)s−y, which is s−y when the labels sum to 1Step 2 for every .j. One-hot labels are the case ,∑iyi=1, so Problem 5 is a special case. If the labels sum to ,a=1,,a>0, the gradient is ,as−y, which is zero only at :s=y/a: the loss pulls s towards the normalised labels, and s−y is not its gradient.
Problem 9
With temperature, .s=softmax(z/T). Compute .∂s/∂z. What happens as T→∞ and as ?T→0+?
Let ,u=z/T, so s=softmax(u) and .∂u/∂z=T1I.Each ui=zi/T depends only on ;zi; what remains is the softmax of Problem 3.
,∂s/∂z=(diag(s)−ss⊤)T1I=T1(diag(s)−ss⊤), with .s=softmax(u).Chain rule, outer Jacobian (Problem 3 at )u) on the left.
:T→∞:,s→1/n, and every entry of ∂s/∂z is at most 1/(4T) in size, so the Jacobian .→0.u→0 and the softmax is continuous. The bracket's entries are si(1−si)≤41 and −sisj with ,si+sj≤1, so .sisj≤41.
,T→0+, unique largest logit ,zm, gap :g=zm−maxk=mzk>0: for ,i=m,,si≤e−g/T, and .1−sm≤(n−1)e−g/T.The denominator of si contains ,ezm/T, so ;si≤e(zi−zm)/T; then .1−sm=∑k=msk.
So s→ the one-hot vector at ,m, a hard argmax, and every entry of ∂s/∂z is at most (n−1)e−g/T/T in size, which .→0.Entry si(δij−sj) is bounded by ,si, by ,sj, or (if )i=j=m) by ;1−sm; step 4 bounds the one whose index is not .m.
,T→0+,r≥2 logits tied for the largest: each tied ,si→1/r, so the diagonal entry .T1si(1−si)→∞.For tied ,i,,si=1/(r+∑ke(zk−zi)/T), summed over untied ,k, whose terms ;→0; so .si(1−si)→r1(1−r1)>0.
∂s/∂z=T1(diag(s)−ss⊤) with ;s=softmax(z/T); as ,T→∞,s→1/n and the Jacobian ;→0; as ,T→0+, when the largest logit is unique the softmax approaches a hard argmax and the Jacobian ,→0, while at a tie for the largest logit entries of the Jacobian grow without boundSteps 2, 3, 5 and 6.
Problem 10
Compute ∇sL for ,L=−∑iyilogsi, then apply the chain rule through the Jacobian of Problem 3 and show you recover the answer to Problem 5.
,∂L/∂si=−yi/si, so ,∇sL=−y/s,.n×1.Treat L as a function of :s: only the i-th term contains ,si, and the derivative of logt is .1/t. Every ,si>0, so the division is defined.
∇zL=J⊤∇sL with .J=∂s/∂z=diag(s)−ss⊤.The chain rule for gradients: the gradient with respect to the input is the transposed Jacobian times the gradient with respect to the output.
.J⊤=J.Problem 3.
.diag(s)(−y/s)=−y.A diagonal matrix acts as an elementwise product: .si⋅(−yi/si)=−yi.
.−ss⊤(−y/s)=s(s⊤(y/s))=s(∑iyi)=s(1⊤y).Associativity lets the scalar s⊤(y/s)=∑isiyi/si be computed first, and each si cancels.
.∇zL=(diag(s)−ss⊤)(−y/s)=−y+s(1⊤y).Steps 4 and 5 are the two terms of .J(−y/s).
,∇zL=J⊤∇sL=s−y, the same as Problem 51⊤y=1 for a one-hot .y. The 1/si in ∇sL cancels against the si in ,J, so the large entry −1/sc of ,∇sL, at the target index c when the target's probability sc is small, becomes a bounded :∇zL: every entry of s−y lies in .[−1,1].
Where this goes wrong
1. Using the i = j case for the whole Jacobian
The diagonal entry si(1−si) has exactly the form of the sigmoid's derivative ,σ(1−σ), and the previous page showed that the sigmoid's Jacobian is diagonal.
∂si/∂zi=si(1−si)Right so far: this is the i=j case of Problem 2, and it is the correct diagonal.
“This is σ(1−σ) again, and the sigmoid's Jacobian is ,diag(σ′(z)), so the softmax's is diagonal too.”The analogy that causes the mistake: the sigmoid is elementwise, but every si contains every zj through the shared denominator.
∂s/∂z=diag(s⊙(1−s))The off-diagonal −sisj terms are dropped. The true Jacobian diag(s)−ss⊤ has columns that sum to zero, since raising one logit takes probability from the others; this one's column j sums to ,sj(1−sj)>0, as if raising zj could raise sj without lowering anything, pushing the total above .1.
2. Stopping at the gradient with respect to s
The loss is written in terms of ,s, so differentiating the formula as written feels like the whole job.
,L=−∑iyilogsi, so ∂L/∂si=−yi/siRight so far: this is ,∇sL, step 1 of Problem 10.
“L is a function of the probabilities, and the probabilities are the network's output, so this is the gradient to send back.”The shortcut that causes the mistake: the formula shows ,s, but the parameters reach L through ,z, and the softmax sits between them.
∇zL=−y/sThat is ;∇sL; one more chain-rule step through the softmax, multiplying by ,(diag(s)−ss⊤)⊤, turns it into s−y (Problem 10). The symptoms: −y/s is zero at every non-target logit, so only the target is ever pushed, and its size ,1/sc, with c the target index, grows without bound as the prediction gets worse, where every entry of s−y stays in .[−1,1].
3. Losing the 1/N in the batch
Each example contributes s−y for its own row, and stacking those rows is the natural way to write the batch.
,L=N1∑kLk, and the gradient of Lk with respect to row k of Z is row k of S−YRight so far: this is Problem 5 for one example, and the loss is the mean.
“Stack the per-example gradients into a matrix.”The shortcut that causes the mistake: stacking gives the gradient of the sum ,∑kLk, and the mean is a different function.
∇ZL=S−YFor the mean loss the correct answer is N1(S−Y) (Problem 7): the mean divides by ,N, and so does its gradient. With a sum loss S−Y is right. The two differ by a factor of ,N, which you would then have to absorb into the learning rate, and which changes whenever the batch size does.
4. Applying s − y to labels that do not sum to one
Soft labels come from label smoothing, from a teacher model, or from annotators' votes, and a vector built by hand can easily fail to sum to one; here the entries sum to .0.9.
,yi≥0,,∑iyi=0.9,L=−∑iyilogsiRight so far: the loss is well defined for any label vector, and nothing has been differentiated yet.
“The gradient of softmax plus cross-entropy is .s−y.”The shortcut that causes the mistake: remembering the result without the hypothesis it used, 1⊤y=1 (Problems 5, 6 and 10 each use it in their last step).
∇zL=s−y with ∑iyi=0.9Problem 8 gives ,(∑iyi)s−y=0.9s−y, so the error is .0.1s. It never goes away: the entries of s−y sum to ,0.1, but those of the true gradient sum to ,0, because the loss does not change when every logit is shifted by the same amount.
5. Treating the logits as identifiable
Training drives softmax(z) towards the target probabilities, and it is tempting to solve for the logits that get there.
At the optimum, softmax(z)=p with every pi>0Right so far: an equation in z that has solutions, for example .zi=logpi.
“n equations in n unknowns, so the solution is unique.”The shortcut that causes the mistake: counting equations. Both sides sum to ,1, so only n−1 of the equations are independent.
“The model has learned .z=logp.”Any z+c1 gives the same s (Problem 1), and the null space of the softmax Jacobian is span{1} (Problem 4): only differences of logits mean anything, .zi−zj=log(pi/pj). A single raw logit, or a comparison of raw logits across examples or models, carries an arbitrary offset.
Print this set: softmax-jacobian.pdf (problems, answers, and worked solutions on separate pages).