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
- A derivative is a function's slope; the chain rule multiplies the slopes of nested functions:
f'(g(x))·g'(x). - Mark an input with
requiresGrad, build an expression, callbackward(), read.grad. - This is exactly the engine the next chapter's autograd runs on.
- In borch:
Tensor.from(v, [], { requiresGrad: true }),backward(),.grad.