Lensa ML
Lensa ML

How Matrix Calculus Works

Step 1 of 6

Partial Derivatives

Hold one variable fixed, differentiate the other

x1.50
y1.00
xy∂f/∂x∂f/∂y(1.5, 1.0)
∂f/∂x = 2x
3.00
∂f/∂y = 2y
2.00
f(x,y) = x² + y²
3.25

The surface is steeper in the x-direction (∂f/∂x = 3.0) than in y (∂f/∂y = 2.0). Moving along x changes f faster.

Step 1 of 6: Partial Derivatives

Hold one variable fixed, differentiate the other

x1.50
y1.00
xy∂f/∂x∂f/∂y(1.5, 1.0)
∂f/∂x = 2x
3.00
∂f/∂y = 2y
2.00
f(x,y) = x² + y²
3.25

The surface is steeper in the x-direction (∂f/∂x = 3.0) than in y (∂f/∂y = 2.0). Moving along x changes f faster.

Step 2 of 6: The Gradient Vector

Combine partials into a vector that points uphill

x2.00
y1.00
∇f
∇f_x = 2x
4.00
∇f_y = 2y
2.00
|∇f| MAGNITUDE
4.47

The gradient is large (|∇f| = 4.5) — the surface is very steep here. The arrow ∇f = [4.0, 2.0] points directly away from the center.

Step 3 of 6: Gradient Points Uphill

The gradient always points toward steepest ascent

x1.50
y-1.00
DIRECTION (°)
-34°
MAGNITUDE
3.61
f(x,y)
3.25

Every arrow points away from the center (uphill on the paraboloid). Negate the gradient to get the steepest descent direction — that's exactly what gradient descent does!

Step 4 of 6: Functions with Vector Outputs

Mapping grids through linear transformations

a1.00
b0.30
c-0.20
d1.00
J = [1.0, 0.3; -0.2, 1.0]
det(J)
1.06
a
1.0
b
0.3
c
-0.2
d
1.0

det(J) = 1.06 > 1 — the transformation expands area. Each unit square becomes 1.06× larger.

Step 5 of 6: The Jacobian Matrix

How a small patch of space gets stretched and rotated

a1.30
b0.40
c-0.30
d1.10
inputoutput
det(J)
1.55
|det(J)| AREA RATIO
1.55
INPUT AREA
1
OUTPUT AREA
1.55

The unit square (area = 1) maps to a parallelogram with area |det(J)| = 1.55. The Jacobian determinant is the local area scaling factor at every point.

Step 6 of 6: Jacobians in Backprop

Gradients flow backward through a chain of Jacobians

w₁ (layer 1 weight)0.80
w₂ (layer 2 weight)1.20
FORWARD SIGNALBACKWARD GRADIENT×w₁×w₂1.000.800.96xw₁·xw₂·w₁·x×J₂=w₂×J₁=w₁0.961.201.00J_total = w₁ × w₂ = 0.96
w₁
0.80
w₂
1.20
J_TOTAL (w₁×w₂)
0.96
GRADIENT
0.96

|w₁ × w₂| = 0.96 ≈ 1 — the gradient is stable! This is the sweet spot where learning signals propagate without vanishing or exploding.