For p=softmax⁡(z)∈RK, how do you derive the full Jacobian matrix J=∂p∂z for both diagonal (i=j) and off-diagonal (i≠j) entries, and why does shifting every logit by a constant c leave p unchanged?

Deep learning, Neurons and activations
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can compute the full vector-valued derivative of softmax cleanly and express it in matrix form.

Key idea

Writing S=∑k=1Kezk and pi=ezi/S, the quotient rule gives ∂pi∂zj=pi(δij−pj), or J=diag⁡(p)−pp⊤ in matrix form. Adding c to all zk multiplies numerator and denominator by ec, which cancels.

Steps

  1. Write pi=ezi/S with S=∑k=1Kezk, noting ∂S∂zj=ezj.
  2. Diagonal case (i=j): ∂pi∂zi=eziS−ezieziS2=eziS(1−eziS)=pi(1−pi).
  3. Off-diagonal case (i≠j): ezi does not depend on zj, so ∂pi∂zj=0⋅S−eziezjS2=−pipj.
  4. Combine with the Kronecker delta δij (1 if i=j, 0 otherwise): Jij=pi(δij−pj), so J=diag⁡(p)−pp⊤. In backprop, multiplying by upstream gradient g=∂L∂p costs O(K) via J⊤g=p⊙(g−p⊤g) without forming the K×K matrix.
  5. Shift invariance: ezi+c∑kezk+c=eceziec∑kezk=pi. Choosing c=−max⁡kzk keeps all exponents ≤0 and prevents overflow.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Last checked on 4 December 2026.

Read this

Previous in Deep learning: How do you express the derivatives of σ(z)=11+e−z and tanh⁡(z)=ez−e−zez+e−z purely in terms of σ(z) and tanh⁡(z), what are their maximum values, and why is tanh⁡ generally preferred over σ in hidden layers?
Next in Deep learning: What are the GELU and SiLU (Swish) activation functions, what is the exact derivative of SiLU⁡(z)=z σ(z), and why do modern deep architectures often prefer them over ReLU?