Lecture notes · Bayesian and Probabilistic Machine Learning · Chapter 5

Neural Networks

Linear models in fixed basis functions (Chapters 3 and 4) are easy to fit but cannot adapt their features to the data. A neural network keeps the form "linear combination of basis functions" but lets each basis function be a nonlinear function of a linear combination of the inputs, with adjustable coefficients. This chapter covers how such networks compute (Section 5.1), what error function to minimise and why (5.2), how to compute its gradient in time proportional to the number of weights (backpropagation, 5.3) and its second derivatives (5.4), how to control over-fitting and build in invariances (5.5), how to model multimodal targets (5.6), and how to treat the weights in a Bayesian way (5.7).

Ch. 1 Introduction Ch. 2 Probability distributions Ch. 3 Linear regression Ch. 4 Linear classification Ch. 5 Neural networks Ch. 6 Kernel methods Ch. 7 Sparse kernel machines Ch. 8 Graphical models Ch. 9 Mixture models and EM Ch. 10 Approximate inference Ch. 11 Sampling methods Ch. 12 Continuous latent variables Ch. 13 Sequential data Ch. 14 Combining models

How to read these notes

They follow the book. Sections carry Bishop's numbers (5.1, 5.3.4, ...) and every displayed equation that appears in the book carries the book's own equation number, e.g. (5.53), so you can read these notes next to the book. Equations without a number are steps that the book skips.

Filling the gaps. Wherever the book says that a result "can be verified", is "straightforward", or leaves it to an exercise, an orange Filling the gap box or a proof gives the full argument. Proofs and derivations start hidden: open one with its bar, or use the button below.

Figures and demonstrations. The numerical figures of the chapter are reproduced from scratch by the Python code; the schematic ones are redrawn. The ▶ Demo boxes open an interactive demonstration already set to the case being discussed. Standard identities (matrix calculus, the Woodbury identity, Gaussian marginals and conditionals, the softmax) are proved once in the mathematical toolkit and linked from here.

Main reference

C. M. Bishop, Pattern Recognition and Machine Learning, Springer (2006), Chapter 5, pages 225–290. Section, equation, figure and exercise numbers below refer to this book. Further reading is listed in the references.

The explanations, derivations and code are my own; equations are cited by their numbers in the book.

The chapter at a glance

  • 5.1 A two-layer network is a linear model whose \(M\) basis functions \(h(\mathbf{w}_j^\T\mathbf{x})\) have their own weights (Definition 5.1). Two-layer networks are universal approximators (Result 5.1), and the weights are far from identifiable: every weight vector has \(M!\,2^M\) equivalent partners (Result 5.2).
  • 5.2 The error function is a negative log likelihood, and the matching output nonlinearity (identity, sigmoid, softmax) is the canonical link, which makes the output error signal equal to \(y_k-t_k\) in all three cases (Results 5.3 and 5.4). The error surface is non-convex, so training is iterative; near a minimum it is a quadratic bowl described by the Hessian (Result 5.5).
  • 5.3 Backpropagation computes all \(W\) partial derivatives in \(O(W)\) operations from one forward and one backward sweep (Result 5.6); a central-difference check validates an implementation; the same idea gives the Jacobian (Result 5.7).
  • 5.4 The \(W\times W\) Hessian can be approximated (diagonal, outer product), inverted by a one-pass recursion, differenced numerically, evaluated exactly in \(O(W^2)\), or applied to a vector in \(O(W)\) by the \(\mathcal{R}\) operator.
  • 5.5 Regularisation: weight decay that respects rescaling of the data, early stopping (a filter on the Hessian eigendirections), and invariances by tangent propagation, by transformed training data (which is the same thing to order \(\xi^2\)), by convolutional structure, or by a mixture-of-Gaussians prior on the weights.
  • 5.6 Mixture density networks output the parameters of a mixture, so they can represent multimodal \(p(\mathbf{t}\given\mathbf{x})\) such as the solutions of an inverse problem.
  • 5.7 The Laplace approximation turns a network into a locally linear-Gaussian model, giving a predictive variance with an input-dependent term, an evidence for model comparison and re-estimation formulas for \(\alpha\) and \(\beta\).

Notation. Inputs \(\mathbf{x}\in\R^D\), targets \(\mathbf{t}\in\R^K\), data \(\{(\mathbf{x}_n,\mathbf{t}_n)\}_{n=1}^N\); \(\mathbf{w}\in\R^W\) collects all weights and biases; \(a\) denotes an activation (pre-nonlinearity), \(z\) the output of a hidden unit, \(h(\cdot)\) the hidden nonlinearity; \(E(\mathbf{w})=\sum_nE_n(\mathbf{w})\). Biases are absorbed as weights from a unit with constant output 1 ((5.8), (5.9)), so the index ranges include 0.

Prerequisites. Linear and logistic regression with maximum likelihood and the Bayesian treatment (Section 3.3, 3.5, 4.3, 4.4, 4.5), including IRLS, the canonical link and the Laplace approximation, and the matrix calculus and Woodbury identity of the toolkit.

Printings. The text used here is a corrected printing. Where Bishop's errata list corrects an equation of this chapter, the notes give the corrected form and a short Erratum note naming the original. One further printed exponent that does not follow from the stated transformation is flagged in Section 5.5.1.

Derivations start hidden: click any bar to open one.

5.1 Feed-forward network functions

pp. 227–231 The linear models of Chapters 3 and 4 have the form \(y(\mathbf{x},\mathbf{w})\)\({}=f\big(\sum_{j}w_j\phi_j(\mathbf{x})\big)\) (5.1) with fixed basis functions \(\phi_j\), which suffers from the curse of dimensionality when the \(\phi_j\) are chosen without looking at the data. A neural network makes the basis functions adaptive: each one is itself "a nonlinear function of a linear combination of the inputs", and the coefficients of these inner linear combinations are learned together with the outer ones.

Definition 5.1 (two-layer network) pp. 227–229, (5.2)–(5.9)

Take \(D\) inputs, \(M\) hidden units and \(K\) outputs. The activations of the hidden units and their outputs are

\[ a_j=\sum_{i=1}^{D}w^{(1)}_{ji}x_i+w^{(1)}_{j0}, \tag{5.2} \] \[ z_j=h(a_j), \tag{5.3} \]

where \(h(\cdot)\) is a differentiable nonlinear activation function (logistic sigmoid or \(\tanh\)). The output-unit activations and the outputs are

\[ a_k=\sum_{j=1}^{M}w^{(2)}_{kj}z_j+w^{(2)}_{k0}, \tag{5.4} \]

and \(y_k=a_k\) (regression), \(y_k=\sigma(a_k)\) (independent binary classifications, (5.5), (5.6)) or \(y_k=\exp(a_k)/\sum_l\exp(a_l)\) (multiclass). For sigmoidal outputs the whole map is

\[ y_k(\mathbf{x},\mathbf{w})=\sigma\!\Big(\sum_{j=1}^{M}w^{(2)}_{kj}\,h\Big(\sum_{i=1}^{D}w^{(1)}_{ji}x_i+w^{(1)}_{j0}\Big)+w^{(2)}_{k0}\Big). \tag{5.7} \]

Introducing \(x_0\equiv1\) and \(z_0\equiv1\) absorbs the biases:

\[ a_j=\sum_{i=0}^{D}w^{(1)}_{ji}x_i, \tag{5.8} \] \[ y_k(\mathbf{x},\mathbf{w})=\sigma\!\Big(\sum_{j=0}^{M}w^{(2)}_{kj}\,h\Big(\sum_{i=0}^{D}w^{(1)}_{ji}x_i\Big)\Big). \tag{5.9} \]

The total number of parameters is \(W=M(D+1)+K(M+1)\). More generally, in an arbitrary feed-forward network (a directed graph without closed directed cycles) every hidden or output unit computes

\[ z_k=h\Big(\sum_jw_{kj}z_j\Big), \tag{5.10} \]

the sum running over the units that send a connection to \(k\) (bias included).

x0x1xD z0z1zM y1yK ⋮⋮⋮ w(1)w(2) inputshiddenoutputs
Figure 5.1 (redrawn). The two-layer network (5.9). The white nodes \(x_0\) and \(z_0\) are the constant units that carry the biases; arrows show the direction of forward propagation. Book: Figure 5.1, p. 228.

Why "two-layer". The count that matters is the number of layers of adaptive weights, so Figure 5.1 is a two-layer network (it is also called a three-layer network, counting layers of units, or a single-hidden-layer network). Both stages resemble the perceptron of Section 4.1.7, which is why the model is also called the multilayer perceptron (MLP), but with smooth instead of step nonlinearities, so \(y_k\) is differentiable in \(\mathbf{w}\), the property on which training rests.

Filling the gap 5.1: linear hidden units, bottlenecks, and skip-layer connections pp. 229–230

The book states without proof that a network with linear hidden units is equivalent to one without hidden units, and that a sigmoidal network can mimic skip-layer connections.

Derivation

Linear hidden units. If \(h(a)=a\), then \(\mathbf{a}^{(1)}=\mathbf{W}^{(1)}\mathbf{x}\) (biases absorbed), \(\mathbf{z}=\mathbf{a}^{(1)}\) and \(\mathbf{y}\)\({}=\mathbf{W}^{(2)}\mathbf{z}\)\({}=\mathbf{W}^{(2)}\mathbf{W}^{(1)}\mathbf{x}\). The map is the single linear map \(\mathbf{W}=\mathbf{W}^{(2)}\mathbf{W}^{(1)}\), of size \(K\times(D+1)\). If \(M<\min(D+1,K)\), then \(\operatorname{rank}\mathbf{W}\le M\), so only the linear maps of rank at most \(M\) are representable: information is lost at the bottleneck. (Section 12.4.2 shows that training such a network for \(\mathbf{t}=\mathbf{x}\) gives principal component analysis.)

Mimicking a skip-layer connection. For \(\tanh\), a Taylor expansion at 0 gives \(\tanh(\epsilon a)=\epsilon a+O(\epsilon^3a^3)\). On a bounded input set \(|a|\le A\), choose first-layer weights \(\epsilon\,\mathbf{w}\) with \(\epsilon\) tiny and output weight \(c/\epsilon\): the contribution \((c/\epsilon)\tanh(\epsilon\,\mathbf{w}^\T\mathbf{x})\)\({}=c\,\mathbf{w}^\T\mathbf{x}+O(\epsilon^2)\) is uniformly as close as desired to a linear term \(c\,\mathbf{w}^\T\mathbf{x}\). The price is a large output weight, which is why skip-layer connections are sometimes included explicitly. ∎

Universal approximation. The book cites a family of theorems (Funahashi 1989; Cybenko 1989; Hornik, Stinchcombe and White 1989; Hornik 1991) saying that two-layer networks with linear outputs can approximate any continuous function on a compact set. The precise statement, and a complete proof in the simplest case that shows where the hidden units come from, are below.

Result 5.1 (universal approximation) p. 230

Let \(h:\R\to\R\) be continuous and not a polynomial (e.g. the logistic sigmoid or \(\tanh\)), let \(\mathcal{K}\subset\R^D\) be compact and \(f:\mathcal{K}\to\R\) continuous. For every \(\varepsilon>0\) there are \(M\) and parameters \(\{w^{(1)}_{ji}\},\{w^{(2)}_{j}\}\) with

\[ \sup_{\mathbf{x}\in\mathcal{K}}\Big|f(\mathbf{x})-\sum_{j=0}^{M}w^{(2)}_{j}\,h\Big(\sum_{i=0}^{D}w^{(1)}_{ji}x_i\Big)\Big|<\varepsilon . \]

The statement says nothing about how large \(M\) must be, nor how to find the weights.

Proof for \(D=1\), the logistic sigmoid, \(\mathcal{K}=[0,1]\) (the general case: Cybenko 1989, Hornik 1991)

The case of general \(D\) and general \(h\) uses the Hahn–Banach theorem (Cybenko) or the Stone–Weierstrass theorem (Hornik et al.) and is not reproduced here; the one-dimensional case is elementary and shows the mechanism.

Step 1: a staircase. By uniform continuity choose \(\delta>0\) with \(|f(x)-f(x')|<\varepsilon/2\) whenever \(|x-x'|<\delta\). Take the uniform grid \(x_j=j/n\) with mesh \(h=1/n<\delta/2\) and put \(d_j=f(x_j)-f(x_{j-1})\). With the Heaviside step \(H\) (so \(H(0)=1\)), the staircase \(g(x)=f(x_0)+\sum_{j=1}^{n}d_j\,H(x-x_{j-1})\) equals \(f(x_i)\) on \([x_{i-1},x_i)\) (telescoping), hence \(|g-f|<\varepsilon/2\) on \([0,1)\).

Step 2: smooth the steps. Replace \(H(x-x_{j-1})\) by \(\sigma_s(x-x_{j-1})\) with \(\sigma_s(u)=\sigma(su)\); this is a hidden unit with first-layer weight \(s\), bias \(-sx_{j-1}\) and output weight \(d_j\), plus the output bias \(f(x_0)\). Let \(g_s\) be the resulting network. For \(|u|\ge\rho\), \(|\sigma_s(u)-H(u)|\le\eta(s):=1/(1+e^{s\rho})\). Take \(\rho=h/2\) (so a point lies within \(\rho\) of at most one grid point).

If \(x\) is farther than \(\rho\) from every \(x_{j-1}\), every term differs from the staircase by at most \(|d_j|\eta(s)\), so \(|g_s(x)-g(x)|\le n\,(2\|f\|_\infty)\,\eta(s)\). If \(|x-x_{j-1}|<\rho\) for one \(j\) (only one, by the choice of \(\rho\)), the other terms are within the same bound, and the \(j\)th contributes \(d_j\sigma_s(x-x_{j-1})\in[0,d_j]\) (or \([d_j,0]\)), so \(g_s(x)\) lies, up to the same bound, between \(f(x_{j-1})\) and \(f(x_j)\). Both of these are within \(\varepsilon/2\) of \(f(x)\), because \(|x-x_{j-1}|<\rho\) and \(|x-x_j|<\rho+h=3h/2<\delta\), and a number between two values within \(\varepsilon/2\) of \(f(x)\) is itself within \(\varepsilon/2\). Hence, in all cases, \(|g_s-f|<\varepsilon/2+2n\|f\|_\infty\eta(s)\) on \([0,1)\). Since \(\eta(s)\to0\) as \(s\to\infty\), choosing \(s\) large makes the right side below \(\varepsilon\); the single point \(x=1\) is handled by the same estimate. ∎

Four panels showing a two-layer network with three tanh hidden units fitted to 50 points sampled from x squared, sine of x, absolute value of x and the Heaviside step function on the interval from minus one to one; the red network output follows the data and the three dashed curves are the hidden unit outputs.
Figure 5.3 (reproduced). A two-layer network with three \(\tanh\) hidden units and a linear output, trained on \(N=50\) points drawn uniformly from \((-1,1)\) for (a) \(f(x)=x^2\), (b) \(\sin 2x\) (the book's \(\sin x\) is almost a straight line on \((-1,1)\)), (c) \(|x|\), (d) the Heaviside step. Red: network function; dashed: the three hidden-unit outputs (times their output weights are summed to give the red curve). Our networks are trained with L-BFGS. Book: Figure 5.3, p. 231. Code: ch05_neural_networks.py, fig5_3. Download: PDF · SVG
▶ DemoThree hidden units on sin(2 pi x) (Fig 5.3)Train the network and watch the red curve being built from the three hidden-unit curves.
▶ DemoApproximating x^2, |x|, step (Fig 5.3)The same three units can bend into a parabola, a V or an (almost) step; the step needs very large first-layer weights, as in the proof above.
A two-dimensional scatter plot of two classes with the 0.5 contours of two tanh hidden units as dashed blue lines, the network decision boundary y=0.5 in red and the optimal decision boundary in green.
Figure 5.4 (reproduced). A two-class problem (synthetic data of Appendix A) solved by a network with two inputs, two \(\tanh\) hidden units and one logistic output. Our own data: 100 points, of which the true Bayes boundary misclassifies 18. Dashed blue: the lines \(z=0.5\) of the two hidden units; red: the network's \(y=0.5\) boundary; green: the optimal boundary computed from the generating distributions. Book: Figure 5.4, p. 232. Code: ch05_neural_networks.py, fig5_4. Download: PDF · SVG

5.1.1 Weight-space symmetries

pp. 231–232 Different weight vectors can define the very same function \(\mathbf{x}\mapsto\mathbf{y}\). This does not matter for prediction, but it matters for the Bayesian model comparison of Section 5.7, where the volume of weight space occupied by a network enters the evidence.

Result 5.2 (sign-flip and interchange symmetries) pp. 231–232

Consider the two-layer network (5.9) with \(h=\tanh\) and \(M\) hidden units. For a sign vector \(\mathbf{s}\in\{\pm1\}^M\) and a permutation \(\pi\) of \(\{1,\dots,M\}\) define new weights by

\[ \widetilde{w}^{(1)}_{ji}=s_j\,w^{(1)}_{\pi(j)i}\quad(i=0,\dots,D),\qquad \widetilde{w}^{(2)}_{kj}=s_j\,w^{(2)}_{k\pi(j)}\quad(j\ge1),\qquad \widetilde{w}^{(2)}_{k0}=w^{(2)}_{k0}. \]

Then the network function is unchanged. These \(2^MM!\) transformations form a group, and for every weight vector outside a null set (one with all first-layer rows non-zero and pairwise different up to sign) they produce \(2^MM!\) distinct weight vectors. So each such \(\mathbf{w}\) belongs to a family of exactly \(M!\,2^M\) equivalent weight vectors; for a network with more than one layer of hidden units the factors of the layers multiply.

Proof

Invariance. Because \(\tanh\) is odd, \(\tanh(s_ja)=s_j\tanh a\) for \(s_j=\pm1\). Hence for each output \[ \sum_{j=1}^{M}\widetilde{w}^{(2)}_{kj}\tanh\Big(\sum_i\widetilde{w}^{(1)}_{ji}x_i\Big)=\sum_{j}s_jw^{(2)}_{k\pi(j)}\,s_j\tanh\Big(\sum_iw^{(1)}_{\pi(j)i}x_i\Big)=\sum_{j}w^{(2)}_{k\pi(j)}\tanh\Big(\sum_iw^{(1)}_{\pi(j)i}x_i\Big), \] because \(s_j^2=1\), and the last sum is the original one with the terms re-ordered (\(\pi\) is a bijection). The output bias is untouched.

Group and count. Composition of two such transformations is another one (compose the permutations and multiply the signs along them), the identity is \((\mathbf{1},\mathrm{id})\) and each element has an inverse, so we have a group \(G\) with \(|G|=2^M\cdot M!\) elements, acting on weight space.

Distinctness. Let \(\mathbf{u}_j=(w^{(1)}_{j0},\dots,w^{(1)}_{jD})\) be the rows of the first layer and assume \(\mathbf{u}_j\ne\mathbf{0}\) and \(\mathbf{u}_j\ne\pm\mathbf{u}_{j'}\) for \(j\ne j'\). Suppose \((\mathbf{s},\pi)\) fixes \(\mathbf{w}\), i.e. \(s_j\mathbf{u}_{\pi(j)}=\mathbf{u}_j\) for all \(j\). If \(\pi(j)\ne j\) this gives \(\mathbf{u}_j=\pm\mathbf{u}_{\pi(j)}\), contradicting the assumption, so \(\pi=\mathrm{id}\); then \(s_j\mathbf{u}_j=\mathbf{u}_j\) with \(\mathbf{u}_j\ne\mathbf{0}\) forces \(s_j=+1\). So only the identity fixes \(\mathbf{w}\) (the action is free there), and \(g\mathbf{w}=g'\mathbf{w}\) implies \(g^{-1}g'\mathbf{w}=\mathbf{w}\), hence \(g=g'\): the orbit has exactly \(|G|\) points. The excluded weights (a zero row, or two rows equal up to sign) form a set of Lebesgue measure zero; there the symmetry is "accidental" and the orbit is smaller. ∎

Remarks. (i) For the logistic sigmoid, \(\sigma(-a)=1-\sigma(a)\), so the sign flip of unit \(j\) is compensated by \(\widetilde{w}^{(2)}_{kj}=-w^{(2)}_{kj}\) together with the output bias shift \(\widetilde{w}^{(2)}_{k0}\)\({}=w^{(2)}_{k0}+w^{(2)}_{kj}\); the count \(2^MM!\) is the same. (ii) The book states, citing Kůrková and Kainen (1994), that these account for all the symmetries apart from accidental ones; that converse is a deeper result and is not proved here.

Filling the gap 5.2: \(\tanh\) and logistic hidden units are equivalent (Exercise 5.1) p. 284

Since \(\sigma(a)=\tfrac12\big(1+\tanh(a/2)\big)\), a network with logistic hidden units equals one with \(\tanh\) hidden units whose parameters differ by linear transformations.

Derivation

\(\tanh(a/2)\)\({}=(e^{a/2}-e^{-a/2})/(e^{a/2}+e^{-a/2})\)\({}=(1-e^{-a})/(1+e^{-a})\)\({}=2\sigma(a)-1\), hence \(\sigma(a)=\tfrac12(1+\tanh(a/2))\). A network with logistic hidden units computes \(a_k=\sum_jw^{(2)}_{kj}\sigma(a_j)+w^{(2)}_{k0}\), with \(a_j=\sum_iw^{(1)}_{ji}x_i\) (biases absorbed). Substituting, \[ a_k=\sum_{j\ge1}\tfrac12w^{(2)}_{kj}\tanh\big(\tfrac12a_j\big)+\Big(w^{(2)}_{k0}+\tfrac12\sum_{j\ge1}w^{(2)}_{kj}\Big). \] This is a \(\tanh\) network with first-layer weights \(\widetilde{w}^{(1)}_{ji}=\tfrac12w^{(1)}_{ji}\), second-layer weights \(\widetilde{w}^{(2)}_{kj}=\tfrac12w^{(2)}_{kj}\) and output biases \(\widetilde{w}^{(2)}_{k0}\)\({}=w^{(2)}_{k0}+\tfrac12\sum_jw^{(2)}_{kj}\), all linear in the old parameters, so the two model classes represent exactly the same functions. ∎

5.2 Network training

pp. 232–241 The simplest recipe for choosing \(\mathbf{w}\) is the curve-fitting one of Section 1.1: minimise the sum of squares \(E(\mathbf{w})\)\({}=\tfrac12\sum_n\|\mathbf{y}(\mathbf{x}_n,\mathbf{w})\)\({}-\mathbf{t}_n\|^2\) (5.11). A probabilistic reading of the network output explains this choice, shows when it is wrong, and tells us which output nonlinearity to pair with which error function.

Result 5.3 (error functions as negative log likelihoods) pp. 232–236, (5.12)–(5.25)

Let the data be i.i.d. and write \(y_n=y(\mathbf{x}_n,\mathbf{w})\), \(y_{nk}=y_k(\mathbf{x}_n,\mathbf{w})\).

  1. Regression. With \(p(t\given\mathbf{x},\mathbf{w})\)\({}=\N(t\given y(\mathbf{x},\mathbf{w}),\beta^{-1})\) (5.12), minus the log likelihood is \[ \frac\beta2\sum_{n=1}^{N}\{y(\mathbf{x}_n,\mathbf{w})-t_n\}^2-\frac N2\ln\beta+\frac N2\ln(2\pi), \tag{5.13} \] so \(\mathbf{w}_{\rm ML}\) minimises the sum-of-squares error \(E(\mathbf{w})=\tfrac12\sum_n\{y_n-t_n\}^2\) (5.14), and then \[ \frac1{\beta_{\rm ML}}=\frac1N\sum_{n=1}^{N}\{y(\mathbf{x}_n,\mathbf{w}_{\rm ML})-t_n\}^2 . \tag{5.15} \] For \(K\) targets, independent given \(\mathbf{x}\) with common precision, \(p(\mathbf{t}\given\mathbf{x},\mathbf{w})\)\({}=\N(\mathbf{t}\given\mathbf{y}(\mathbf{x},\mathbf{w}),\beta^{-1}\mathbf{I})\) (5.16); \(\mathbf{w}_{\rm ML}\) minimises (5.11) and \(1/\beta_{\rm ML}\)\({}=\frac1{NK}\sum_n\|\mathbf{y}(\mathbf{x}_n,\mathbf{w}_{\rm ML})\)\({}-\mathbf{t}_n\|^2\) (5.17).
  2. One binary target (\(t\in\{0,1\}\), \(y=\sigma(a)=p(\mathcal{C}_1\given\mathbf{x})\), (5.19)): \(p(t\given\mathbf{x},\mathbf{w})=y^t(1-y)^{1-t}\) (5.20) and the cross-entropy error is \[ E(\mathbf{w})=-\sum_{n=1}^{N}\{t_n\ln y_n+(1-t_n)\ln(1-y_n)\}. \tag{5.21} \]
  3. \(K\) independent binary targets with \(y_k=\sigma(a_k)\): \(p(\mathbf{t}\given\mathbf{x},\mathbf{w})\)\({}=\prod_ky_k^{t_k}(1-y_k)^{1-t_k}\) (5.22) and \[ E(\mathbf{w})=-\sum_{n=1}^{N}\sum_{k=1}^{K}\{t_{nk}\ln y_{nk}+(1-t_{nk})\ln(1-y_{nk})\}. \tag{5.23} \]
  4. Multiclass (1-of-\(K\) coding, \(y_k=p(t_k=1\given\mathbf{x})\)): \[ E(\mathbf{w})=-\sum_{n=1}^{N}\sum_{k=1}^{K}t_{nk}\ln y_k(\mathbf{x}_n,\mathbf{w}), \tag{5.24} \] with the softmax \(y_k=\exp(a_k)/\sum_j\exp(a_j)\) (5.25). Erratum. The book prints \(t_{kn}\) in (5.24); the index order above is the corrected one.
Derivation (Exercises 5.2 and 5.5, and the choice of \(\beta_{\rm ML}\))

(1) By independence, \(-\ln p(\mathbf{t}\given\mathbf{X},\mathbf{w},\beta)\)\({}=-\sum_n\ln\N(t_n\given y_n,\beta^{-1})\) and \(-\ln\N(t\given y,\beta^{-1})\)\({}=\tfrac\beta2(y-t)^2-\tfrac12\ln\beta\)\({}+\tfrac12\ln2\pi\), which gives (5.13). For fixed \(\beta>0\) the terms \(-\tfrac N2\ln\beta+\tfrac N2\ln2\pi\) do not depend on \(\mathbf{w}\) and \(\beta\) multiplies the rest, so the minimiser in \(\mathbf{w}\) is that of (5.14). Setting the \(\beta\)-derivative of (5.13) to zero, \(\tfrac12\sum_n(y_n-t_n)^2-\tfrac N{2\beta}=0\), gives (5.15); it is a minimum because the second derivative of (5.13) in \(\beta\) is \(N/(2\beta^2)>0\). For \(K\) targets, \(\N(\mathbf{t}\given\mathbf{y},\beta^{-1}\mathbf{I})\)\({}=\prod_k\N(t_k\given y_k,\beta^{-1})\), so the exponent is \(\tfrac\beta2\|\mathbf{y}-\mathbf{t}\|^2\) and the normaliser contributes \(-\tfrac{NK}2\ln\beta\); the stationarity condition in \(\beta\) is \(\tfrac12\sum_n\|\mathbf{y}_n-\mathbf{t}_n\|^2\)\({}=\tfrac{NK}{2\beta}\), i.e. (5.17).

(2)–(3) Take \(-\ln\) of the product over \(n\) of (5.20) and (5.22). Because the first layer is shared, the \(K\) problems are coupled through \(\mathbf{w}\) (the hidden units are shared features), unlike \(K\) separate linear classifiers.

(4) With 1-of-\(K\) targets, \(p(\mathbf{t}\given\mathbf{x})=\prod_ky_k^{t_k}\) (exactly one \(t_k\) is 1), so \(-\ln\prod_n\prod_ky_{nk}^{t_{nk}}\) is (5.24). The softmax is invariant to adding a constant \(c\) to every \(a_k\) (numerator and denominator both pick up \(e^c\)), so \(E\) is constant along these directions of weight space; a regulariser (Section 5.5) removes the degeneracy. ∎

Filling the gap 5.3: correlated targets, noisy labels and \(\pm1\) targets (Exercises 5.3, 5.4, 5.9) pp. 233, 284

The book remarks that "the assumption of independence can be dropped", that the cross-entropy model "is easily extended to allow for labelling errors", and asks for the version with targets \(\pm1\). Here are the three results.

Derivation

Unknown noise covariance (Exercise 5.3). Let \(p(\mathbf{t}\given\mathbf{x},\mathbf{w})\)\({}=\N(\mathbf{t}\given\mathbf{y}(\mathbf{x},\mathbf{w}),\bm\Sigma)\). With \(\bm\Sigma\) known, minus the log likelihood is \(\tfrac12\sum_n(\mathbf{y}_n-\mathbf{t}_n)^\T\bm\Sigma^{-1}(\mathbf{y}_n-\mathbf{t}_n)\)\({}+\const\), so \(\mathbf{w}\) minimises this weighted sum of squares. With \(\bm\Sigma\) unknown the negative log likelihood is \(\tfrac N2\ln|\bm\Sigma|\)\({}+\tfrac12\Tr\{\bm\Sigma^{-1}\mathbf{S}(\mathbf{w})\}\)\({}+\const\) with \(\mathbf{S}(\mathbf{w})\)\({}=\sum_n(\mathbf{y}_n-\mathbf{t}_n)(\mathbf{y}_n-\mathbf{t}_n)^\T\). Using \(\partial\ln|\bm\Sigma|/\partial\bm\Sigma\)\({}=\bm\Sigma^{-\T}\) and \(\partial\Tr(\bm\Sigma^{-1}\mathbf{S})/\partial\bm\Sigma\)\({}=-\bm\Sigma^{-1}\mathbf{S}\bm\Sigma^{-1}\) (toolkit), stationarity gives \(\bm\Sigma_{\rm ML}=\mathbf{S}(\mathbf{w})/N\), the sample covariance of the residuals. This depends on \(\mathbf{w}\), and \(\mathbf{w}\) in turn depends on \(\bm\Sigma\) (through the weighting): the two optimisations are coupled. Substituting \(\bm\Sigma_{\rm ML}\) back, the profile objective is \(\tfrac N2\ln|\mathbf{S}(\mathbf{w})|+\const\), to be minimised over \(\mathbf{w}\) alone (if \(\bm\Sigma\propto\mathbf{I}\) this is (5.14)).

Label noise (Exercise 5.4). Suppose the recorded label equals the true one with probability \(1-\epsilon\) and is flipped with probability \(\epsilon\). Then \(P(t=1\given\mathbf{x})\)\({}=(1-\epsilon)y+\epsilon(1-y)\) and \(P(t=0\given\mathbf{x})\)\({}=\epsilon y+(1-\epsilon)(1-y)\), so \[ E(\mathbf{w})=-\sum_n\Big\{t_n\ln\big[(1-\epsilon)y_n+\epsilon(1-y_n)\big]+(1-t_n)\ln\big[\epsilon y_n+(1-\epsilon)(1-y_n)\big]\Big\}. \] For \(\epsilon=0\) this is (5.21). For \(\epsilon>0\) each term is bounded: even if \(y_n\to1\) for a point with \(t_n=0\), the loss tends to \(-\ln\epsilon\), not to \(+\infty\), which makes the model robust to mislabelled points.

Targets \(\pm1\) (Exercise 5.9). Let \(t=1\) for \(\mathcal{C}_1\), \(t=-1\) for \(\mathcal{C}_2\), and \(y\in[-1,1]\). Interpret \(p(\mathcal{C}_1\given\mathbf{x})=(1+y)/2\), \(p(\mathcal{C}_2\given\mathbf{x})=(1-y)/2\). Then \[ E(\mathbf{w})=-\sum_n\Big\{\frac{1+t_n}2\ln\frac{1+y_n}2+\frac{1-t_n}2\ln\frac{1-y_n}2\Big\}. \] This is (5.21) with \(y\to(1+y)/2\), \(t\to(1+t)/2\). The appropriate output nonlinearity is the one that maps the activation to \((-1,1)\) in the canonical way: if \((1+y)/2=\sigma(a)\), then \(y=2\sigma(a)-1=\tanh(a/2)\) (the identity of Filling the gap 5.2). ∎

Result 5.4 (canonical link: the output error signal is \(y_k-t_k\)) pp. 233–236, (5.18)

For each of the three pairings of Result 5.3 (identity with sum of squares, logistic sigmoid with cross-entropy, softmax with multiclass cross-entropy), the derivative of the error of one pattern with respect to the activation of output unit \(k\) is \[ \frac{\partial E_n}{\partial a_k}=y_k-t_k. \tag{5.18} \]

Proof (Exercises 5.6 and 5.7)

Identity. \(E_n=\tfrac12\sum_k(y_k-t_k)^2\) and \(y_k=a_k\), so \(\partial E_n/\partial a_k=y_k-t_k\).

Sigmoid. \(\sigma'(a)=\sigma(a)(1-\sigma(a))\) (differentiate \(\sigma=(1+e^{-a})^{-1}\): \(\sigma'=e^{-a}/(1+e^{-a})^2=\sigma\,(1-\sigma)\)). With \(E_n=-\sum_k\{t_k\ln y_k+(1-t_k)\ln(1-y_k)\}\) and \(y_k=\sigma(a_k)\) only the \(k\)th term depends on \(a_k\): \[ \frac{\partial E_n}{\partial a_k}=-\Big\{\frac{t_k}{y_k}-\frac{1-t_k}{1-y_k}\Big\}y_k(1-y_k)=-t_k(1-y_k)+(1-t_k)y_k=y_k-t_k. \]

Softmax. From \(y_k=e^{a_k}/\sum_je^{a_j}\), \(\partial y_k/\partial a_l=y_k(\delta_{kl}-y_l)\) (the quotient rule: \(\delta_{kl}y_k-y_ky_l\)). With \(E_n=-\sum_kt_k\ln y_k\), \[ \frac{\partial E_n}{\partial a_l}=-\sum_kt_k\frac1{y_k}\,y_k(\delta_{kl}-y_l)=-t_l+y_l\sum_kt_k=y_l-t_l,\] using \(\sum_kt_k=1\) for 1-of-\(K\) coding. ∎

Why it is not an accident. Each pairing is a generalised linear model with an exponential-family noise model in the natural parameter \(a\): \(-\ln p(t\given a)=A(a)-ta+\const\), so \(\partial E/\partial a=A'(a)-t\), and \(A'(a)=\E[t]=y\) is the mean, i.e. the canonical response function (toolkit, Section 4.3.6). For two classes, a single sigmoid output and a two-output softmax coincide: \(y_1=e^{a_1}/(e^{a_1}+e^{a_2})=\sigma(a_1-a_2)\).

5.2.1 Parameter optimization

pp. 236–237 Think of \(E(\mathbf{w})\) as a surface over weight space (Figure 5.5). For a small step, \(\delta E\)\({}\simeq\delta\mathbf{w}^\T\nabla E(\mathbf{w})\), so \(\nabla E\) points uphill, and at a minimiser the gradient must vanish, \[ \nabla E(\mathbf{w})=\mathbf{0}, \tag{5.26} \] since otherwise a small step along \(-\nabla E\) would lower the error. Stationary points are minima, maxima or saddle points. By Result 5.2, every local minimum of a two-layer network comes with \(M!\,2^M\) equivalent copies, and there will in general also be inequivalent local minima, so it is useful to compare several (from different random starts, judged on a validation set). There is no closed-form solution of (5.26), so one iterates \[ \mathbf{w}^{(\tau+1)}=\mathbf{w}^{(\tau)}+\Delta\mathbf{w}^{(\tau)}, \tag{5.27} \] with an update \(\Delta\mathbf{w}^{(\tau)}\) that differs from algorithm to algorithm and, in most, uses \(\nabla E\) at the new point.

5.2.2 Local quadratic approximation

pp. 237–239 Expanding about a point \(\widehat{\mathbf{w}}\) to second order, \[ E(\mathbf{w})\simeq E(\widehat{\mathbf{w}})+(\mathbf{w}-\widehat{\mathbf{w}})^\T\mathbf{b}+\tfrac12(\mathbf{w}-\widehat{\mathbf{w}})^\T\mathbf{H}(\mathbf{w}-\widehat{\mathbf{w}}), \tag{5.28} \] where the gradient \(\mathbf{b}\)\({}\equiv\nabla E|_{\mathbf{w}=\widehat{\mathbf{w}}}\) (5.29) and the Hessian \(\mathbf{H}=\nabla\nabla E\) has entries \((\mathbf{H})_{ij}\)\({}=\partial^2E/\partial w_i\partial w_j|_{\mathbf{w}=\widehat{\mathbf{w}}}\) (5.30). The local gradient is then \[ \nabla E\simeq\mathbf{b}+\mathbf{H}(\mathbf{w}-\widehat{\mathbf{w}}). \tag{5.31} \]

Result 5.5 (geometry near a minimum) pp. 238–239, (5.32)–(5.40)

Let \(\mathbf{w}^\star\) be a stationary point and \(\mathbf{H}\) the (symmetric) Hessian there, with orthonormal eigenvectors \(\mathbf{u}_i\), \(\mathbf{H}\mathbf{u}_i=\lambda_i\mathbf{u}_i\) (5.33), (5.34). In the quadratic approximation, with \(\mathbf{w}-\mathbf{w}^\star\)\({}=\sum_i\alpha_i\mathbf{u}_i\) (5.35):

  1. \(E(\mathbf{w})\)\({}\simeq E(\mathbf{w}^\star)\)\({}+\tfrac12(\mathbf{w}-\mathbf{w}^\star)^\T\mathbf{H}(\mathbf{w}-\mathbf{w}^\star)\)\({}=E(\mathbf{w}^\star)\)\({}+\tfrac12\sum_i\lambda_i\alpha_i^2\) (5.32), (5.36).
  2. \(\mathbf{H}\) is positive definite, \(\mathbf{v}^\T\mathbf{H}\mathbf{v}>0\) for all \(\mathbf{v}\ne\mathbf{0}\) (5.37), if and only if all \(\lambda_i>0\); for \(\mathbf{v}=\sum_ic_i\mathbf{u}_i\), \(\mathbf{v}^\T\mathbf{H}\mathbf{v}\)\({}=\sum_ic_i^2\lambda_i\) (5.39).
  3. The contours \(E=E(\mathbf{w}^\star)+c\) (\(c>0\)) are ellipsoids centred at \(\mathbf{w}^\star\) with axes along the \(\mathbf{u}_i\) and semi-axis lengths \(\sqrt{2c/\lambda_i}\), inversely proportional to \(\sqrt{\lambda_i}\) (Figure 5.6).
  4. If \(\mathbf{H}\) is positive definite, \(\mathbf{w}^\star\) is a strict local minimum; if some \(\lambda_i<0\) it is not a minimum (in one dimension the condition is \(\partial^2E/\partial w^2>0\), (5.40)). If the smallest eigenvalue is zero the quadratic model is flat in that direction and higher-order terms decide.
Proof (Exercises 5.10, 5.11, 5.12)

(1) Substitute (5.35) into (5.32): \((\mathbf{w}-\mathbf{w}^\star)^\T\mathbf{H}(\mathbf{w}-\mathbf{w}^\star)\)\({}=\sum_{i,j}\alpha_i\alpha_j\mathbf{u}_i^\T\mathbf{H}\mathbf{u}_j\)\({}=\sum_{i,j}\alpha_i\alpha_j\lambda_j\mathbf{u}_i^\T\mathbf{u}_j\)\({}=\sum_i\lambda_i\alpha_i^2\) by (5.33) and (5.34). The linear term is absent because \(\nabla E(\mathbf{w}^\star)=\mathbf{0}\). The coordinates \(\alpha_i\)\({}=\mathbf{u}_i^\T(\mathbf{w}-\mathbf{w}^\star)\) are the old coordinates translated to \(\mathbf{w}^\star\) and rotated to the eigenbasis.

(2) The eigenvectors of a symmetric matrix are a complete orthonormal basis, so every \(\mathbf{v}\) is \(\sum c_i\mathbf{u}_i\) with \(c_i=\mathbf{u}_i^\T\mathbf{v}\) (5.38), and the computation of (1) gives (5.39). If all \(\lambda_i>0\), \(\sum c_i^2\lambda_i>0\) unless every \(c_i=0\), i.e. \(\mathbf{v}=\mathbf{0}\). Conversely, taking \(\mathbf{v}=\mathbf{u}_i\) in (5.37) gives \(\mathbf{u}_i^\T\mathbf{H}\mathbf{u}_i\)\({}=\lambda_i>0\). Erratum. The book writes "for all \(\mathbf{v}\)"; \(\mathbf{v}=\mathbf{0}\) must be excluded.

(3) The set \(\tfrac12\sum_i\lambda_i\alpha_i^2=c\) is \(\sum_i\alpha_i^2/(2c/\lambda_i)=1\): an ellipsoid with semi-axis \(\sqrt{2c/\lambda_i}\) along \(\mathbf{u}_i\) when all \(\lambda_i>0\). The direction of smallest curvature has the longest axis (the error changes slowest there).

(4) By (1), \(E-E(\mathbf{w}^\star)\)\({}\simeq\tfrac12\sum\lambda_i\alpha_i^2>0\) for \(\boldsymbol{\alpha}\ne\mathbf{0}\) when all \(\lambda_i>0\) (strict minimum of the quadratic model, and of \(E\) itself since the remainder is \(o(\|\boldsymbol\alpha\|^2)\)). If \(\lambda_i<0\), moving along \(\pm\mathbf{u}_i\) lowers \(E\) at rate \(\tfrac12\lambda_i\alpha_i^2<0\), so \(\mathbf{w}^\star\) is not a minimum. Hence for a quadratic error "minimum \(\iff\) positive definite" as the book asserts (up to the borderline of a zero eigenvalue, where the quadratic term is silent). ∎

w*u2u1
Figure 5.6 (redrawn). Near a minimum \(\mathbf{w}^\star\) the contours of constant error are ellipses aligned with the eigenvectors \(\mathbf{u}_i\) of the Hessian, with semi-axis lengths proportional to \(\lambda_i^{-1/2}\): short in the stiff direction, long in the flat one. Book: Figure 5.6, p. 239. Erratum (caption): the book says "eigenvectors" where it means eigenvalues.

5.2.3 Use of gradient information

pp. 239–240

Filling the gap 5.4: \(W(W+3)/2\) parameters and the \(O(W^3)\) versus \(O(W^2)\) argument (Exercise 5.13) p. 239

Derivation

The quadratic (5.28) is specified by \(\mathbf{b}\) (\(W\) numbers) and the symmetric \(\mathbf{H}\): a symmetric \(W\times W\) matrix is determined by its diagonal and the entries above it, \(W+\tfrac{W(W-1)}2=\tfrac{W(W+1)}2\) numbers. In total \(W+\tfrac{W(W+1)}2=\tfrac{W(W+3)}2=O(W^2)\) independent elements; the location of the minimum \(\widehat{\mathbf{w}}-\mathbf{H}^{-1}\mathbf{b}\) depends on all of them.

Without gradients. Each function evaluation returns one number and costs \(O(W)\) operations (one forward sweep touches every weight once), and \(O(W^2)\) numbers are needed, so \(O(W^2)\) evaluations of \(O(W)\) each: \(O(W^3)\). With gradients. A gradient evaluation delivers \(W\) numbers, so \(O(W)\) of them suffice, and by backpropagation (Section 5.3) each costs \(O(W)\): in total \(O(W^2)\). This heuristic count is the reason all practical training algorithms use \(\nabla E\). ∎

5.2.4 Gradient descent optimization

pp. 240–241 The simplest use of \(\nabla E\) is a step of size \(\eta>0\) (the learning rate) against the gradient, \[ \mathbf{w}^{(\tau+1)}=\mathbf{w}^{(\tau)}-\eta\nabla E(\mathbf{w}^{(\tau)}). \tag{5.41} \] This is a batch method: each step needs the gradient over the whole training set. When the error is a sum over data points, \[ E(\mathbf{w})=\sum_{n=1}^{N}E_n(\mathbf{w}), \tag{5.42} \] the on-line (sequential, stochastic) version updates after each point, \[ \mathbf{w}^{(\tau+1)}=\mathbf{w}^{(\tau)}-\eta\nabla E_n(\mathbf{w}^{(\tau)}), \tag{5.43} \] cycling through the data or sampling with replacement.

Algorithm 5.1 (gradient descent, batch and on-line) pp. 240–241

  1. Choose \(\mathbf{w}^{(0)}\) (small random values; several restarts) and \(\eta>0\).
  2. Batch: compute \(\nabla E=\sum_n\nabla E_n\) by backpropagation (Algorithm 5.2) and set \(\mathbf{w}\leftarrow\mathbf{w}-\eta\nabla E\). On-line: pick a point \(n\), compute \(\nabla E_n\) and set \(\mathbf{w}\leftarrow\mathbf{w}-\eta\nabla E_n\).
  3. Stop when \(\|\nabla E\|\) is small or the validation error stops improving (Section 5.5.2).

Why on-line methods cope with redundancy. Duplicate every data point: the error doubles, \(2E\), and so does the batch gradient (twice the work for the same direction), while a sweep of on-line updates simply sees each point twice at no extra cost per update. A stationary point of \(E\) is generally not stationary for the individual \(E_n\) (their gradients cancel only in the sum), so on-line updates keep moving and can leave a shallow local minimum. Plain steepest descent is nonetheless a poor batch algorithm (Bishop and Nabney, 2008): its behaviour on the elongated bowls of Result 5.5 is analysed in Result 5.13 (it converges only for \(\eta<2/\lambda_{\max}\) and slowly along directions with small \(\lambda_i\)), while conjugate gradients and quasi-Newton methods decrease the error monotonically and are faster. The demo weight decay and early stopping show the effect of the learning dynamics on fit.

5.3 Error backpropagation

pp. 241–249 Gradient-based training needs \(\nabla E\) at every step, so its cost matters. Backpropagation evaluates all the partial derivatives by passing information forwards and then backwards through the network, at a cost comparable to a single forward evaluation. The word is used loosely in the literature (for the MLP itself, or for gradient descent on the sum of squares). Here it names only the first of the two stages of a training step, the evaluation of derivatives; the second stage, using the derivatives to change the weights, can be done by any optimiser of Section 5.2. The same derivative-propagation idea also yields the Jacobian (5.3.4) and the Hessian (5.4).

5.3.1 Evaluation of error-function derivatives

pp. 242–245 For i.i.d. data the error is a sum over patterns, \[ E(\mathbf{w})=\sum_{n=1}^{N}E_n(\mathbf{w}) , \tag{5.44} \] so it is enough to compute \(\nabla E_n\) for one pattern (and sum, (5.57)). As a warm-up, for a single linear layer \(y_k=\sum_iw_{ki}x_i\) (5.45) with \(E_n=\tfrac12\sum_k(y_{nk}-t_{nk})^2\) (5.46), \[ \frac{\partial E_n}{\partial w_{ji}}=(y_{nj}-t_{nj})\,x_{ni}, \tag{5.47} \] an "error signal" at the output end of the link times the variable at its input end. Backpropagation shows that the same local structure persists in any feed-forward network.

Result 5.6 (backpropagation formulae) pp. 242–245, (5.48)–(5.57)

In a feed-forward network each unit \(j\) forms \(a_j=\sum_iw_{ji}z_i\) (5.48) from the outputs \(z_i\) of the units (or inputs) \(i\) that send it a connection, and outputs \(z_j=h(a_j)\) (5.49). Define the error (or "delta") of unit \(j\) for one pattern as \(\delta_j\equiv\partial E_n/\partial a_j\) (5.51). Then

\[ \frac{\partial E_n}{\partial w_{ji}}=\delta_j\,z_i, \tag{5.53} \]

for output units with the canonical link \(\delta_k=y_k-t_k\) (5.54) (Result 5.4), and for every non-output unit \(j\),

\[ \delta_j=h'(a_j)\sum_kw_{kj}\,\delta_k, \tag{5.56} \]

the sum running over all units \(k\) to which \(j\) sends a connection. The total derivative is \(\partial E/\partial w_{ji}\)\({}=\sum_n\partial E_n/\partial w_{ji}\) (5.57).

Proof (the chain rule, step by step)

Fix a pattern and drop the index \(n\). All the \(a\)'s and \(z\)'s are functions of the weights through the forward pass.

(5.50)–(5.53). The weight \(w_{ji}\) enters the forward pass only in the sum \(a_j\), so \(E\) depends on \(w_{ji}\) only through \(a_j\) and \[ \frac{\partial E}{\partial w_{ji}}=\frac{\partial E}{\partial a_j}\frac{\partial a_j}{\partial w_{ji}}\tag{5.50}. \] By the definition (5.51) the first factor is \(\delta_j\); by (5.48) the second is \(\partial a_j/\partial w_{ji}=z_i\) (5.52) (with \(z_i=1\) for a bias). Substituting gives (5.53).

(5.54). For an output unit with canonical link this is Result 5.4. (For another output activation \(y_k=f(a_k)\) and \(E_n\) a function of \(\mathbf{y}\), the chain rule gives \(\delta_k=f'(a_k)\,\partial E_n/\partial y_k\).)

(5.55)–(5.56). A hidden unit \(j\) influences the error only through the units \(k\) it feeds, because the only place \(a_j\) appears downstream is in the \(a_k\) of those units, \(a_k=\sum_{j'}w_{kj'}z_{j'}\ni w_{kj}h(a_j)\). Variations of \(a_j\) therefore change \(E\) only through variations of the \(a_k\): \[ \delta_j=\frac{\partial E}{\partial a_j}=\sum_k\frac{\partial E}{\partial a_k}\frac{\partial a_k}{\partial a_j}=\sum_k\delta_k\frac{\partial a_k}{\partial a_j}\tag{5.55}. \] Now \(\partial a_k/\partial a_j=w_{kj}\,h'(a_j)\) holds for the direct dependence: \(a_k=\sum_{j'}w_{kj'}h(a_{j'})\) contains \(a_j\) only in the term \(w_{kj}h(a_j)\). Nothing is lost by using only the direct dependence, because \(\delta_k=\partial E/\partial a_k\) is a total derivative that already accounts for everything downstream of \(a_k\), and every path from \(a_j\) to the error leaves \(j\) through one of the units \(k\) that it feeds. This gives (5.56).

Order of evaluation. Because the graph has no directed cycle, its units can be ordered so that every unit comes after the units that feed it (a topological order). Forward propagation visits them in this order; \(\delta\) for the outputs is known from (5.54), and (5.56) computes \(\delta_j\) from the \(\delta_k\) of units later in the order, so visiting units in the reverse order evaluates every \(\delta\). Note that (5.56) sums over the first index of \(w_{kj}\) (units above \(j\)), whereas forward propagation (5.10) sums over the second.

If different units have different activation functions, use \(h_j\) in (5.56). ∎

zizjδkδ1 wjiwkjδj
Figure 5.7 (redrawn). The error \(\delta_j\) of a hidden unit is obtained by sending the errors \(\delta_k\) of the units \(k\) it feeds backwards along the same weights (red) and multiplying their weighted sum by \(h'(a_j)\), (5.56). Book: Figure 5.7, p. 244.

Algorithm 5.2 (error backpropagation) p. 244

  1. Forward. Apply \(\mathbf{x}_n\) and compute the activations \(a_j\) and outputs \(z_j\) of all hidden and output units by (5.48), (5.49).
  2. Output errors. Compute \(\delta_k\) for all output units, (5.54).
  3. Backward. Compute \(\delta_j\) for every hidden unit by (5.56), in reverse topological order.
  4. Derivatives. \(\partial E_n/\partial w_{ji}=\delta_jz_i\), (5.53). For a batch method, sum over \(n\), (5.57).

5.3.2 A simple example

pp. 245–246 Take the two-layer network of Figure 5.1 with \(\tanh\) hidden units, linear outputs and \(E_n=\tfrac12\sum_k(y_k-t_k)^2\) (5.61). The hidden activation is \[ h(a)=\tanh a=\frac{e^{a}-e^{-a}}{e^{a}+e^{-a}} \tag{5.58, 5.59} \] and its derivative is a function of its value (Erratum: "logistic sigmoid" on p. 245 should read "sigmoidal"), \[ h'(a)=1-h(a)^2. \tag{5.60} \]

Derivation of (5.60) (Exercise 5.8)

Write \(\tanh a=N/D\) with \(N=e^a-e^{-a}\), \(D=e^a+e^{-a}\); then \(N'=D\) and \(D'=N\), so \((N/D)'=(D^2-N^2)/D^2=1-(N/D)^2\), because \(D^2-N^2=4\). The logistic sigmoid satisfies \(\sigma'=\sigma(1-\sigma)\) (Result 5.4), and via \(\tanh a=2\sigma(2a)-1\) the two statements are the same: \(\tfrac{d}{da}\tanh a\)\({}=4\sigma'(2a)\)\({}=4\sigma(2a)(1-\sigma(2a))\)\({}=1-\tanh^2a\). ∎

For each pattern, forward propagation is \[ a_j=\sum_{i=0}^{D}w^{(1)}_{ji}x_i,\qquad z_j=\tanh(a_j),\qquad y_k=\sum_{j=0}^{M}w^{(2)}_{kj}z_j, \tag{5.62, 5.63, 5.64} \] then the output errors \(\delta_k=y_k-t_k\) (5.65), the hidden errors \[ \delta_j=(1-z_j^2)\sum_{k=1}^{K}w^{(2)}_{kj}\delta_k, \tag{5.66} \] and the derivatives \[ \frac{\partial E_n}{\partial w^{(1)}_{ji}}=\delta_jx_i,\qquad \frac{\partial E_n}{\partial w^{(2)}_{kj}}=\delta_kz_j. \tag{5.67} \] Note that (5.66) is (5.56) with \(h'(a_j)=1-z_j^2\) computed from the stored forward values \(z_j\), at no extra cost.

Filling the gap 5.5: skip-layer connections and tied weights (Exercises 5.18 and 5.28) pp. 249, 269, 285, 289

Derivation

Skip-layer connections (Exercise 5.18). Add weights \(w^{(\rm skip)}_{ki}\) from input \(x_i\) straight to output \(k\): \(y_k\)\({}=\sum_jw^{(2)}_{kj}z_j\)\({}+\sum_iw^{(\rm skip)}_{ki}x_i\). The hidden-unit errors do not change, because \(a_j\) still reaches the error only through the \(a_k\) and the skip connections do not depend on \(a_j\); so (5.66), (5.67) hold as before. By (5.53) with the input-end variable \(x_i\) and the output-end error \(\delta_k=y_k-t_k\): \(\partial E_n/\partial w^{(\rm skip)}_{ki}\)\({}=\delta_k\,x_i\).

Tied weights (Exercise 5.28). If several weights are constrained to the same value, \(w_{r}=w_{s}=\dots=\omega\), the error is a function of the single parameter \(\omega\) and the chain rule gives \(\partial E/\partial\omega\)\({}=\sum_{(ji)\in\mathcal{S}}\partial E/\partial w_{ji}\), the sum of the ordinary backpropagation derivatives over all the links in the tied set \(\mathcal{S}\). So run standard backpropagation, treating each link as free, then add the gradients within each group before applying the update (and initialise tied weights equally, so that they stay equal). ∎

5.3.3 Efficiency of backpropagation

pp. 246–247 Let \(W\) be the number of weights and biases. Forward propagation costs \(O(W)\): except for very sparse networks \(W\) far exceeds the number of units, so the cost is dominated by the multiply-adds of the sums (5.48), and the evaluation of the activation functions is a small overhead. Backpropagation does the same amount of work in reverse: (5.56) uses each weight once, and (5.53) once more, so the whole gradient costs \(O(W)\), only a small constant factor more than evaluating the error itself.

Filling the gap 5.6: finite differences and why they cost \(O(W^2)\) (Exercise 5.14) pp. 246–247, (5.68), (5.69)

An alternative to backpropagation perturbs each weight in turn: \[ \frac{\partial E_n}{\partial w_{ji}}=\frac{E_n(w_{ji}+\epsilon)-E_n(w_{ji})}\epsilon+O(\epsilon), \tag{5.68} \] \[ \frac{\partial E_n}{\partial w_{ji}}=\frac{E_n(w_{ji}+\epsilon)-E_n(w_{ji}-\epsilon)}{2\epsilon}+O(\epsilon^2). \tag{5.69} \]

Derivation, the cost, and a gradient check

Taylor expansion. Write \(f(\epsilon)=E_n(w_{ji}+\epsilon)\) with all other weights fixed. Then \(f(\pm\epsilon)\)\({}=f(0)\pm\epsilon f'(0)\)\({}+\tfrac12\epsilon^2f''(0)\pm\tfrac16\epsilon^3f'''(0)\)\({}+O(\epsilon^4)\). The one-sided quotient is \([f(\epsilon)-f(0)]/\epsilon\)\({}=f'(0)+\tfrac12\epsilon f''(0)+O(\epsilon^2)\)\({}=f'(0)+O(\epsilon)\), which is (5.68). In the central quotient the even terms cancel in \(f(\epsilon)-f(-\epsilon)\)\({}=2\epsilon f'(0)+\tfrac13\epsilon^3f'''(0)\)\({}+O(\epsilon^5)\), so \([f(\epsilon)-f(-\epsilon)]/(2\epsilon)\)\({}=f'(0)+\tfrac16\epsilon^2f'''(0)+O(\epsilon^4)\): the \(O(\epsilon)\) corrections cancel, as claimed, at roughly twice the cost.

Cost. Each quotient needs forward passes costing \(O(W)\), and there are \(W\) weights to perturb: \(O(W^2)\) in total instead of backpropagation's \(O(W)\). In floating point the truncation error \(\propto\epsilon^2\) must be balanced against the rounding error \(\propto u/\epsilon\) (\(u\approx10^{-16}\)), so the best central difference uses \(\epsilon\approx u^{1/3}\sim10^{-5}\) and is accurate to about \(u^{2/3}\sim10^{-10}\) relative.

A gradient check (the use that matters). Numerical differentiation is far too slow for training but is the standard test of a backpropagation implementation. I ran it on a small network (\(D=2\), \(M=3\), \(K=2\) \(\tanh\)/linear, \(W=17\) weights and biases, \(N=6\) random patterns, weights drawn from \(\N(0,0.7^2)\), seed 1) with the formulae (5.62)–(5.67):

\(\epsilon\)\(10^{-1}\)\(10^{-2}\)\(10^{-3}\)\(10^{-4}\)\(10^{-5}\)\(10^{-6}\)\(10^{-8}\)
max error, forward difference (5.68)3.0e-13.0e-23.0e-33.0e-43.0e-53.0e-64.5e-7
max error, central difference (5.69)2.8e-32.8e-52.8e-72.8e-91.7e-102.3e-92.8e-7

The errors are the largest absolute difference over the 17 components against the backpropagation gradient (whose largest component is 12.5). The forward difference falls like \(\epsilon\), the central one like \(\epsilon^2\), until rounding error takes over below \(\epsilon\approx10^{-5}\): exactly the behaviour derived above, and the agreement to \(1.7\times10^{-10}\) confirms (5.66), (5.67) including the bias terms. The preset Backprop gradient against finite differences shows the same comparison interactively.

def grad(w):                       # backpropagation, (5.62)-(5.67), summed over patterns
    for x, t in zip(X, T):
        xe, a, z, ze, y = forward(w, x)          # xe = (1, x), ze = (1, z)
        dk = y - t                                # (5.65)
        dj = (1 - z**2) * (W2[:, 1:].T @ dk)      # (5.66)
        g1 += np.outer(dj, xe); g2 += np.outer(dk, ze)   # (5.67)
num = [(E(w + eps*e) - E(w - eps*e)) / (2*eps) for e in np.eye(W)]   # (5.69)
∎
▶ DemoBackprop gradient against finite differences (Section 5.3.2)Pick a weight and compare \(\delta_jz_i\) with the central difference; shrink \(\epsilon\) until rounding error takes over.

Modular systems and the Jacobian. Backpropagation applies to any differentiable module, not only to sigmoidal layers. In a system built of modules (Figure 5.8: an adaptive module with parameter \(\mathbf{w}\) feeding other modules that produce \(\mathbf{y}\)), the chain rule gives \(\partial E/\partial\mathbf{w}\)\({}=\sum_{k,j}\frac{\partial E}{\partial y_k}\frac{\partial y_k}{\partial z_j}\frac{\partial z_j}{\partial\mathbf{w}}\) (5.71); the middle factor is the Jacobian of the intermediate module.

5.3.4 The Jacobian matrix

pp. 247–249 The Jacobian of the network map has entries \[ J_{ki}\equiv\frac{\partial y_k}{\partial x_i}, \tag{5.70} \] each evaluated with the other inputs held fixed. It measures how sensitive the outputs are to the inputs; for small known input errors \(\Delta x_i\), \[ \Delta y_k\simeq\sum_i\frac{\partial y_k}{\partial x_i}\Delta x_i, \tag{5.72} \] valid only locally, because the network is nonlinear and \(\mathbf{J}\) must be re-evaluated at each input.

Result 5.7 (Jacobian by backpropagation) pp. 248–249, (5.73)–(5.77)

With the notation of Result 5.6, \[ J_{ki}=\sum_jw_{ji}\frac{\partial y_k}{\partial a_j}, \tag{5.73} \] the sum running over all units \(j\) that the input \(i\) feeds, and the derivatives \(\partial y_k/\partial a_j\) obey the backward recursion \[ \frac{\partial y_k}{\partial a_j}=h'(a_j)\sum_lw_{lj}\frac{\partial y_k}{\partial a_l}, \tag{5.74} \] started at the outputs with \[ \frac{\partial y_k}{\partial a_l}=\delta_{kl}\,\sigma'(a_l)\ \text{(sigmoid outputs)}\ (5.75),\qquad \frac{\partial y_k}{\partial a_l}=\delta_{kl}\,y_k-y_ky_l\ \text{(softmax outputs)}\ (5.76). \] Erratum. The book indexes these with \(j\); the index \(l\) above conforms with (5.74). For linear outputs \(\partial y_k/\partial a_l=\delta_{kl}\). Row \(k\) of \(\mathbf{J}\) costs one backward sweep, so the full Jacobian costs \(K\) sweeps, each \(O(W)\).

Proof, the forward alternative (Exercise 5.15), and the check (5.77)

(5.73). The input \(x_i\) affects \(y_k\) only through the activations \(a_j\) of the units it feeds, hence \(J_{ki}\)\({}=\sum_j\frac{\partial y_k}{\partial a_j}\frac{\partial a_j}{\partial x_i}\) with \(\partial a_j/\partial x_i=w_{ji}\) by (5.48).

(5.74). The same argument as for (5.56), with \(\partial y_k/\partial a_j\)\({}=\sum_l\frac{\partial y_k}{\partial a_l}\frac{\partial a_l}{\partial a_j}\) and \(\partial a_l/\partial a_j=w_{lj}h'(a_j)\) for the units \(l\) that \(j\) feeds.

(5.75), (5.76). For a logistic sigmoid output \(y_k=\sigma(a_k)\) depends only on \(a_k\): \(\partial y_k/\partial a_l\)\({}=\delta_{kl}\sigma'(a_l)\). For the softmax, \(\partial y_k/\partial a_l=y_k(\delta_{kl}-y_l)\) (Result 5.4).

Forward alternative (Exercise 5.15). Instead of a backward sweep per output, propagate the sensitivities of every unit to one input \(x_i\) forwards. Put \(\partial x_{i'}/\partial x_i=\delta_{ii'}\) for the inputs and, in topological order, \(\partial a_j/\partial x_i\)\({}=\sum_{l}w_{jl}\,\partial z_l/\partial x_i\), \(\partial z_j/\partial x_i\)\({}=h'(a_j)\,\partial a_j/\partial x_i\) (the chain rule applied to (5.48), (5.49)); at an output \(J_{ki}=f'(a_k)\,\partial a_k/\partial x_i\) (or the softmax combination of (5.76)). One forward sweep gives column \(i\) of \(\mathbf{J}\), so this needs \(D\) sweeps against \(K\) for backpropagation: use whichever of \(D\) and \(K\) is smaller. The two formalisms are analytically identical (both are the chain rule, summed in opposite orders).

Check (5.77). Central differences \(\partial y_k/\partial x_i\)\({}=[y_k(x_i+\epsilon)-y_k(x_i-\epsilon)]/(2\epsilon)\)\({}+O(\epsilon^2)\), at the cost of \(2D\) forward propagations. ∎

5.4 The Hessian matrix

pp. 249–256 Backpropagation also gives second derivatives \(\partial^2E/\partial w_{ji}\partial w_{lk}\) (5.78); collecting all \(W\) parameters in one vector, these are the entries \(H_{ij}\) of the Hessian. The Hessian is used for: (1) second-order optimisation algorithms; (2) fast re-training after a small change of the data; (3) pruning, where the inverse Hessian ranks weights by their importance; (4) the Laplace approximation of Section 5.7, where \(\mathbf{H}^{-1}\) gives the predictive variance, its eigenvalues give the hyperparameters and its determinant the evidence. A \(W\times W\) matrix has \(W^2\) entries, so no method can do better than \(O(W^2)\) per pattern for the whole matrix; the exact algorithm of Section 5.4.5 attains this.

5.4.1 Diagonal approximation

pp. 250 The inverse of a diagonal matrix is trivial, so a diagonal approximation is cheap to invert. Work pattern by pattern (\(E=\sum_nE_n\)).

Result 5.8 (diagonal Hessian elements) p. 250, (5.79)–(5.81)

For one pattern, \[ \frac{\partial^2E_n}{\partial w_{ji}^2}=\frac{\partial^2E_n}{\partial a_j^2}\,z_i^2, \tag{5.79} \] \[ \frac{\partial^2E_n}{\partial a_j^2}=h'(a_j)^2\sum_k\sum_{k'}w_{kj}w_{k'j}\frac{\partial^2E_n}{\partial a_k\partial a_{k'}}+h''(a_j)\sum_kw_{kj}\frac{\partial E_n}{\partial a_k}, \tag{5.80} \] where \(k,k'\) run over the units fed by \(j\). Neglecting the off-diagonal second derivatives \(k\ne k'\) gives the recursion \[ \frac{\partial^2E_n}{\partial a_j^2}\simeq h'(a_j)^2\sum_kw_{kj}^2\frac{\partial^2E_n}{\partial a_k^2}+h''(a_j)\sum_kw_{kj}\frac{\partial E_n}{\partial a_k}, \tag{5.81} \] which costs \(O(W)\). Erratum. The official errata correct a typographical error in the derivative in the last term of (5.80); the form above is the one the derivation gives.

Proof

(5.79): \(E_n\) depends on \(w_{ji}\) only through \(a_j=\sum_iw_{ji}z_i\), with \(\partial a_j/\partial w_{ji}=z_i\) independent of \(w_{ji}\); so \(\partial^2E_n/\partial w_{ji}^2\)\({}=\frac{\partial}{\partial w_{ji}}(\delta_jz_i)\)\({}=z_i\frac{\partial\delta_j}{\partial a_j}z_i\).

(5.80): start from (5.56), \(\delta_j\)\({}=\partial E_n/\partial a_j\)\({}=h'(a_j)\sum_kw_{kj}\,\partial E_n/\partial a_k\), and differentiate again with respect to \(a_j\), by the product rule. The factor \(h'(a_j)\) gives \(h''(a_j)\sum_kw_{kj}\partial E_n/\partial a_k\). The sum is differentiated through \(\partial E_n/\partial a_k\), which depends on \(a_j\) through the activations \(a_{k'}\) of the units fed by \(j\) with \(\partial a_{k'}/\partial a_j=w_{k'j}h'(a_j)\): \(\partial(\partial E_n/\partial a_k)/\partial a_j\)\({}=\sum_{k'}\frac{\partial^2E_n}{\partial a_k\partial a_{k'}}w_{k'j}h'(a_j)\). Multiplying by the outer \(h'(a_j)w_{kj}\) and summing over \(k\) gives the first term of (5.80).

(5.81): keep only \(k=k'\) in the double sum. The result is a backward recursion of the same shape as (5.56), started at the output units with \(\partial^2E_n/\partial a_k^2\) (equal to 1 for linear outputs and the sum of squares). ∎

Ricotti et al. (1988) keep all terms in (5.80), which makes the diagonal exact but the cost no longer \(O(W)\). In practice the Hessian of a trained network is strongly non-diagonal, so diagonal approximations driven by convenience must be used with care.

5.4.2 Outer product approximation

pp. 251 For regression with a single output and \(E=\tfrac12\sum_n(y_n-t_n)^2\) (5.82) the Hessian is \[ \mathbf{H}=\nabla\nabla E=\sum_{n=1}^{N}\nabla y_n(\nabla y_n)^\T+\sum_{n=1}^{N}(y_n-t_n)\nabla\nabla y_n. \tag{5.83} \] Erratum. The book omits the transpose in the first term. Dropping the second term gives the Levenberg–Marquardt or outer product approximation \[ \mathbf{H}\simeq\sum_{n=1}^{N}\mathbf{b}_n\mathbf{b}_n^\T,\qquad \mathbf{b}_n\equiv\nabla a_n=\nabla y_n . \tag{5.84} \] Erratum. The book writes \(\mathbf{b}_n=\nabla y_n=\nabla a_n\); the order of definition above is the corrected one.

Result 5.9 (outer product approximation: when it is good, other error functions, and its meaning) pp. 251, 286–287, (5.83)–(5.85); Exercises 5.16, 5.17, 5.19, 5.20

  1. Why the dropped term is small. If \(y_n\approx t_n\) after training, \((y_n-t_n)\nabla\nabla y_n\) is small. More generally, \(y_n-t_n\) has zero mean given \(\mathbf{x}_n\) at the optimal \(y=\E[t\given\mathbf{x}]\) (Section 1.5.5); if it is uncorrelated with \(\nabla\nabla y_n\), the sum over \(n\) averages out.
  2. Several outputs (Exercise 5.16). \(\mathbf{H}\)\({}\simeq\sum_n\sum_{k=1}^K\nabla y_{nk}(\nabla y_{nk})^\T\).
  3. Logistic sigmoid with cross-entropy (Exercise 5.19): \(\displaystyle\mathbf{H}\)\({}\simeq\sum_{n=1}^{N}y_n(1-y_n)\,\mathbf{b}_n\mathbf{b}_n^\T\) (5.85), with \(\mathbf{b}_n=\nabla a_n\).
  4. Softmax with cross-entropy (Exercise 5.20): \(\displaystyle\mathbf{H}\)\({}\simeq\sum_n\sum_{k,l}y_{nk}(\delta_{kl}-y_{nl})\,\mathbf{b}_{nk}\mathbf{b}_{nl}^\T\), with \(\mathbf{b}_{nk}=\nabla a_{nk}\).
  5. Population version (Exercise 5.17). For \(E\)\({}=\tfrac12\iint\{y(\mathbf{x},\mathbf{w})\)\({}-t\}^2p(\mathbf{x},t)\,d\mathbf{x}\,dt\) at the minimiser \(y=\E[t\given\mathbf{x}]\), \(\displaystyle\frac{\partial^2E}{\partial w_r\partial w_s}\)\({}=\int\frac{\partial y}{\partial w_r}\frac{\partial y}{\partial w_s}p(\mathbf{x})\,d\mathbf{x}\) (5.194); a finite sample from \(p(\mathbf{x})\) gives (5.84).
  6. Meaning. The matrix in (5.84) is the Gauss–Newton matrix \(\mathbf{J}^\T\mathbf{J}\) of the residuals, positive semi-definite, and under the Gaussian noise model (5.12) it is \(\beta^{-1}\) times the Fisher information; (5.85) and item 4 are the Fisher information of the Bernoulli and categorical models. Its rank is at most \(NK\).
Proof

(5.83). \(\nabla E=\sum_n(y_n-t_n)\nabla y_n\). Differentiating once more by the product rule gives \(\sum_n\nabla y_n(\nabla y_n)^\T\)\({}+\sum_n(y_n-t_n)\nabla\nabla y_n\). (2) Same with a sum over \(k\). (3) For \(E_n=-[t\ln y+(1-t)\ln(1-y)]\), \(y=\sigma(a)\): \(\nabla E_n=(y-t)\nabla a\) (Result 5.4 and the chain rule), so \(\nabla\nabla E_n\)\({}=\sigma'(a)\nabla a\nabla a^\T\)\({}+(y-t)\nabla\nabla a\) with \(\sigma'(a)=y(1-y)\); drop the second term. (4) \(\nabla E_n=\sum_k(y_{nk}-t_{nk})\nabla a_{nk}\), and \(\nabla\nabla E_n\)\({}=\sum_{k,l}\frac{\partial y_{nk}}{\partial a_{nl}}\nabla a_{nl}\nabla a_{nk}^\T\)\({}+\sum_k(y_{nk}-t_{nk})\nabla\nabla a_{nk}\); with \(\partial y_{k}/\partial a_{l}\)\({}=y_k(\delta_{kl}-y_l)\) (Result 5.4) and the second term dropped, this is the formula. (5) \(\partial E/\partial w_r\)\({}=\iint\{y-t\}\frac{\partial y}{\partial w_r}p\), so \(\partial^2E/\partial w_r\partial w_s\)\({}=\iint\frac{\partial y}{\partial w_r}\frac{\partial y}{\partial w_s}p\)\({}+\iint\{y\)\({}-t\}\frac{\partial^2y}{\partial w_r\partial w_s}p\). Integrate the second term over \(t\) first: \(\int\{y-t\}p(t\given\mathbf{x})dt\)\({}=y-\E[t\given\mathbf{x}]\)\({}=0\), so only the first term survives.

(6) Under (5.12), \(\nabla_{\mathbf{w}}\ln p(t\given\mathbf{x},\mathbf{w})\)\({}=\beta(t-y)\nabla y\), and \(\E[(t-y)^2\given\mathbf{x}]=\beta^{-1}\), so the Fisher information is \(\sum_n\E[\beta^2(t-y_n)^2]\nabla y_n\nabla y_n^\T\)\({}=\beta\sum_n\nabla y_n\nabla y_n^\T\)\({}=\beta\,\mathbf{H}_{\rm GN}\). In the Bernoulli case \(\nabla_{\mathbf{w}}\ln p=(t-y)\nabla a\) and \(\E[(t-y)^2]=y(1-y)\), giving (5.85). The expected Hessian of the negative log likelihood is the Fisher information, because the dropped term is linear in \(y-t\), which has zero expectation under the model: so (5.84), (5.85) are the Hessian with the model-expected second term removed. Finally \(\mathbf{H}_{\rm GN}\)\({}=\sum_n\mathbf{b}_n\mathbf{b}_n^\T\) is a sum of \(NK\) rank-one positive semi-definite matrices, so it is PSD of rank at most \(NK\). ∎

Link to Levenberg–Marquardt. Minimising \(\tfrac12\sum_n(y_n(\mathbf{w})-t_n)^2\) by linearising \(y_n(\mathbf{w}+\Delta)\)\({}\simeq y_n+\mathbf{b}_n^\T\Delta\) gives a linear least-squares problem whose normal equations are \(\mathbf{H}_{\rm GN}\Delta=-\nabla E\) (Gauss–Newton step). Levenberg (1944) and Marquardt (1963) damp it, \((\mathbf{H}_{\rm GN}+\mu\mathbf{I})\Delta\)\({}=-\nabla E\): for \(\mu\to0\) a Gauss–Newton step, for \(\mu\to\infty\) a gradient-descent step of size \(1/\mu\); the damping also repairs the rank deficiency. In the small network of Section 5.3.3 (\(N=6\), \(K=2\), \(W=17\)) the outer-product matrix indeed had rank 12 \(=NK

5.4.3 Inverse Hessian

pp. 252 Write the outer-product approximation as \(\mathbf{H}_N\)\({}=\sum_{n=1}^{N}\mathbf{b}_n\mathbf{b}_n^\T\) (5.86) and add the data points one at a time, \(\mathbf{H}_{L+1}\)\({}=\mathbf{H}_L\)\({}+\mathbf{b}_{L+1}\mathbf{b}_{L+1}^\T\) (5.87).

Result 5.10 (sequential inverse Hessian) p. 252, (5.88), (5.89)

For an invertible symmetric \(\mathbf{M}\) and a vector \(\mathbf{v}\) with \(1+\mathbf{v}^\T\mathbf{M}^{-1}\mathbf{v}\ne0\), \[ (\mathbf{M}+\mathbf{v}\mathbf{v}^\T)^{-1}=\mathbf{M}^{-1}-\frac{(\mathbf{M}^{-1}\mathbf{v})(\mathbf{v}^\T\mathbf{M}^{-1})}{1+\mathbf{v}^\T\mathbf{M}^{-1}\mathbf{v}} . \tag{5.88} \] Hence, with \(\mathbf{H}_0=\alpha\mathbf{I}\), \[ \mathbf{H}_{L+1}^{-1}=\mathbf{H}_L^{-1}-\frac{\mathbf{H}_L^{-1}\mathbf{b}_{L+1}\mathbf{b}_{L+1}^\T\mathbf{H}_L^{-1}}{1+\mathbf{b}_{L+1}^\T\mathbf{H}_L^{-1}\mathbf{b}_{L+1}} , \tag{5.89} \] which after one pass over the data gives \((\mathbf{H}_N+\alpha\mathbf{I})^{-1}\) in \(O(NW^2)\) operations. With \(K\) outputs, treat each pair \((n,k)\) as a separate rank-one term (below).

Proof (Exercise 5.21 included)

(5.88) is the case of the Woodbury identity (toolkit) with a rank-one update. Direct check: multiply \((\mathbf{M}+\mathbf{v}\mathbf{v}^\T)\) by the right-hand side and write \(\mathbf{u}=\mathbf{M}^{-1}\mathbf{v}\), \(c=1+\mathbf{v}^\T\mathbf{u}\): \[ (\mathbf{M}+\mathbf{v}\mathbf{v}^\T)\Big(\mathbf{M}^{-1}-\frac{\mathbf{u}\mathbf{u}^\T}{c}\Big)=\mathbf{I}+\mathbf{v}\mathbf{u}^\T-\frac{\mathbf{v}\mathbf{u}^\T}{c}-\frac{\mathbf{v}(\mathbf{v}^\T\mathbf{u})\mathbf{u}^\T}{c}=\mathbf{I}+\mathbf{v}\mathbf{u}^\T\Big[1-\frac{1+\mathbf{v}^\T\mathbf{u}}{c}\Big]=\mathbf{I}. \] (5.89) is (5.88) with \(\mathbf{M}=\mathbf{H}_L\), \(\mathbf{v}=\mathbf{b}_{L+1}\), where \(\mathbf{H}_L\) is the Hessian of the first \(L\) points plus \(\alpha\mathbf{I}\) and so is invertible; \(c>0\) because \(\mathbf{M}^{-1}\) is positive definite. The start \(\mathbf{H}_0^{-1}=\alpha^{-1}\mathbf{I}\) means the recursion computes the inverse of \(\mathbf{H}_N+\alpha\mathbf{I}\); the result is not sensitive to the small value of \(\alpha\). Each step costs \(O(W^2)\) (matrix-vector product and outer product).

Several outputs (Exercise 5.21). The multi-output outer product is \(\sum_n\sum_k\mathbf{b}_{nk}\mathbf{b}_{nk}^\T\) (Result 5.9, item 2), so apply (5.89) once for each pair \((n,k)\) with \(\mathbf{b}_{nk}=\nabla y_{nk}\): \(NK\) updates of cost \(O(W^2)\). For the sigmoid cross-entropy case use \(\mathbf{b}\to\sqrt{y_n(1-y_n)}\,\mathbf{b}_n\); for the softmax, factor the positive semi-definite \(K\times K\) matrix \(\bm\Lambda_n\)\({}=\diag(\mathbf{y}_n)\)\({}-\mathbf{y}_n\mathbf{y}_n^\T\)\({}=\sum_{r}\mathbf{c}_r\mathbf{c}_r^\T\) (e.g. by Cholesky or eigendecomposition); then item 4 of Result 5.9 equals \(\sum_r(\mathbf{B}_n^\T\mathbf{c}_r)(\mathbf{B}_n^\T\mathbf{c}_r)^\T\) with \(\mathbf{B}_n\)\({}=\partial\mathbf{a}_n/\partial\mathbf{w}\), again a sum of rank-one terms. Check. On the small network of Section 5.3.3, absorbing the \(NK=12\) terms by (5.89) with \(\alpha=10^{-3}\) reproduced \((\mathbf{H}_{\rm GN}+\alpha\mathbf{I})^{-1}\) computed by a direct inverse to \(2.6\times10^{-10}\) (absolute, in matrix entries of order \(10^{3}\)). ∎

Quasi-Newton optimisers (BFGS and relatives) build an approximation to \(\mathbf{H}^{-1}\) as a by-product of training (Bishop and Nabney, 2008).

5.4.4 Finite differences

pp. 252–253 Perturbing every pair of weights, \[ \frac{\partial^2E}{\partial w_{ji}\partial w_{lk}}=\frac1{4\epsilon^2}\{E(w_{ji}+\epsilon,w_{lk}+\epsilon)-E(w_{ji}+\epsilon,w_{lk}-\epsilon)-E(w_{ji}-\epsilon,w_{lk}+\epsilon)+E(w_{ji}-\epsilon,w_{lk}-\epsilon)\}+O(\epsilon^2). \tag{5.90} \] Applying central differences to the first derivatives, themselves obtained by backpropagation, is cheaper: \[ \frac{\partial^2E}{\partial w_{ji}\partial w_{lk}}=\frac1{2\epsilon}\Big\{\frac{\partial E}{\partial w_{ji}}(w_{lk}+\epsilon)-\frac{\partial E}{\partial w_{ji}}(w_{lk}-\epsilon)\Big\}+O(\epsilon^2). \tag{5.91} \]

Filling the gap 5.7: why (5.90) and (5.91) are \(O(\epsilon^2)\), and their costs pp. 252–253

Derivation

Write \(a,b\) for the two weights and expand \(E(w_a+s\epsilon,w_b+t\epsilon)\) with \(s,t=\pm1\) in a Taylor series; the combination in (5.90) is \(\sum_{s,t}st\,E(w_a+s\epsilon,w_b+t\epsilon)\). A monomial \(s^pt^q\) multiplied by \(st\) sums over \(s,t\in\{\pm1\}\) to \(\sum_ss^{p+1}\sum_tt^{q+1}\), which is 4 if both \(p+1\) and \(q+1\) are even and 0 otherwise. The terms of total degree \(d=p+q\): for \(d=0\) (\(p=q=0\)) and \(d=1\) one of \(p+1,q+1\) is odd, so they vanish; for \(d=2\) only \(p=q=1\) survives, with coefficient \(\partial^2E/\partial w_a\partial w_b\,\epsilon^2\) (the cross term of \(\tfrac12(s\epsilon\partial_a+t\epsilon\partial_b)^2\) is \(st\,\epsilon^2E_{ab}\)), giving \(4\epsilon^2E_{ab}\); for odd \(d\) (and in particular \(d=3\)) \(p+1\) and \(q+1\) cannot both be even, so all odd orders vanish. The first neglected term is \(d=4\), \(O(\epsilon^4)\). Dividing by \(4\epsilon^2\) gives \(E_{ab}+O(\epsilon^2)\), which is (5.90). For (5.91), the central quotient (5.69) applied to the function \(w_{lk}\mapsto\partial E/\partial w_{ji}\) has error \(O(\epsilon^2)\).

Cost. There are \(W^2\) entries (\(W(W+1)/2\) by symmetry), each needing 4 forward passes of cost \(O(W)\) per pattern: \(O(W^3)\) for (5.90). In (5.91) only the \(W\) weights are perturbed, each time twice evaluating a gradient at \(O(W)\); each perturbation yields a whole row of \(\mathbf{H}\), so the cost is \(O(W^2)\). Both are valuable as checks of a Hessian code. ∎

5.4.5 Exact evaluation of the Hessian

pp. 253–254 For a network with two layers of weights the exact Hessian is derived by differentiating the backpropagation formulae. Indices \(i,i'\) denote inputs, \(j,j'\) hidden units, \(k,k'\) outputs, and \[ \delta_k=\frac{\partial E_n}{\partial a_k},\qquad M_{kk'}\equiv\frac{\partial^2E_n}{\partial a_k\partial a_{k'}} . \tag{5.92} \] (For the sum of squares with linear outputs \(M_{kk'}=\delta_{kk'}\), for sigmoid cross-entropy \(M_{kk'}=\delta_{kk'}y_k(1-y_k)\), for softmax cross-entropy \(M_{kk'}=y_k\delta_{kk'}-y_ky_{k'}\).)

Result 5.11 (exact Hessian of a two-layer network) pp. 253–254, (5.93)–(5.95)

  1. Both weights in the second layer: \[ \frac{\partial^2E_n}{\partial w^{(2)}_{kj}\partial w^{(2)}_{k'j'}}=z_jz_{j'}M_{kk'} .\tag{5.93} \]
  2. Both weights in the first layer (\(I_{jj'}\) is the identity matrix): \[ \frac{\partial^2E_n}{\partial w^{(1)}_{ji}\partial w^{(1)}_{j'i'}}=x_ix_{i'}h''(a_{j'})I_{jj'}\sum_kw^{(2)}_{kj'}\delta_k+x_ix_{i'}h'(a_{j'})h'(a_j)\sum_k\sum_{k'}w^{(2)}_{k'j'}w^{(2)}_{kj}M_{kk'} .\tag{5.94} \]
  3. One weight in each layer: \[ \frac{\partial^2E_n}{\partial w^{(1)}_{ji}\partial w^{(2)}_{kj'}}=x_ih'(a_j)\Big\{\delta_kI_{jj'}+z_{j'}\sum_{k'}w^{(2)}_{k'j}M_{kk'}\Big\}. \tag{5.95} \] Erratum. The printed (5.95) has \(H_{kk'}\) where \(M_{kk'}\) is meant, and the indices \(j,j'\) are interchanged; the form above is the corrected one (and is what the derivation gives).

If a weight is a bias, set the corresponding \(x_i\) or \(z_j\) to 1. The full Hessian is the sum over patterns and costs \(O(W^2)\).

Proof (Exercise 5.22) and skip-layer connections (Exercise 5.23)

Use (5.53): \(\partial E_n/\partial w^{(2)}_{kj}=\delta_kz_j\) and \(\partial E_n/\partial w^{(1)}_{ji}=\delta_jx_i\), with \(\delta_j=h'(a_j)\sum_kw^{(2)}_{kj}\delta_k\). Two chain-rule facts: \(\partial a_{k'}/\partial w^{(2)}_{kj}\)\({}=\delta_{kk'}z_j\) and, for first-layer weights, \(\partial a_{j}/\partial w^{(1)}_{j'i'}\)\({}=I_{jj'}x_{i'}\), \(\partial a_{k'}/\partial w^{(1)}_{j'i'}\)\({}=w^{(2)}_{k'j'}h'(a_{j'})x_{i'}\). Also \(\partial\delta_k/\partial a_{k'}=M_{kk'}\) by definition.

(5.93). \(z_j\) does not depend on second-layer weights, and \(\delta_k\) depends on \(w^{(2)}_{k'j'}\) through \(a_{k'}\) only: \(\partial\delta_k/\partial w^{(2)}_{k'j'}\)\({}=M_{kk'}z_{j'}\). Hence \(\partial(\delta_kz_j)/\partial w^{(2)}_{k'j'}\)\({}=z_jz_{j'}M_{kk'}\).

(5.94). Differentiate \(\delta_jx_i\) with respect to \(w^{(1)}_{j'i'}\), which acts through \(a_{j'}\) (factor \(x_{i'}\)): \(x_ix_{i'}\,\partial\delta_j/\partial a_{j'}\). Differentiate \(\delta_j=h'(a_j)\sum_kw^{(2)}_{kj}\delta_k\). The factor \(h'(a_j)\) depends on \(a_{j'}\) only if \(j=j'\), giving \(h''(a_{j'})I_{jj'}\sum_kw^{(2)}_{kj'}\delta_k\) (the first term). Each \(\delta_k\) depends on \(a_{j'}\) through every \(a_{k'}\): \(\partial\delta_k/\partial a_{j'}\)\({}=\sum_{k'}M_{kk'}w^{(2)}_{k'j'}h'(a_{j'})\), giving \(h'(a_j)\sum_kw^{(2)}_{kj}\sum_{k'}M_{kk'}w^{(2)}_{k'j'}h'(a_{j'})\), the second term.

(5.95). Differentiate \(\delta_kz_{j'}\) with respect to \(w^{(1)}_{ji}\): \(z_{j'}\) gives \(h'(a_{j'})I_{jj'}x_i\), and \(\delta_k\) gives \(\sum_{k'}M_{kk'}w^{(2)}_{k'j}h'(a_j)x_i\). Sum: \(x_ih'(a_j)\{\delta_kI_{jj'}\)\({}+z_{j'}\sum_{k'}M_{kk'}w^{(2)}_{k'j}\}\), using \(h'(a_{j'})I_{jj'}=h'(a_j)I_{jj'}\).

Skip-layer connections (Exercise 5.23). With \(a_k\)\({}=\sum_jw^{(2)}_{kj}z_j+\sum_iw^{\rm s}_{ki}x_i\), \(\partial a_{k'}/\partial w^{\rm s}_{ki}\)\({}=\delta_{kk'}x_i\). Equations (5.93)–(5.95) are unchanged (the \(\delta_k,M_{kk'}\) now include the skip contributions to \(a_k\)), and the new blocks are \(\partial^2E_n/\partial w^{\rm s}_{ki}\partial w^{\rm s}_{k'i'}\)\({}=x_ix_{i'}M_{kk'}\), \(\partial^2E_n/\partial w^{\rm s}_{ki}\partial w^{(2)}_{k'j}\)\({}=x_iz_jM_{kk'}\), and \(\partial^2E_n/\partial w^{\rm s}_{ki}\partial w^{(1)}_{ji'}\)\({}=x_ix_{i'}h'(a_j)\sum_{k'}M_{kk'}w^{(2)}_{k'j}\), obtained exactly as above.

Numerical check. I coded (5.93)–(5.95) (with biases as constant units) for the small network of Section 5.3.3 with the sum of squares (\(M_{kk'}=\delta_{kk'}\)) and compared with the Hessian obtained by central differences of the backpropagated gradient, (5.91): the largest entry-wise difference was \(1.7\times10^{-10}\). ∎

5.4.6 Fast multiplication by the Hessian

pp. 254–256 Often one needs \(\mathbf{H}\mathbf{v}\) (or \(\mathbf{v}^\T\mathbf{H}\)), not \(\mathbf{H}\): conjugate-gradient or power iterations, trace estimates, eigenvalues for \(\gamma\) (Section 5.7.2). Forming \(\mathbf{H}\) costs \(O(W^2)\) time and storage, but \(\mathbf{v}^\T\mathbf{H}\) has only \(W\) entries, and can be had in \(O(W)\). Since \(\mathbf{H}\) is symmetric, \[ \mathbf{v}^\T\mathbf{H}=\mathbf{v}^\T\nabla(\nabla E), \tag{5.96} \] i.e. the directional derivative of the gradient. Define the differential operator \(\mathcal{R}\{\cdot\}\equiv\mathbf{v}^\T\nabla\) (Pearlmutter, 1994); it obeys the ordinary rules of calculus (linearity, product rule, chain rule) and \[ \mathcal{R}\{\mathbf{w}\}=\mathbf{v}. \tag{5.97} \]

Algorithm 5.3 (\(\mathcal{R}\)-operator evaluation of \(\mathbf{v}^\T\mathbf{H}\) for a two-layer network) pp. 254–256, (5.98)–(5.111)

Linear outputs, sum-of-squares error, one pattern; \(v_{ji}\) is the component of \(\mathbf{v}\) that belongs to \(w_{ji}\).

  1. Ordinary forward and backward pass (5.98)–(5.100), (5.104), (5.105): \(a_j=\sum_iw_{ji}x_i\), \(z_j=h(a_j)\), \(y_k=\sum_jw_{kj}z_j\), \(\delta_k=y_k-t_k\), \(\delta_j=h'(a_j)\sum_kw_{kj}\delta_k\).
  2. Forward \(\mathcal{R}\) pass: \(\mathcal{R}\{a_j\}=\sum_iv_{ji}x_i\) (5.101); \(\mathcal{R}\{z_j\}=h'(a_j)\mathcal{R}\{a_j\}\) (5.102); \(\mathcal{R}\{y_k\}\)\({}=\sum_jw_{kj}\mathcal{R}\{z_j\}\)\({}+\sum_jv_{kj}z_j\) (5.103).
  3. Backward \(\mathcal{R}\) pass: \(\mathcal{R}\{\delta_k\}=\mathcal{R}\{y_k\}\) (5.106); \(\mathcal{R}\{\delta_j\}\)\({}=h''(a_j)\mathcal{R}\{a_j\}\sum_kw_{kj}\delta_k\)\({}+h'(a_j)\sum_kv_{kj}\delta_k\)\({}+h'(a_j)\sum_kw_{kj}\mathcal{R}\{\delta_k\}\) (5.107).
  4. Output: \((\mathbf{v}^\T\mathbf{H})_{kj}\)\({}=\mathcal{R}\{\delta_k\}z_j\)\({}+\delta_k\mathcal{R}\{z_j\}\) (5.110); \((\mathbf{v}^\T\mathbf{H})_{ji}\)\({}=x_i\mathcal{R}\{\delta_j\}\) (5.111). Sum over patterns.
Derivation of each step, cost, and a numerical check

Why \(\mathcal{R}\{\nabla E\}=\mathbf{v}^\T\mathbf{H}\). The \(r\)th component of \(\mathcal{R}\{\nabla E\}\) is \(\sum_sv_s\,\partial(\partial E/\partial w_r)/\partial w_s\)\({}=\sum_sH_{rs}v_s\)\({}=(\mathbf{H}\mathbf{v})_r\)\({}=(\mathbf{v}^\T\mathbf{H})_r\).

Rules used. The operator is a first-order derivative, so \(\mathcal{R}\{fg\}\)\({}=\mathcal{R}\{f\}g+f\mathcal{R}\{g\}\) and \(\mathcal{R}\{f(u)\}=f'(u)\mathcal{R}\{u\}\); inputs \(x_i\) and targets \(t_k\) do not depend on \(\mathbf{w}\), so \(\mathcal{R}\{x_i\}=\mathcal{R}\{t_k\}=0\).

(5.101)–(5.103). \(\mathcal{R}\{a_j\}\)\({}=\sum_i\mathcal{R}\{w_{ji}\}x_i\)\({}=\sum_iv_{ji}x_i\); \(\mathcal{R}\{z_j\}\)\({}=\mathcal{R}\{h(a_j)\}\)\({}=h'(a_j)\mathcal{R}\{a_j\}\); \(\mathcal{R}\{y_k\}\)\({}=\sum_j(\mathcal{R}\{w_{kj}\}z_j+w_{kj}\mathcal{R}\{z_j\})\).

(5.106), (5.107). \(\mathcal{R}\{\delta_k\}\)\({}=\mathcal{R}\{y_k-t_k\}\)\({}=\mathcal{R}\{y_k\}\). For \(\delta_j=h'(a_j)\sum_kw_{kj}\delta_k\) apply the product rule to the three factors \(h'(a_j)\), \(w_{kj}\), \(\delta_k\): \(\mathcal{R}\{h'(a_j)\}\)\({}=h''(a_j)\mathcal{R}\{a_j\}\), \(\mathcal{R}\{w_{kj}\}=v_{kj}\), and \(\mathcal{R}\{\delta_k\}\) from (5.106).

(5.110), (5.111). Apply \(\mathcal{R}\) to \(\partial E/\partial w_{kj}=\delta_kz_j\) and \(\partial E/\partial w_{ji}=\delta_jx_i\).

Cost. The \(\mathcal{R}\) pass mirrors the ordinary forward and backward passes, with a few extra multiply-adds per weight; the cost is \(O(W)\), a small multiple of the cost of the gradient. Taking \(\mathbf{v}\) to be the unit vectors \((0,\dots,1,\dots,0)\) one at a time gives the columns of \(\mathbf{H}\): \(W\) passes of \(O(W)\), \(O(W^2)\) in total, analytically the same as the algorithm of Section 5.4.5 (Bishop 1992), with some redundant computation.

Numerical check. On the small network of Section 5.3.3 (\(W=17\), sum of squares) I computed \(\mathbf{H}\mathbf{v}\) for a random \(\mathbf{v}\) with Algorithm 5.3 and with the Hessian obtained from (5.91): the largest difference over the 17 components was \(2.6\times10^{-10}\), while \(\max|\mathbf{H}\mathbf{v}|=9.9\). A cheap alternative that needs only gradient evaluations is \(\mathbf{H}\mathbf{v}\)\({}\approx[\nabla E(\mathbf{w}+\epsilon\mathbf{v})-\nabla E(\mathbf{w}-\epsilon\mathbf{v})]/(2\epsilon)\), but the \(\mathcal{R}\) operator is exact. ∎

5.5 Regularization in neural networks

pp. 256–272 The numbers of inputs and outputs are fixed by the data, but the number \(M\) of hidden units is free and controls model complexity. One might hope for an optimal \(M\) balancing under- and over-fitting (Figure 5.9), but the generalisation error is not a smooth function of \(M\), because of local minima: Figure 5.10 shows 30 random starts for each \(M\), and the best validation error happened at \(M=8\). An alternative is to take \(M\) large and add a regulariser, the simplest being weight decay \[ \widetilde{E}(\mathbf{w})=E(\mathbf{w})+\frac\lambda2\mathbf{w}^\T\mathbf{w}, \tag{5.112} \] the negative log of a zero-mean Gaussian prior with precision \(\lambda\) (Chapter 3).

Three panels showing the same ten noisy sine data points fitted by two-layer networks with M=1, 3 and 10 hidden units; the M=1 fit is too stiff, M=3 follows the sine, and M=10 wiggles through every point.
Figure 5.9 (reproduced). Two-layer networks with \(M=1,3,10\) hidden units fitted to \(N=10\) points from the sinusoidal data set by minimising the sum of squares with a L-BFGS (the book uses scaled conjugate gradients) with a tiny weight decay of \(10^{-3}\), which was needed to avoid near-vertical spikes in the \(M=3\) and \(M=10\) fits. Book: Figure 5.9, p. 257. Code: ch05_neural_networks.py, fig5_9. Download: PDF · SVG
▶ DemoOver-fitting: more hidden units (Fig 5.9)Increase \(M\) with ten data points: the training error keeps falling while the curve starts to oscillate between the points.
Scatter plot of test-set sum-of-squares error against the number of hidden units from 1 to 10, with 30 random starts per size, showing a wide spread of final errors caused by local minima.
Figure 5.10 (reproduced). Test-set sum-of-squares error against the number \(M\) of hidden units, with 30 random starting weight vectors (isotropic Gaussian, variance 10) for each \(M\): the spread at fixed \(M\) comes from local minima. Our run uses 60 training and 600 test points; the best test sum of squares is 59.4 at \(M=6\), against a noise floor of 54. Book: Figure 5.10, p. 257. Code: ch05_neural_networks.py, fig5_10. Download: PDF · SVG
▶ DemoWeight decayFix a large \(M\) and increase \(\lambda\) in (5.112): the over-fitted curve is smoothed without changing the architecture.

5.5.1 Consistent Gaussian priors

pp. 257–259 Simple weight decay treats all weights alike and is inconsistent with the scaling properties of networks. Take a two-layer network with linear outputs, \[ z_j=h\Big(\sum_iw_{ji}x_i+w_{j0}\Big),\qquad y_k=\sum_jw_{kj}z_j+w_{k0}, \tag{5.113, 5.114} \] and rescale the data linearly, \[ x_i\to\widetilde{x}_i=ax_i+b,\qquad y_k\to\widetilde{y}_k=cy_k+d . \tag{5.115, 5.118} \]

Result 5.12 (weight transformations and the consistent regulariser) pp. 258–259, (5.116)–(5.123); Exercise 5.24

The network mapping is unchanged under the input transformation (5.115) if the first-layer weights and biases are replaced by \[ \widetilde{w}_{ji}=\frac1a\,w_{ji},\qquad \widetilde{w}_{j0}=w_{j0}-\frac ba\sum_iw_{ji}, \tag{5.116, 5.117} \] and the output transformation (5.118) is realised by \[ \widetilde{w}_{kj}=c\,w_{kj},\qquad \widetilde{w}_{k0}=c\,w_{k0}+d. \tag{5.119, 5.120} \] The regulariser \[ \frac{\lambda_1}2\sum_{w\in\mathcal{W}_1}w^2+\frac{\lambda_2}2\sum_{w\in\mathcal{W}_2}w^2, \tag{5.121} \] (\(\mathcal{W}_1,\mathcal{W}_2\): first- and second-layer weights, biases excluded) is invariant under the shifts of the biases, and its value is unchanged under the rescalings (5.116), (5.119) if \(\lambda_1\) and \(\lambda_2\) are rescaled to \(\widetilde\lambda_1=a^{2}\lambda_1\) and \(\widetilde\lambda_2=c^{-2}\lambda_2\). Plain weight decay (5.112) has neither property.

Proof

First layer. The new hidden activation is \(\widetilde a_j\)\({}=\sum_i\widetilde w_{ji}\widetilde x_i\)\({}+\widetilde w_{j0}\)\({}=\sum_i\frac{w_{ji}}a(ax_i+b)+w_{j0}\)\({}-\frac ba\sum_iw_{ji}\)\({}=\sum_iw_{ji}x_i+\frac ba\sum_iw_{ji}+w_{j0}\)\({}-\frac ba\sum_iw_{ji}\)\({}=a_j\). So \(\widetilde z_j=z_j\) for every input. Second layer. With (5.119), (5.120): \(\widetilde y_k=\sum_jcw_{kj}z_j+cw_{k0}+d=cy_k+d\). Together they prove the first two claims (Exercise 5.24).

Regulariser. Biases do not appear in (5.121), so the bias shifts (5.117), (5.120) change nothing. Under (5.116), \(\sum_{\mathcal{W}_1}\widetilde w^2\)\({}=a^{-2}\sum_{\mathcal{W}_1}w^2\); under (5.119), \(\sum_{\mathcal{W}_2}\widetilde w^2\)\({}=c^2\sum_{\mathcal{W}_2}w^2\). To keep the value of each group unchanged, \(\widetilde\lambda_1a^{-2}=\lambda_1\) and \(\widetilde\lambda_2c^2=\lambda_2\), i.e. \(\widetilde\lambda_1=a^2\lambda_1\), \(\widetilde\lambda_2=c^{-2}\lambda_2\).

Why plain weight decay fails. \(\lambda\,\mathbf{w}^\T\mathbf{w}\) includes the biases (not invariant to the shift (5.117), which moves \(w_{j0}\)) and weights the two layers equally, while (5.116) and (5.119) multiply the weights of the two layers by different factors \(1/a\) and \(c\): for \(a\ne c^{-1}\)... for any \(a\ne1\) or \(c\ne1\) the regulariser value changes, so training on rescaled data returns a different solution than the rescaled solution. A remark on the printed exponents. The book writes the rescalings as \(\lambda_1\to a^{1/2}\lambda_1\) and \(\lambda_2\to c^{-1/2}\lambda_2\). Following the book's own requirement (the regulariser keeps its value under the weight transformations), the computation above gives the exponents \(2\) and \(-2\); the printed \(1/2\) does not follow, and I could not find it in the official errata list. (If one also demands that the whole sum-of-squares objective is consistent, note that the data term scales by \(c^2\) under (5.118), so the full condition is \(\widetilde\lambda_1=a^2c^2\lambda_1\), \(\widetilde\lambda_2=\lambda_2\); the two forms differ by the overall factor \(c^2\) common to both coefficients.) ∎

The regulariser (5.121) is the negative log of the (improper) prior \[ p(\mathbf{w}\given\alpha_1,\alpha_2)\propto\exp\Big(-\frac{\alpha_1}2\sum_{w\in\mathcal{W}_1}w^2-\frac{\alpha_2}2\sum_{w\in\mathcal{W}_2}w^2\Big), \tag{5.122} \] improper because the biases are unconstrained, so it cannot be normalised and its evidence would be zero. For Bayesian model comparison one therefore gives the biases their own Gaussian priors (breaking the shift invariance), leaving four hyperparameters \(\alpha_1^w,\alpha_1^b,\alpha_2^w,\alpha_2^b\). More generally, any partition of the weights into groups \(\mathcal{W}_k\) gives \[ p(\mathbf{w})\propto\exp\Big(-\frac12\sum_k\alpha_k\|\mathbf{w}\|_k^2\Big),\qquad \|\mathbf{w}\|_k^2=\sum_{j\in\mathcal{W}_k}w_j^2 . \tag{5.123, 5.124} \] If the groups are the weights leaving each input unit and the \(\alpha_k\) are chosen by maximising the marginal likelihood, this is automatic relevance determination (Section 7.2.2): inputs with large \(\alpha_k\) are switched off.

Four panels, each showing ten functions of one input sampled from the prior of a two-layer network with 12 tanh hidden units, for different values of the four prior precisions; changing the second-layer weight precision changes the vertical scale and changing the first-layer precisions changes the horizontal scale and range of variation.
Figure 5.11 (reproduced). Functions sampled from the prior (5.122) of a network with one input, one linear output and 12 \(\tanh\) hidden units, for four settings of \(\alpha_1^b,\alpha_1^w,\alpha_2^b,\alpha_2^w\) (precisions of first-layer biases, first-layer weights, second-layer biases, second-layer weights). Book: Figure 5.11, p. 260 (in the book's panel titles the powers of ten on the right of the equals signs should carry a minus sign, an erratum). Code: ch05_neural_networks.py, fig5_11. Download: PDF · SVG

What the four hyperparameters do (a heuristic derivation). The output is \(f(x)\)\({}=\sum_jw^{(2)}_j\tanh(w^{(1)}_jx+b_j)+b^{(2)}\). (i) With independent zero-mean second-layer weights of precision \(\alpha_2^w\), \(\var f(x)\approx M\,\E[\tanh^2]/\alpha_2^w\): \(\alpha_2^w\) sets the vertical scale, and \(\alpha_2^b\) the range of vertical offsets. (ii) A hidden unit switches near \(x_j=-b_j/w_j\) over a width \(\sim1/|w_j|\); so \(\alpha_1^w\) (the scale of \(w_j\)) sets the horizontal scale of the variations, large precision meaning gentle slopes. (iii) The switch position is the ratio of two independent zero-mean Gaussians, which has a Cauchy distribution with scale \(\sigma_b/\sigma_w=\sqrt{\alpha_1^w/\alpha_1^b}\): a small \(\alpha_1^b\) spreads the switch locations over a wide range of \(x\) (the horizontal range of variation).

5.5.2 Early stopping

pp. 259–262 The error on an independent validation set usually falls and then rises as the network begins to over-fit while the training error keeps decreasing; stopping at the minimum of the validation error (Figure 5.12) gives good generalisation. Qualitatively the number of effective degrees of freedom starts small and grows with training. For a quadratic error this can be made precise.

Two panels against iteration number: the training error decreasing monotonically, and the validation error decreasing to a minimum and then increasing, with a vertical dashed line at the minimum.
Figure 5.12 (reproduced). Training-set error (left) and validation-set error (right) during a training run on the sinusoidal data, against iteration step. The dashed line marks the minimum of the validation error (step 24, in units of 400 gradient steps, RMS error 0.429, rising to 0.451 afterwards; the training RMS error falls to 0.189), where training should be stopped. Book: Figure 5.12, p. 261. Code: ch05_neural_networks.py, fig5_12. Download: PDF · SVG
▶ DemoEarly stopping (Fig 5.12)Training error falls monotonically; validation error turns up. Stop at the minimum.

Result 5.13 (gradient descent on a quadratic is a spectral filter; early stopping and weight decay) pp. 261–262, (5.195)–(5.200); Exercise 5.25

Let \(E(\mathbf{w})\)\({}=E_0\)\({}+\tfrac12(\mathbf{w}-\mathbf{w}^\star)^\T\mathbf{H}(\mathbf{w}-\mathbf{w}^\star)\) (5.195) with \(\mathbf{H}\) constant, positive definite, with eigenpairs \(\mathbf{H}\mathbf{u}_j=\eta_j\mathbf{u}_j\) (5.198) (the book uses \(\eta_j\) for the eigenvalues here; the learning rate is \(\rho\)). Gradient descent \(\mathbf{w}^{(\tau)}\)\({}=\mathbf{w}^{(\tau-1)}-\rho\nabla E\) (5.196) from \(\mathbf{w}^{(0)}=\mathbf{0}\) gives, for the components \(w_j=\mathbf{w}^\T\mathbf{u}_j\), \[ w^{(\tau)}_j=\big\{1-(1-\rho\eta_j)^\tau\big\}\,w^\star_j . \tag{5.197} \] Hence: (a) \(\mathbf{w}^{(\tau)}\to\mathbf{w}^\star\) iff \(|1-\rho\eta_j|<1\) for all \(j\), i.e. \(0<\rho<2/\eta_{\max}\); (b) if training stops after \(\tau\) steps with \(\rho\eta_j\ll1\), then \[ w^{(\tau)}_j\simeq w^\star_j\ \ (\eta_j\gg(\rho\tau)^{-1}),\qquad |w^{(\tau)}_j|\ll|w^\star_j|\ \ (\eta_j\ll(\rho\tau)^{-1}); \tag{5.199, 5.200} \] (c) this mirrors weight decay, whose minimiser has \(w_j=\frac{\eta_j}{\eta_j+\lambda}w_j^\star\) (Section 3.5.3), with \((\rho\tau)^{-1}\) playing the role of \(\lambda\); (d) the effective number of parameters, defined by analogy with (3.91) as the sum of the filter factors \(\gamma(\tau)=\sum_j\{1-(1-\rho\eta_j)^\tau\}\), grows from 0 to \(W\) as training proceeds.

Proof

The gradient is \(\nabla E=\mathbf{H}(\mathbf{w}-\mathbf{w}^\star)\). Subtract \(\mathbf{w}^\star\) from the update: \(\mathbf{w}^{(\tau)}-\mathbf{w}^\star\)\({}=(\mathbf{I}-\rho\mathbf{H})(\mathbf{w}^{(\tau-1)}-\mathbf{w}^\star)\). Project on \(\mathbf{u}_j\) (using \(\mathbf{u}_j^\T\mathbf{H}=\eta_j\mathbf{u}_j^\T\)): \(w^{(\tau)}_j-w^\star_j\)\({}=(1-\rho\eta_j)(w^{(\tau-1)}_j-w^\star_j)\)\({}=(1-\rho\eta_j)^\tau(w^{(0)}_j-w^\star_j)\)\({}=-(1-\rho\eta_j)^\tau w^\star_j\), since \(\mathbf{w}^{(0)}=\mathbf{0}\). Rearranging gives (5.197).

(a) \((1-\rho\eta_j)^\tau\to0\) iff \(|1-\rho\eta_j|<1\) iff \(0<\rho\eta_j<2\); this holds for all \(j\) iff \(\rho<2/\eta_{\max}\). (b) For \(\rho\eta_j\ll1\), \((1-\rho\eta_j)^\tau\)\({}=\exp\{\tau\ln(1-\rho\eta_j)\}\)\({}\simeq e^{-\rho\tau\eta_j}\). If \(\eta_j\gg(\rho\tau)^{-1}\) this is \(\simeq0\) and \(w_j^{(\tau)}\simeq w_j^\star\); if \(\eta_j\ll(\rho\tau)^{-1}\), \(1-e^{-\rho\tau\eta_j}\simeq\rho\tau\eta_j\ll1\), so the component has barely moved from 0. (c) Weight decay minimises \(E+\tfrac\lambda2\|\mathbf{w}\|^2\): setting the gradient to zero, \(\mathbf{H}(\mathbf{w}-\mathbf{w}^\star)\)\({}+\lambda\mathbf{w}\)\({}=\mathbf{0}\), i.e. \((\mathbf{H}+\lambda\mathbf{I})\mathbf{w}\)\({}=\mathbf{H}\mathbf{w}^\star\), so \(w_j=\frac{\eta_j}{\eta_j+\lambda}w_j^\star\): factor \(\simeq1\) for \(\eta_j\gg\lambda\) and \(\simeq\eta_j/\lambda\) for \(\eta_j\ll\lambda\). Compare the early-stopping factor, \(\simeq1\) for \(\eta_j\gg(\rho\tau)^{-1}\) and \(\simeq\rho\tau\eta_j\) for \(\eta_j\ll(\rho\tau)^{-1}\): identical behaviour at both ends under \(\lambda=(\rho\tau)^{-1}\). Directions of large curvature (well determined by the data) are fitted, directions of small curvature are left near the origin. (d) In Section 3.5.3 the effective number of parameters is \(\gamma=\sum_i\lambda_i/(\alpha+\lambda_i)\), the sum of the same ridge shrinkage factors; replacing them by the early-stopping factors gives \(\gamma(\tau)\), which increases with \(\tau\) towards \(W\) because each \(1-(1-\rho\eta_j)^\tau\) increases from 0 to 1. ∎

Rate. The slowest component has factor \(1-\rho\eta_{\min}\) per step, and \(\rho<2/\eta_{\max}\), so the number of steps to fit it is of order \(\eta_{\max}/\eta_{\min}\), the condition number of the Hessian: the reason steepest descent is slow on the long thin valleys of Figure 5.6.

An elliptical error contour in a two-dimensional weight space, with the gradient-descent path from the origin first moving along the steep axis and then towards the minimum, and an early-stopping point marked on the path.
Figure 5.13 (reproduced). Why early stopping resembles weight decay for a quadratic error: starting at the origin, the gradient path first moves along the stiff axis (large \(\eta_j\)) and only later along the flat one, so a point on the path before the minimum \(\mathbf{w}_{\rm ML}\) is qualitatively like a weight-decay solution (compare Figure 3.15). Here the error is quadratic with Hessian eigenvalues 0.1 and 1. Book: Figure 5.13, p. 262. Code: ch05_neural_networks.py, fig5_13. Download: PDF · SVG

5.5.3 Invariances

pp. 261–263 In many tasks the prediction should not change under transformations of the input: translation or scale of an object in an image, small non-linear time warping in speech. If enough training examples of the transformations are available, an adaptive model can learn the invariance, but the number of combinations grows exponentially with the number of invariances. Four ways to encourage invariance are listed:

  1. Augment the training set with transformed replicas (for on-line training, apply a freshly drawn transformation each time a pattern is presented; for batch training replicate each pattern several times). Easy, can give complex invariances (Figure 5.14) and clearly helps generalisation, but is computationally costly.
  2. Add a regulariser that penalises changes of the output under the transformation: tangent propagation (5.5.4). Section 5.5.5 shows it is closely related to approach 1.
  3. Invariant features: pre-process to extract features that are invariant; any subsequent classifier inherits the invariance and extrapolates beyond the transformations seen in training, but hand-made invariant features may discard discriminating information.
  4. Build the invariance into the network structure, via local receptive fields and weight sharing (convolutional networks, 5.5.6), or into the kernel (Chapters 6 and 7).
A handwritten digit and three warped versions of it, produced by random smooth displacement fields shown below each warped digit.
Figure 5.14 (reproduced). Synthetic warping of a handwritten digit. Left: the original image; right, top row: three warped digits, with the displacement fields that produced them below. The fields are random displacements at every pixel smoothed by Gaussians of increasing width (0.01, 2.5 and 5 pixels on a 28-pixel digit, not the book's much larger image). Book: Figure 5.14, p. 263. Code: ch05_neural_networks.py, fig5_14. Download: PDF · SVG

5.5.4 Tangent propagation

pp. 263–265 A continuous transformation (translation, rotation, but not reflection) governed by a parameter \(\xi\) moves the input \(\mathbf{x}_n\) along a one-dimensional manifold \(\mathcal{M}\) (Figure 5.15). Let \(\mathbf{s}(\mathbf{x},\xi)\) be the transformed vector, with \(\mathbf{s}(\mathbf{x},0)=\mathbf{x}\). The tangent vector at \(\mathbf{x}_n\) is \[ \bm\tau_n=\frac{\partial\mathbf{s}(\mathbf{x}_n,\xi)}{\partial\xi}\Big|_{\xi=0}. \tag{5.125} \] In practice \(\bm\tau_n\) is approximated by a finite difference, \((\mathbf{s}(\mathbf{x}_n,\xi)-\mathbf{x}_n)/\xi\) for a small \(\xi\) (Figure 5.16).

Four small digit images: the original, its tangent vector for an infinitesimal clockwise rotation shown in blue and yellow for positive and negative values, the original plus 15 degrees times the tangent vector, and the true image rotated by 15 degrees.
Figure 5.16 (reproduced). (a) A digit image \(\mathbf{x}\); (b) the tangent vector \(\bm\tau\) of an infinitesimal clockwise rotation (blue positive, yellow negative); (c) \(\mathbf{x}+\epsilon\bm\tau\) with \(\epsilon=15\) degrees (tangent vector of a 15 degree rotation); (d) the true rotated image for comparison: the linear approximation is good for small angles only. Book: Figure 5.16, p. 265. Code: ch05_neural_networks.py, fig5_16. Download: PDF · SVG

Result 5.14 (tangent propagation and its backpropagation) pp. 264–265, 288, (5.126)–(5.128), (5.201)–(5.207); Exercise 5.26

Under the transformation, output \(k\) changes at the rate \[ \frac{\partial y_k}{\partial\xi}\Big|_{\xi=0}=\sum_{i=1}^{D}\frac{\partial y_k}{\partial x_i}\frac{\partial x_i}{\partial\xi}\Big|_{\xi=0}=\sum_{i=1}^{D}J_{ki}\tau_i , \tag{5.126} \] with \(J_{ki}\) the Jacobian (5.70). The tangent propagation error is \(\widetilde E=E+\lambda\Omega\) (5.127) with \[ \Omega=\frac12\sum_n\sum_k\Big(\frac{\partial y_{nk}}{\partial\xi}\Big|_{\xi=0}\Big)^2=\frac12\sum_n\sum_k\Big(\sum_{i=1}^{D}J_{nki}\tau_{ni}\Big)^2 . \tag{5.128} \] It vanishes when the network is locally invariant at every training pattern. Moreover, with the differential operator \(\mathcal{G}\)\({}\equiv\sum_i\tau_i\,\partial/\partial x_i\) (5.202), \[ \Omega_n=\frac12\sum_k(\mathcal{G}y_k)^2 \tag{5.201} \] and \(\Omega_n\) can be computed by forward propagation of \(\alpha_j\equiv\mathcal{G}z_j\), \(\beta_j\equiv\mathcal{G}a_j\): \[ \alpha_j=h'(a_j)\beta_j,\qquad\beta_j=\sum_iw_{ji}\alpha_i, \tag{5.204} \] starting from \(\alpha_i=\tau_i\) at the inputs. For a weight \(w_{rs}\), \[ \frac{\partial\Omega_n}{\partial w_{rs}}=\sum_k\alpha_k\{\phi_{kr}z_s+\delta_{kr}\alpha_s\},\qquad\delta_{kr}\equiv\frac{\partial y_k}{\partial a_r},\ \ \phi_{kr}\equiv\mathcal{G}\delta_{kr}, \tag{5.206, 5.207} \] where the \(\delta_{kr}\) obey the backward recursion \(\delta_{kr}=h'(a_r)\sum_lw_{lr}\delta_{kl}\) (5.74) and the \(\phi_{kr}\) the recursion \[ \phi_{kr}=h''(a_r)\,\beta_r\sum_lw_{lr}\delta_{kl}+h'(a_r)\sum_lw_{lr}\phi_{kl}, \] both started at the output units from the output activation function (for linear outputs \(\delta_{kl}=\delta^{\rm Kr}_{kl}\) and \(\phi_{kl}=0\); for a sigmoid \(\delta_{kl}=\delta^{\rm Kr}_{kl}\sigma'(a_l)\), \(\phi_{kl}\)\({}=\delta^{\rm Kr}_{kl}\sigma''(a_l)\beta_l\)). (Here \(\delta^{\rm Kr}\) is the Kronecker delta, not an error signal.) Cost: a small multiple of ordinary backpropagation. For \(L\) transformation parameters add \(L\) such terms (the manifold has dimension \(L\)); if the network is made invariant to each separately, it is locally invariant to their combinations (Simard et al., 1992).

Proof (Exercise 5.26)

(5.126) is the chain rule and (5.128) the sum of squares of (5.126). For (5.201), \(\mathcal{G}y_k\)\({}=\sum_i\tau_i\,\partial y_k/\partial x_i\)\({}=\sum_iJ_{ki}\tau_i\).

Forward equations. \(\mathcal{G}\) is a first-order differential operator in \(\mathbf{x}\) with the usual chain and product rules, \(\mathcal{G}x_i=\tau_i\), and \(\mathcal{G}w=0\) (weights do not depend on \(\mathbf{x}\)). Apply it to \(a_j=\sum_iw_{ji}z_i\): \(\beta_j=\sum_iw_{ji}\alpha_i\); to \(z_j=h(a_j)\): \(\alpha_j=h'(a_j)\beta_j\). These are (5.204), evaluated in forward order from \(\alpha_i=\tau_i\). For linear outputs \(\mathcal{G}y_k=\beta_k=\alpha_k\); in general write \(\alpha_k\equiv\mathcal{G}y_k\).

Weight derivatives. \(\mathcal{G}\) involves only \(\partial/\partial x_i\) and so commutes with \(\partial/\partial w_{rs}\). Hence \(\frac{\partial\Omega_n}{\partial w_{rs}}\)\({}=\sum_k(\mathcal{G}y_k)\,\mathcal{G}\frac{\partial y_k}{\partial w_{rs}}\). Now \(w_{rs}\) enters only through \(a_r=\sum_sw_{rs}z_s\), so \(\partial y_k/\partial w_{rs}\)\({}=\frac{\partial y_k}{\partial a_r}z_s\)\({}=\delta_{kr}z_s\) (this is (5.53) with \(E\) replaced by \(y_k\)). Applying \(\mathcal{G}\) and the product rule: \(\mathcal{G}(\delta_{kr}z_s)\)\({}=(\mathcal{G}\delta_{kr})z_s\)\({}+\delta_{kr}\mathcal{G}z_s\)\({}=\phi_{kr}z_s+\delta_{kr}\alpha_s\). Substituting gives (5.206).

The recursion for \(\delta_{kr}\) is (5.74). Apply \(\mathcal{G}\) to it: \(\mathcal{G}\{h'(a_r)\sum_lw_{lr}\delta_{kl}\}\)\({}=h''(a_r)(\mathcal{G}a_r)\sum_lw_{lr}\delta_{kl}\)\({}+h'(a_r)\sum_lw_{lr}\mathcal{G}\delta_{kl}\), which is the stated recursion for \(\phi_{kr}\). At the output the starting values follow from \(\delta_{kl}=\partial y_k/\partial a_l\) for the chosen activation function. ∎

A related technique, tangent distance, builds the same invariance into distance-based methods such as nearest neighbours (Simard et al., 1993).

5.5.5 Training with transformed data

pp. 265–267 Augmenting the data with transformed copies is closely related to tangent propagation. Consider a single output and the sum of squares in the infinite-data limit, \[ E=\frac12\iint\{y(\mathbf{x})-t\}^2p(t\given\mathbf{x})p(\mathbf{x})\,d\mathbf{x}\,dt , \tag{5.129} \] and an infinite number of copies of each pattern, each transformed with \(\xi\) drawn from \(p(\xi)\): \[ \widetilde E=\frac12\iiint\{y(\mathbf{s}(\mathbf{x},\xi))-t\}^2p(t\given\mathbf{x})p(\mathbf{x})p(\xi)\,d\mathbf{x}\,dt\,d\xi . \tag{5.130} \]

Result 5.15 (transformed data = tangent propagation to order \(\xi^2\)) pp. 266–267, (5.131)–(5.135); Exercise 5.27

Let \(\xi\) have zero mean and small variance \(\E[\xi^2]=\lambda\). Then, up to terms of order \(\xi^3\), \(\widetilde E=E+\lambda\Omega\) (5.131) with \[ \Omega=\frac12\int\Big\{\{y(\mathbf{x})-\E[t\given\mathbf{x}]\}\big((\bm\tau')^\T\nabla y(\mathbf{x})+\bm\tau^\T\nabla\nabla y(\mathbf{x})\,\bm\tau\big)+\big(\bm\tau^\T\nabla y(\mathbf{x})\big)^2\Big\}p(\mathbf{x})\,d\mathbf{x}, \tag{5.132} \] where \(\bm\tau=\partial\mathbf{s}/\partial\xi|_0\) and \(\bm\tau'=\partial^2\mathbf{s}/\partial\xi^2|_0\). The function minimising \(\widetilde E\) is \(y(\mathbf{x})=\E[t\given\mathbf{x}]+O(\xi^2)\) (5.133), so the first term of (5.132) contributes only at higher order and \[ \Omega=\frac12\int\big(\bm\tau^\T\nabla y(\mathbf{x})\big)^2p(\mathbf{x})\,d\mathbf{x}, \tag{5.134} \] which is the tangent-propagation regulariser (5.128) (in expectation over the data). For additive noise, \(\mathbf{x}\to\mathbf{x}+\bm\xi\) with \(\bm\xi\) Gaussian, zero mean, covariance \(\lambda\mathbf{I}\), the result is Tikhonov regularisation, \(\Omega\)\({}=\frac12\int\|\nabla y(\mathbf{x})\|^2p(\mathbf{x})\,d\mathbf{x}\) (5.135).

Errata. The printed third term before (5.131) and the printed (5.132) lacked the correct grouping/scale (the corrected forms are above), and in the discussion of (5.133) both "\(O(\xi)\)" should read "\(O(\xi^2)\)", with "to leading order in \(\xi\)" replaced by "to order \(\xi^2\)".

Proof (including the Taylor expansion and Exercise 5.27)

Expansion of the transformation. \(\mathbf{s}(\mathbf{x},\xi)\)\({}=\mathbf{x}+\xi\bm\tau+\tfrac12\xi^2\bm\tau'\)\({}+O(\xi^3)\) (Taylor in \(\xi\) at 0, using \(\mathbf{s}(\mathbf{x},0)=\mathbf{x}\)). Put \(\bm\delta=\xi\bm\tau+\tfrac12\xi^2\bm\tau'\). Then \(y(\mathbf{s})\)\({}=y+\bm\delta^\T\nabla y\)\({}+\tfrac12\bm\delta^\T\nabla\nabla y\,\bm\delta\)\({}+O(\xi^3)\), and keeping terms up to \(\xi^2\): \[ y(\mathbf{s}(\mathbf{x},\xi))=y(\mathbf{x})+\xi\bm\tau^\T\nabla y+\frac{\xi^2}2\big[(\bm\tau')^\T\nabla y+\bm\tau^\T\nabla\nabla y\,\bm\tau\big]+O(\xi^3). \] Squaring. Writing \(r=y-t\), \(A=\bm\tau^\T\nabla y\), \(B\)\({}=(\bm\tau')^\T\nabla y\)\({}+\bm\tau^\T\nabla\nabla y\,\bm\tau\): \(\{y(\mathbf{s})-t\}^2\)\({}=r^2+2r\xi A+\xi^2(rB+A^2)+O(\xi^3)\). So \[ \tfrac12\{y(\mathbf{s})-t\}^2=\tfrac12r^2+\xi\,rA+\tfrac{\xi^2}2(rB+A^2)+O(\xi^3). \] Averaging over \(\xi\): \(\E[\xi]=0\) kills the middle term and \(\E[\xi^2]=\lambda\) gives \(\tfrac\lambda2(rB+A^2)\). Integrating over \(t\) with \(p(t\given\mathbf{x})\) turns \(r=y-t\) into \(y-\E[t\given\mathbf{x}]\) (the \(A^2\) and \(B\) factors do not depend on \(t\)), which gives (5.131) with (5.132).

(5.133), (5.134). \(\widetilde E=E+\lambda\Omega\) differs from the plain sum of squares \(E\) by a term of order \(\lambda=\E[\xi^2]\), so the minimising function differs from the minimiser \(\E[t\given\mathbf{x}]\) of \(E\) (Section 1.5.5) by \(O(\xi^2)\) as well. Then the factor \(y-\E[t\given\mathbf{x}]\) in the first term of (5.132) is \(O(\xi^2)\), so that term contributes \(O(\lambda\xi^2)=O(\xi^4)\) to \(\widetilde E\) and is dropped, leaving (5.134).

Tikhonov (Exercise 5.27). Take \(\mathbf{s}=\mathbf{x}+\bm\xi\) with a vector \(\bm\xi\), \(\E[\bm\xi]=\mathbf{0}\), \(\E[\bm\xi\bm\xi^\T]=\lambda\mathbf{I}\) (the exercise takes \(\lambda=1\)). Then \(y(\mathbf{x}+\bm\xi)\)\({}=y+\bm\xi^\T\nabla y\)\({}+\tfrac12\bm\xi^\T\nabla\nabla y\,\bm\xi\)\({}+O(\xi^3)\) and \(\E[\{y(\mathbf{x}+\bm\xi)-t\}^2]\)\({}=r^2+\lambda\|\nabla y\|^2\)\({}+\lambda\,r\,\nabla^2y\)\({}+O(\xi^3)\), using \(\E[(\bm\xi^\T\nabla y)^2]=\lambda\|\nabla y\|^2\) and \(\E[\bm\xi^\T\nabla\nabla y\,\bm\xi]\)\({}=\lambda\Tr\nabla\nabla y\)\({}=\lambda\nabla^2y\). Integrating over \(t\), the last term has the factor \(y-\E[t\given\mathbf{x}]=O(\xi^2)\) and drops out, leaving \(\widetilde E\)\({}=E\)\({}+\tfrac\lambda2\int\|\nabla y\|^2p(\mathbf{x})d\mathbf{x}\). ∎

A numerical check. For a fixed point \(x=0.7\) with target \(t=0.1\), \(y(x)=\tanh(1.3x+0.2)\) and the multiplicative transformation \(s(x,\xi)=xe^\xi\) (so \(\tau=\tau'=x\)), I computed \(\E_\xi[\tfrac12(y(s)-t)^2]-\tfrac12(y(x)-t)^2\) by Gauss–Hermite quadrature for \(\xi\sim\N(0,\sigma^2)\) and compared it with the integrand of \(\lambda\Omega\), (5.132), \(\lambda=\sigma^2\). The difference divided by \(\sigma^4\) was \(-0.064,-0.072,-0.075,-0.075\) for \(\sigma=0.2,0.1,0.05,0.025\): the discrepancy is \(O(\sigma^4)\), as the expansion says.

5.5.6 Convolutional networks

pp. 267–269 Instead of learning invariance from examples, convolutional networks (Le Cun et al., 1989; LeCun et al., 1998) build it into the structure, exploiting two facts about images: nearby pixels are more strongly correlated than distant ones, and a feature useful in one region is useful in others. Three mechanisms are used: local receptive fields, weight sharing and subsampling (Figure 5.17).

Result 5.16 (convolutional layers: shift equivariance, parameter counts, subsampling) pp. 267–269

Let the input image be \(x(\mathbf{u})\) on a grid, and let a feature map be \(f(\mathbf{u})\)\({}=h\big(\sum_{\mathbf{v}\in P}w_{\mathbf{v}}\,x(\mathbf{u}+\mathbf{v})+b\big)\), with a \(k\times k\) patch \(P\) and one set of weights \(\{w_{\mathbf{v}}\},b\) shared by all positions \(\mathbf{u}\). Then:

  1. Equivariance. If the image is shifted, \(x'(\mathbf{u})=x(\mathbf{u}-\mathbf{s})\), the map is shifted by the same amount, \(f'(\mathbf{u})=f(\mathbf{u}-\mathbf{s})\) (away from the borders). The sums are cross-correlations (convolutions with the flipped kernel) of the image with the kernel \(\mathbf{w}\).
  2. Counting. A feature map of \(n_f\times n_f\) units on an image of side \(n_f+k-1\) has \(n_f^2\) units but only \(k^2+1\) independent parameters, against \(n_f^2(k^2+1)\) weights if unshared. (Book example: \(k=5\), \(n_f=10\): 26 parameters instead of 2600 for one map.) Using several feature maps multiplies the count by the number of maps.
  3. Subsampling. A unit of the subsampling layer averages a non-overlapping \(2\times2\) block of one feature map, multiplies by an adaptive weight, adds an adaptive bias and applies a sigmoid; the number of rows and columns halves.
Proof

(1) \(f'(\mathbf{u})\)\({}=h\big(\sum_{\mathbf{v}}w_{\mathbf{v}}x'(\mathbf{u}+\mathbf{v})+b\big)\)\({}=h\big(\sum_{\mathbf{v}}w_{\mathbf{v}}x(\mathbf{u}-\mathbf{s}+\mathbf{v})+b\big)\)\({}=f(\mathbf{u}-\mathbf{s})\): the same weights at each position, so only the argument shifts. Without weight sharing the weights depend on \(\mathbf{u}\) and the identity fails. (2) A valid \(k\times k\) patch fits at \(n_f=n-k+1\) positions per row for an image of side \(n\); the free parameters are the \(k^2\) kernel entries and one bias. (3) With the \(2\times2\) block and no overlap, \(n_f\) rows give \(n_f/2\) rows. A shift of the input by one pixel changes the activations of the convolutional layer by a one-pixel shift, and the block average of a \(2\times2\) window changes less than the individual values do (a shift that stays inside the window leaves the average unchanged), giving approximate insensitivity to small shifts; stacking pairs of layers increases the degree of invariance, compensated by an increasing number of feature maps. ∎

The final layer is typically fully connected with a softmax output. The whole network is trained by backpropagation with the shared-weight modification of Filling the gap 5.5: derivatives of tied weights are summed. The local receptive fields reduce the number of weights, and weight sharing reduces the number of independent parameters much further. Figure 5.17 of the book is a schematic of a convolutional layer followed by a subsampling layer.

5.5.7 Soft weight sharing

pp. 269–272 Weight sharing in the hard form requires knowing the constraints in advance. In the soft form (Nowlan and Hinton, 1992) the weights are encouraged to form groups with similar values by a mixture-of-Gaussians prior, whose centres, widths and weights are learned together with the network: \[ p(\mathbf{w})=\prod_ip(w_i),\qquad p(w_i)=\sum_{j=1}^{M}\pi_j\N(w_i\given\mu_j,\sigma_j^2), \tag{5.136, 5.137} \] \[ \Omega(\mathbf{w})=-\sum_i\ln\Big(\sum_{j=1}^{M}\pi_j\N(w_i\given\mu_j,\sigma_j^2)\Big),\qquad\widetilde E(\mathbf{w})=E(\mathbf{w})+\lambda\Omega(\mathbf{w}). \tag{5.138, 5.139} \] This is minimised jointly over \(\mathbf{w}\) and \(\{\pi_j,\mu_j,\sigma_j\}\) (a joint optimisation avoids the numerical instability of alternating EM, because the weight distribution keeps moving). Regard \(\pi_j\) as prior probabilities and introduce the posterior probabilities (responsibilities) \[ \gamma_j(w)=\frac{\pi_j\N(w\given\mu_j,\sigma_j^2)}{\sum_k\pi_k\N(w\given\mu_k,\sigma_k^2)} . \tag{5.140} \]

Result 5.17 (derivatives of the soft weight-sharing regulariser) pp. 270–272, (5.141)–(5.147); Exercises 5.29–5.32

\[ \frac{\partial\widetilde E}{\partial w_i}=\frac{\partial E}{\partial w_i}+\lambda\sum_j\gamma_j(w_i)\frac{w_i-\mu_j}{\sigma_j^2}, \tag{5.141} \] \[ \frac{\partial\widetilde E}{\partial\mu_j}=\lambda\sum_i\gamma_j(w_i)\frac{\mu_j-w_i}{\sigma_j^2}, \tag{5.142} \] \[ \frac{\partial\widetilde E}{\partial\sigma_j}=\lambda\sum_i\gamma_j(w_i)\Big(\frac1{\sigma_j}-\frac{(w_i-\mu_j)^2}{\sigma_j^3}\Big). \tag{5.143} \] With \(\sigma_j^2=\exp(\xi_j)\) (5.144) (the book calls the variable \(\eta_j\) in the first printing; erratum: it should be \(\xi_j\), as \(\eta_j\) is used for the softmax variables), \(\partial\widetilde E/\partial\xi_j\)\({}=\frac\lambda2\sum_i\gamma_j(w_i)\{1\)\({}-(w_i-\mu_j)^2/\sigma_j^2\}\). For the mixing coefficients, parametrised by the softmax \[ \pi_j=\frac{\exp(\eta_j)}{\sum_{k=1}^{M}\exp(\eta_k)}, \tag{5.146} \] \[ \frac{\partial\pi_k}{\partial\eta_j}=\delta_{jk}\pi_j-\pi_j\pi_k \tag{5.208} \] \[ \frac{\partial\widetilde E}{\partial\eta_j}=\lambda\sum_i\{\pi_j-\gamma_j(w_i)\}. \tag{5.147} \] Erratum. The book prints the numerator of (5.142) as \(w_i-\mu_j\); the sign above is the corrected one. The errata also note that once the \(\sigma_j\) are free, the coefficient \(\lambda\) is redundant for a negative-log-likelihood \(E\) and can be dropped (set \(\lambda=1\), the MAP reading); the formulae keep \(\lambda\) as in the book.

Proof

Write \(N_{ij}=\N(w_i\given\mu_j,\sigma_j^2)\) and \(S_i=\sum_k\pi_kN_{ik}\), so \(\Omega=-\sum_i\ln S_i\) and \(\gamma_j(w_i)=\pi_jN_{ij}/S_i\). Since \(\partial\Omega/\partial\theta\)\({}=-\sum_iS_i^{-1}\partial S_i/\partial\theta\), we need \(\partial S_i/\partial\theta\).

(5.141). \(\partial N_{ij}/\partial w_i\)\({}=-\frac{w_i-\mu_j}{\sigma_j^2}N_{ij}\). So \(\partial\Omega/\partial w_i\)\({}=-S_i^{-1}\sum_j\pi_j\big(-\frac{w_i-\mu_j}{\sigma_j^2}\big)N_{ij}\)\({}=\sum_j\gamma_j(w_i)\frac{w_i-\mu_j}{\sigma_j^2}\). Each weight is pulled towards the centre \(\mu_j\), with force proportional to the posterior probability of component \(j\) for that weight.

(5.142). \(\partial N_{ij}/\partial\mu_j\)\({}=+\frac{w_i-\mu_j}{\sigma_j^2}N_{ij}\) (only component \(j\) depends on \(\mu_j\)), so \(\partial\Omega/\partial\mu_j\)\({}=-\sum_i\gamma_j(w_i)\frac{w_i-\mu_j}{\sigma_j^2}\)\({}=\sum_i\gamma_j(w_i)\frac{\mu_j-w_i}{\sigma_j^2}\). Setting it to zero gives \(\mu_j=\sum_i\gamma_j(w_i)w_i/\sum_i\gamma_j(w_i)\), a posterior-weighted average of the weights.

(5.143). \(\ln N_{ij}\)\({}=-\ln\sigma_j\)\({}-\frac{(w_i-\mu_j)^2}{2\sigma_j^2}\)\({}-\tfrac12\ln2\pi\), so \(\partial N_{ij}/\partial\sigma_j\)\({}=N_{ij}\big(-\frac1{\sigma_j}+\frac{(w_i-\mu_j)^2}{\sigma_j^3}\big)\) and \(\partial\Omega/\partial\sigma_j\)\({}=\sum_i\gamma_j(w_i)\big(\frac1{\sigma_j}-\frac{(w_i-\mu_j)^2}{\sigma_j^3}\big)\). Setting it to zero: \(\sigma_j^2\)\({}=\sum_i\gamma_j(w_i)(w_i-\mu_j)^2/\sum_i\gamma_j(w_i)\). With \(\sigma_j=e^{\xi_j/2}\), \(d\sigma_j/d\xi_j=\sigma_j/2\), hence \(\partial\Omega/\partial\xi_j\)\({}=\tfrac{\sigma_j}2\partial\Omega/\partial\sigma_j\), the stated expression. The reparametrisation keeps \(\sigma_j>0\) and discourages the pathological solution in which a component collapses onto one weight, \(\sigma_j\to0\) (which makes \(\Omega\to-\infty\); the singularity of Section 9.2.1).

(5.208). From (5.146), by the quotient rule, \(\partial\pi_k/\partial\eta_j\)\({}=\frac{\delta_{jk}e^{\eta_k}}{\sum_le^{\eta_l}}\)\({}-\frac{e^{\eta_k}e^{\eta_j}}{(\sum_le^{\eta_l})^2}\)\({}=\delta_{jk}\pi_k-\pi_k\pi_j\), which equals \(\delta_{jk}\pi_j-\pi_j\pi_k\).

(5.147). \(\partial\Omega/\partial\pi_k\)\({}=-\sum_iN_{ik}/S_i\)\({}=-\sum_i\gamma_k(w_i)/\pi_k\). By the chain rule, \[ \frac{\partial\Omega}{\partial\eta_j}=\sum_k\frac{\partial\Omega}{\partial\pi_k}\frac{\partial\pi_k}{\partial\eta_j}=-\sum_i\sum_k\frac{\gamma_k(w_i)}{\pi_k}(\delta_{jk}\pi_j-\pi_j\pi_k)=-\sum_i\gamma_j(w_i)+\pi_j\sum_i\sum_k\gamma_k(w_i)=\sum_i\{\pi_j-\gamma_j(w_i)\}, \] using \(\sum_k\gamma_k(w_i)=1\) for every \(i\). Setting to zero gives \(\pi_j=\frac1{W}\sum_i\gamma_j(w_i)\) (average responsibility). The three stationarity conditions are exactly the M-step of EM for a Gaussian mixture fitted to the weights. ∎

5.6 Mixture density networks

pp. 272–277 Least squares is maximum likelihood under a Gaussian \(p(\mathbf{t}\given\mathbf{x})\), which is a poor assumption for inverse problems. In the two-link robot arm of Figure 5.18 the forward map from joint angles \((\theta_1,\theta_2)\) to the end-effector position is a function, \[ x_1=L_1\cos\theta_1+L_2\cos(\theta_1+\theta_2),\qquad x_2=L_1\sin\theta_1+L_2\sin(\theta_1+\theta_2) \] (Exercise 5.33; origin at the shoulder), but the inverse has two solutions, "elbow up" and "elbow down": reflect the elbow across the line from shoulder to end effector. The same happens whenever a many-to-one forward map is inverted (several diseases, same symptoms).

The toy problem: draw \(x_n\sim U(0,1)\), set \(t_n=x_n+0.3\sin(2\pi x_n)+\) uniform noise on \((-0.1,0.1)\); the inverse problem swaps the roles of \(x\) and \(t\). Figure 5.19 shows that a sum-of-squares network fits the forward data well and the inverse data very badly.

Filling the gap 5.8: why least squares returns the conditional mean, and why that fails here pp. 273, 277; Section 1.5.5

Derivation

For any function \(y(\mathbf{x})\), \(\iint\{y-t\}^2p(\mathbf{x},t)\)\({}=\iint\{y-\E[t\given\mathbf{x}]\}^2p\)\({}+\iint\{\E[t\given\mathbf{x}]-t\}^2p\) (expand \(y-t\)\({}=(y-\E[t\given\mathbf{x}])\)\({}+(\E[t\given\mathbf{x}]-t)\); the cross term vanishes because \(\int(\E[t\given\mathbf{x}]-t)p(t\given\mathbf{x})dt\)\({}=0\)). So a flexible network trained by least squares approximates \(\E[t\given\mathbf{x}]\). For a bimodal \(p(t\given\mathbf{x})\) with modes at \(t_1,t_2\) of equal weight the mean is the midpoint, which has near-zero density, and for the robot arm the average of the two joint-angle solutions is not itself a solution. ∎

Left: the forward problem data and a good network fit. Right: the same points with x and t exchanged, and a poor fit by the same network that cuts through the middle of the multimodal data.
Figure 5.19 (reproduced). Left: the forward-problem data and the sum-of-squares fit of a two-layer network (6 hidden units, linear output). Right: the inverse problem (roles of \(x\) and \(t\) exchanged); the same network gives a very poor fit because the data are multimodal. Book: Figure 5.19, p. 273. Code: ch05_neural_networks.py, fig5_19. Download: PDF · SVG
▶ DemoThe forward problem (Fig 5.19)A single-valued function: least squares works.
▶ DemoA sum-of-squares network fails on the inverse problem (Fig 5.21)The fitted curve passes through regions with no data, between the branches.

Definition 5.2 (mixture density network) pp. 273–275, (5.148)–(5.153)

Let a feed-forward network take \(\mathbf{x}\) as input and have \((L+2)K\) outputs (\(L\) = dimension of \(\mathbf{t}\), \(K\) = number of components): \(K\) activations \(a^\pi_k\), \(K\) activations \(a^\sigma_k\) and \(KL\) activations \(a^\mu_{kj}\). The model is the isotropic Gaussian mixture \[ p(\mathbf{t}\given\mathbf{x})=\sum_{k=1}^{K}\pi_k(\mathbf{x})\,\N\big(\mathbf{t}\given\bm\mu_k(\mathbf{x}),\sigma_k^2(\mathbf{x})\mathbf{I}\big) \tag{5.148} \] (erratum: the book omits \(\mathbf{I}\)), with the parametrisation that enforces the constraints: \[ \sum_k\pi_k(\mathbf{x})=1,\ 0\le\pi_k\le1:\quad\pi_k=\frac{\exp(a^\pi_k)}{\sum_{l=1}^K\exp(a^\pi_l)} \tag{5.149, 5.150} \] \[ \sigma_k(\mathbf{x})=\exp(a^\sigma_k),\qquad\mu_{kj}(\mathbf{x})=a^\mu_{kj}. \tag{5.151, 5.152} \] The error function is the negative log likelihood \[ E(\mathbf{w})=-\sum_{n=1}^{N}\ln\Big\{\sum_{k=1}^{K}\pi_k(\mathbf{x}_n,\mathbf{w})\N\big(\mathbf{t}_n\given\bm\mu_k(\mathbf{x}_n,\mathbf{w}),\sigma_k^2(\mathbf{x}_n,\mathbf{w})\mathbf{I}\big)\Big\}. \tag{5.153} \] (Erratum: the inner sum runs to \(K\), and \(\mathbf{I}\) multiplies \(\sigma_k^2\).) This is a heteroscedastic model (the noise variance depends on \(\mathbf{x}\)), and \(p(\mathbf{t}\given\mathbf{x})\) does not factorise over the components of \(\mathbf{t}\) because of the mixture. It is close to the mixture of experts of Section 14.5.3; the difference is that all component parameters here are outputs of one network sharing its hidden units.

Result 5.18 (error signals of a mixture density network) p. 275, (5.154)–(5.157); Exercises 5.34–5.36

Let \(\gamma_{nk}\)\({}=\gamma_k(\mathbf{t}_n\given\mathbf{x}_n)\)\({}=\pi_kN_{nk}/\sum_l\pi_lN_{nl}\) (5.154) be the posterior probability of component \(k\) (with \(N_{nk}\)\({}=\N(\mathbf{t}_n\given\bm\mu_k(\mathbf{x}_n),\sigma_k^2(\mathbf{x}_n)\mathbf{I})\)). The derivatives of the error of pattern \(n\) with respect to the output activations are \[ \frac{\partial E_n}{\partial a^\pi_k}=\pi_k-\gamma_{nk},\qquad \frac{\partial E_n}{\partial a^\mu_{kl}}=\gamma_{nk}\frac{\mu_{kl}-t_{nl}}{\sigma_k^2},\qquad \frac{\partial E_n}{\partial a^\sigma_k}=\gamma_{nk}\Big(L-\frac{\|\mathbf{t}_n-\bm\mu_k\|^2}{\sigma_k^2}\Big). \tag{5.155, 5.156, 5.157} \] (Errata: \(\gamma_k\) in the book should be \(\gamma_{nk}\); \(t_l\) in (5.156) should be \(t_{nl}\); (5.157) has the factor \(L\), not \(1\).) These are the \(\delta\) values at the output units; they are backpropagated with (5.56) as usual.

Proof

Let \(S=\sum_l\pi_lN_{l}\) (pattern index dropped) so \(E_n=-\ln S\) and \(\partial E_n/\partial\theta\)\({}=-S^{-1}\sum_l\partial(\pi_lN_l)/\partial\theta\).

Mixing coefficients (5.155). The \(N_l\) do not depend on \(a^\pi_k\); only \(\pi_l\) does, with \(\partial\pi_l/\partial a^\pi_k\)\({}=\pi_l(\delta_{lk}-\pi_k)\) (softmax derivative, Result 5.4). Then \(\partial E_n/\partial a^\pi_k\)\({}=-S^{-1}\sum_lN_l\pi_l(\delta_{lk}-\pi_k)\)\({}=-\gamma_k+\pi_k\sum_l\gamma_l\)\({}=\pi_k-\gamma_k\).

Means (5.156). \(a^\mu_{kl}\) enters only the \(k\)th component. \(\ln N_k\)\({}=-\tfrac L2\ln(2\pi)-L\ln\sigma_k-\|\mathbf{t}\)\({}-\bm\mu_k\|^2/(2\sigma_k^2)\), so \(\partial N_k/\partial\mu_{kl}\)\({}=N_k(t_l-\mu_{kl})/\sigma_k^2\). Then \(\partial E_n/\partial a^\mu_{kl}\)\({}=-S^{-1}\pi_kN_k(t_l-\mu_{kl})/\sigma_k^2\)\({}=\gamma_k(\mu_{kl}-t_l)/\sigma_k^2\), since \(\mu_{kl}=a^\mu_{kl}\).

Widths (5.157). \(\partial\ln N_k/\partial\sigma_k\)\({}=-L/\sigma_k+\|\mathbf{t}\)\({}-\bm\mu_k\|^2/\sigma_k^3\) and \(\partial\sigma_k/\partial a^\sigma_k=\sigma_k\), so \(\partial N_k/\partial a^\sigma_k\)\({}=N_k(-L+\|\mathbf{t}-\bm\mu_k\|^2/\sigma_k^2)\) and \(\partial E_n/\partial a^\sigma_k\)\({}=-\gamma_k(-L+\|\mathbf{t}-\bm\mu_k\|^2/\sigma_k^2)\)\({}=\gamma_k(L-\|\mathbf{t}-\bm\mu_k\|^2/\sigma_k^2)\). ∎

Reading. At a stationary point, \(\sum_n\gamma_{nk}\) matches \(\sum_n\pi_k(\mathbf{x}_n)\) (the mixing coefficient equals the mean responsibility), the means move towards the targets for which their component is responsible, and \(\sigma_k^2\) towards the responsibility-weighted mean squared residual per dimension. A single component with \(\gamma=1\) recovers (5.18).

Four panels for a mixture density network trained on the inverse problem: (a) the three mixing coefficients as functions of x; (b) the three component means; (c) contours of the conditional density showing one mode at small and large x and three modes in the middle; (d) the mean of the most probable component, shown as red points.
Figure 5.21 (reproduced). A mixture density network with three Gaussian components and five \(\tanh\) hidden units (nine outputs), trained on the inverse problem (final \(E=-308.9\)). (a) Mixing coefficients \(\pi_k(x)\): at small and large \(x\) one component dominates, in the middle all three are comparable. (b) The means \(\mu_k(x)\). (c) Contours of \(p(t\given x)\): unimodal at the ends, trimodal in the middle. (d) The approximate conditional mode (mean of the most probable component). Book: Figure 5.21, p. 276. Code: ch05_neural_networks.py, fig5_21. Download: PDF · SVG
▶ DemoMixture density network: mixing coefficients, means, widths (Fig 5.21)The \(\pi_k(x)\) hand the modelling over from one branch to three as \(x\) moves into the multivalued region.
▶ DemoMore mixture componentsExtra components are used only where needed; unused ones get small \(\pi_k\).

Result 5.19 (conditional mean, variance and mode) pp. 276–277, (5.158)–(5.160); Exercise 5.37

For the model (5.148), \[ \E[\mathbf{t}\given\mathbf{x}]=\sum_{k=1}^{K}\pi_k(\mathbf{x})\bm\mu_k(\mathbf{x}), \tag{5.158} \] \[ s^2(\mathbf{x})=\E\big[\|\mathbf{t}-\E[\mathbf{t}\given\mathbf{x}]\|^2\given\mathbf{x}\big]=\sum_{k=1}^{K}\pi_k(\mathbf{x})\Big\{L\sigma_k^2(\mathbf{x})+\Big\|\bm\mu_k(\mathbf{x})-\sum_{l=1}^{K}\pi_l(\mathbf{x})\bm\mu_l(\mathbf{x})\Big\|^2\Big\}. \tag{5.159, 5.160} \] (The book writes \(\sigma_k^2\) in place of \(L\sigma_k^2\) in (5.160): it is stated for a scalar target, \(L=1\); the vector version has the factor \(L\), since \(\E\|\mathbf{t}-\bm\mu_k\|^2=L\sigma_k^2\) for an isotropic component.) A mode satisfies the fixed-point equation \[ \mathbf{t}=\frac{\sum_k\pi_kN_k(\mathbf{t})\,\bm\mu_k/\sigma_k^2}{\sum_k\pi_kN_k(\mathbf{t})/\sigma_k^2}, \] which can be iterated from each \(\bm\mu_k\); the book's cheaper alternative is the mean of the component with the largest \(\pi_k(\mathbf{x})\) (Figure 5.21d).

Proof

(5.158): \(\int\mathbf{t}\,p(\mathbf{t}\given\mathbf{x})d\mathbf{t}\)\({}=\sum_k\pi_k\int\mathbf{t}\,\N(\mathbf{t}\given\bm\mu_k,\sigma_k^2\mathbf{I})d\mathbf{t}\)\({}=\sum_k\pi_k\bm\mu_k\). Write \(\bar{\mathbf{t}}=\sum_l\pi_l\bm\mu_l\).

(5.160): \(\E\|\mathbf{t}-\bar{\mathbf{t}}\|^2\)\({}=\sum_k\pi_k\E_k\|(\mathbf{t}-\bm\mu_k)\)\({}+(\bm\mu_k-\bar{\mathbf{t}})\|^2\); within component \(k\), the cross term has zero mean and \(\E_k\|\mathbf{t}-\bm\mu_k\|^2\)\({}=\operatorname{tr}(\sigma_k^2\mathbf{I})\)\({}=L\sigma_k^2\). This is the law of total variance: the mean of the within-component variances plus the variance of the component means (the heteroscedastic improvement over the constant \(\beta^{-1}\) of least squares).

Mode. At a mode \(\nabla_{\mathbf{t}}p=0\): \(\sum_k\pi_kN_k(\mathbf{t})(\bm\mu_k-\mathbf{t})/\sigma_k^2\)\({}=\mathbf{0}\), since \(\nabla_{\mathbf{t}}N_k\)\({}=-N_k(\mathbf{t}-\bm\mu_k)/\sigma_k^2\); solving for \(\mathbf{t}\) gives the fixed-point equation, a responsibility-weighted average of the means (a mean-shift iteration). There is no closed form because \(N_k(\mathbf{t})\) depends on \(\mathbf{t}\). ∎

Why not the mean? In the multivalued region the mean (5.158) falls between the branches, exactly the failure of Figure 5.19; with \(s^2\) it is still informative, since the second term in (5.160) is large when the component means are far apart.

▶ DemoThe conditional modeCompare the mean (which falls between the branches), the mean of the most probable component, and the true modes.

5.7 Bayesian neural networks

pp. 277–284 Regularised maximum likelihood is MAP estimation. A full Bayesian treatment marginalises over the weights, but the nonlinear dependence on \(\mathbf{w}\) makes the posterior non-Gaussian and multimodal, so exact inference is impossible. Variational approximations exist (Hinton and van Camp 1993; Barber and Bishop 1998) but the most complete treatment is based on the Laplace approximation (MacKay 1992a,b), assuming (i) a Gaussian approximation around a mode, and (ii) a posterior so narrow that the network is approximately linear in the weights over its support. Then the results of Chapters 3 and 4 apply.

5.7.1 Posterior parameter distribution

pp. 278–280 For a single target, take \[ p(t\given\mathbf{x},\mathbf{w},\beta)=\N(t\given y(\mathbf{x},\mathbf{w}),\beta^{-1}),\qquad p(\mathbf{w}\given\alpha)=\N(\mathbf{w}\given\mathbf{0},\alpha^{-1}\mathbf{I}), \tag{5.161, 5.162} \] so for data \(\mathcal{D}=\{t_1,\dots,t_N\}\) with inputs \(\mathbf{x}_n\), \(p(\mathcal{D}\given\mathbf{w},\beta)\)\({}=\prod_n\N(t_n\given y(\mathbf{x}_n,\mathbf{w}),\beta^{-1})\) (5.163) and \[ p(\mathbf{w}\given\mathcal{D},\alpha,\beta)\propto p(\mathbf{w}\given\alpha)p(\mathcal{D}\given\mathbf{w},\beta) \tag{5.164} \] which is non-Gaussian because \(y\) is nonlinear in \(\mathbf{w}\). Its logarithm is \[ \ln p(\mathbf{w}\given\mathcal{D})=-\frac\alpha2\mathbf{w}^\T\mathbf{w}-\frac\beta2\sum_{n=1}^{N}\{y(\mathbf{x}_n,\mathbf{w})-t_n\}^2+\const , \tag{5.165} \] a regularised sum of squares, maximised by gradient methods at \(\mathbf{w}_{\rm MAP}\) (with backpropagation). The Laplace approximation needs the curvature of the negative log posterior, \[ \mathbf{A}=-\nabla\nabla\ln p(\mathbf{w}\given\mathcal{D},\alpha,\beta)=\alpha\mathbf{I}+\beta\mathbf{H}, \tag{5.166} \] where \(\mathbf{H}\) is the Hessian of the sum-of-squares error \(\tfrac12\sum_n\{y_n-t_n\}^2\) (Section 5.4), and the Gaussian approximation is \[ q(\mathbf{w}\given\mathcal{D})=\N(\mathbf{w}\given\mathbf{w}_{\rm MAP},\mathbf{A}^{-1}). \tag{5.167} \] The predictive distribution is \(p(t\given\mathbf{x},\mathcal{D})\)\({}=\int p(t\given\mathbf{x},\mathbf{w})q(\mathbf{w}\given\mathcal{D})d\mathbf{w}\) (5.168), still intractable. With a small posterior variance, linearise the network at the mode, \[ y(\mathbf{x},\mathbf{w})\simeq y(\mathbf{x},\mathbf{w}_{\rm MAP})+\mathbf{g}^\T(\mathbf{w}-\mathbf{w}_{\rm MAP}),\qquad \mathbf{g}=\nabla_{\mathbf{w}}y(\mathbf{x},\mathbf{w})\big|_{\mathbf{w}_{\rm MAP}}, \tag{5.169, 5.170} \] so that \(p(t\given\mathbf{x},\mathbf{w},\beta)\)\({}\simeq\N(t\given y(\mathbf{x},\mathbf{w}_{\rm MAP})+\mathbf{g}^\T(\mathbf{w}-\mathbf{w}_{\rm MAP}),\beta^{-1})\) (5.171), a linear-Gaussian model.

Result 5.20 (Laplace posterior and predictive distribution for regression) pp. 278–280, (5.166)–(5.173); Exercise 5.38

Under (5.167) and (5.171), \[ p(t\given\mathbf{x},\mathcal{D},\alpha,\beta)=\N\big(t\given y(\mathbf{x},\mathbf{w}_{\rm MAP}),\sigma^2(\mathbf{x})\big),\qquad\sigma^2(\mathbf{x})=\beta^{-1}+\mathbf{g}^\T\mathbf{A}^{-1}\mathbf{g}. \tag{5.172, 5.173} \] The mean is the network at the MAP weights; the variance is the intrinsic noise plus an input-dependent term for the uncertainty in the weights (compare (3.58), (3.59) with \(\bm\phi\to\mathbf{g}\)).

Proof (Exercise 5.38; also the Hessian (5.166))

(5.166). The negative log posterior is \(\tfrac\alpha2\mathbf{w}^\T\mathbf{w}\)\({}+\beta E(\mathbf{w})+\const\); the Hessian of the first term is \(\alpha\mathbf{I}\), of the second \(\beta\mathbf{H}\).

(5.172), (5.173). This is the marginal of a linear-Gaussian pair (toolkit, (2.113)–(2.115)): if \(p(\mathbf{w})\)\({}=\N(\mathbf{w}\given\mathbf{m},\mathbf{S})\) and \(p(t\given\mathbf{w})\)\({}=\N(t\given\mathbf{g}^\T\mathbf{w}+b,\beta^{-1})\) then \(p(t)\)\({}=\N(t\given\mathbf{g}^\T\mathbf{m}+b,\ \beta^{-1}+\mathbf{g}^\T\mathbf{S}\mathbf{g})\). Here \(\mathbf{m}=\mathbf{w}_{\rm MAP}\), \(\mathbf{S}=\mathbf{A}^{-1}\) and the mean of \(t\) given \(\mathbf{w}\) is \(y_{\rm MAP}\)\({}+\mathbf{g}^\T(\mathbf{w}-\mathbf{w}_{\rm MAP})\)\({}=\mathbf{g}^\T\mathbf{w}+b\) with \(b=y_{\rm MAP}-\mathbf{g}^\T\mathbf{w}_{\rm MAP}\). So the mean is \(\mathbf{g}^\T\mathbf{w}_{\rm MAP}+b=y_{\rm MAP}\) and the variance \(\beta^{-1}\)\({}+\mathbf{g}^\T\mathbf{A}^{-1}\mathbf{g}\). Directly: \(t=y_{\rm MAP}+\mathbf{g}^\T\bm\epsilon+e\) with \(\bm\epsilon\sim\N(\mathbf{0},\mathbf{A}^{-1})\), \(e\sim\N(0,\beta^{-1})\) independent, a sum of independent Gaussians. ∎

▶ DemoMAP network (Section 5.7.1)The mode \(\mathbf{w}_{\rm MAP}\) of (5.165): a regularised least-squares network.
▶ DemoPosterior samples around the modeNetworks with weights drawn from (5.167): they differ most where the data are sparse.
▶ DemoPredictive band (Section 5.7.2)The band \(y\pm\sigma(x)\) from (5.173): narrow near the data, wide away from them.

5.7.2 Hyperparameter optimization

pp. 280–281 The evidence framework (Section 3.5) with the Laplace approximation gives point estimates of \(\alpha\) and \(\beta\). The evidence is \[ p(\mathcal{D}\given\alpha,\beta)=\int p(\mathcal{D}\given\mathbf{w},\beta)p(\mathbf{w}\given\alpha)d\mathbf{w}. \tag{5.174} \]

Result 5.21 (evidence, re-estimation of \(\alpha\) and \(\beta\), effective number of parameters) pp. 280–281, (5.174)–(5.180); Exercise 5.39

With \(W\) parameters, within the Laplace approximation (Section 4.4) \[ \ln p(\mathcal{D}\given\alpha,\beta)\simeq-E(\mathbf{w}_{\rm MAP})-\frac12\ln|\mathbf{A}|+\frac W2\ln\alpha+\frac N2\ln\beta-\frac N2\ln(2\pi), \tag{5.175} \] \[ E(\mathbf{w}_{\rm MAP})=\frac\beta2\sum_{n=1}^{N}\{y(\mathbf{x}_n,\mathbf{w}_{\rm MAP})-t_n\}^2+\frac\alpha2\mathbf{w}_{\rm MAP}^\T\mathbf{w}_{\rm MAP}. \tag{5.176} \] Let \(\beta\mathbf{H}\mathbf{u}_i\)\({}=\lambda_i\mathbf{u}_i\) (5.177) (\(\mathbf{H}\) at \(\mathbf{w}_{\rm MAP}\)). Treating the \(\lambda_i\) as independent of \(\alpha\), maximising (5.175) gives \[ \alpha=\frac{\gamma}{\mathbf{w}_{\rm MAP}^\T\mathbf{w}_{\rm MAP}},\qquad\gamma=\sum_{i=1}^{W}\frac{\lambda_i}{\alpha+\lambda_i}, \tag{5.178, 5.179} \] \[ \frac1\beta=\frac1{N-\gamma}\sum_{n=1}^{N}\{y(\mathbf{x}_n,\mathbf{w}_{\rm MAP})-t_n\}^2 . \tag{5.180} \] \(\gamma\) is the effective number of parameters: each direction contributes \(\lambda_i/(\alpha+\lambda_i)\in(0,1)\), close to 1 when the data constrain it (\(\lambda_i\gg\alpha\)) and close to 0 when the prior does (\(\lambda_i\ll\alpha\)). In our experiments the shrinkage factors \(1-e^{-s}\) and \(s/(1+s)\) differ by at most 0.204 (early stopping against weight decay); \(\gamma\) clips negative Hessian eigenvalues to zero. With too little data the procedure can collapse: from \(\alpha=1,\beta=10\) on 15 points it runs to the constant function (\(\alpha\to100\)), whose Laplace evidence (-13.7) beats the fitted network's (-20.4); with 30 points the network wins (demo preset 4 settles at \(\alpha\approx0.12\), \(\beta\approx28\), true precision 25). Alternate (5.178), (5.180) with re-fitting \(\mathbf{w}_{\rm MAP}\) and \(\mathbf{H}\).

Proof (Exercise 5.39)

(5.175). The integrand is \(\exp\{-E(\mathbf{w})\}\) times the normalisers \((\beta/2\pi)^{N/2}(\alpha/2\pi)^{W/2}\), where \(E(\mathbf{w})\)\({}=\frac\beta2\sum_n\{y_n-t_n\}^2\)\({}+\frac\alpha2\mathbf{w}^\T\mathbf{w}\) (by (5.161)–(5.163)). Expanding \(E\) to second order about \(\mathbf{w}_{\rm MAP}\) (where \(\nabla E=0\)), \(E(\mathbf{w})\)\({}\simeq E(\mathbf{w}_{\rm MAP})\)\({}+\tfrac12(\mathbf{w}-\mathbf{w}_{\rm MAP})^\T\mathbf{A}(\mathbf{w}-\mathbf{w}_{\rm MAP})\), and the Gaussian integral is \(e^{-E(\mathbf{w}_{\rm MAP})}(2\pi)^{W/2}|\mathbf{A}|^{-1/2}\) (the Laplace formula (4.135)). Hence \[ \ln p(\mathcal{D}\given\alpha,\beta)\simeq-E(\mathbf{w}_{\rm MAP})+\tfrac W2\ln(2\pi)-\tfrac12\ln|\mathbf{A}|+\tfrac N2\ln\beta-\tfrac N2\ln2\pi+\tfrac W2\ln\alpha-\tfrac W2\ln2\pi, \] and the \(\pm\tfrac W2\ln 2\pi\) terms cancel, giving (5.175).

(5.178), (5.179). The eigenvalues of \(\mathbf{A}=\alpha\mathbf{I}+\beta\mathbf{H}\) are \(\alpha+\lambda_i\), so \(\ln|\mathbf{A}|=\sum_i\ln(\alpha+\lambda_i)\) and \(\partial\ln|\mathbf{A}|/\partial\alpha\)\({}=\sum_i1/(\alpha+\lambda_i)\) (ignoring the dependence of \(\lambda_i\) on \(\alpha\) through \(\mathbf{w}_{\rm MAP}\) and \(\mathbf{H}\): the approximation the book mentions). Using \(\partial E(\mathbf{w}_{\rm MAP})/\partial\alpha\)\({}=\tfrac12\mathbf{w}_{\rm MAP}^\T\mathbf{w}_{\rm MAP}\) (the stationarity of \(E\) in \(\mathbf{w}\) removes the implicit dependence), set \[ -\tfrac12\mathbf{w}_{\rm MAP}^\T\mathbf{w}_{\rm MAP}-\tfrac12\sum_i\frac1{\alpha+\lambda_i}+\frac W{2\alpha}=0\ \Longrightarrow\ \alpha\mathbf{w}_{\rm MAP}^\T\mathbf{w}_{\rm MAP}=W-\sum_i\frac\alpha{\alpha+\lambda_i}=\sum_i\frac{\lambda_i}{\alpha+\lambda_i}=\gamma. \] (5.180). With \(\lambda_i=\beta\eta_i\) (\(\eta_i\) the eigenvalues of \(\mathbf{H}\)), \(\partial\lambda_i/\partial\beta=\lambda_i/\beta\), so \(\partial\ln|\mathbf{A}|/\partial\beta\)\({}=\frac1\beta\sum_i\lambda_i/(\alpha+\lambda_i)\)\({}=\gamma/\beta\). With \(E_D=\tfrac12\sum_n\{y_n-t_n\}^2\), \(\partial E(\mathbf{w}_{\rm MAP})/\partial\beta\)\({}=E_D\), and \[ -E_D-\frac\gamma{2\beta}+\frac N{2\beta}=0\ \Longrightarrow\ \frac1\beta=\frac{2E_D}{N-\gamma}=\frac1{N-\gamma}\sum_n\{y_n-t_n\}^2. \] The factor \(N-\gamma\) instead of \(N\) corrects the bias of the residual variance, \(\gamma\) parameters having been fitted (as in (3.95)). For the linear model this was exact; for a network it neglects \(\partial\lambda_i/\partial\alpha\). ∎

Model comparison and symmetries. The evidence of different architectures is approximated by (5.175) at the optimised \(\alpha,\beta\); a more careful treatment marginalises over \(\alpha,\beta\) by another Gaussian approximation. It needs \(|\mathbf{A}|\), which is sensitive to the small eigenvalues that are hard to determine, unlike the trace. Because the Laplace approximation is local to one mode and each mode of a two-layer net has \(M!\,2^M\) equivalent copies (Result 5.2), the evidence of an \(M\)-unit network should be multiplied by \(M!\,2^M\) when comparing different \(M\). Different inequivalent modes give different \(\alpha\), so the solution depends on the initialisation.

Two-class scatter plot with decision boundaries: the optimal boundary in green, an over-fitted network trained by maximum likelihood in black, and a smoother boundary from a network whose regularisation constant alpha was set by the evidence procedure, in red.
Figure 5.22 (reproduced). The evidence framework on a synthetic two-class set. Green: optimal boundary; black: maximum-likelihood fit of a two-layer network with 8 hidden units (over-fitted); red: the network with a regulariser whose \(\alpha\) is optimised by the evidence procedure from the start value \(\alpha=0\). In our run \(\alpha\approx0.8\) and \(\gamma=7.0\) of 33 weights; the regularised network makes 19 training errors against 4 for the maximum-likelihood fit, and its boundary is close to a straight line. Book: Figure 5.22, p. 283. Code: ch05_neural_networks.py, fig5_22. Download: PDF · SVG
▶ DemoEvidence over hyperparameters (Section 5.7.3)Iterate (5.178) and (5.180): \(\alpha\), \(\beta\) and \(\gamma\) settle, and the evidence rises.

5.7.3 Bayesian neural networks for classification

pp. 281–284 For a single logistic output \(y=\sigma(a)\), \(t\in\{0,1\}\) there is no \(\beta\) (labels are assumed correct): \[ \ln p(\mathcal{D}\given\mathbf{w})=\sum_{n=1}^{N}\{t_n\ln y_n+(1-t_n)\ln(1-y_n)\}, \tag{5.181} \] with the same prior (5.162). The MAP weights minimise \(E(\mathbf{w})\)\({}=-\ln p(\mathcal{D}\given\mathbf{w})\)\({}+\tfrac\alpha2\mathbf{w}^\T\mathbf{w}\) (5.182); the Hessian \(\mathbf{H}\) of the negative log likelihood is computed exactly (5.4.5) or by the outer-product approximation (5.85); then \(\mathbf{A}=\alpha\mathbf{I}+\mathbf{H}\) (5.166) and \(q(\mathbf{w}\given\mathcal{D})\) is (5.167) again. Erratum. In (5.181) the sum \(\sum_n\) must be written as \(\sum_{n=1}^N\), and (5.183) has no additive constant.

Result 5.22 (evidence and predictive distribution for classification) pp. 282–284, (5.183)–(5.190); Exercises 5.40, 5.41

\[ \ln p(\mathcal{D}\given\alpha)\simeq-E(\mathbf{w}_{\rm MAP})-\frac12\ln|\mathbf{A}|+\frac W2\ln\alpha, \tag{5.183} \] \[ E(\mathbf{w}_{\rm MAP})=-\sum_{n=1}^{N}\{t_n\ln y_n+(1-t_n)\ln(1-y_n)\}+\frac\alpha2\mathbf{w}_{\rm MAP}^\T\mathbf{w}_{\rm MAP},\quad y_n=y(\mathbf{x}_n,\mathbf{w}_{\rm MAP}), \tag{5.184} \] and maximising over \(\alpha\) gives (5.178) again (with \(\lambda_i\) the eigenvalues of \(\mathbf{H}\) itself, since there is no \(\beta\)). The simplest prediction is the plug-in \(p(t=1\given\mathbf{x},\mathcal{D})\)\({}\simeq p(t=1\given\mathbf{x},\mathbf{w}_{\rm MAP})\) (5.185). To account for the weight uncertainty linearise the output activation, not the output (a linearised output would leave the interval \((0,1)\)): \[ a(\mathbf{x},\mathbf{w})\simeq a_{\rm MAP}(\mathbf{x})+\mathbf{b}^\T(\mathbf{w}-\mathbf{w}_{\rm MAP}),\qquad\mathbf{b}=\nabla a(\mathbf{x},\mathbf{w}_{\rm MAP}), \tag{5.186} \] so \(a\given\mathbf{x},\mathcal{D}\) is Gaussian with mean \(a_{\rm MAP}\) and variance \[ \sigma_a^2(\mathbf{x})=\mathbf{b}^\T(\mathbf{x})\mathbf{A}^{-1}\mathbf{b}(\mathbf{x}) , \tag{5.188} \] ((5.187) defines this distribution by pushing the posterior through the linearisation: an integral of a delta function against \(q(\mathbf{w}\given\mathcal{D})\).) and \[ p(t=1\given\mathbf{x},\mathcal{D})=\int\sigma(a)\,p(a\given\mathbf{x},\mathcal{D})\,da\simeq\sigma\big(\kappa(\sigma_a^2)\,a_{\rm MAP}\big),\qquad\kappa(\sigma^2)=\Big(1+\frac{\pi\sigma^2}8\Big)^{-1/2}. \tag{5.189, 5.190} \] Erratum. The printed (5.190) has \(\mathbf{b}^\T\mathbf{w}_{\rm MAP}\) where \(a_{\rm MAP}\) is meant. The effect: contours of equal probability spread out and the predictions are shifted towards \(0.5\), while the \(y=0.5\) contour (\(a_{\rm MAP}=0\)) is unchanged, because \(\kappa>0\) cannot change the sign of \(a_{\rm MAP}\).

Proof (Exercises 5.40, 5.41, and the probit approximation)

(5.183) (Exercise 5.41). As for (5.175), with the likelihood \(\prod_ny_n^{t_n}(1-y_n)^{1-t_n}\), prior normaliser \((\alpha/2\pi)^{W/2}\) and no \(\beta\) terms. Laplace: \(\int e^{-E(\mathbf{w})}d\mathbf{w}\)\({}\simeq e^{-E(\mathbf{w}_{\rm MAP})}(2\pi)^{W/2}|\mathbf{A}|^{-1/2}\), with \(E\) the regularised error (5.182), so \(\ln p(\mathcal{D}\given\alpha)\)\({}\simeq-E(\mathbf{w}_{\rm MAP})\)\({}-\tfrac12\ln|\mathbf{A}|+\tfrac W2\ln\alpha\) (the \(\tfrac W2\ln2\pi\) terms cancel; no constant remains). Differentiating in \(\alpha\) as in Result 5.21 with \(\lambda_i\) now the eigenvalues of \(\mathbf{H}\) gives (5.178).

(5.187), (5.188). Because \(a\) is a linear function of the Gaussian \(\mathbf{w}\), it is Gaussian with mean \(a_{\rm MAP}\) and variance \(\mathbf{b}^\T\mathbf{A}^{-1}\mathbf{b}\) (Section 4.5.2; the same linear-Gaussian marginal as above, with no noise term).

The approximation (4.153), (4.154). Approximate \(\sigma(a)\simeq\Phi(\lambda a)\) with the probit \(\Phi\) and \(\lambda^2=\pi/8\) (this matches the slopes at \(a=0\): \(\sigma'(0)=\tfrac14\), \(\Phi'(0)=\lambda/\sqrt{2\pi}\), so \(\lambda=\sqrt{2\pi}/4\), \(\lambda^2=\pi/8\)). The convolution of a probit with a Gaussian is a probit: for \(\Phi(\lambda a)=P(Z\le\lambda a)\), \(Z\sim\N(0,1)\) independent of \(a\sim\N(\mu,s^2)\), \(\int\Phi(\lambda a)\N(a\given\mu,s^2)da\)\({}=P(Z-\lambda a\le0)\), and \(Z-\lambda a\sim\N(-\lambda\mu,1+\lambda^2s^2)\), so the result is \(\Phi\big(\lambda\mu/\sqrt{1+\lambda^2s^2}\big)\). Replacing \(\Phi(x)\) back by \(\sigma(x/\lambda)\) gives \(\sigma\big(\mu/\sqrt{1+\pi s^2/8}\big)\), which is (5.190) with \(\kappa(s^2)=(1+\pi s^2/8)^{-1/2}\).

Multiclass (Exercise 5.40). For softmax outputs \(y_k=\exp(a_k)/\sum_j\exp(a_j)\) and error (5.24): the log likelihood is \(\sum_n\sum_kt_{nk}\ln y_{nk}\); the prior (5.162) is unchanged, with a single \(\alpha\) (or one per group); \(\mathbf{w}_{\rm MAP}\) minimises \(-\ln p(\mathcal{D}\given\mathbf{w})\)\({}+\tfrac\alpha2\mathbf{w}^\T\mathbf{w}\). The Hessian \(\mathbf{H}\) of the cross-entropy is computed exactly (5.4.5, \(M_{kk'}=y_k\delta_{kk'}-y_ky_{k'}\)) or with the softmax outer product (Result 5.9, item 4); \(\mathbf{A}=\alpha\mathbf{I}+\mathbf{H}\); the evidence is (5.183) with the multiclass \(E\); (5.178) holds for \(\alpha\). For the prediction, linearise each activation, \(a_k\)\({}\simeq a_k^{\rm MAP}\)\({}+\mathbf{b}_k^\T(\mathbf{w}-\mathbf{w}_{\rm MAP})\), \(\mathbf{b}_k=\nabla a_k\), so the vector \(\mathbf{a}\) is Gaussian with covariance \(\mathrm{cov}(a_k,a_l)\)\({}=\mathbf{b}_k^\T\mathbf{A}^{-1}\mathbf{b}_l\); the integral of the softmax against this Gaussian is then approximated (Section 4.5.2 gives the binary case; for \(K>2\) by sampling, or by a generalised probit-type approximation). ∎

Two panels showing probability contours of a Bayesian neural network classifier on a two-class data set: on the left the point-estimate predictions, on the right the predictions with the posterior variance included, which are less confident, with contours spread apart.
Figure 5.23 (reproduced). Laplace approximation for a network with 8 \(\tanh\) hidden units and a logistic output, weights by scaled conjugate gradients and \(\alpha\) by the evidence framework. Left: the plug-in prediction (5.185); right: the marginalised prediction (5.190). The green curve is \(y=0.5\); other contours are \(y=0.1,0.3,0.7,0.9\). Marginalisation spreads the contours and makes predictions less confident, shifting probabilities towards 0.5, while the \(y=0.5\) contour does not move. Because our regularised boundary is nearly straight, the two sets of contours differ only slightly here. Book: Figure 5.23, p. 284. Code: ch05_neural_networks.py, fig5_23. Download: PDF · SVG
▶ DemoBayesian logistic network classificationSwitch between the plug-in (5.185) and marginalised (5.190) probability contours.

Exercises

All 41 exercises of Chapter 5 (Bishop, 2006, pp. 284–290), restated in my own words, with complete solutions (hidden). Stars give the book's difficulty and "www" marks the exercises with official solutions. Most are the derivations of the results above; their solutions repeat the essential steps and point to the full treatment. Solution manuals were checked and the steps verified independently (several numerically, as reported above).

  1. Exercise 5.1 (★★). A two-layer network has logistic-sigmoid hidden units. Show there is an equivalent network, computing exactly the same function, with \(\tanh\) hidden units, and that the parameters differ by linear transformations. (Erratum: the book's "\(g(\cdot)\)" in the exercise should be \(h(\cdot)\).)

    Solution

    \(\sigma(a)=\tfrac12(1+\tanh(a/2))\) because \(\tanh\tfrac a2=\tfrac{1-e^{-a}}{1+e^{-a}}\). Substituting into \(a_k=\sum_jw^{(2)}_{kj}\sigma(a_j)+w^{(2)}_{k0}\) gives a \(\tanh\) network with \(\widetilde w^{(1)}_{ji}=\tfrac12w^{(1)}_{ji}\), \(\widetilde w^{(2)}_{kj}=\tfrac12w^{(2)}_{kj}\), \(\widetilde w^{(2)}_{k0}\)\({}=w^{(2)}_{k0}+\tfrac12\sum_jw^{(2)}_{kj}\), all linear in the original parameters. Details: Filling the gap 5.2.

  2. Exercise 5.2 (★, www). Show that maximising the likelihood under (5.16) for a multi-output network is equivalent to minimising the sum-of-squares error (5.11).

    Solution

    \(-\ln\prod_n\N(\mathbf{t}_n\given\mathbf{y}_n,\beta^{-1}\mathbf{I})\)\({}=\frac\beta2\sum_n\|\mathbf{y}_n\)\({}-\mathbf{t}_n\|^2-\frac{NK}2\ln\beta\)\({}+\frac{NK}2\ln2\pi\). For fixed \(\beta>0\) only the first term depends on \(\mathbf{w}\), and it is \(\beta E(\mathbf{w})\) with \(E\) from (5.11); a positive multiple has the same minimisers. Setting \(\partial/\partial\beta=0\) gives \(1/\beta_{\rm ML}\)\({}=\frac1{NK}\sum_n\|\mathbf{y}_n\)\({}-\mathbf{t}_n\|^2\) (5.17). See Result 5.3.

  3. Exercise 5.3 (★★). Targets have Gaussian conditional \(\N(\mathbf{t}\given\mathbf{y}(\mathbf{x},\mathbf{w}),\bm\Sigma)\). Write the error to minimise for known \(\bm\Sigma\); then let \(\bm\Sigma\) be unknown and give its ML solution, noting that the optimisations of \(\mathbf{w}\) and \(\bm\Sigma\) are now coupled.

    Solution

    Known \(\bm\Sigma\): \(E(\mathbf{w})\)\({}=\tfrac12\sum_n(\mathbf{y}_n-\mathbf{t}_n)^\T\bm\Sigma^{-1}(\mathbf{y}_n-\mathbf{t}_n)\). Unknown: \(\bm\Sigma_{\rm ML}\)\({}=\frac1N\sum_n(\mathbf{y}_n-\mathbf{t}_n)(\mathbf{y}_n-\mathbf{t}_n)^\T\) at the current \(\mathbf{w}\), which in turn changes the weighting in \(E\); the profile objective is \(\tfrac N2\ln|\mathbf{S}(\mathbf{w})/N|\). Derivation in Filling the gap 5.3.

  4. Exercise 5.4 (★★). Binary targets \(t\in\{0,1\}\), network output \(y=p(t=1\given\mathbf{x})\), and each recorded label is wrong with probability \(\epsilon\). Write the negative log likelihood and verify that (5.21) is recovered for \(\epsilon=0\).

    Solution

    \(E\)\({}=-\sum_n\{t_n\ln[(1-\epsilon)y_n+\epsilon(1-y_n)]\)\({}+(1-t_n)\ln[\epsilon y_n+(1-\epsilon)(1-y_n)]\}\); at \(\epsilon=0\) the arguments are \(y_n\) and \(1-y_n\), which is (5.21). Bounded loss, hence robust: see Filling the gap 5.3.

  5. Exercise 5.5 (★, www). Show that maximising the likelihood of a multiclass network with \(y_k=p(t_k=1\given\mathbf{x})\) is equivalent to minimising the cross-entropy (5.24).

    Solution

    With 1-of-\(K\) targets \(p(\mathbf{t}\given\mathbf{x})=\prod_ky_k^{t_k}\); the log likelihood of i.i.d. data is \(\sum_n\sum_kt_{nk}\ln y_k(\mathbf{x}_n,\mathbf{w})\), whose negative is (5.24). Result 5.3(4).

  6. Exercise 5.6 (★, www). Show that the derivative of (5.21) with respect to the activation \(a_k\) of a logistic-sigmoid output unit is \(y_k-t_k\), (5.18).

    Solution

    \(\sigma'=\sigma(1-\sigma)\), so \(\partial E_n/\partial a_k\)\({}=-\{t_k/y_k-(1-t_k)/(1-y_k)\}y_k(1-y_k)\)\({}=-t_k(1-y_k)+(1-t_k)y_k\)\({}=y_k-t_k\). Result 5.4.

  7. Exercise 5.7 (★). Show the same for the softmax output and the error (5.24).

    Solution

    \(\partial y_k/\partial a_l=y_k(\delta_{kl}-y_l)\), so \(\partial E_n/\partial a_l\)\({}=-\sum_kt_k(\delta_{kl}-y_l)\)\({}=-t_l+y_l\sum_kt_k\)\({}=y_l-t_l\) since \(\sum_kt_k=1\). Result 5.4.

  8. Exercise 5.8 (★). Derive the analogue of \(\sigma'=\sigma(1-\sigma)\) for \(\tanh\).

    Solution

    \(\tanh a=N/D\) with \(N=e^a-e^{-a}\), \(D=e^a+e^{-a}\), \(N'=D\), \(D'=N\): \((N/D)'=(D^2-N^2)/D^2=1-\tanh^2a\) since \(D^2-N^2=4\). This is (5.60), derived in Section 5.3.2.

  9. Exercise 5.9 (★, www). Derive the error function for a network with output \(-1\le y\le1\) and targets \(t=+1\) for \(\mathcal{C}_1\), \(t=-1\) for \(\mathcal{C}_2\). What is the appropriate output activation?

    Solution

    Put \(p(\mathcal{C}_1\given\mathbf{x})=(1+y)/2\). Then \(E\)\({}=-\sum_n\{\frac{1+t_n}2\ln\frac{1+y_n}2\)\({}+\frac{1-t_n}2\ln\frac{1-y_n}2\}\). The activation is \(y=\tanh(a/2)=2\sigma(a)-1\) (so that \((1+y)/2=\sigma(a)\)); with this choice \(\partial E_n/\partial a=\tfrac12(y_n-t_n)\). Check: \(\partial E_n/\partial y\)\({}=-\frac{1+t}{2(1+y)}+\frac{1-t}{2(1-y)}\); for \(t=1\) this is \(-1/(1+y)\), and \(dy/da=\tfrac12(1-y^2)\), so \(\partial E_n/\partial a\)\({}=-\tfrac12(1-y)\)\({}=\tfrac12(y-1)\)\({}=\tfrac12(y-t)\); \(t=-1\) similarly. Filling the gap 5.3.

  10. Exercise 5.10 (★, www). Putting \(\mathbf{v}=\mathbf{u}_i\) in (5.39), show that \(\mathbf{H}\) is positive definite iff all its eigenvalues are positive.

    Solution

    If \(\mathbf{H}\) is positive definite, \(\mathbf{u}_i^\T\mathbf{H}\mathbf{u}_i\)\({}=\lambda_i\|\mathbf{u}_i\|^2\)\({}=\lambda_i>0\). Conversely, if all \(\lambda_i>0\), then for \(\mathbf{v}=\sum c_i\mathbf{u}_i\ne\mathbf{0}\), \(\mathbf{v}^\T\mathbf{H}\mathbf{v}\)\({}=\sum c_i^2\lambda_i>0\). Result 5.5(2).

  11. Exercise 5.11 (★★, www). Show that the contours of the quadratic (5.32) are ellipses aligned with the eigenvectors of \(\mathbf{H}\), with lengths inversely proportional to \(\sqrt{\lambda_i}\).

    Solution

    In the coordinates \(\alpha_i\)\({}=\mathbf{u}_i^\T(\mathbf{w}-\mathbf{w}^\star)\), \(E-E^\star=\tfrac12\sum\lambda_i\alpha_i^2=c\) is \(\sum\alpha_i^2/(2c/\lambda_i)=1\), an ellipsoid with semi-axes \(\sqrt{2c/\lambda_i}\propto\lambda_i^{-1/2}\) along \(\mathbf{u}_i\) (Result 5.5(3)).

  12. Exercise 5.12 (★★, www). From the Taylor expansion (5.32) about a stationary point, show that the condition for a local minimum is that \(\mathbf{H}\) be positive definite.

    Solution

    Positive definite \(\Rightarrow\) \(E-E^\star\simeq\tfrac12\sum\lambda_i\alpha_i^2>0\) for small \(\boldsymbol\alpha\ne\mathbf{0}\): a strict minimum. A negative eigenvalue \(\lambda_i<0\) makes \(E\) decrease along \(\mathbf{u}_i\), so no minimum. Hence for the quadratic model "minimum iff positive definite" (only the borderline of a zero eigenvalue needs higher-order terms). Result 5.5(4).

  13. Exercise 5.13 (★). Show that the quadratic (5.28) has \(W(W+3)/2\) independent elements.

    Solution

    \(\mathbf{b}\): \(W\); symmetric \(\mathbf{H}\): \(W(W+1)/2\). Sum \(=W(W+3)/2\). Filling the gap 5.4.

  14. Exercise 5.14 (★). Verify by Taylor expansion that the \(O(\epsilon)\) terms cancel in the central difference (5.69).

    Solution

    \(f(\pm\epsilon)\)\({}=f\pm\epsilon f'\)\({}+\tfrac12\epsilon^2f''\pm\tfrac16\epsilon^3f'''\)\({}+\dots\), so \([f(\epsilon)-f(-\epsilon)]/(2\epsilon)\)\({}=f'+\tfrac16\epsilon^2f'''+O(\epsilon^4)\). The even terms cancel in the difference. Filling the gap 5.6.

  15. Exercise 5.15 (★★). Derive a forward-propagation formalism for the Jacobian instead of the backward one.

    Solution

    For input \(i\): set \(\partial x_{i'}/\partial x_i=\delta_{ii'}\); then in forward order \(\partial a_j/\partial x_i\)\({}=\sum_lw_{jl}\partial z_l/\partial x_i\) and \(\partial z_j/\partial x_i\)\({}=h'(a_j)\partial a_j/\partial x_i\); at outputs \(J_{ki}=f'(a_k)\partial a_k/\partial x_i\) (softmax: combine with (5.76)). One sweep per input (\(D\) sweeps), against \(K\) for the backward method. Result 5.7.

  16. Exercise 5.16 (★). Extend the outer-product approximation (5.84) to several outputs.

    Solution

    For \(E=\tfrac12\sum_n\sum_k(y_{nk}-t_{nk})^2\), \(\mathbf{H}\)\({}=\sum_n\sum_k\nabla y_{nk}\nabla y_{nk}^\T\)\({}+\sum_n\sum_k(y_{nk}-t_{nk})\nabla\nabla y_{nk}\); dropping the second term, \(\mathbf{H}\)\({}\simeq\sum_n\sum_k\mathbf{b}_{nk}\mathbf{b}_{nk}^\T\), \(\mathbf{b}_{nk}=\nabla y_{nk}\). Result 5.9(2).

  17. Exercise 5.17 (★). For the population error \(E\)\({}=\tfrac12\iint\{y(\mathbf{x},\mathbf{w})\)\({}-t\}^2p(\mathbf{x},t)\), use the fact that the minimiser is \(\E[t\given\mathbf{x}]\) to show \(\partial^2E/\partial w_r\partial w_s\)\({}=\int\frac{\partial y}{\partial w_r}\frac{\partial y}{\partial w_s}p(\mathbf{x})d\mathbf{x}\).

    Solution

    Differentiating twice gives \(\iint\frac{\partial y}{\partial w_r}\frac{\partial y}{\partial w_s}p\)\({}+\iint(y-t)\frac{\partial^2y}{\partial w_r\partial w_s}p\). The \(t\)-integral of the second term is \(\int(y-\E[t\given\mathbf{x}])\frac{\partial^2y}{\partial w_r\partial w_s}p(\mathbf{x})d\mathbf{x}\)\({}=0\) when \(y=\E[t\given\mathbf{x}]\). Result 5.9(5).

  18. Exercise 5.18 (★). Add skip-layer connections to a two-layer network and write the derivatives with respect to the new parameters.

    Solution

    With \(a_k\)\({}=\sum_jw^{(2)}_{kj}z_j+\sum_iw^{\rm s}_{ki}x_i\): \(\partial E_n/\partial w^{\rm s}_{ki}=\delta_kx_i\) (\(\delta_k=y_k-t_k\) for linear outputs); (5.66), (5.67) unchanged. Filling the gap 5.5.

  19. Exercise 5.19 (★, www). Derive (5.85), the outer-product Hessian for a single sigmoid output with cross-entropy.

    Solution

    \(\nabla E_n=(y_n-t_n)\nabla a_n\), \(\nabla\nabla E_n\)\({}=y_n(1-y_n)\nabla a_n\nabla a_n^\T\)\({}+(y_n-t_n)\nabla\nabla a_n\); neglecting the last term gives (5.85). Result 5.9(3).

  20. Exercise 5.20 (★). Outer-product Hessian for \(K\) softmax outputs with cross-entropy.

    Solution

    \(\mathbf{H}\)\({}\simeq\sum_n\sum_{k,l}y_{nk}(\delta_{kl}-y_{nl})\mathbf{b}_{nk}\mathbf{b}_{nl}^\T\), \(\mathbf{b}_{nk}=\nabla a_{nk}\): from \(\nabla E_n=\sum_k(y_{nk}-t_{nk})\nabla a_{nk}\) and \(\partial y_k/\partial a_l=y_k(\delta_{kl}-y_l)\). Result 5.9(4).

  21. Exercise 5.21 (★★★). Extend (5.86) to \(K>1\) outputs and obtain a form allowing (5.87) to absorb outputs and patterns one at a time, so that (5.89) gives the inverse Hessian sequentially.

    Solution

    \(\mathbf{H}_N\)\({}=\sum_n\sum_k\mathbf{b}_{nk}\mathbf{b}_{nk}^\T\); order the \(NK\) pairs \((n,k)\) and set \(\mathbf{H}_{L+1}\)\({}=\mathbf{H}_L\)\({}+\mathbf{b}_{L+1}\mathbf{b}_{L+1}^\T\) with \(\mathbf{b}_{L+1}\) the \(L+1\)th pair, then apply (5.89) \(NK\) times from \(\mathbf{H}_0^{-1}=\alpha^{-1}\mathbf{I}\). For cross-entropy models factor the output-space matrix (\(y(1-y)\), or \(\diag\mathbf{y}-\mathbf{y}\mathbf{y}^\T\)) into rank-one terms first. Result 5.10.

  22. Exercise 5.22 (★★). Derive (5.93)–(5.95) by the chain rule.

    Solution

    Differentiate \(\partial E_n/\partial w^{(2)}_{kj}=\delta_kz_j\) and \(\partial E_n/\partial w^{(1)}_{ji}=\delta_jx_i\) using \(\partial\delta_k/\partial a_{k'}=M_{kk'}\), \(\partial a_{k'}/\partial w^{(2)}_{kj}\)\({}=\delta_{kk'}z_j\), \(\partial a_{j}/\partial w^{(1)}_{j'i'}\)\({}=I_{jj'}x_{i'}\). Full steps and a numerical confirmation: Result 5.11.

  23. Exercise 5.23 (★★). Include skip-layer connections in the exact Hessian of Section 5.4.5.

    Solution

    (5.93)–(5.95) are unchanged; new blocks: skip–skip \(x_ix_{i'}M_{kk'}\); skip–second layer \(x_iz_jM_{kk'}\); skip–first layer \(x_ix_{i'}h'(a_j)\sum_{k'}M_{kk'}w^{(2)}_{k'j}\). Result 5.11.

  24. Exercise 5.24 (★). Verify that the network (5.113), (5.114) is invariant under (5.115) with the weight changes (5.116), (5.117), and the output change (5.118) is produced by (5.119), (5.120).

    Solution

    \(\sum_i\frac{w_{ji}}a(ax_i+b)+w_{j0}\)\({}-\frac ba\sum_iw_{ji}\)\({}=\sum_iw_{ji}x_i+w_{j0}\), so each hidden activation is unchanged; \(\sum_jcw_{kj}z_j+cw_{k0}+d=cy_k+d\). Result 5.12.

  25. Exercise 5.25 (★★★, www). For the quadratic (5.195) and gradient descent from the origin, show \(w^{(\tau)}_j=\{1-(1-\rho\eta_j)^\tau\}w^\star_j\), convergence for \(|1-\rho\eta_j|<1\), the limits (5.199), (5.200), the analogy \(\lambda\leftrightarrow(\rho\tau)^{-1}\), and that the effective number of parameters grows.

    Solution

    \(\nabla E=\mathbf{H}(\mathbf{w}-\mathbf{w}^\star)\) so \(w^{(\tau)}_j-w_j^\star\)\({}=(1-\rho\eta_j)(w^{(\tau-1)}_j-w_j^\star)\) and, from \(w^{(0)}=0\), \(w^{(\tau)}_j=\{1-(1-\rho\eta_j)^\tau\}w_j^\star\). The rest (limits via \((1-\rho\eta)^\tau\simeq e^{-\rho\tau\eta}\); ridge factor \(\eta_j/(\eta_j+\lambda)\); \(\gamma(\tau)=\sum_j\{1-(1-\rho\eta_j)^\tau\}\) increasing) is in Result 5.13.

  26. Exercise 5.26 (★★). For tangent propagation with a general feed-forward network, show \(\Omega_n=\tfrac12\sum_k(\mathcal{G}y_k)^2\), derive the forward equations for \(\alpha_j=\mathcal{G}z_j\), \(\beta_j=\mathcal{G}a_j\), the weight derivatives (5.206), and the backpropagation equations for \(\delta_{kr}\) and \(\phi_{kr}\).

    Solution

    \(\mathcal{G}y_k=\sum_iJ_{ki}\tau_i\); \(\alpha_j=h'(a_j)\beta_j\), \(\beta_j=\sum_iw_{ji}\alpha_i\); \(\partial\Omega_n/\partial w_{rs}\)\({}=\sum_k\alpha_k\{\phi_{kr}z_s\)\({}+\delta_{kr}\alpha_s\}\); \(\delta_{kr}=h'(a_r)\sum_lw_{lr}\delta_{kl}\), \(\phi_{kr}\)\({}=h''(a_r)\beta_r\sum_lw_{lr}\delta_{kl}\)\({}+h'(a_r)\sum_lw_{lr}\phi_{kl}\). Derivation: Result 5.14.

  27. Exercise 5.27 (★★, www). For additive noise \(\mathbf{x}\to\mathbf{x}+\bm\xi\) (zero mean, unit covariance), follow Section 5.5.5 to obtain the Tikhonov form (5.135).

    Solution

    Expand \(y(\mathbf{x}+\bm\xi)\) to second order, average with \(\E[\bm\xi\bm\xi^\T]=\mathbf{I}\): \(\E\{y(\mathbf{x}+\bm\xi)-t\}^2\)\({}=(y-t)^2+\|\nabla y\|^2+(y-t)\nabla^2y\); integrating over \(t\), the last term carries \(y-\E[t\given\mathbf{x}]=O(\xi^2)\) and is dropped, leaving \(\Omega\)\({}=\tfrac12\int\|\nabla y\|^2p(\mathbf{x})d\mathbf{x}\). Result 5.15.

  28. Exercise 5.28 (★, www). How must backpropagation change when several weights are constrained to be equal (convolutional networks)?

    Solution

    The error depends on the shared value \(\omega\), so \(\partial E/\partial\omega\) is the sum of the ordinary derivatives \(\delta_jz_i\) over all links in the tied set; update the single parameter. Filling the gap 5.5.

  29. Exercise 5.29 (★, www). Verify (5.141).

    Solution

    \(\partial\Omega/\partial w_i\)\({}=-S_i^{-1}\sum_j\pi_j\partial N_{ij}/\partial w_i\)\({}=\sum_j\gamma_j(w_i)(w_i-\mu_j)/\sigma_j^2\). Result 5.17.

  30. Exercise 5.30 (★). Verify (5.142).

    Solution

    \(\partial N_{ij}/\partial\mu_j\)\({}=N_{ij}(w_i-\mu_j)/\sigma_j^2\), so \(\partial\Omega/\partial\mu_j\)\({}=-\sum_i\gamma_j(w_i)(w_i-\mu_j)/\sigma_j^2\)\({}=\sum_i\gamma_j(w_i)(\mu_j-w_i)/\sigma_j^2\). Result 5.17.

  31. Exercise 5.31 (★). Verify (5.143).

    Solution

    \(\partial\ln N_{ij}/\partial\sigma_j\)\({}=-1/\sigma_j+(w_i-\mu_j)^2/\sigma_j^3\), hence \(\partial\Omega/\partial\sigma_j\)\({}=\sum_i\gamma_j(w_i)\{1/\sigma_j\)\({}-(w_i-\mu_j)^2/\sigma_j^3\}\). Result 5.17.

  32. Exercise 5.32 (★★). Show \(\partial\pi_k/\partial\eta_j\)\({}=\delta_{jk}\pi_j-\pi_j\pi_k\) for the softmax (5.146) and then derive (5.147) using \(\sum_k\gamma_k(w_i)=1\).

    Solution

    Quotient rule for the first part. Then \(\partial\Omega/\partial\eta_j\)\({}=\sum_k(-\sum_i\gamma_k(w_i)/\pi_k)(\delta_{jk}\pi_j-\pi_j\pi_k)\)\({}=-\sum_i\gamma_j(w_i)\)\({}+\pi_j\sum_i\sum_k\gamma_k(w_i)\)\({}=\sum_i\{\pi_j-\gamma_j(w_i)\}\). Result 5.17.

  33. Exercise 5.33 (★). Write the forward kinematics of the two-link arm of Figure 5.18 (origin at the attachment point of the lower arm).

    Solution

    \(x_1=L_1\cos\theta_1+L_2\cos(\theta_1+\theta_2)\), \(x_2=L_1\sin\theta_1+L_2\sin(\theta_1+\theta_2)\), where \(\theta_1\) is the angle of the first link from the \(x_1\)-axis and \(\theta_2\) the angle of the second relative to the first. Inverse: with \(r^2=x_1^2+x_2^2\), \(\cos\theta_2=(r^2-L_1^2-L_2^2)/(2L_1L_2)\), giving \(\theta_2=\pm\arccos(\cdot)\) (elbow up and elbow down, two solutions when \(|L_1-L_2|\({}=\operatorname{atan2}(x_2,x_1)\)\({}-\operatorname{atan2}(L_2\sin\theta_2,L_1+L_2\cos\theta_2)\). The two solutions are mirror images across the shoulder–end-effector line, and their average does not reach the target (Filling the gap 5.8).

  34. Exercise 5.34 (★, www). Derive (5.155).

    Solution

    With \(\partial\pi_l/\partial a^\pi_k\)\({}=\pi_l(\delta_{lk}-\pi_k)\): \(\partial E_n/\partial a^\pi_k\)\({}=-\sum_l\gamma_l(\delta_{lk}-\pi_k)\)\({}=\pi_k-\gamma_k\). Result 5.18.

  35. Exercise 5.35 (★). Derive (5.156).

    Solution

    \(\partial N_k/\partial\mu_{kl}\)\({}=N_k(t_l-\mu_{kl})/\sigma_k^2\); then \(\partial E_n/\partial a^\mu_{kl}\)\({}=\gamma_{nk}(\mu_{kl}-t_{nl})/\sigma_k^2\). Result 5.18.

  36. Exercise 5.36 (★). Derive (5.157).

    Solution

    \(\partial N_k/\partial a^\sigma_k\)\({}=N_k(-L+\|\mathbf{t}-\bm\mu_k\|^2/\sigma_k^2)\) because \(\sigma_k=e^{a^\sigma_k}\); hence \(\partial E_n/\partial a^\sigma_k\)\({}=\gamma_{nk}(L-\|\mathbf{t}_n-\bm\mu_k\|^2/\sigma_k^2)\). Result 5.18.

  37. Exercise 5.37 (★). Verify (5.158) and (5.160).

    Solution

    Mean: \(\sum_k\pi_k\bm\mu_k\). Variance: split \(\mathbf{t}-\bar{\mathbf{t}}\)\({}=(\mathbf{t}-\bm\mu_k)\)\({}+(\bm\mu_k-\bar{\mathbf{t}})\) inside each component; the cross term has zero mean; \(\E_k\|\mathbf{t}-\bm\mu_k\|^2=L\sigma_k^2\) (\(=\sigma_k^2\) for the book's scalar case). Result 5.19.

  38. Exercise 5.38 (★). Using (2.115), derive the predictive distribution (5.172) for the Laplace-approximated Bayesian network.

    Solution

    Marginal of the linear-Gaussian pair (5.167), (5.171): \(\N(t\given y_{\rm MAP},\beta^{-1}+\mathbf{g}^\T\mathbf{A}^{-1}\mathbf{g})\). Result 5.20.

  39. Exercise 5.39 (★, www). Use the Laplace result (4.135) to show the evidence is approximated by (5.175).

    Solution

    \(\int e^{-E(\mathbf{w})}d\mathbf{w}\)\({}\simeq e^{-E(\mathbf{w}_{\rm MAP})}(2\pi)^{W/2}|\mathbf{A}|^{-1/2}\), multiplied by the normalisers \((\beta/2\pi)^{N/2}(\alpha/2\pi)^{W/2}\); the \(W\ln2\pi\) terms cancel. Result 5.21.

  40. Exercise 5.40 (★, www). Outline the changes to Section 5.7.3 for multiclass networks with softmax outputs.

    Solution

    Likelihood \(\sum_n\sum_kt_{nk}\ln y_{nk}\); same prior; \(\mathbf{H}\) from the softmax cross-entropy (exact with \(M_{kk'}=y_k\delta_{kk'}-y_ky_{k'}\), or the outer-product form of Result 5.9(4)); \(\mathbf{A}=\alpha\mathbf{I}+\mathbf{H}\); evidence (5.183) and \(\alpha\) update (5.178) unchanged in form; predictions by linearising each activation \(a_k\) and integrating the softmax against the resulting Gaussian, approximately. See Result 5.22.

  41. Exercise 5.41 (★★). By analogy with Sections 5.7.1 and 5.7.2, derive (5.183) for the network with a sigmoid output and cross-entropy.

    Solution

    Apply Laplace to \(\int e^{-E(\mathbf{w})}d\mathbf{w}\) with the regularised cross-entropy (5.182) and the prior normaliser \((\alpha/2\pi)^{W/2}\): \(\ln p(\mathcal{D}\given\alpha)\)\({}\simeq-E(\mathbf{w}_{\rm MAP})\)\({}-\tfrac12\ln|\mathbf{A}|+\tfrac W2\ln\alpha\), without constant (erratum). Result 5.22.

Demonstrations for this chapter

Each demonstration runs in the browser, with seeded random numbers so that a class sees the same pictures, and links back to these notes. The ▶ Demo boxes above open them already set to the case under discussion.

DemonstrationWhat you can seeSections and results
Two-layer network regression and backpropagationHidden units, the network function and its hidden-unit components, a backpropagation-versus-finite-difference gradient check, weight decay and early stopping. Presets: 1 three hidden units on sin(2 pi x) (Fig. 5.3); 2 x^2, |x|, step (Fig. 5.3); 3 gradient check (5.3.2); 4 over-fitting with more hidden units (Fig. 5.9); 5 early stopping (Fig. 5.12); 6 weight decay.5.1, 5.2, 5.3, 5.5; Result 5.1, 5.6, 5.13
Mixture density network on an inverse problemThe toy inverse problem: a sum-of-squares network against a mixture density network, the mixing coefficients, means and widths, the conditional mode, and the number of components. Presets: 1 the forward problem (Fig. 5.19); 2 sum-of-squares fails on the inverse problem; 3 mixture density network (Fig. 5.21); 4 conditional mode; 5 more components.5.6; Result 5.18, 5.19
Bayesian neural network: Laplace approximationThe MAP network, posterior samples around the mode, the predictive band, the evidence over \(\alpha\) and \(\beta\) with the effective number of parameters, and Bayesian logistic-network classification. Presets: 1 MAP network; 2 posterior samples; 3 predictive band; 4 evidence over hyperparameters; 5 classification (Fig. 5.23).5.7.1–5.7.3; Result 5.20, 5.21, 5.22

The Python code that produces every figure of this page is code/ch05_neural_networks.py (see the code index).

References

  1. Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer, New York. Chapter 5, pp. 225–290 (and the official errata).
  2. Bishop, C. M. (1992). Exact calculation of the Hessian matrix for the multilayer perceptron. Neural Computation, 4(4), 494–501. doi:10.1162/neco.1992.4.4.494.
  3. Bishop, C. M. (1995). Training with noise is equivalent to Tikhonov regularization. Neural Computation, 7(1), 108–116. doi:10.1162/neco.1995.7.1.108.
  4. Bishop, C. M. (1994). Mixture density networks. Technical Report NCRG/94/004, Aston University.
  5. Chen, A. M., Lu, H. and Hecht-Nielsen, R. (1993). On the geometry of feedforward neural network error surfaces. Neural Computation, 5(6), 910–927. doi:10.1162/neco.1993.5.6.910.
  6. Cybenko, G. (1989). Approximation by superpositions of a sigmoidal function. Mathematics of Control, Signals and Systems, 2(4), 303–314. doi:10.1007/BF02551274.
  7. Funahashi, K. (1989). On the approximate realization of continuous mappings by neural networks. Neural Networks, 2(3), 183–192. doi:10.1016/0893-6080(89)90003-8.
  8. Hassibi, B. and Stork, D. G. (1993). Second order derivatives for network pruning: optimal brain surgeon. In Advances in Neural Information Processing Systems 5, 164–171. Morgan Kaufmann.
  9. Hinton, G. E. and van Camp, D. (1993). Keeping the neural networks simple by minimizing the description length of the weights. In Proceedings of the Sixth Annual Conference on Computational Learning Theory, 5–13. doi:10.1145/168304.168306.
  10. Hornik, K. (1991). Approximation capabilities of multilayer feedforward networks. Neural Networks, 4(2), 251–257. doi:10.1016/0893-6080(91)90009-T.
  11. Hornik, K., Stinchcombe, M. and White, H. (1989). Multilayer feedforward networks are universal approximators. Neural Networks, 2(5), 359–366. doi:10.1016/0893-6080(89)90020-8.
  12. Kůrková, V. and Kainen, P. C. (1994). Functionally equivalent feedforward neural networks. Neural Computation, 6(3), 543–558. doi:10.1162/neco.1994.6.3.543.
  13. Le Cun, Y., Boser, B., Denker, J. S., Henderson, D., Howard, R. E., Hubbard, W. and Jackel, L. D. (1989). Backpropagation applied to handwritten zip code recognition. Neural Computation, 1(4), 541–551. doi:10.1162/neco.1989.1.4.541.
  14. LeCun, Y., Bottou, L., Bengio, Y. and Haffner, P. (1998). Gradient-based learning applied to document recognition. Proceedings of the IEEE, 86(11), 2278–2324. doi:10.1109/5.726791.
  15. Leen, T. K. (1995). From data distributions to regularization in invariant learning. Neural Computation, 7(5), 974–981. doi:10.1162/neco.1995.7.5.974.
  16. Levenberg, K. (1944). A method for the solution of certain non-linear problems in least squares. Quarterly of Applied Mathematics, 2(2), 164–168. doi:10.1090/qam/10666.
  17. MacKay, D. J. C. (1992a). A practical Bayesian framework for backpropagation networks. Neural Computation, 4(3), 448–472. doi:10.1162/neco.1992.4.3.448.
  18. MacKay, D. J. C. (1992b). The evidence framework applied to classification networks. Neural Computation, 4(5), 720–736. doi:10.1162/neco.1992.4.5.720.
  19. Marquardt, D. W. (1963). An algorithm for least-squares estimation of nonlinear parameters. Journal of the Society for Industrial and Applied Mathematics, 11(2), 431–441. doi:10.1137/0111030.
  20. Nowlan, S. J. and Hinton, G. E. (1992). Simplifying neural networks by soft weight-sharing. Neural Computation, 4(4), 473–493. doi:10.1162/neco.1992.4.4.473.
  21. Pearlmutter, B. A. (1994). Fast exact multiplication by the Hessian. Neural Computation, 6(1), 147–160. doi:10.1162/neco.1994.6.1.147.
  22. 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.
  23. Sietsma, J. and Dow, R. J. F. (1991). Creating artificial neural networks that generalize. Neural Networks, 4(1), 67–79. doi:10.1016/0893-6080(91)90033-2.
  24. Simard, P. Y., Steinkraus, D. and Platt, J. C. (2003). Best practices for convolutional neural networks applied to visual document analysis. In Seventh International Conference on Document Analysis and Recognition, 958–963. doi:10.1109/ICDAR.2003.1227801.
  25. Simard, P., Victorri, B., LeCun, Y. and Denker, J. (1992). Tangent prop: a formalism for specifying selected invariances in an adaptive network. In Advances in Neural Information Processing Systems 4, 895–903. Morgan Kaufmann.

DOIs were checked against Crossref. The numerical statements made in the text (backpropagation against central differences, the exact Hessian blocks, the \(\mathcal{R}\)-operator product, the sequential inverse Hessian and the transformed-data expansion) come from short Python computations written for these notes.