Matrix-Shaped Gradients
When a scalar loss depends on a matrix, its gradient has the same shape as that matrix.
On this page 7 sections
01A derivative for each parameter entry#
When a scalar loss depends on a matrix, its gradient has the same shape as that matrix. Entry holds the partial derivative with respect to parameter entry .
The output loss is one number. The gradient is an array because there is a separate sensitivity for every adjustable parameter. It is not the full Jacobian of a matrix-valued output.
02Differentiate and place the entries#
Let be and . The four partial derivatives, in row order, are .
At with rows , those partial derivatives give
For example, increasing locally decreases the loss because its partial derivative is .
W has rows (1,2),(3,4). L=W₁₁²+2W₁₂−W₂₁. Gradient rows?
Show answer and explanation
Place partial derivatives 2W₁₁,2,−1,0 in their original positions.
03Preserve shape and meaning#
A parameter matrix has a scalar-loss gradient, even if the model's prediction has a different shape. Check the variable named by the differentiation symbol.
If the loss does not depend on a particular entry, that entry's derivative is zero. If an entry appears in several terms, add all of its contributions before placing the derivative into the gradient.
A draft transposes the array of partial derivatives for a scalar loss of a rectangular W. What should it preserve?
Show answer and explanation
Entrywise matrix gradients use the same layout as their variables.
A transposed layout can be a valid convention for a vector Jacobian, but here we use entrywise matrix gradients shaped like their variables. Keep that convention consistent throughout matrix backpropagation.
Assemble the gradient of a scalar function of a real matrix from its entrywise partial derivatives.
- Assemble the gradient of a scalar function of a real matrix from its entrywise partial derivatives.
Sources & further reading
- [1]MIT 6.390, Appendix A: Matrix Calculus ↗introml.mit.edu · Article
- [2]Boyd and Vandenberghe, Introduction to Applied Linear Algebra ↗stanford.edu · Article