Auto-differentiation as a computational graph

Part 0, Neural networks from absolute zero

Learning objectives

  • Run the chain rule forwards through a network's graph, each node carrying its value and its first and second derivatives with respect to the input
  • Read the slope and curvature of the column's fit at a depth, exact to rounding, in m/s per metre and per square metre
  • Measure a central difference's error against its step and name its two sources, truncation and rounding
  • Say why a PINN takes its residual's derivatives by automatic differentiation and not by differences, and what forward mode costs with more inputs

Derivatives Carried Forward

A PINN's loss holds derivatives of the network with respect to its inputs: the wave equation needs uzzu_{zz} and uttu_{tt}. Forward-mode automatic differentiation gets them exactly, by carrying beside each node's value its first and second derivatives with respect to the input, through the same graph in the same pass: the chain rule applied forwards. On the book's 1-4-4-1 fit of the Marmousi column at 700 m it gives the fit's slope, 0.667 m/s per metre, and its curvature, 0.000996 m/s per square metre, exact to rounding. A central difference of the curvature is at best 4.5Γ—10βˆ’94.5 \times 10^{-9} off, at a step of 0.1 m only, and at a step of 1 mm it is 2.6Γ—10βˆ’52.6 \times 10^{-5} off.

Forward Mode DerivativesInteractive figure, enable JavaScript to interact.

The Graph and Its Tangents

Each node of the network computes a=Wh+ba = W h + b and h=tanh⁑ah = \tanh a. Differentiating both with respect to the input z^\hat z, the depth over 1800 m, gives the rules forward mode follows,

aβ€²=Whβ€²,aβ€²β€²=Whβ€²β€²,hβ€²=tanh⁑′(a) aβ€²,hβ€²β€²=tanh⁑′′(a) (aβ€²)2+tanh⁑′(a) aβ€²β€²,a' = W h', \quad a'' = W h'', \qquad h' = \tanh'(a)\, a', \quad h'' = \tanh''(a)\, (a')^2 + \tanh'(a)\, a'',

starting from (z^,1,0)(\hat z, 1, 0) at the input. Each node now carries three numbers instead of one, and the output's three are uu, uβ€²u' and uβ€²β€²u''. Baydin et al. (2018) describe forward mode as evaluating the function on dual numbers, truncated Taylor series v+vΛ™Ο΅v + \dot v \epsilon with Ο΅2=0\epsilon^2 = 0; carrying the second derivative is the same idea taken one order further. Plate (a) shows what each node of the network carries at the chosen depth. The network's velocity is v=V0uv = V_0 u with V0V_0 = 1500 m/s, so dv/dz=(V0/L) uβ€²dv/dz = (V_0/L)\, u' and d2v/dz2=(V0/L2) uβ€²β€²d^2v/dz^2 = (V_0/L^2)\, u'' with LL = 1800 m.

What a Difference Can Do

A central difference, uβ€²β€²β‰ˆ(u(z^+h)βˆ’2u(z^)+u(z^βˆ’h))/h2u'' \approx \big(u(\hat z + h) - 2u(\hat z) + u(\hat z - h)\big)/h^2, needs only three values of the network, but it has two errors. Its truncation error, the higher derivatives it ignores, falls as h2h^2: at 700 m a step of 100 m misses the curvature by 8.5Γ—10βˆ’38.5 \times 10^{-3} and the slope by 5.1Γ—10βˆ’45.1 \times 10^{-4}. Its rounding error grows as Ξ΅/h2\varepsilon / h^2, with Ξ΅\varepsilon double precision's rounding unit, because the three values agree in nearly all their digits: at a step of 1 mm the curvature is 2.6Γ—10βˆ’52.6 \times 10^{-5} off. The best step balances the two, 0.1 m for the curvature, 4.5Γ—10βˆ’94.5 \times 10^{-9} off, and 1 cm for the slope, 6.7Γ—10βˆ’136.7 \times 10^{-13} off, and the best step moves with the depth. Baydin et al. (2018) call numerical approximations of derivatives inherently ill-conditioned and unstable, for these two reasons.

What It Costs

Forward mode carries one channel for each derivative it is asked for. With one input that is three numbers a node: the value, the slope and the curvature. The 1D wave of Part 1 has two inputs, depth and time, and its residual needs uzzu_{zz} and uttu_{tt}; the book's kernel carries the value, the two first derivatives and the three second derivatives, the mixed uztu_{zt} among them, six numbers a node, and ten for the 2D wave's three inputs. Baydin et al. (2018) give the general rule: forward mode computes the full Jacobian of a function of nn inputs in nn evaluations, one for each input, which suits a network with few inputs, while backpropagation (Section 0.6) suits a loss with many parameters and one output. A PINN uses both: forward mode for the derivatives in its inputs, then reverse mode back through them for its parameters.

What Comes Next

Section 0.8 puts the gradients to work: four optimisers train one network on the column, step by step.

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.

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