Guide To AI LogoGuide To AI
Unit 09

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

In deep learning, we optimize functions that operate on vectors, matrices, and tensors. Single-variable calculus is too tedious for tracking these complex high-dimensional parameter systems. We use Matrix Calculus to write derivatives compactly and compute thousands of partial gradients simultaneously.

When we take the derivative of a scalar function f(x)f(x) with respect to a column vector xRdx \in \mathbb{R}^d, the result is a gradient vector of partial derivatives:

xf(x)=fx=[fx1fx2fxd]\nabla_x f(x) = \frac{\partial f}{\partial x} = \begin{bmatrix} \frac{\partial f}{\partial x_1} & \frac{\partial f}{\partial x_2} & \dots & \frac{\partial f}{\partial x_d} \end{bmatrix}

Let's trace a mathematical proof for a symmetric quadratic form f(x)=xTAxf(x) = x^T A x where ARd×dA \in \mathbb{R}^{d \times d} is symmetric (A=ATA = A^T). Expanding this expression using summation notations yields: f(x)=i=1dj=1dxiAijxjf(x) = \sum_{i=1}^d \sum_{j=1}^d x_i A_{ij} x_j If we take the partial derivative with respect to a single component xkx_k: fxk=xk(xk2Akk+xkjkAkjxj+xkikxiAik)\frac{\partial f}{\partial x_k} = \frac{\partial}{\partial x_k} \left( x_k^2 A_{kk} + x_k \sum_{j \neq k} A_{kj} x_j + x_k \sum_{i \neq k} x_i A_{ik} \right) fxk=2xkAkk+jkAkjxj+ikxiAik\frac{\partial f}{\partial x_k} = 2 x_k A_{kk} + \sum_{j \neq k} A_{kj} x_j + \sum_{i \neq k} x_i A_{ik} Since AA is symmetric (Aik=AkiA_{ik} = A_{ki}), the two summation terms are identical, merging into a single clean dot product: fxk=2j=1dAkjxj=2(Ax)k\frac{\partial f}{\partial x_k} = 2 \sum_{j=1}^d A_{kj} x_j = 2 (A x)_k When we stack these partial results back into a full row vector, we derive our compact gradient identity: x(xTAx)=2xTA\nabla_x(x^T A x) = 2 x^T A

This identity is used repeatedly when deriving the analytical solutions of Least Squares and Ridge regressions!

2. The Jacobian Matrix and Hessian Matrix

When dealing with vector-valued functions (where the output of a layer is a vector, such as y=f(x)Rmy = f(x) \in \mathbb{R}^m), we represent all possible first-order partial derivatives inside the Jacobian Matrix.

The Jacobian JRm×dJ \in \mathbb{R}^{m \times d} of a function f:RdRmf: \mathbb{R}^d \to \mathbb{R}^m contains the partial derivative of every output component with respect to every input parameter:

J=[f1x1f1xdfmx1fmxd]J = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \dots & \frac{\partial f_1}{\partial x_d} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \dots & \frac{\partial f_m}{\partial x_d} \end{bmatrix}

The Hessian Matrix (HH): 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:

Hij=2fxixjH_{ij} = \frac{\partial^2 f}{\partial x_i \partial x_j}

If the Hessian is positive definite (H0H \succ 0, 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.

3. Computational Graphs and Backpropagation Flow

A neural network is essentially an enormous composite function. To optimize its parameters, we must compute the partial derivative of the final Loss (LL) with respect to every single internal weight weight (ww). We do this systematically using computational graphs and the Backpropagation Algorithm.

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 z=wxz = wx and a loss L=f(z)L = f(z). During the forward pass, we calculate zz and LL. During the backward pass, our goal is to find the gradient Lw\frac{\partial L}{\partial w}. We apply the multivariable Chain Rule:

Lw=Lzzw\frac{\partial L}{\partial w} = \frac{\partial L}{\partial z} \cdot \frac{\partial z}{\partial w}

Since z=wxz = wx, the local derivative is zw=x\frac{\partial z}{\partial w} = x. Therefore, the gradient passed back upstream is: Lw=Lzx\frac{\partial L}{\partial w} = \frac{\partial L}{\partial z} \cdot x This shows that backpropagation is simply the recursive execution of local node derivative multiplications flowing backward through our graph!

Interactive Practice Quiz

Test your understanding with instant feedback

QUESTION 01

What is the gradient vector x(aTx)\nabla_x (a^T x) with respect to vector xx?

QUESTION 02

Which of the following describes the 'Jacobian Matrix' of a vector-valued function?

QUESTION 03

What is the purpose of the 'Hessian Matrix' in advanced optimization algorithms?

QUESTION 04

During Backpropagation, how is the gradient of a parent node passed back to its input child node?

QUESTION 05

Calculate the gradient vector of the symmetric quadratic form f(x)=xTAxf(x) = x^T A x with respect to vector xx (where AA is symmetric):

QUESTION 06

Compute the gradient vector x(wTx)\nabla_x (w^T x) with respect to column vector xx:

QUESTION 07

For a vector function f:RdRmf: \mathbb{R}^d \to \mathbb{R}^m, what are the dimensions of its Jacobian Matrix JJ?

QUESTION 08

For a scalar function f:RdRf: \mathbb{R}^d \to \mathbb{R}, what are the dimensions of its second-order Hessian Matrix HH?

QUESTION 09

During backpropagation, what is the local gradient contribution of an addition node z=x+yz = x + y with respect to its input xx?

QUESTION 10

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