borch

Foundations · 7

Derivatives & the chain rule

A derivative is the slope of a function — how fast the output moves when the input nudges. borch computes them for you with backward(); the chain rule is the engine underneath, and it is where the next chapter's autograd comes from.

A derivative, by autograd

Mark an input with requiresGrad, build an expression, call backward(), and read .grad. For y = x² the slope is 2x.

The chain rule

For a composition f(g(x)) the slopes multiply: f'(g(x)) · g'(x). For sin(x²) that is cos(x²) · 2x — and autograd agrees to the last digit.

Seeing the slope: the derivative, drawn

autograd does not just give one number — call it at many inputs and you get the whole derivative curve. Here is d/dx sin(x²) = cos(x²)·2x, plotted straight from .grad.

Your turn: finish the chain rule

The hand derivative of sin(x²) below is missing the inner factor from the chain rule. Add it so it matches what autograd computed.

What to remember

If a block above errors, that is worth knowing. Every example on this page runs against the same library the tests run against — nothing here is a screenshot. Press Reset to get the original code back.