The chain rule and backpropagation

Part 0, Neural networks from absolute zero

Learning objectives

  • State backpropagation as the chain rule run from the loss backwards, each node's delta reused for every edge into it
  • Read a weight's partial as its delta times the value it multiplied, at one depth of the Marmousi column
  • Say why backpropagation costs about two passes a point, against two evaluations of the whole loss per parameter for central differences
  • Check backpropagation against central differences and say why they agree to about eight significant digits and no further

One Backward Pass

Training a network means moving its parameters down the gradient of a loss, so every step needs the derivative of the loss with respect to every parameter. Backpropagation gets all of them from one forward and one backward pass through the network a point: the backward pass carries each node's delta, the derivative of the point's squared misfit with respect to that node's pre-activation, from the output back to the input by the chain rule, and the partial of a weight is its delta times the value it multiplied. On the book's 1-4-4-1 fit of the Marmousi column, after 1000 Adam steps, backpropagation's gradient of the loss and central differences agree to 8 significant digits of its largest partial; the differences take 66 evaluations of the whole loss for the network's 33 parameters.

Backprop In One SweepInteractive figure, enable JavaScript to interact.

The Network and Its Loss

The data are the Marmousi trace at x = 3600 m, the book's column through the Marmousi2 window (Section 0.10 introduces the window, a 2D acoustic, constant-density slice of a synthetic model, not field data): 450 m of water at 1500 m/s, then rock from 1574 m/s at the sea floor to 3553 m/s at 1500 m. The network takes the depth over 1800 m and returns the velocity over 1500 m/s, the book's scales, so its input and output are of order one. It has two hidden layers of four tanh units: 4 weights and 4 biases into the first, 16 and 4 into the second, 4 and 1 into the output, 33 parameters. The loss is the mean of the squared misfits at 176 depths every 10 m from the surface to 1750 m,

L(ΞΈ)=1Nβˆ‘i(uΞΈ(z^i)βˆ’ci)2,ci=v(zi)/V0.L(\theta) = \tfrac1N \sum_i \big(u_\theta(\hat z_i) - c_i\big)^2, \qquad c_i = v(z_i)/V_0.

The run is the book's own, trained by Adam from a seeded start and kept in its training table at four checkpoints: the seed's network, and after 300, 1000 and 3000 steps. Its loss falls from 2.40 to 0.00531.

The Chain Rule, Backwards

Write each layer as a(l)=W(l)h(lβˆ’1)+b(l)a^{(l)} = W^{(l)} h^{(l-1)} + b^{(l)} and h(l)=tanh⁑a(l)h^{(l)} = \tanh a^{(l)}, with h(0)=z^h^{(0)} = \hat z and the last layer linear. At one depth the squared misfit β„“=(uβˆ’c)2\ell = (u - c)^2 depends on a first-layer weight only through the layers after it, and the chain rule multiplies their local derivatives. Backpropagation forms those products once for all the weights: it starts from the output's delta Ξ΄(L)=2(uβˆ’c)\delta^{(L)} = 2(u - c) and steps back,

Ξ΄(l)=tanh⁑′(a(l))βŠ™W(l+1)⊀δ(l+1),βˆ‚β„“βˆ‚Wjk(l)=Ξ΄j(l) hk(lβˆ’1),βˆ‚β„“βˆ‚bj(l)=Ξ΄j(l).\delta^{(l)} = \tanh'\big(a^{(l)}\big) \odot W^{(l+1)\top} \delta^{(l+1)}, \qquad \frac{\partial \ell}{\partial W^{(l)}_{jk}} = \delta^{(l)}_j\, h^{(l-1)}_k, \qquad \frac{\partial \ell}{\partial b^{(l)}_j} = \delta^{(l)}_j.

Each node's delta is computed once and used for every edge into it. At 700 m, after 1000 steps, the network says 1664 m/s where the column has 1631 m/s, and the output's delta is 0.044. Where it misses the column most, at 1750 m, it says 3438 m/s against 2514 m/s and the output's delta is 1.2; in the water at the surface it is 0.024. A depth the network misses pushes its weights hard, and a depth it fits hardly at all: plate (a) draws the deltas as the size of the nodes and each weight's partial as the width of its edge.

Rumelhart, Hinton and Williams (1986) described this learning procedure, back-propagation, for networks with hidden units: it repeatedly adjusts the weights of the connections to minimise a measure of the difference between the network's output and the desired output, and the hidden units, part of neither the input nor the output, come to represent important features of the task.

What It Costs

The forward pass keeps each layer's values; the backward pass visits each weight once more. So the gradient of one depth's misfit costs about two passes, and the loss's gradient over the 176 depths about two passes a depth, whatever the number of parameters. Baydin et al. (2018) say it in general: backpropagation is a special case of reverse-mode automatic differentiation, and for a function of many inputs and one output one application of the reverse mode gives the whole gradient, at a cost of a constant times the function's own that is below six and typically two to three. Central differences move one parameter at a time, βˆ‚L/βˆ‚ΞΈkβ‰ˆ(L(ΞΈ+hek)βˆ’L(ΞΈβˆ’hek))/2h\partial L / \partial \theta_k \approx \big(L(\theta + h e_k) - L(\theta - h e_k)\big) / 2h: 66 evaluations of the whole loss for 33 parameters, and 4482 for the 2241 parameters of the book's 1D wave PINN (Part 4).

Checking It

A wrong gradient can still descend, slowly, somewhere, so a gradient code is checked against differences. Plate (c) sets the 33 partials of the loss by backpropagation beside central differences at a step of 10βˆ’610^{-6} in each parameter. For the seed's network they agree to 9 significant digits of the largest partial; after 300 and after 1000 steps to 8; after 3000 to 7. They agree no further because the difference has errors of its own: its truncation, the curvature it ignores, which falls as the square of the step, and its rounding, which grows as one over it. As training shrinks the partials, the largest from 2.9 for the seed to 0.00020 after 3000 steps, the difference's rounding becomes a larger share of them. Backpropagation's partials are exact to rounding; the differences are the check, not the method.

A PINN's Loss

The loss here holds only the network's value. A PINN's loss holds its derivatives too: the wave residual of Part 1 needs uzzu_{zz} and uttu_{tt} of the network at every collocation point. The book's kernel carries those derivatives forward through the graph (Section 0.7) and then runs backpropagation back through them, so the gradient of a PDE residual with respect to every weight still costs a few passes a point; the deltas then carry the adjoints of the derivatives as well as of the value.

What Comes Next

Section 0.7 runs the chain rule the other way: forward through the graph, carrying the derivatives of the network with respect to its input that a PDE residual needs.

References

  • Baydin, A. G., Pearlmutter, B. A., Radul, A. A. and Siskind, J. M. (2018). Automatic differentiation in machine learning: a survey. arXiv:1502.05767.
  • Rumelhart, D. E., Hinton, G. E. and Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323, 533-536. doi:10.1038/323533a0. Abstract read.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.