← 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 ∂xTAx∂x=(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=ezi∑jezjp_i = \frac{e^{z_i}}{\sum_j e^{z_j}} and L=−∑iyilog⁡piL = -\sum_i y_i \log p_i, the softmax derivative splits into two cases — for i=ji = j: ∂pi∂zi=pi(1−pi)\frac{\partial p_i}{\partial z_i} = p_i(1-p_i); for i≠ji \neq j: ∂pj∂zi=−pipj\frac{\partial p_j}{\partial z_i} = -p_i p_j. The chain rule gives ∂L∂zi=∑j∂L∂pj∂pj∂zi=−yipi⋅pi(1−pi)+∑j≠iyjpj⋅pipj=−yi(1−pi)+pi∑j≠iyj\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+pi−piyi=pi−yi-y_i + y_i p_i + p_i - p_i y_i = p_i - y_i, i.e. ∂L∂zi=pi−yi\frac{\partial L}{\partial z_i} = p_i - y_i.
💡Use Cases
interviewers ask for on-the-whiteboard derivations of ∂L∂z=p−y\frac{\partial L}{\partial z} = p - y, ∂xTAx∂x=(A+AT)x\frac{\partial x^TAx}{\partial x} = (A+A^T)x and ∂∂x∥Ax−b∥2=2AT(Ax−b)\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 pi−yip_i - y_i: the two-case signs cancel and the log⁡pj\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.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Matrix Calculus & Softmax Gradient.
Use Cases
In production engineering, what are the primary use cases for Matrix Calculus & Softmax Gradient and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Whiteboard: expand xTAx=∑i,jxiAijxjx^TAx = \sum_{i,j}x_iA_{ij}x_j to derive ∂xTAx∂x=(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 ∂L∂zi=pi−yi\frac{\partial L}{\partial z_i} = p_i - y_i: derive ∂pj∂zi\frac{\partial p_j}{\partial z_i} for i=ji=j vs i≠ji\neq j and simplify with ∑jyj=1\sum_j y_j = 1.
3
Derive the least-squares gradient ∂∂x∥Ax−b∥22=2AT(Ax−b)\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 log⁡pj\log p_j in CE cancel the softmax denominator to leave p−yp-y? Is the gradient still clean under squared loss 12(pi−yi)2\frac{1}{2}(p_i - y_i)^2?
5
Dimension-check the chain rule: given ∂L∂z∈RC\frac{\partial L}{\partial z} \in \mathbb{R}^C and the shape of ∂z∂W\frac{\partial z}{\partial W}, how do they multiply to yield ∂L∂W\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 Card →Adam/AdamW Math

🔗 More AI Math Knowledge Cards

Bayesian InferenceBias-Variance DecompositionBootstrapCausal Inference (Rubin)