Guide To AI Logo
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

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 f(x)f(x) with respect to a column vector xRdx \in \mathbb{R}^d, the result is a gradient vector of partial derivatives:

This lesson uses the column-gradient convention, so xf(x)=[fx1fx2fxd]Rd×1.\nabla_x f(x)=\begin{bmatrix}\frac{\partial f}{\partial x_1}\\\frac{\partial f}{\partial x_2}\\\vdots\\\frac{\partial f}{\partial x_d}\end{bmatrix}\in\mathbb{R}^{d\times1}. Some references transpose scalar-by-vector derivatives; consistency matters more than the choice.

Expand f(x)=xTAx=ijxiAijxjf(x)=x^TAx=\sum_i\sum_jx_iA_{ij}x_j. A coordinate xkx_k appears once as the left factor and once as the right factor, giving fxk=jAkjxj+ixiAik\frac{\partial f}{\partial x_k}=\sum_jA_{kj}x_j+\sum_i x_iA_{ik}. Stacking the coordinates yields the general identity below.

For the column-gradient convention used here, the general identity is x(xTAx)=(A+AT)x.\nabla_x(x^TAx)=(A+A^T)x. It reduces to 2Ax2Ax when AA is symmetric. The gradient has the same d×1d\times1 shape as xx.

Worked Example 1

Evaluate a Quadratic-Form Gradient

Problem

Let A=[1203]A=\begin{bmatrix}1&2\\0&3\end{bmatrix} and x=(1,1)Tx=(1,-1)^T. Evaluate x(xTAx)\nabla_x(x^TAx).

Step-by-step solution

1.A+AT=[2226]A+A^T=\begin{bmatrix}2&2\\2&6\end{bmatrix}.

2.(A+AT)x=(0,4)T(A+A^T)x=(0,-4)^T.

Final answer and interpretation

The result is a 2×12\times1 gradient, matching the input shape.

Using 2Ax2Ax here would be wrong because AA 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 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.

A Jacobian Maps Input Changes to Output Changes

dxn × 1Jm × ndym × 1×=dy ≈ J dx
Read diagram labels
  • dx
  • n × 1
  • J
  • m × n
  • dy
  • m × 1
  • ×
  • =
  • dy ≈ J dx
Worked Example 1

Jacobian with an Explicit Shape

Problem

For f(x,y)=(x2y,x+y)Tf(x,y)=(x^2y,\,x+y)^T, find JfJ_f at (2,3)(2,3).

Step-by-step solution

1.Jf=[2xyx211]J_f=\begin{bmatrix}2xy&x^2\\1&1\end{bmatrix}.

2.Jf(2,3)=[12411]J_f(2,3)=\begin{bmatrix}12&4\\1&1\end{bmatrix}.

Final answer and interpretation

Two outputs differentiated with respect to two inputs produce a 2×22\times2 Jacobian.

Worked Example 2

Hessian and Curvature

Problem

Find the Hessian of g(x,y)=x2+xy+2y2g(x,y)=x^2+xy+2y^2 and classify its curvature.

Step-by-step solution

1.H=[2114]H=\begin{bmatrix}2&1\\1&4\end{bmatrix}.

2.Its leading minors are 2>02>0 and 81=7>08-1=7>0, so HH is positive definite.

Final answer and interpretation

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 LL with respect to every internal weight ww 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 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 upstream is Lw=Lzx\frac{\partial L}{\partial w}=\frac{\partial L}{\partial z}x. Reverse-mode automatic differentiation reuses these local products instead of constructing a full Jacobian at every node.

Forward Values and Reverse-Mode Gradients

xz=xw+bŷ=σ(z)L(ŷ,y)forward computation →← local derivatives multiply backward
Read diagram labels
  • x
  • z=xw+b
  • ŷ=σ(z)
  • L(ŷ,y)
  • forward computation →
  • ← local derivatives multiply backward
Worked Example 1

Complete Numerical Forward and Backward Pass

Problem

For x=3x=3, w=2w=2, b=1b=1, compute z=wx+bz=wx+b, y^=z2\hat y=z^2, and L=(y^25)2L=(\hat y-25)^2, then find L/w\partial L/\partial w.

Step-by-step solution

1.Forward: z=2(3)+1=7z=2(3)+1=7, y^=49\hat y=49, and L=(4925)2=576L=(49-25)^2=576.

2.Backward local derivatives: L/y^=2(4925)=48\partial L/\partial\hat y=2(49-25)=48, y^/z=2z=14\partial\hat y/\partial z=2z=14, and z/w=x=3\partial z/\partial w=x=3.

Final answer and interpretation

L/w=48143=2016\partial L/\partial w=48\cdot14\cdot3=2016. Also L/b=4814=672\partial L/\partial b=48\cdot14=672.

Backpropagation caches forward values and multiplies local derivatives in reverse topological order.

Worked Example 2

Cumulative Shape Check

Problem

If f:R4R3f:\mathbb{R}^4\to\mathbb{R}^3 feeds a scalar loss LL, what are the shapes of JfJ_f and xL\nabla_xL?

Step-by-step solution

1.JfJ_f has one row per output and one column per input, so it is 3×43\times4.

2.Reverse mode multiplies JfT(4×3)J_f^T(4\times3) by fL(3×1)\nabla_fL(3\times1).

Final answer and interpretation

Thus xL\nabla_xL is 4×14\times1.

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?