Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 09

Matrix Calculus

Extend calculus to vectors and matrices with Jacobians and Hessians — the mathematical language of backpropagation.

Beginner30 min

jacobian

The Jacobian Matrix is the first major concept in Matrix Calculus. It extends derivatives from one output to multiple outputs.

Jacobian = A matrix containing all first-order partial derivatives of a vector-valued function.

In simple words:

  • Gradient → One output, many inputs
  • Jacobian → Many outputs, many inputs

Why do we need a Jacobian?

In calculus, you learned:

Equation y = x²
  • 1 input
  • 1 output

Derivative:

Equation (dy)/(dx)

Now imagine an AI model.

Inputs:

  • Area
  • Bedrooms

Outputs:

  • House Price
  • Rent
Diagram

Now we have:

  • 2 inputs
  • 2 outputs

A single derivative is not enough.

We need a Jacobian Matrix.

What is a Jacobian?

Suppose the function is:

Equation F(x,y) column vector

The Jacobian is:

Equation Jacobian matrix of partial derivatives

Don't memorize it yet.

Think of it as a table of sensitivities.

Jacobian Layout (Rows × Columns)

A Jacobian is organized like this:

Diagram

Rule to remember:

  • Rows = Outputs
  • Columns = Inputs

Step-by-Step Example

Suppose:

Equation f₁ = x + y
Equation f₂ = xy

Step 1: Find partial derivatives

For f₁:

Equation (∂f₁)/(∂x) = 1
Equation (∂f₁)/(∂y) = 1

For f₂:

Equation (∂f₂)/(∂x) = y
Equation (∂f₂)/(∂y) = x

Step 2: Build the Jacobian

Equation J = [[1, 1], [y, x]]

That is the complete Jacobian Matrix.

ML Use Case: House Price + Rent Prediction

Inputs:

Feature Value
Area 1200
Bedrooms 3

Outputs:

Output Meaning
Price Selling price
Rent Monthly rent

The Jacobian tells us:

Area Bedrooms
Price 4500 8,00,000
Rent 22 3500

Interpretation:

  • Increasing Area affects both Price and Rent.
  • Increasing Bedrooms also affects both outputs.

The Jacobian captures all relationships at once.

ML Use Case: Image Transformation

An image has coordinates:

Equation (x, y)

A rotation transforms them into new coordinates:

Equation (x', y')
Diagram

Computer vision uses Jacobians to compute how tiny changes in pixels affect transformed images.

Used in:

  • Image warping
  • Optical flow
  • SLAM (robotics)

ML Use Case: Neural Networks

Suppose one layer has:

Input vector:

Equation column vector [x₁, x₂, x₃]

Output vector:

Equation column vector [y₁, y₂]

The Jacobian becomes:

Equation (∂Y)/(∂X)

This is exactly what backpropagation multiplies through every layer using the Chain Rule.

Without Jacobians, gradients could not flow through multi-output networks.

Gradient vs Jacobian

This is the most important distinction.

Gradient Jacobian
One output Multiple outputs
Many inputs Many inputs
Vector Matrix
Shape: n × 1 Shape: m × n

Visual comparison

Gradient

Diagram

One output → many inputs

Jacobian

Diagram

Many outputs → many inputs

Memory rule:

  • Gradient = Vector
  • Jacobian = Matrix

Python Example (PyTorch)

python
import torch

x = torch.tensor([2., 3.], requires_grad=True)

def f(x):
    return torch.stack([
        x[0] + x[1],
        x[0] * x[1]
    ])

J = torch.autograd.functional.jacobian(f, x)

print(J)

Output:

text
tensor([[1., 1.],
        [3., 2.]])

This matches our manual Jacobian:

Equation matrix [[1, 1], [3, 2]]

Summary

Concept Meaning
Jacobian Matrix of first-order partial derivatives
Rows Outputs
Columns Inputs
Shape Outputs × Inputs
Used In Backpropagation, Computer Vision, Robotics, Neural Networks

Memory Trick

  • Gradient → One output = Vector
  • Jacobian → Many outputs = Matrix
  • Hessian (next lesson) → Second derivatives = Matrix of curvature

Interview Question

Q: What is the Jacobian Matrix in Machine Learning?

Answer: The Jacobian is a matrix containing the first-order partial derivatives of multiple outputs with respect to multiple inputs. It measures how each output changes when each input changes and is fundamental to backpropagation, neural network differentiation, and many computer vision algorithms.

hessian

After learning the Gradient and Jacobian, the next concept is the Hessian Matrix.

Hessian Matrix = A matrix of second-order partial derivatives that measures the curvature of a function.

If the gradient tells you which direction to move, the Hessian tells you how steep or curved the surface is.

This is essential in optimization algorithms and understanding loss landscapes in deep learning.

Why do we need the Hessian?

Imagine you're hiking.

The gradient tells you:

"Walk downhill."

But it doesn't tell you whether you're on:

  • a gentle slope
  • a steep valley
  • the top of a hill
  • a saddle point

The Hessian provides that information.

Diagram

The curve is what the Hessian measures.

First Derivative vs Second Derivative

Suppose:

Equation f(x) = x²

First derivative

Equation (df)/(dx) = 2x

This gives the slope.

Second derivative

Equation d²f/dx² = 2

This tells us the curvature.

Derivative Meaning
First Direction & slope
Second Curvature

From Gradient to Hessian

For two variables:

Equation f(x, y) = x² + y²

Gradient:

Equation ∇f = [[2x], [2y]]

Now differentiate the gradient again.

Hessian:

Equation H = [[2, 0], [0, 2]]

The Hessian is simply the derivative of the gradient.

Hessian Formula

Equation Hessian matrix of second partial derivatives

Don't worry about memorizing it.

Just remember:

  • Rows = variables
  • Columns = variables
  • Every cell = second derivative

Step-by-Step Example

Function:

Equation f(x, y) = x² + 3xy + y²

Step 1: First derivatives

Equation (∂f)/(∂x) = 2x + 3y
Equation (∂f)/(∂y) = 3x + 2y

Step 2: Differentiate again

Equation ∂²f/∂x² = 2
Equation ∂²f/∂x∂y = 3
Equation ∂²f/∂y∂x = 3
Equation ∂²f/∂y² = 2

Final Hessian

Equation H = [[2, 3], [3, 2]]

How Hessian identifies surfaces

Positive Hessian → Minimum

Diagram

Example:

Equation x²

Second derivative:

Equation 2 > 0

Bowl shape.

Negative Hessian → Maximum

Diagram

Example:

Equation -x²

Second derivative:

Equation -2 < 0

Hill shape.

Mixed Hessian → Saddle Point

Diagram

One direction curves up.

Another curves down.

This is common in deep learning loss landscapes.

AI Use Case: Loss Function Curvature

Imagine a loss function.

Diagram

Gradient tells:

Move right.

Hessian tells:

The valley is becoming flatter.

Optimization algorithms like Newton's Method use both gradient and Hessian to converge faster.

AI Use Case: Logistic Regression

Loss surface:

Diagram

The Hessian is positive.

This guarantees a unique minimum.

That's why logistic regression is easier to optimize than deep neural networks.

AI Use Case: Deep Learning

A neural network has millions of weights.

The Hessian becomes enormous.

Example:

Equation H in mathbb{R}^{10⁶ × 10⁶}

Computing the full Hessian is extremely expensive.

Therefore, most deep learning uses:

  • Gradient
  • Approximate Hessian
  • Adam Optimizer
  • SGD

Instead of the full matrix.

Gradient vs Jacobian vs Hessian

Concept Shape Used For
Gradient Vector One output
Jacobian Matrix Multiple outputs
Hessian Matrix Second derivatives

Visual memory

Gradient

Diagram

Vector

Jacobian

Diagram

2D Matrix

Hessian

Diagram

Curvature

Python Example (PyTorch)

python
import torch

x = torch.tensor([2., 3.], requires_grad=True)

def f(x):
    return x[0]**2 + x[1]**2

H = torch.autograd.functional.hessian(f, x)

print(H)

Output:

text
tensor([[2., 0.],
        [0., 2.]])

This matches the Hessian of:

Equation x² + y²

Summary

Concept Meaning
Hessian Matrix of second-order derivatives
Purpose Measure curvature
Positive Minimum
Negative Maximum
Mixed Saddle point
Used In Newton's Method, Optimization, Deep Learning Research

Memory Trick

  • Gradient → Which way should I go?
  • Jacobian → How do multiple outputs change?
  • Hessian → How curved is the surface?

Interview Question

Q: Why isn't the Hessian commonly used in large neural networks?

Answer: The Hessian grows quadratically with the number of parameters. For millions of weights, storing and computing the full Hessian is computationally impractical, so deep learning typically relies on first-order methods like SGD and Adam, or uses approximate second-order information when needed.

introduction to matrix calculus

You've completed:

  • Linear Algebra
  • Calculus
  • Probability & Statistics
  • Loss Functions
  • Gradient Descent
  • Numerical Methods

Now you enter the mathematics that powers Backpropagation.

Matrix Calculus is the extension of calculus to vectors and matrices, allowing us to compute derivatives when variables are multi-dimensional.

This is the mathematical language used by PyTorch, TensorFlow, and every neural network.

Why do we need Matrix Calculus?

In school, derivatives looked like this:

Equation y = x²

One input → One output

But an ML model looks like this:

Diagram

Now we have:

  • Many inputs
  • Many weights
  • Many outputs

A normal derivative is no longer enough.

We need Matrix Calculus.

Scalar vs Vector vs Matrix Derivative

Scalar — One value

Equation x

Vector — Multiple values

Equation column vector [x₁, x₂, x₃]

Matrix — Rows × Columns

Equation matrix [[1, 2], [3, 4]]

As the data becomes larger, the derivative also becomes larger.

Real ML Example

Suppose you're predicting house prices.

Input vector:

Equation X = [1200, 3, 2]

Meaning:

Feature Value
Area 1200
BHK 3
Parking 2

Weight vector:

Equation W = [0.05, 10, 4]

Prediction:

Equation Y = WT X

Instead of one variable, we're differentiating with respect to all weights simultaneously.

That is Matrix Calculus.

What is a Gradient?

For one variable:

Equation (dy)/(dx)

For many variables:

Equation gradient as column of partial derivatives

This is called the Gradient.

Intuition

Imagine loss depends on three weights.

Diagram

The gradient tells the optimizer which direction reduces loss fastest.

Partial Derivatives Become a Vector

Suppose:

Equation L = x² + y²

We already know:

Equation (∂L)/(∂x) = 2x
Equation (∂L)/(∂y) = 2y

Combine them:

Equation ∇L = [[2x], [2y]]

This single vector is what Gradient Descent uses.

Why is this important in Neural Networks?

A neural network has millions of weights.

Example:

Diagram

Every line has its own weight:

  • w₁
  • w₂
  • w₃
  • …
  • w₁{ 000{,}000}

Matrix Calculus computes the derivative of the loss with respect to every weight.

This is called Backpropagation.

AI Use Cases

House Price Prediction

Features:

  • Area
  • BHK
  • Parking

Weights are updated together using a gradient vector, not one derivative.

Image Classification

A CNN filter may contain a 3×3 matrix of weights.

Diagram

Each weight receives its own partial derivative during training.

ChatGPT

Each transformer layer contains enormous weight matrices.

Example (conceptually):

Equation W in mathbb{R}⁴⁰⁹⁶ × ⁴⁰⁹⁶

Matrix Calculus allows gradients to flow through these matrices efficiently.

Python Example

python
import torch

# Input vector
x = torch.tensor([1200., 3., 2.])

# Weight vector (requires gradient)
w = torch.tensor([0.05, 10., 4.], requires_grad=True)

# Prediction
y = torch.dot(w, x)

# Backpropagation
y.backward()

print(w.grad)

Output (conceptually):

text
tensor([1200., 3., 2.])

PyTorch automatically computes the gradient vector using Matrix Calculus.

Roadmap of Matrix Calculus

Lesson Topic
1 Introduction to Matrix Calculus
2 Jacobian Matrix
3 Hessian Matrix
4 Vector Chain Rule
5 Backpropagation Mathematics

Summary

Concept Meaning
Scalar Derivative One variable
Partial Derivative One variable while others are fixed
Gradient Vector of all partial derivatives
Matrix Calculus Calculus for vectors and matrices
Used In Backpropagation, PyTorch, TensorFlow

Memory Trick

  • Derivative → One variable
  • Partial Derivative → One variable among many
  • Gradient → All partial derivatives together
  • Matrix Calculus → The math that lets neural networks learn millions of weights

Interview Question

Q: Why is Matrix Calculus essential in Deep Learning?

Answer: Neural networks contain vectors and matrices of parameters rather than single variables. Matrix Calculus extends derivatives to these multi-dimensional structures, enabling gradients to be computed for every weight simultaneously during backpropagation, which makes efficient learning possible.