Back to AI Math Mind Map
中文·English
📐 AI MathID: matrix-calculus-deep

Matrix Calculus & Softmax Gradient

矩阵求导与 Softmax 梯度
🎯Core Definition
Matrix calculus and the softmax gradient are whiteboard fundamentals of derivation interviews. First, quadratic forms: expand xTAx=i,jxiAijxjx^TAx = \sum_{i,j} x_i A_{ij} x_j and differentiate w.r.t. xkx_k — terms with i=ki=k contribute jAkjxj\sum_j A_{kj}x_j, terms with j=kj=k contribute iAikxi\sum_i A_{ik}x_i, so (xTAx)xk=(Ax)k+(ATx)k\frac{\partial (x^TAx)}{\partial x_k} = (Ax)_k + (A^Tx)_k and hence xTAxx=(A+AT)x\frac{\partial x^TAx}{\partial x} = (A + A^T)x (=2Ax= 2Ax when AA is symmetric). Second, the softmax + cross-entropy joint gradient: with pi=ezijezjp_i = \frac{e^{z_i}}{\sum_j e^{z_j}} and L=iyilogpiL = -\sum_i y_i \log p_i, the softmax derivative splits into two cases — for i=ji = j: pizi=pi(1pi)\frac{\partial p_i}{\partial z_i} = p_i(1-p_i); for iji \neq j: pjzi=pipj\frac{\partial p_j}{\partial z_i} = -p_i p_j. The chain rule gives Lzi=jLpjpjzi=yipipi(1pi)+jiyjpjpipj=yi(1pi)+pijiyj\frac{\partial L}{\partial z_i} = \sum_j \frac{\partial L}{\partial p_j}\frac{\partial p_j}{\partial z_i} = -\frac{y_i}{p_i}\cdot p_i(1-p_i) + \sum_{j\neq i}\frac{y_j}{p_j}\cdot p_i p_j = -y_i(1-p_i) + p_i\sum_{j\neq i} y_j. Since jyj=1\sum_j y_j = 1, we get yi+yipi+pipiyi=piyi-y_i + y_i p_i + p_i - p_i y_i = p_i - y_i, i.e. Lzi=piyi\frac{\partial L}{\partial z_i} = p_i - y_i.
💡Use Cases
interviewers ask for on-the-whiteboard derivations of Lz=py\frac{\partial L}{\partial z} = p - y, xTAxx=(A+AT)x\frac{\partial x^TAx}{\partial x} = (A+A^T)x and xAxb2=2AT(Axb)\frac{\partial}{\partial x}\Vert Ax - b\Vert^2 = 2A^T(Ax-b); it is the prerequisite for understanding backpropagation and Jacobian/Hessian dimension checks.
Key Problems Solved
the derivation collapses to a memorizable final form piyip_i - y_i: the two-case signs cancel and the logpj\log p_j in CE exactly cancels the softmax denominator, turning an O(C2)O(C^2) Jacobian computation into an O(C)O(C) vector subtraction — exactly how PyTorch/TensorFlow implement the Softmax+CE gradient layer.
🎯5 High-Frequency Exam Points
1
Whiteboard: expand xTAx=i,jxiAijxjx^TAx = \sum_{i,j}x_iA_{ij}x_j to derive xTAxx=(A+AT)x\frac{\partial x^TAx}{\partial x} = (A+A^T)x; why does it reduce to 2Ax2Ax when AA is symmetric?
2
Whiteboard the softmax+CE joint gradient Lzi=piyi\frac{\partial L}{\partial z_i} = p_i - y_i: derive pjzi\frac{\partial p_j}{\partial z_i} for i=ji=j vs iji\neq j and simplify with jyj=1\sum_j y_j = 1.
3
Derive the least-squares gradient xAxb22=2AT(Axb)\frac{\partial}{\partial x}\Vert Ax - b\Vert_2^2 = 2A^T(Ax-b) and apply the dimension-check rule to each factor.
4
Why does logpj\log p_j in CE cancel the softmax denominator to leave pyp-y? Is the gradient still clean under squared loss 12(piyi)2\frac{1}{2}(p_i - y_i)^2?
5
Dimension-check the chain rule: given LzRC\frac{\partial L}{\partial z} \in \mathbb{R}^C and the shape of zW\frac{\partial z}{\partial W}, how do they multiply to yield LW\frac{\partial L}{\partial W}?
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "Matrix Calculus & Softmax Gradient"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardGradient DescentNext CardAdam/AdamW Math

🔗 More AI Math Knowledge Cards

Bayesian InferenceBias-Variance DecompositionBootstrapCausal Inference (Rubin)