Lesson 09
Matrix Calculus
Extend calculus to vectors and matrices with Jacobians and Hessians — the mathematical language of backpropagation.
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:
- 1 input
- 1 output
Derivative:
Now imagine an AI model.
Inputs:
- Area
- Bedrooms
Outputs:
- House Price
- Rent
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:
The Jacobian is:
Don't memorize it yet.
Think of it as a table of sensitivities.
Jacobian Layout (Rows × Columns)
A Jacobian is organized like this:
Rule to remember:
- Rows = Outputs
- Columns = Inputs
Step-by-Step Example
Suppose:
Step 1: Find partial derivatives
For f₁:
For f₂:
Step 2: Build the Jacobian
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:
A rotation transforms them into new coordinates:
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:
Output vector:
The Jacobian becomes:
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
One output → many inputs
Jacobian
Many outputs → many inputs
Memory rule:
- Gradient = Vector
- Jacobian = Matrix
Python Example (PyTorch)
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:
tensor([[1., 1.],
[3., 2.]])This matches our manual Jacobian:
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.
The curve is what the Hessian measures.
First Derivative vs Second Derivative
Suppose:
First derivative
This gives the slope.
Second derivative
This tells us the curvature.
| Derivative | Meaning |
|---|---|
| First | Direction & slope |
| Second | Curvature |
From Gradient to Hessian
For two variables:
Gradient:
Now differentiate the gradient again.
Hessian:
The Hessian is simply the derivative of the gradient.
Hessian Formula
Don't worry about memorizing it.
Just remember:
- Rows = variables
- Columns = variables
- Every cell = second derivative
Step-by-Step Example
Function:
Step 1: First derivatives
Step 2: Differentiate again
Final Hessian
How Hessian identifies surfaces
Positive Hessian → Minimum
Example:
Second derivative:
Bowl shape.
Negative Hessian → Maximum
Example:
Second derivative:
Hill shape.
Mixed Hessian → Saddle Point
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.
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:
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:
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
Vector
Jacobian
2D Matrix
Hessian
Curvature
Python Example (PyTorch)
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:
tensor([[2., 0.],
[0., 2.]])This matches the Hessian of:
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:
One input → One output
But an ML model looks like this:
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
Vector — Multiple values
Matrix — Rows × Columns
As the data becomes larger, the derivative also becomes larger.
Real ML Example
Suppose you're predicting house prices.
Input vector:
Meaning:
| Feature | Value |
|---|---|
| Area | 1200 |
| BHK | 3 |
| Parking | 2 |
Weight vector:
Prediction:
Instead of one variable, we're differentiating with respect to all weights simultaneously.
That is Matrix Calculus.
What is a Gradient?
For one variable:
For many variables:
This is called the Gradient.
Intuition
Imagine loss depends on three weights.
The gradient tells the optimizer which direction reduces loss fastest.
Partial Derivatives Become a Vector
Suppose:
We already know:
Combine them:
This single vector is what Gradient Descent uses.
Why is this important in Neural Networks?
A neural network has millions of weights.
Example:
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.
Each weight receives its own partial derivative during training.
ChatGPT
Each transformer layer contains enormous weight matrices.
Example (conceptually):
Matrix Calculus allows gradients to flow through these matrices efficiently.
Python Example
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):
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.