Skip to content

Gradients and the chain rule

The gradient points uphill on the loss surface, and the chain rule multiplies derivatives through each stage of a model. Work both by hand on a one-neuron model, then see why the same steps scale to millions of weights.

Applied ~13 min7 steps

You will be able to

  • Compute partial derivatives and assemble the gradient of a loss
  • Explain why the gradient is the direction of steepest increase
  • Apply the chain rule to a composition of functions, including the Jacobian form

Read first

01

One slope per parameter

Read

A loss L(w1,w2)L(w_1, w_2) depends on two weights. The partial derivative ∂L/∂w1\partial L/\partial w_1 is the slope when you nudge w1w_1 and hold w2w_2 still.

Stack the partials and you get the gradientGradientThe vector of a function's partial derivatives: it points in the direction of steepest increase.:

∇L=(∂L/∂w1∂L/∂w2).\nabla L = \begin{pmatrix} \partial L/\partial w_1 \\ \partial L/\partial w_2 \end{pmatrix}.

For L=w12+3w22L = w_1^2 + 3w_2^2, ∇L=(2w1,  6w2)\nabla L = (2w_1,\; 6w_2). At (1,1)(1, 1) it is (2,6)(2, 6): the loss climbs three times faster along w2w_2.

02

Why it points uphill

Read

For a small step δ\delta, the change in loss is about ∇L⋅δ\nabla L \cdot \delta. Among all steps of the same length, that dot product is largest when δ\delta points along ∇L\nabla L and most negative when it points the opposite way.

So ∇L\nabla L is the direction of steepest increase, −∇L-\nabla L of steepest decrease, and the gradient is always at right angles to the contour lines of the loss. Gradient descent steps along −∇L-\nabla L.

DExplorable · Gradient descent on a loss surface

Why does the learning rate decide whether training works at all?

With this learning rate and this surface, what will the path do?

How sure?

Surface

Optimizer

0.050
60
20
0.0

Orange dot: the start. Blue dot: where the path has reached. Darker shading is higher loss.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

03

Functions of functions

Read

Models are stacks: a linear score, then a squashing function, then a loss. The chain ruleChain ruleThe derivative of a composition is the product of the derivatives of its pieces, taken along the path. says the derivative of a composition is the product of the pieces' derivatives:

ddwf(g(w))=f′(g(w)) g′(w).\frac{d}{dw} f\big(g(w)\big) = f'\big(g(w)\big)\, g'(w).

Each stage only needs its own local derivative. Multiply them along the path from the parameter to the loss.

04

A one-neuron model by hand

Read

Score z=wx+bz = wx + b, probability p=σ(z)=1/(1+e−z)p = \sigma(z) = 1/(1+e^{-z}), loss L=−[ylog⁡p+(1−y)log⁡(1−p)]L = -[y\log p + (1-y)\log(1-p)].

The local derivatives are

∂L∂p=p−yp(1−p),∂p∂z=p(1−p),∂z∂w=x.\frac{\partial L}{\partial p} = \frac{p-y}{p(1-p)}, \qquad \frac{\partial p}{\partial z} = p(1-p), \qquad \frac{\partial z}{\partial w} = x .

Multiply them and most of it cancels:

∂L∂w=(p−y) x,∂L∂b=p−y.\frac{\partial L}{\partial w} = (p - y)\,x, \qquad \frac{\partial L}{\partial b} = p - y .

That is the gradient logistic regression uses, summed over rows: X⊤(p−y)X^\top(p - y).

05

Many outputs: the Jacobian

Read

When a stage maps a vector to a vector, its derivative is a matrix, the JacobianJacobianFor a function with several outputs, the table of every output's derivative with respect to every input.: entry (i,j)(i, j) is ∂(output i)/∂(input j)\partial(\text{output } i)/\partial(\text{input } j).

The chain rule keeps its shape: the Jacobian of a composition is the product of the Jacobians, Jf∘g=Jf JgJ_{f\circ g} = J_f\, J_g. A deep network is a long product of Jacobians, one per layer. The order you multiply them in turns out to matter enormously for cost, which is the subject of the next lesson.

06

Check: gradients

Checkpoint

Check yourself

L(w1,w2)=w12+3w22L(w_1, w_2) = w_1^2 + 3 w_2^2. What is ∂L/∂w2\partial L/\partial w_2 at (w1,w2)=(5,2)(w_1, w_2) = (5, 2)?

Show the answer

Answer: 12

∂L/∂w2=6w2=12\partial L/\partial w_2 = 6 w_2 = 12.

Check yourself

Which statement about the gradient at a point is true?

Show the answer

Answer: a

The gradient is a local quantity: steepest ascent right here. It need not point at the minimum, which is why stretched bowls make descent zigzag.

Check yourself

h(w)=(3w+1)2h(w) = (3w + 1)^2. Use the chain rule: what is h′(1)h'(1)?

Show the answer

Answer: 24

Outer derivative 2(3w+1)2(3w+1), inner derivative 3. At w=1w = 1: 2⋅4⋅3=242 \cdot 4 \cdot 3 = 24.

Check yourself

In the one-neuron logistic model, the prediction is p=0.9p = 0.9 for a row with y=1y = 1 and x=2x = 2. What is ∂L/∂w\partial L/\partial w?

Show the answer

Answer: a

∂L/∂w=(p−y)x\partial L/\partial w = (p - y)x. It is negative, so a gradient step raises ww and pushes pp further toward 1.

Check yourself

A layer maps 4 inputs to 3 outputs. What shape is its Jacobian?

Show the answer

Answer: a

One row per output, one column per input: 3×43 \times 4.

07

Recap

Recap
  • The gradient stacks partial derivatives and points in the direction of steepest increase.
  • The chain rule multiplies local derivatives along the path from parameter to loss.
  • For vector stages the local derivatives are Jacobians, and a network is a product of them.

Concepts in this lesson