Matrix Calculus for Machine Learning
derivatives, gradients, and optimization across vector and matrix structures
Core Concepts Covered
- Gradients of vector functions and matrix products
- Jacobians and Hessians for multivariable systems
- Computational graphs and backpropagation mechanics
1. Vector and Matrix Derivatives
A deep learning model may contain thousands or millions of parameters arranged in vectors, matrices, and tensors. Writing a separate derivative for each one would quickly become unmanageable. Matrix Calculus lets us describe all of those related derivatives compactly and compute them together.
When we take the derivative of a scalar function with respect to a column vector , the result is a gradient vector of partial derivatives:
This lesson uses the column-gradient convention, so Some references transpose scalar-by-vector derivatives; consistency matters more than the choice.
Expand . A coordinate appears once as the left factor and once as the right factor, giving . Stacking the coordinates yields the general identity below.
For the column-gradient convention used here, the general identity is It reduces to when is symmetric. The gradient has the same shape as .
Evaluate a Quadratic-Form Gradient
Let and . Evaluate .
1..
2..
The result is a gradient, matching the input shape.
Using here would be wrong because is not symmetric.
2. The Jacobian Matrix and Hessian Matrix
When dealing with vector-valued functions (where the output of a layer is a vector, such as ), we represent all possible first-order partial derivatives inside the Jacobian Matrix.
The Jacobian of a function contains the partial derivative of every output component with respect to every input parameter:
The Hessian Matrix (): A square matrix containing all second-order partial derivatives of a scalar function. While the gradient vector measures the slope (first-order change), the Hessian measures the local curvature (second-order change) of the loss surface:
If the Hessian is positive definite (, all eigenvalues are positive), the loss landscape curves upward like a bowl, indicating a local minimum. If eigenvalues are mixed (some positive, some negative), the landscape represents a saddle point, which stalls vanilla gradient descent.
A Jacobian Maps Input Changes to Output Changes
Read diagram labels
- dx
- n × 1
- J
- m × n
- dy
- m × 1
- ×
- =
- dy ≈ J dx
Jacobian with an Explicit Shape
For , find at .
1..
2..
Two outputs differentiated with respect to two inputs produce a Jacobian.
Hessian and Curvature
Find the Hessian of and classify its curvature.
1..
2.Its leading minors are and , so is positive definite.
The quadratic is strictly convex and has a unique minimum.
3. Computational Graphs and Backpropagation Flow
A neural network is an enormous composite function. To optimize it, we compute the partial derivative of the final loss with respect to every internal weight using computational graphs and backpropagation.
• Computational Graphs: Represent mathematical formulas as directed graphs where nodes are mathematical operators and edges are tensors flowing forward.
• The Backpropagation Trace: Let's trace a concrete example. Suppose we have a node computing and a loss . During the forward pass, we calculate and . During the backward pass, our goal is to find the gradient . We apply the multivariable Chain Rule:
Since , the local derivative is . Therefore, the gradient passed upstream is . Reverse-mode automatic differentiation reuses these local products instead of constructing a full Jacobian at every node.
Forward Values and Reverse-Mode Gradients
Read diagram labels
- x
- z=xw+b
- ŷ=σ(z)
- L(ŷ,y)
- forward computation →
- ← local derivatives multiply backward
Complete Numerical Forward and Backward Pass
For , , , compute , , and , then find .
1.Forward: , , and .
2.Backward local derivatives: , , and .
. Also .
Backpropagation caches forward values and multiplies local derivatives in reverse topological order.
Cumulative Shape Check
If feeds a scalar loss , what are the shapes of and ?
1. has one row per output and one column per input, so it is .
2.Reverse mode multiplies by .
Thus is .
Interactive Practice Quiz
Test your understanding with instant feedback
What is the gradient vector with respect to vector ?
Which of the following describes the 'Jacobian Matrix' of a vector-valued function?
What is the purpose of the 'Hessian Matrix' in advanced optimization algorithms?
During Backpropagation, how is the gradient of a parent node passed back to its input child node?
Calculate the gradient vector of the symmetric quadratic form with respect to vector (where is symmetric):
Compute the gradient vector with respect to column vector :
For a vector function , what are the dimensions of its Jacobian Matrix ?
For a scalar function , what are the dimensions of its second-order Hessian Matrix ?
During backpropagation, what is the local gradient contribution of an addition node with respect to its input ?
What does the Hessian Matrix eigenvalues determine about a critical point in optimization landscapes?
Further Readings
Explore these highly recommended external references to deepen your understanding
