AI from scratch · Calculus 2 · Foundations
Many inputs: partials, gradients, Jacobians
A real loss doesn't depend on one number, it depends on millions of weights at once. This lesson takes the derivative from a curve to a landscape: slopes along each axis, the gradient arrow that combines them, the shapes of the gradients you'll meet in every model, and the Jacobian, the matrix that says how a whole layer bends space near a point. Backprop turns out to be nothing but multiplying Jacobians in the right order.
1. Slopes on a landscape
Here is \(f(x, y) = x^2 + xy + 2y^2\) seen from above: brighter is higher, and each band is one unit of height, so band edges are contour lines. Drag the yellow point. Freeze \(y\) and walk in \(x\): the slope you feel is the partial derivative \(\partial f/\partial x\), computed by treating \(y\) as a constant. Same for \(y\).
\[ \frac{\partial f}{\partial x} = 2x + y, \qquad \frac{\partial f}{\partial y} = x + 4y, \qquad \nabla f = \begin{pmatrix} 2x + y \\ x + 4y \end{pmatrix} \]
gradient ∇f your direction u along the contour
Stack the partials into a vector and you get the gradient \(\nabla f\). Walk in any direction \(u\) (a unit vector: an arrow of length 1, so it says which way and nothing about how far) and your slope is the directional derivative \(\nabla f \cdot u = \lVert \nabla f \rVert\cos\alpha\), where \(\alpha\) is the angle between them. Turn θ and watch the readout: the slope is biggest, exactly \(\lVert \nabla f \rVert\), when \(u\) points along the gradient, and it's zero when you walk along the contour. That's why the gradient always crosses contour lines at right angles, and why "step against the gradient" is the fastest way down.
A partial derivative is a slope with everything else frozen. Stacked, they make the gradient, and the slope in any direction is the gradient dotted with that direction.
Why do two slopes give the slope in every direction?
- Zoom in far enough on a smooth landscape and it looks like a flat, tilted board (the 2D version of "a curve looks like a line", calculus 1).
- A tilted board is fixed by two numbers: how fast it rises going east, and going north. Those are the two partials.
- Step \(u = (u_1, u_2)\): you go \(u_1\) east and \(u_2\) north, so the height changes by \(\frac{\partial f}{\partial x} u_1 + \frac{\partial f}{\partial y} u_2\).
- That sum is a dot product: \(\nabla f \cdot u\). And chapter 1's \(\lVert a \rVert \lVert b \rVert \cos\theta\) makes it biggest along the gradient and zero along the contour.
Where it breaks: interactions, and units
- Moving many knobs at once. Each partial assumes every other input stays put. Here the \(xy\) term makes the slope in \(x\) depend on \(y\), so changing both by a lot isn't the sum of the two separate effects. Training changes every weight in each step, which is one more reason the steps must be small.
- Units bend "steepest". Measure \(x\) in kilometres instead of metres and \(\partial f/\partial x\) gets 1,000 times bigger, so the gradient swings toward \(x\). "Steepest" depends on how you measure each axis, which is why features are standardized before gradient descent (probability 2).
What is \(\partial f/\partial y\) at the point \((1, 1)\)?
In which direction is the directional derivative zero?
- Along the gradient
- Against the gradient
- Along the contour line, at right angles to the gradient
2. The gradients you'll meet every day
In ML the inputs are vectors and matrices, and a handful of gradients cover nearly everything. Each can be checked one partial at a time, like section 1. A small subscript on the gradient says which knobs it's taken with respect to: \(\nabla_w\) is "the gradient as \(w\)'s numbers change, everything else held fixed".
| function (a number) | gradient | why |
|---|---|---|
| \(w \cdot x\) | \(\nabla_w = x\) | \(\partial/\partial w_i\) of \(\sum w_j x_j\) leaves \(x_i\) |
| \(\lVert w \rVert^2\) | \(\nabla_w = 2w\) | each \(w_i^2\) gives \(2w_i\) (weight decay's pull toward 0) |
| \(\tfrac1n \lVert Xw - y \rVert^2\) | \(\nabla_w = \tfrac2n X^\top (Xw - y)\) | MSE: the errors, sent back through the data |
| \(L(Wx)\), with \(g = \nabla L\) at \(Wx\) | \(\nabla_W = g\,x^\top\) | weight \(W_{ij}\) touches only output \(i\), with strength \(x_j\) |
The last row is the one backprop uses for every layer: the gradient for a weight matrix is an outer product (a table holding every \(g_i\) times every \(x_j\): \(g\) as a column times \(x\) transposed into a row, linear algebra 1), "blame on each output" times "input on each wire". And there's a shape rule that catches most bugs: the gradient of a number with respect to anything has the same shape as that thing. \(W\) is 3×4, so \(\nabla_W L\) is 3×4.
The gradient for a weight matrix is "blame on each output" times "input on each wire", and it always has the matrix's own shape.
Why is a weight matrix's gradient an outer product?
- Output \(i\) of the layer is \(z_i = \sum_j W_{ij} x_j\). The weight \(W_{ij}\) appears in that one output and nowhere else.
- Its slope there is \(\partial z_i / \partial W_{ij} = x_j\): the input on its wire.
- The loss's sensitivity to output \(i\) is \(g_i\). Chain rule: \(\partial L / \partial W_{ij} = g_i\, x_j\).
- A table with \(g_i x_j\) in row \(i\), column \(j\) is \(g\, x^\top\). A wire carrying 0 gets no blame, and an output the loss doesn't care about passes none back.
A layer has a weight matrix \(W\) of shape 3×4. How many numbers are in \(\partial L / \partial W\)?
Now the MSE gradient, checked against central differences (Calculus 1) and then used to train. The data comes from \(y = 1 + 3x_1 - 2x_2\) plus noise; the first column of \(X\) is all ones, so \(w_0\) is the bias.
3. The Jacobian: a matrix for every point
Chapter 1 showed a matrix as a move of the whole plane that keeps grid lines straight. A layer with a nonlinearity bends them. Below, \(F(x, y) = (x + 0.4\sin 1.5y,\; y + 0.4\sin 1.5x)\) warps the grid. But zoom in on any point and the bend disappears: near a point, every smooth map looks like a matrix. That matrix is the Jacobian, the table of every output's partial with respect to every input:
\[ J = \begin{pmatrix} \partial F_1/\partial x & \partial F_1/\partial y \\ \partial F_2/\partial x & \partial F_2/\partial y \end{pmatrix} = \begin{pmatrix} 1 & 0.6\cos 1.5y \\ 0.6\cos 1.5x & 1 \end{pmatrix} \]
the square (drag it) where F sends it where J predicts
Shrink the square: the yellow curved patch and the red parallelogram become the same shape, the 2D version of the secant turning into the tangent. And just as in chapter 1, the Jacobian's determinant is the local area scale: the patch's area divided by the square's tends to \(\det J\). That fact comes back in Calculus 4, when a change of variables has to keep a probability's total area at 1.
Near any point, a smooth map is a matrix: its Jacobian. The Jacobian's determinant is how much the map scales small areas there.
At some point the Jacobian is \(\begin{pmatrix} 2 & 0 \\ 0 & 3 \end{pmatrix}\). A tiny patch of area 0.01 around it is mapped to a patch of about what area?
4. What to carry forward
- A partial derivative is a slope with every other input frozen; the gradient stacks them into the arrow of steepest ascent, at right angles to the contours.
- The slope in direction \(u\) is \(\nabla f \cdot u\): biggest along the gradient, zero along a contour, most negative against it.
- Shape rule: \(\nabla_W L\) has \(W\)'s shape, and for a linear layer it is the outer product \(g\,x^\top\).
- The Jacobian is the local matrix of any smooth map; its determinant is the local area scale.
- Chapter 4 puts these together: backprop is the chain rule carried backwards through a network, one Jacobian at a time.
All chapters: AI from scratch
Last updated