Calculus
This lesson assembles the differential calculus used in training networks: the gradient and directional derivative, the Jacobian and Hessian, and the chain rule in scalar, vector, and matrix form. From the chain rule it derives back-propagation as a single sweep over the computational graph, tabulates the matrix-calculus identities that recur in layer gradients, reads optimization off a second-order Taylor expansion, and ends with why reverse-mode automatic differentiation is the algorithm every framework runs.
╌╌╌╌
A network learns by descending a loss, and descent needs a direction: the gradient. Every weight update a model ever makes is a derivative of the loss with respect to a parameter, and the algorithm that computes all of those derivatives at once, back-propagation, is the chain rule applied with care to a graph of operations. This lesson is the differential calculus that machinery rests on, kept to the objects a network differentiates and the one rule that ties them together.1
Derivatives, gradients, and directions
For a scalar function of a scalar, , the derivative is the local rate of change: scale a small step and the output moves by about . Deep learning almost never has one input, though. A loss is a function of millions of parameters at once, , so the single derivative becomes a vector of partial derivatives, each holding all coordinates fixed but one.
The steepest
claim follows from the directional derivative,
the rate of change of as you move along a unit vector . It equals
, and since ,
it is largest when aligns with and most negative when points the
opposite way.1 That is the whole justification for moving along
: among all directions of a fixed length, it decreases fastest.
For example, take , a stretched bowl, evaluated at . The partials are and , so
Now compare three unit directions and read the directional derivative off each. Along the steepest direction , the slope is — the maximum. Along a horizontal step it is , and along it is : the bowl climbs three times faster in the direction, exactly because that axis has the steeper coefficient. Every one of these is with the corresponding angle, and the largest, , belongs to . Descent flips the sign: stepping along drops at rate , faster than any other unit step.
The Jacobian and the Hessian
When the output is itself a vector, as it is at every hidden layer (), the first derivative is a matrix.
For example, the softmax layer maps ; its Jacobian entries are . At the outputs are , so
Each row sums to zero — a nudge that raises one logit lowers the other output by the same amount, since softmax outputs must keep summing to one. That structural fact, visible directly in the Jacobian, is why softmax gradients redistribute probability rather than create it.
Second derivatives measure curvature. For a scalar loss the matrix of second partials is the Hessian , with ; because differentiation order does not matter for the smooth losses we meet, is symmetric. Its eigenvalues are the curvatures along the principal directions, and their spread — the condition number — decides how hard the loss surface is to descend: a direction of large curvature forces a small step, while a direction of small curvature then crawls.2 For the bowl the Hessian is , already diagonal, so its eigenvalues are and : the axis curves three times as sharply. The condition number is , a mild stretch here, but real losses reach in the thousands, and that ratio is precisely how many more steps gradient descent needs. Curvature is what separates first-order methods (gradient descent, which sees only ) from second-order ones (Newton's method, which divides by ).
The chain rule
Composition is the one operation a network does over and over, since a layer feeds the next, so the rule for differentiating a composition is the rule that matters most. For scalars, if and , then
Local rates multiply. The vector form is the same statement with Jacobians in place of derivatives, and order now matters because matrix products do not commute: for , , and a scalar ,
where is the Jacobian of .3 Read right to left, this says: take the gradient with respect to the later quantity , then pull it back through the layer's Jacobian to get the gradient with respect to the earlier quantity . Stack many layers and you multiply many Jacobians — and the order in which you multiply them is the difference between a tractable algorithm and an intractable one.
Back-propagation: the chain rule on a graph
Write the network as a computational graph: a directed acyclic graph whose nodes are intermediate values and whose edges are the operations that produce them. The forward pass evaluates nodes in topological order to reach the loss. The backward pass then visits the nodes in reverse order, and at each one multiplies the incoming gradient by that node's local derivative — the chain rule, applied one edge at a time.
The local factors read off the operations directly: comes from the loss, is the activation's slope, and is the linear layer's weight. Each blue arrow multiplies the gradient arriving from its right by one local derivative, which is all back-propagation ever does.
Writing for the gradient of the loss with respect to a node , the backward sweep is the recurrence , summed over the children that feeds. The key property is the cost: one forward and one backward pass compute the gradient with respect to every parameter in time proportional to the forward pass itself, independent of how many parameters there are.4 A full gradient for roughly the price of one evaluation is what makes training networks with millions of weights feasible at all.
A backward pass by hand
For example, take the single-neuron graph above with , , input , a squared-error loss against target , and a activation. The forward pass fills in each node:
The backward pass starts at the output with and multiplies one local derivative per edge, right to left:
The weight gradient is tiny even though the loss is large: has saturated at , so throttles every gradient flowing back through it. This is the vanishing-gradient mechanism in miniature — a saturated activation multiplies the backward signal by nearly zero, and stacking many such factors is why deep sigmoid/tanh networks were once hard to train.
When a value feeds two places
The single-neuron chain hid a subtlety: what happens when a node feeds more than one child? The recurrence says the gradients add. Consider used twice, once as and once as , with the loss . The forward graph forks at and rejoins at ; the backward pass sends a gradient down each branch and sums them at :
With both , so — which is exactly for , checked directly. The multivariate chain rule is this summation: a shared value accumulates one contribution per path it influences the loss through. In a real network the shared value is a layer's activation feeding every unit of the next layer, and summing those contributions is what the matrix-transpose in performs at once.
Matrix calculus: the identities that recur
Layer gradients are the same handful of expressions over and over, so it pays to know them by shape rather than rederiving each one. With vectors and matrices of compatible shape:
| Expression | Gradient | Where it shows up |
|---|---|---|
| a linear readout | ||
| an penalty | ||
| a quadratic form / curvature term | ||
| a linear layer's weight gradient | ||
| weight decay |
None of these need memorizing as facts; each falls out of the scalar rules applied componentwise. Take the quadratic form . Differentiating with respect to , the terms that survive are those containing : one from (giving , the -th entry of ) and one from (giving , the -th entry of ). Their sum is the -th entry of , which is the whole gradient. When is symmetric this collapses to the familiar , the multivariate analogue of .
The outer-product shape of the linear layer's gradient, , is worth memorizing: the weight gradient is the upstream gradient times the layer's input, transposed — exactly the local rule the graph above applies at a matrix multiply. One small case makes the shapes concrete. Let a layer have input , and suppose the backward pass has delivered an upstream gradient at the layer's three outputs. Then
a matrix matching exactly, while the gradient passed further back to the input is . Row , column of is : output 's error scaled by the input that fed the weight connecting them. The shapes line up as an outer product, an column times a row giving the gradient that matches exactly:
Taylor expansion and what curvature buys
Truncating the Taylor series turns a messy loss into a polynomial we can optimize in closed form, which is where the optimization algorithms come from. Around a point , a step changes the loss by
Keep only the linear term and minimizing pushes along : that is gradient descent, and the step size must stay small enough for the linear approximation to hold. Keep the quadratic term and minimizing over gives : Newton's method, which rescales the gradient by the inverse curvature so every direction descends at a matched rate.5
The one-step property is easiest to see on the very bowl from earlier, , starting from . Its gradient is and its Hessian is the constant , so the Newton step is
landing at — the exact minimum, in a single step, from a starting point where plain gradient descent would zig-zag because the axis has three times the curvature of . Newton divides each coordinate by its own curvature ( and ), undoing the conditioning that gradient descent's single step size cannot. On a quadratic this is exact; on a real loss it holds only locally, and the Hessian is far too large to invert. Newton is too expensive to run on a full network (the Hessian is parameter-by-parameter), but the expansion explains why poorly conditioned losses are slow for plain gradient descent and why so much of optimization is, in effect, cheap approximations to .
Automatic differentiation
Back-propagation is one instance of automatic differentiation (autodiff):
mechanically applying the chain rule across a program's operations, each of which
ships with a known local derivative. Two orderings are possible. Forward mode
pushes derivatives along with values during the forward pass, which is efficient
when there are few inputs and many outputs. Reverse mode records the forward
computation, then walks it backward, efficient when there are many inputs and one
output. A loss maps many inputs to a single output, so reverse-mode autodiff is
back-propagation, and it is what every framework's automatic-gradient engine
runs.6 In practice you write only the forward computation; the engine
records each operation on a tape and replays it in reverse to accumulate every
gradient, so a model's backward step is autodiff, not a derivative you ever code
by hand.7
The choice of mode is a cost argument about matrix-product association. Composing layers, the chain rule is a product of Jacobians . Forward mode multiplies right to left, carrying an tangent forward; reverse mode multiplies left to right, carrying a gradient backward. For inputs and output, reverse mode touches each intermediate once and wins decisively.
For example, a network with parameters and a single scalar loss would, under forward mode, need one sweep per input direction to fill in all partials — roughly forward passes for one gradient. Reverse mode records a single forward pass, then a single backward pass recovers all partials at once, because the gradient it carries backward already has one slot per parameter. The asymmetry is entirely about which end has one number: differentiate a map instead and forward mode would win by the same margin. Losses are always the many-in, one-out shape, so every training framework defaults to reverse mode.
Differentiation as a programmable object
The chain rule Goodfellow presents as a fixed backward sweep has, since 2016, become a programmable object. Three developments matter for practice.
Composable transforms. The JAX system (Bradbury et al., 2018) exposes
reverse-mode (grad) and forward-mode (jvp) as functions that take a function
and return a function, so they compose freely. A Hessian-vector product ,
needed by second-order and curvature-based methods, is then just forward-over-reverse
differentiation, , computed in the cost of two passes and
without ever forming the Hessian. That trick is what makes
Newton-flavored ideas usable on networks the naive inverse rules out.
Trading memory for compute. Reverse mode must store every intermediate from the forward pass to reuse on the way back, and for a deep network that tape is the memory bottleneck. Gradient checkpointing (Chen et al., 2016) keeps only a sparse set of activations and recomputes the rest during the backward pass, cutting memory from to at the price of one extra forward pass — the standard way very deep or very wide models fit in device memory.
Differentiating through everything. Once autodiff is a general program
transformation, the operations
it differentiates need not be layers: physics
simulators, ODE solvers (the neural-ODE line of Chen et al., 2018, which
back-propagates through an adaptive integrator via the adjoint method), and even
optimization solvers become differentiable subroutines. The single idea of this
lesson — local derivatives multiplied along a graph in reverse — is what all of
them run underneath.89
Takeaways
- The gradient points in the direction of steepest increase; descent moves along because it decreases the loss fastest among all directions.
- The Jacobian is the first derivative of a vector-valued map; the Hessian is the second derivative of a scalar loss, and its eigenvalue spread (conditioning) governs how hard the surface is to descend.
- The chain rule multiplies local rates; in vector form it pulls a gradient back through a layer's Jacobian, and the multiplication order is what makes the algorithm cheap or expensive.
- Back-propagation is the chain rule run in reverse topological order over the computational graph, computing every parameter's gradient for about the cost of one forward pass.
- A second-order Taylor expansion reads off gradient descent (linear term) and Newton's method (quadratic term), and explains why conditioning controls convergence.
- Reverse-mode automatic differentiation is back-propagation generalized to
arbitrary programs, and it is the algorithm the framework runs when you call
backward.
Footnotes
- Goodfellow, §4.3 — Gradient-Based Optimization: the gradient as the direction of steepest ascent and the directional-derivative argument for descending along its negative. ↩ ↩2
- Goodfellow, §4.3.1 — the Hessian's eigenvalues as directional curvatures, and the condition number as the cause of slow, zig-zagging descent on poorly scaled problems. ↩
- Goodfellow, §6.5.2 — the chain rule of calculus in scalar and vector (Jacobian) form, the basis for propagating gradients through composed functions. ↩
- Goodfellow, §6.5 — Back-Propagation: computing the gradient of the loss with respect to all parameters in a single reverse sweep at the cost of the forward pass. ↩
- Goodfellow, §4.3 — the second-order Taylor expansion, the gradient-descent step from its linear term, and Newton's step from the quadratic term. ↩
- Goodfellow, §6.5.9 — back-propagation as a special case of automatic differentiation, and the reverse-mode ordering for the many-inputs, single-output case. ↩
- Stevens, Deep Learning with PyTorch, Ch. 5 — autograd records operations on the forward pass and replays them in reverse, so
backwardaccumulates gradients without hand-coded derivatives. ↩ - Bradbury, Frostig, Hawkins, Johnson, Leary, Maclaurin, Necula, Paszke, VanderPlas, Wanderman-Milne, Zhang (2018), JAX: composable transformations of Python+NumPy programs —
grad/jvp/vjpas composable function transforms, and Hessian-vector products via forward-over-reverse differentiation without materializing the Hessian. ↩ - Chen, Xu, Zhang, Guestrin (2016), Training Deep Nets with Sublinear Memory Cost, arXiv:1604.06174 — gradient checkpointing recomputes activations in the backward pass to reduce memory from to ; see also Chen, Rubanova, Bettencourt, Duvenaud (2018), Neural Ordinary Differential Equations, NeurIPS, for the adjoint method that differentiates through an ODE solver. ↩
╌╌ END ╌╌