Practice / Backprop by hand

The softmax Jacobian

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/N1/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\partial y/\partial x is m×nm \times n for y∈Rmy \in \mathbb{R}^m, x∈Rnx \in \mathbb{R}^n), 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 LL of uu with uu a function of xx, ∇xL=(∂u/∂x)⊤∇uL\nabla_x L = (\partial u/\partial x)^\top \nabla_u L.
  • The logits are z∈Rnz \in \mathbb{R}^n, one per class, and s=softmax⁡(z)s = \operatorname{softmax}(z) has entries si=ezi/∑kezks_i = e^{z_i}/\sum_k e^{z_k}. Every si>0s_i > 0 and ∑isi=1\sum_i s_i = 1.
  • 1\mathbf{1} is the all-ones vector, so 1⊤v=∑ivi\mathbf{1}^\top v = \sum_i v_i and 1⊤s=s⊤1=1\mathbf{1}^\top s = s^\top \mathbf{1} = 1.
  • δij\delta_{ij} is the Kronecker delta: 11 if i=ji = j and 00 otherwise, so δij\delta_{ij} is entry (i,j)(i,j) of II, the identity matrix.
  • The cross-entropy loss is L=−∑iyilog⁡siL = -\sum_i y_i \log s_i (natural log). Unless a problem says otherwise, yy is one-hot: one entry is 11, at the target class, and the rest are 00.
  • Batched: Z∈RN×nZ \in \mathbb{R}^{N \times n} has one example per row, S=softmax⁡(Z)S = \operatorname{softmax}(Z) is taken row by row, YY holds the one-hot targets as rows, and the loss is the mean L=1N∑kLkL = \tfrac1N \sum_k L_k of the per-row losses.
  • diag⁡(v)\operatorname{diag}(v) and the elementwise product ⊙\odot are as on the previous page; y/sy/s is elementwise division, (y/s)i=yi/si(y/s)_i = y_i/s_i.
  • These results are the backward pass of the last layer of every classifier trained with cross-entropy.

Builds on: Matrix calculus conventions, Jacobians and the chain rule

Problems

  1. ·

    Show that softmax⁡(z+c1)=softmax⁡(z)\operatorname{softmax}(z + c\mathbf{1}) = \operatorname{softmax}(z) for any scalar cc. Why does an implementation subtract max⁡kzk\max_k z_k first?

  2. ··

    Compute ∂si/∂zj\partial s_i/\partial z_j, treating i=ji = j and i≠ji \neq j separately, then write both cases as one formula.

  3. ··

    Write ∂s/∂z\partial s/\partial z as a matrix. Show it is symmetric and that (∂s/∂z)1=0\big(\partial s/\partial z\big)\mathbf{1} = 0.

  4. ···

    What is the rank of ∂s/∂z\partial s/\partial z when every si>0s_i > 0? Describe its null space, and say what that means for how the logits determine the probabilities.

  5. ··

    Let L=−∑iyilog⁡siL = -\sum_i y_i \log s_i with yy one-hot. Compute ∇zL\nabla_z L.

  6. ··

    Let ℓ=log⁡softmax⁡(z)\ell = \log \operatorname{softmax}(z), i.e. ℓi=zi−log⁡∑kezk\ell_i = z_i - \log\sum_k e^{z_k}. Compute ∂ℓ/∂z\partial \ell/\partial z. Use it to redo Problem 5 in one line.

  7. ···

    Batched: Z∈RN×nZ \in \mathbb{R}^{N \times n}, S=softmax⁡(Z)S = \operatorname{softmax}(Z) row-wise, YY one-hot rows, L=−1N∑k,iYkilog⁡SkiL = -\tfrac1N\sum_{k,i} Y_{ki}\log S_{ki}. Compute ∇ZL\nabla_Z L and state its shape.

  8. ··

    Repeat Problem 5 for soft labels yy with yi≥0y_i \ge 0, ∑iyi=1\sum_i y_i = 1. What changes if ∑iyi≠1\sum_i y_i \neq 1?

  9. ···

    With temperature, s=softmax⁡(z/T)s = \operatorname{softmax}(z/T). Compute ∂s/∂z\partial s/\partial z. What happens as T→∞T \to \infty and as T→0+T \to 0^+?

  10. ···

    Compute ∇sL\nabla_s L for L=−∑iyilog⁡siL = -\sum_i y_i \log s_i, then apply the chain rule through the Jacobian of Problem 3 and show you recover the answer to Problem 5.

Worked solutions

Problem 1

Show that softmax⁡(z+c1)=softmax⁡(z)\operatorname{softmax}(z + c\mathbf{1}) = \operatorname{softmax}(z) for any scalar cc. Why does an implementation subtract max⁡kzk\max_k z_k first?

  1. softmax⁡(z+c1)i=ezi+c∑kezk+c\operatorname{softmax}(z + c\mathbf{1})_i = \dfrac{e^{z_i + c}}{\sum_k e^{z_k + c}}.Adding c1c\mathbf{1} adds the same cc to every logit: the one in the numerator and every one in the sum.
  2. =ec eziec∑kezk= \dfrac{e^c\, e^{z_i}}{e^c \sum_k e^{z_k}}.ea+b=eaebe^{a + b} = e^a e^b, and ece^c is the same factor in every term of the sum, so it comes out of the sum.
  3. =ezi∑kezk=si= \dfrac{e^{z_i}}{\sum_k e^{z_k}} = s_i.ec>0e^c > 0, so it cancels. This holds for every ii and every real cc.
  4. Take c=−max⁡kzkc = -\max_k z_k. Then every exponent zk−max⁡kzkz_k - \max_k z_k is ≤0\le 0, and it is 00 for the largest logit.Step 3 allows any cc, so choose the one that makes the exponentials safe. Unshifted, ezke^{z_k} overflows to infinity once zkz_k exceeds about 709709 in 64-bit floats (about 8888 in 32-bit), and the ratio becomes ∞/∞\infty/\infty, which is NaN.
  5. softmax⁡(z+c1)=softmax⁡(z)\operatorname{softmax}(z + c\mathbf{1}) = \operatorname{softmax}(z); subtracting max⁡kzk\max_k z_k keeps every exponent ≤0\le 0, so nothing overflows and the largest term is exactly 11Every shifted exponential lies in (0,1](0, 1], and because one of them is exactly 11 the denominator is at least 11: it cannot overflow, and it cannot underflow to 00 however negative the other logits are.

Problem 2

Compute ∂si/∂zj\partial s_i/\partial z_j, treating i=ji = j and i≠ji \neq j separately, then write both cases as one formula.

  1. Let Σ=∑kezk\Sigma = \sum_k e^{z_k}, so si=ezi/Σs_i = e^{z_i}/\Sigma.Naming the denominator separates the two places zz appears: ziz_i in the numerator, and every zkz_k in Σ\Sigma.
  2. ∂Σ/∂zj=ezj\partial \Sigma/\partial z_j = e^{z_j}.Only the jj-th term of Σ\Sigma depends on zjz_j, and ete^t is its own derivative.
  3. i=ji = j: ∂si∂zi=ezi Σ−ezi eziΣ2=si−si2\dfrac{\partial s_i}{\partial z_i} = \dfrac{e^{z_i}\,\Sigma - e^{z_i}\, e^{z_i}}{\Sigma^2} = s_i - s_i^2.Both the numerator and the denominator contain ziz_i, so each contributes a term; then ezi/Σ=sie^{z_i}/\Sigma = s_i in both.
  4. i≠ji \neq j: ∂si∂zj=−ezi ezjΣ2=−sisj\dfrac{\partial s_i}{\partial z_j} = -\dfrac{e^{z_i}\, e^{z_j}}{\Sigma^2} = -s_i s_j.The numerator ezie^{z_i} does not contain zjz_j, so only the denominator contributes, through step 2.
  5. si−si2=si(1−si)s_i - s_i^2 = s_i(1 - s_i) and −sisj=si(0−sj)-s_i s_j = s_i(0 - s_j).Factoring out sis_i leaves two expressions that differ only in the 11 or 00 in front of sjs_j, and that 11 or 00 is δij\delta_{ij}.
  6. ∂si/∂zj=si(δij−sj)\partial s_i/\partial z_j = s_i(\delta_{ij} - s_j)δij\delta_{ij} is 11 exactly in the case i=ji = j. Sanity check: summing over ii gives sj−sj∑isi=0s_j - s_j \sum_i s_i = 0, as it must, because ∑isi=1\sum_i s_i = 1 for every zz, so no change in zjz_j can change the total.

Problem 3

Write ∂s/∂z\partial s/\partial z as a matrix. Show it is symmetric and that (∂s/∂z)1=0\big(\partial s/\partial z\big)\mathbf{1} = 0.

  1. Entry (i,j)(i,j) of ∂s/∂z\partial s/\partial z is ∂si/∂zj=siδij−sisj\partial s_i/\partial z_j = s_i\delta_{ij} - s_i s_j.Numerator layout puts sis_i on the rows and zjz_j on the columns; the value is Problem 2, multiplied out.
  2. siδijs_i\delta_{ij} is entry (i,j)(i,j) of diag⁡(s)\operatorname{diag}(s), and sisjs_i s_j is entry (i,j)(i,j) of the outer product ss⊤ss^\top.diag⁡(s)\operatorname{diag}(s) has sis_i at (i,i)(i,i) and zeros elsewhere; (ab⊤)ij=aibj(ab^\top)_{ij} = a_i b_j, here with a=b=sa = b = s.
  3. Let J=∂s/∂z=diag⁡(s)−ss⊤J = \partial s/\partial z = \operatorname{diag}(s) - ss^\top, n×nn \times n.Steps 1 and 2 agree entry by entry.
  4. J⊤=diag⁡(s)⊤−(ss⊤)⊤=diag⁡(s)−ss⊤=JJ^\top = \operatorname{diag}(s)^\top - (ss^\top)^\top = \operatorname{diag}(s) - ss^\top = J.A diagonal matrix is its own transpose, and (ss⊤)⊤=(s⊤)⊤s⊤=ss⊤(ss^\top)^\top = (s^\top)^\top s^\top = ss^\top.
  5. J1=diag⁡(s) 1−s (s⊤1)=s−sJ\mathbf{1} = \operatorname{diag}(s)\,\mathbf{1} - s\,(s^\top\mathbf{1}) = s - s.diag⁡(s) 1=s⊙1=s\operatorname{diag}(s)\,\mathbf{1} = s \odot \mathbf{1} = s; associativity lets s⊤1s^\top\mathbf{1} be computed first, and it is ∑isi=1\sum_i s_i = 1.
  6. ∂s/∂z=diag⁡(s)−ss⊤\partial s/\partial z = \operatorname{diag}(s) - ss^\top; symmetric because both terms are; (diag⁡(s)−ss⊤)1=s−s(s⊤1)=s−s=0(\operatorname{diag}(s) - ss^\top)\mathbf{1} = s - s(s^\top\mathbf{1}) = s - s = 0J1=0J\mathbf{1} = 0 is Problem 1 in derivative form: moving zz along 1\mathbf{1} does not change ss, so the directional derivative in that direction is zero.

Problem 4

What is the rank of ∂s/∂z\partial s/\partial z when every si>0s_i > 0? Describe its null space, and say what that means for how the logits determine the probabilities.

  1. Let J=diag⁡(s)−ss⊤J = \operatorname{diag}(s) - ss^\top (Problem 3), and for any v∈Rnv \in \mathbb{R}^n let vˉ=∑isivi\bar v = \sum_i s_i v_i. Then v⊤Jv=∑isivi2−vˉ2v^\top J v = \sum_i s_i v_i^2 - \bar v^2.v⊤diag⁡(s) v=∑isivi2v^\top \operatorname{diag}(s)\, v = \sum_i s_i v_i^2, and v⊤ss⊤v=(s⊤v)2=vˉ2v^\top ss^\top v = (s^\top v)^2 = \bar v^2 by associativity.
  2. v⊤Jv=∑isi(vi−vˉ)2=Var⁡s(v)v^\top J v = \sum_i s_i (v_i - \bar v)^2 = \operatorname{Var}_s(v), the variance of the entries of vv when entry ii is drawn with probability sis_i.Expanding the square gives ∑isivi2−2vˉ2+vˉ2\sum_i s_i v_i^2 - 2\bar v^2 + \bar v^2, because ∑isivi=vˉ\sum_i s_i v_i = \bar v and ∑isi=1\sum_i s_i = 1.
  3. Var⁡s(v)≥0\operatorname{Var}_s(v) \ge 0, with equality if and only if vi=vˉv_i = \bar v for every ii, that is, vv is a multiple of 1\mathbf{1}.Each term is ≥0\ge 0, and because every si>0s_i > 0 the sum is 00 only if every vi−vˉv_i - \bar v is 00. Conversely, v=a1v = a\mathbf{1} gives vˉ=a\bar v = a.
  4. If Jv=0Jv = 0 then v⊤Jv=0v^\top J v = 0, so v∈span⁡{1}v \in \operatorname{span}\{\mathbf{1}\}; and J1=0J\mathbf{1} = 0. So the null space is exactly span⁡{1}\operatorname{span}\{\mathbf{1}\}, and rank⁡J=n−1\operatorname{rank} J = n - 1.Multiply by v⊤v^\top and apply step 3; Problem 3 gives the reverse inclusion; then rank–nullity for an n×nn \times n matrix.
  5. If softmax⁡(z)=softmax⁡(z′)\operatorname{softmax}(z) = \operatorname{softmax}(z'), then z′=z+(log⁡Σ′−log⁡Σ) 1z' = z + (\log\Sigma' - \log\Sigma)\,\mathbf{1}, where Σ=∑kezk\Sigma = \sum_k e^{z_k} and Σ′=∑kezk′\Sigma' = \sum_k e^{z'_k}.Taking logs, log⁡si=zi−log⁡Σ=zi′−log⁡Σ′\log s_i = z_i - \log\Sigma = z'_i - \log\Sigma', and the correction is the same for every ii. Step 4 is about first-order changes; this covers finite ones.
  6. rank⁡=n−1\operatorname{rank} = n - 1; null space span⁡{1}\operatorname{span}\{\mathbf{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)z_i - z_j = \log(s_i/s_j) are fixed by ss. Because JJ is symmetric, its column space is the orthogonal complement of its null space: every first-order change J dzJ\,dz in ss has entries summing to zero.

Problem 5

Let L=−∑iyilog⁡siL = -\sum_i y_i \log s_i with yy one-hot. Compute ∇zL\nabla_z L.

  1. Let cc be the index with yc=1y_c = 1. Then L=−log⁡scL = -\log s_c.Every other term of the sum has yi=0y_i = 0.
  2. With Σ=∑kezk\Sigma = \sum_k e^{z_k}, log⁡sc=zc−log⁡Σ\log s_c = z_c - \log\Sigma, so L=−zc+log⁡ΣL = -z_c + \log\Sigma.The log of a quotient is a difference, and log⁡ezc=zc\log e^{z_c} = z_c. This form never divides by scs_c.
  3. ∂L/∂zj=−δjc+ezj/Σ=−δjc+sj\partial L/\partial z_j = -\delta_{jc} + e^{z_j}/\Sigma = -\delta_{jc} + s_j.zcz_c contains zjz_j only when j=cj = c; the derivative of log⁡Σ\log\Sigma is (1/Σ) ∂Σ/∂zj(1/\Sigma)\,\partial\Sigma/\partial z_j, and only the jj-th term of Σ\Sigma depends on zjz_j.
  4. δjc=yj\delta_{jc} = y_j.yy is 11 at cc and 00 elsewhere, which is the definition of δjc\delta_{jc} as a function of jj.
  5. ∇zL=s−y\nabla_z L = s - yEntry jj of the gradient is ∂L/∂zj=sj−yj\partial L/\partial z_j = s_j - y_j, and the result is n×1n \times 1, the shape of zz. Sanity check: its entries sum to 1−1=01 - 1 = 0, as they must, because LL depends on zz only through ss, which ignores a shift along 1\mathbf{1}.

Problem 6

Let ℓ=log⁡softmax⁡(z)\ell = \log \operatorname{softmax}(z), i.e. ℓi=zi−log⁡∑kezk\ell_i = z_i - \log\sum_k e^{z_k}. Compute ∂ℓ/∂z\partial \ell/\partial z. Use it to redo Problem 5 in one line.

  1. With Σ=∑kezk\Sigma = \sum_k e^{z_k}, ∂ℓi/∂zj=δij−ezj/Σ=δij−sj\partial \ell_i/\partial z_j = \delta_{ij} - e^{z_j}/\Sigma = \delta_{ij} - s_j.∂zi/∂zj=δij\partial z_i/\partial z_j = \delta_{ij}, and ∂log⁡Σ/∂zj=sj\partial \log\Sigma/\partial z_j = s_j as in Problem 5, step 3; the second term is the same for every ii.
  2. δij\delta_{ij} is entry (i,j)(i,j) of II, and sjs_j is entry (i,j)(i,j) of 1s⊤\mathbf{1}s^\top.(1s⊤)ij=1⋅sj(\mathbf{1}s^\top)_{ij} = 1 \cdot s_j: every row of 1s⊤\mathbf{1}s^\top is s⊤s^\top, matching a term that does not depend on ii.
  3. ∂ℓ/∂z=I−1s⊤\partial \ell/\partial z = I - \mathbf{1}s^\top, n×nn \times n.Step 1 entry by entry. It is not symmetric: entry (i,j)(i,j) subtracts sjs_j and entry (j,i)(j,i) subtracts sis_i. Multiplying by diag⁡(s)\operatorname{diag}(s) on the left gives diag⁡(s)−ss⊤\operatorname{diag}(s) - ss^\top, Problem 3, because s=eℓs = e^{\ell} elementwise has Jacobian diag⁡(s)\operatorname{diag}(s) with respect to ℓ\ell.
  4. L=−y⊤ℓL = -y^\top \ell, so ∇zL=(∂ℓ/∂z)⊤∇ℓL=−(∂ℓ/∂z)⊤y\nabla_z L = (\partial\ell/\partial z)^\top \nabla_\ell L = -(\partial\ell/\partial z)^\top y.L=−∑iyiℓiL = -\sum_i y_i \ell_i is linear in ℓ\ell with gradient −y-y; then the chain rule for gradients.
  5. (I−1s⊤)⊤y=y−s 1⊤y(I - \mathbf{1}s^\top)^\top y = y - s\,\mathbf{1}^\top y.(1s⊤)⊤=s 1⊤(\mathbf{1}s^\top)^\top = s\,\mathbf{1}^\top, and associativity lets 1⊤y\mathbf{1}^\top y be computed first.
  6. ∂ℓ/∂z=I−1s⊤\partial \ell/\partial z = I - \mathbf{1}s^\top; then ∇zL=−(∂ℓ/∂z)⊤y=−(y−s 1⊤y)=s−y\nabla_z L = -(\partial\ell/\partial z)^\top y = -(y - s\,\mathbf{1}^\top y) = s - y1⊤y=∑iyi=1\mathbf{1}^\top y = \sum_i y_i = 1 for a one-hot yy. This is why cross-entropy is usually computed through log-softmax: it is evaluated from zz directly, as z−log⁡Σ 1z - \log\Sigma\,\mathbf{1}, so log⁡si\log s_i never has to be taken of an sis_i that has underflowed to 00.

Problem 7

Batched: Z∈RN×nZ \in \mathbb{R}^{N \times n}, S=softmax⁡(Z)S = \operatorname{softmax}(Z) row-wise, YY one-hot rows, L=−1N∑k,iYkilog⁡SkiL = -\tfrac1N\sum_{k,i} Y_{ki}\log S_{ki}. Compute ∇ZL\nabla_Z L and state its shape.

  1. L=1N∑kLkL = \tfrac1N \sum_k L_k with Lk=−∑iYkilog⁡SkiL_k = -\sum_i Y_{ki}\log S_{ki}.Group the double sum by rows; LkL_k is the loss of example kk.
  2. LkL_k depends on ZZ only through row kk.The softmax is taken row by row, so row kk of SS is computed from row kk of ZZ alone, and LkL_k uses only row kk of SS and YY.
  3. ∂L/∂Zkj=1N ∂Lk/∂Zkj=1N(Skj−Ykj)\partial L/\partial Z_{kj} = \tfrac1N\, \partial L_k/\partial Z_{kj} = \tfrac1N (S_{kj} - Y_{kj}).By step 2, only the kk-th term of the mean contains ZkjZ_{kj}; the value is Problem 5 applied to row kk, whose softmax is row kk of SS.
  4. ∇ZL=1N(S−Y)\nabla_Z L = \tfrac1N (S - Y), N×nN \times nEntry (k,j)(k,j) of a matrix gradient is ∂L/∂Zkj\partial L/\partial Z_{kj}, and step 3 fills every entry. The shape is that of ZZ.

Problem 8

Repeat Problem 5 for soft labels yy with yi≥0y_i \ge 0, ∑iyi=1\sum_i y_i = 1. What changes if ∑iyi≠1\sum_i y_i \neq 1?

  1. With Σ=∑kezk\Sigma = \sum_k e^{z_k}, L=−∑iyi(zi−log⁡Σ)=−∑iyizi+(∑iyi)log⁡ΣL = -\sum_i y_i (z_i - \log\Sigma) = -\sum_i y_i z_i + \big(\sum_i y_i\big)\log\Sigma.log⁡si=zi−log⁡Σ\log s_i = z_i - \log\Sigma, and log⁡Σ\log\Sigma does not depend on ii, so it leaves the sum with coefficient ∑iyi\sum_i y_i. Nothing about yy has been assumed yet.
  2. ∂L/∂zj=−yj+(∑iyi)sj\partial L/\partial z_j = -y_j + \big(\sum_i y_i\big)s_j.Only the jj-th term of ∑iyizi\sum_i y_i z_i contains zjz_j, and ∂log⁡Σ/∂zj=sj\partial\log\Sigma/\partial z_j = s_j (Problem 5, step 3).
  3. ∇zL=(∑iyi) s−y\nabla_z L = (\sum_i y_i)\,s - y, which is s−ys - y when the labels sum to 11Step 2 for every jj. One-hot labels are the case ∑iyi=1\sum_i y_i = 1, so Problem 5 is a special case. If the labels sum to a≠1a \neq 1, a>0a > 0, the gradient is as−ya s - y, which is zero only at s=y/as = y/a: the loss pulls ss towards the normalised labels, and s−ys - y is not its gradient.

Problem 9

With temperature, s=softmax⁡(z/T)s = \operatorname{softmax}(z/T). Compute ∂s/∂z\partial s/\partial z. What happens as T→∞T \to \infty and as T→0+T \to 0^+?

  1. Let u=z/Tu = z/T, so s=softmax⁡(u)s = \operatorname{softmax}(u) and ∂u/∂z=1TI\partial u/\partial z = \tfrac1T I.Each ui=zi/Tu_i = z_i/T depends only on ziz_i; what remains is the softmax of Problem 3.
  2. ∂s/∂z=(diag⁡(s)−ss⊤) 1TI=1T(diag⁡(s)−ss⊤)\partial s/\partial z = \big(\operatorname{diag}(s) - ss^\top\big)\,\tfrac1T I = \tfrac1T\big(\operatorname{diag}(s) - ss^\top\big), with s=softmax⁡(u)s = \operatorname{softmax}(u).Chain rule, outer Jacobian (Problem 3 at uu) on the left.
  3. T→∞T \to \infty: s→1/ns \to \mathbf{1}/n, and every entry of ∂s/∂z\partial s/\partial z is at most 1/(4T)1/(4T) in size, so the Jacobian →0\to 0.u→0u \to 0 and the softmax is continuous. The bracket's entries are si(1−si)≤14s_i(1 - s_i) \le \tfrac14 and −sisj-s_i s_j with si+sj≤1s_i + s_j \le 1, so sisj≤14s_i s_j \le \tfrac14.
  4. T→0+T \to 0^+, unique largest logit zmz_m, gap g=zm−max⁡k≠mzk>0g = z_m - \max_{k \neq m} z_k > 0: for i≠mi \neq m, si≤e−g/Ts_i \le e^{-g/T}, and 1−sm≤(n−1)e−g/T1 - s_m \le (n - 1)e^{-g/T}.The denominator of sis_i contains ezm/Te^{z_m/T}, so si≤e(zi−zm)/Ts_i \le e^{(z_i - z_m)/T}; then 1−sm=∑k≠msk1 - s_m = \sum_{k \neq m} s_k.
  5. So s→s \to the one-hot vector at mm, a hard argmax, and every entry of ∂s/∂z\partial s/\partial z is at most (n−1)e−g/T/T(n - 1)e^{-g/T}/T in size, which →0\to 0.Entry si(δij−sj)s_i(\delta_{ij} - s_j) is bounded by sis_i, by sjs_j, or (if i=j=mi = j = m) by 1−sm1 - s_m; step 4 bounds the one whose index is not mm.
  6. T→0+T \to 0^+, r≥2r \ge 2 logits tied for the largest: each tied si→1/rs_i \to 1/r, so the diagonal entry 1Tsi(1−si)→∞\tfrac1T s_i(1 - s_i) \to \infty.For tied ii, si=1/(r+∑ke(zk−zi)/T)s_i = 1/\big(r + \sum_k e^{(z_k - z_i)/T}\big), summed over untied kk, whose terms →0\to 0; so si(1−si)→1r(1−1r)>0s_i(1 - s_i) \to \tfrac1r(1 - \tfrac1r) > 0.
  7. ∂s/∂z=1T(diag⁡(s)−ss⊤)\partial s/\partial z = \tfrac1T\big(\operatorname{diag}(s) - ss^\top\big) with s=softmax⁡(z/T)s = \operatorname{softmax}(z/T); as T→∞T \to \infty, s→1/ns \to \mathbf{1}/n and the Jacobian →0\to 0; as T→0+T \to 0^+, when the largest logit is unique the softmax approaches a hard argmax and the Jacobian →0\to 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\nabla_s L for L=−∑iyilog⁡siL = -\sum_i y_i \log s_i, then apply the chain rule through the Jacobian of Problem 3 and show you recover the answer to Problem 5.

  1. ∂L/∂si=−yi/si\partial L/\partial s_i = -y_i/s_i, so ∇sL=−y/s\nabla_s L = -y/s, n×1n \times 1.Treat LL as a function of ss: only the ii-th term contains sis_i, and the derivative of log⁡t\log t is 1/t1/t. Every si>0s_i > 0, so the division is defined.
  2. ∇zL=J⊤∇sL\nabla_z L = J^\top \nabla_s L with J=∂s/∂z=diag⁡(s)−ss⊤J = \partial s/\partial z = \operatorname{diag}(s) - ss^\top.The chain rule for gradients: the gradient with respect to the input is the transposed Jacobian times the gradient with respect to the output.
  3. J⊤=JJ^\top = J.Problem 3.
  4. diag⁡(s) (−y/s)=−y\operatorname{diag}(s)\,(-y/s) = -y.A diagonal matrix acts as an elementwise product: si⋅(−yi/si)=−yis_i \cdot (-y_i/s_i) = -y_i.
  5. −ss⊤(−y/s)=s (s⊤(y/s))=s (∑iyi)=s (1⊤y)-ss^\top(-y/s) = s\,\big(s^\top (y/s)\big) = s\,\big(\textstyle\sum_i y_i\big) = s\,(\mathbf{1}^\top y).Associativity lets the scalar s⊤(y/s)=∑isi yi/sis^\top(y/s) = \sum_i s_i\, y_i/s_i be computed first, and each sis_i cancels.
  6. ∇zL=(diag⁡(s)−ss⊤)(−y/s)=−y+s (1⊤y)\nabla_z L = (\operatorname{diag}(s) - ss^\top)(-y/s) = -y + s\,(\mathbf{1}^\top y).Steps 4 and 5 are the two terms of J(−y/s)J(-y/s).
  7. ∇zL=J⊤∇sL=s−y\nabla_z L = J^\top \nabla_s L = s - y, the same as Problem 51⊤y=1\mathbf{1}^\top y = 1 for a one-hot yy. The 1/si1/s_i in ∇sL\nabla_s L cancels against the sis_i in JJ, so the large entry −1/sc-1/s_c of ∇sL\nabla_s L, at the target index cc when the target's probability scs_c is small, becomes a bounded ∇zL\nabla_z L: every entry of s−ys - y lies in [−1,1][-1, 1].

Where this goes wrong

1. Using the i = j case for the whole Jacobian

The diagonal entry si(1−si)s_i(1 - s_i) has exactly the form of the sigmoid's derivative σ(1−σ)\sigma(1 - \sigma), and the previous page showed that the sigmoid's Jacobian is diagonal.

  1. ∂si/∂zi=si(1−si)\partial s_i/\partial z_i = s_i(1 - s_i)Right so far: this is the i=ji = j case of Problem 2, and it is the correct diagonal.
  2. “This is σ(1−σ)\sigma(1 - \sigma) again, and the sigmoid's Jacobian is diag⁡(σ′(z))\operatorname{diag}(\sigma'(z)), so the softmax's is diagonal too.”The analogy that causes the mistake: the sigmoid is elementwise, but every sis_i contains every zjz_j through the shared denominator.
  3. ∂s/∂z=diag⁡(s⊙(1−s))\partial s/\partial z = \operatorname{diag}\big(s \odot (1 - s)\big)The off-diagonal −sisj-s_i s_j terms are dropped. The true Jacobian diag⁡(s)−ss⊤\operatorname{diag}(s) - ss^\top has columns that sum to zero, since raising one logit takes probability from the others; this one's column jj sums to sj(1−sj)>0s_j(1 - s_j) > 0, as if raising zjz_j could raise sjs_j without lowering anything, pushing the total above 11.

2. Stopping at the gradient with respect to s

The loss is written in terms of ss, so differentiating the formula as written feels like the whole job.

  1. L=−∑iyilog⁡siL = -\sum_i y_i \log s_i, so ∂L/∂si=−yi/si\partial L/\partial s_i = -y_i/s_iRight so far: this is ∇sL\nabla_s L, step 1 of Problem 10.
  2. “LL 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 ss, but the parameters reach LL through zz, and the softmax sits between them.
  3. ∇zL=−y/s\nabla_z L = -y/sThat is ∇sL\nabla_s L; one more chain-rule step through the softmax, multiplying by (diag⁡(s)−ss⊤)⊤(\operatorname{diag}(s) - ss^\top)^\top, turns it into s−ys - y (Problem 10). The symptoms: −y/s-y/s is zero at every non-target logit, so only the target is ever pushed, and its size 1/sc1/s_c, with cc the target index, grows without bound as the prediction gets worse, where every entry of s−ys - y stays in [−1,1][-1, 1].

3. Losing the 1/N in the batch

Each example contributes s−ys - y for its own row, and stacking those rows is the natural way to write the batch.

  1. L=1N∑kLkL = \tfrac1N \sum_k L_k, and the gradient of LkL_k with respect to row kk of ZZ is row kk of S−YS - YRight so far: this is Problem 5 for one example, and the loss is the mean.
  2. “Stack the per-example gradients into a matrix.”The shortcut that causes the mistake: stacking gives the gradient of the sum ∑kLk\sum_k L_k, and the mean is a different function.
  3. ∇ZL=S−Y\nabla_Z L = S - YFor the mean loss the correct answer is 1N(S−Y)\tfrac1N(S - Y) (Problem 7): the mean divides by NN, and so does its gradient. With a sum loss S−YS - Y is right. The two differ by a factor of NN, 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.90.9.

  1. yi≥0y_i \ge 0, ∑iyi=0.9\sum_i y_i = 0.9, L=−∑iyilog⁡siL = -\sum_i y_i \log s_iRight so far: the loss is well defined for any label vector, and nothing has been differentiated yet.
  2. “The gradient of softmax plus cross-entropy is s−ys - y.”The shortcut that causes the mistake: remembering the result without the hypothesis it used, 1⊤y=1\mathbf{1}^\top y = 1 (Problems 5, 6 and 10 each use it in their last step).
  3. ∇zL=s−y\nabla_z L = s - y with ∑iyi=0.9\sum_i y_i = 0.9Problem 8 gives (∑iyi) s−y=0.9 s−y(\sum_i y_i)\,s - y = 0.9\,s - y, so the error is 0.1 s0.1\,s. It never goes away: the entries of s−ys - y sum to 0.10.1, but those of the true gradient sum to 00, 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)\operatorname{softmax}(z) towards the target probabilities, and it is tempting to solve for the logits that get there.

  1. At the optimum, softmax⁡(z)=p\operatorname{softmax}(z) = p with every pi>0p_i > 0Right so far: an equation in zz that has solutions, for example zi=log⁡piz_i = \log p_i.
  2. “nn equations in nn unknowns, so the solution is unique.”The shortcut that causes the mistake: counting equations. Both sides sum to 11, so only n−1n - 1 of the equations are independent.
  3. “The model has learned z=log⁡pz = \log p.”Any z+c1z + c\mathbf{1} gives the same ss (Problem 1), and the null space of the softmax Jacobian is span⁡{1}\operatorname{span}\{\mathbf{1}\} (Problem 4): only differences of logits mean anything, zi−zj=log⁡(pi/pj)z_i - z_j = \log(p_i/p_j). 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).