Summary
Matrix calculus transports local changes through vector-valued maps while preserving which output depends on which parameter. Use column gradients and the Jacobian convention throughout this lesson. For a scalar loss, the gradient has the parameter’s shape; the Jacobian records every output-parameter sensitivity. Twice continuously differentiable scalar losses also have a symmetric Hessian of second derivatives.
The four shape combinations
| Gradient | Jacobian , |
| Second derivative: Hessian , | (rarely used; tensor-valued) |
Layout names differ across references; inspect the declared Jacobian entries and dimensions rather than guessing from a field’s terminology. Define the matrix gradient by . For , that gradient has shape .
Identities you actually use
| Function | Gradient |
|---|---|
| , symmetric | |
| ( PD) | |
Chain rule
Let and let be the incoming loss gradient. Since , the loss change is
Therefore . This reverse operation is a vector-Jacobian product under the usual row-cotangent terminology, not the forward product .
For , the Jacobian is
With , its transpose returns . For example, parameter two receives through the two output routes.
Learning objective
Contribution tiles: gather into parameter columns
Reference drawing
Learning objective
Each parameter receives contributions from every output it influences
Derivation
(3 x 2)(2 x 1) = 3 x 1. The example follows the JAX vector-Jacobian product contract.Change the Requirement
Differentiate a Trainable Matrix
New task: Differentiate with respect to the matrix, input, and bias.
Picture and change: Expand inside .
Check: For and , .
Use when: Parameters are arrays rather than one vector. For a shared bias across examples, sum example gradients; a mean loss additionally divides by batch size.
Hessian and second-order methods
New task: Obtain curvature along a fixed direction without storing the Hessian .
Picture and change: Differentiate the scalar with respect to . For a twice continuously differentiable loss, this returns .
Check: has . For , the result is . Its Newton step reaches the minimum in one update because this particular loss is a strictly convex quadratic.
Use when: A solver needs Hessian products, not all entries. Automatic differentiation can compose forward and reverse operations without materializing . An indefinite Hessian does not guarantee a descending Newton direction; damping or trust-region safeguards may be needed.
Adam’s second moment is an average of squared stochastic gradients, not generally a Hessian diagonal. K-FAC approximates Fisher-related blocks; Shampoo uses gradient-based preconditioners. Neither should be described as an exact Hessian inverse.
Common pitfalls
- Wrong orientation. Multiplying a Jacobian by a two-coordinate incoming gradient is dimensionally invalid. Its transpose has the required shape.
- Wrong parameter shape. A scalar loss can have a matrix gradient.
- Full Jacobian allocation. A dense Jacobian has entries. Reverse products avoid building that matrix when only one incoming gradient is needed.
- Wrong reduction. Sum shared-parameter contributions before applying the loss’s declared sum or mean normalization.
Reconstruct
Why is the gradient of not always ?
Reasoned answer
The differential contributes through both occurrences of , giving . It reduces to only when is symmetric. For and , the gradient is , not .