7 Neural Networks
On this page
Neural networks are a very versatile set of models to make predictions. In this section, we discuss how they work. We start with a network with a single neuron. We then stack neurons into layers and place multiple neurons side by side within each layer. We show how to use gradient descent of Section 5.1 to update the parameters of the network to fit the training data. We also discuss methods for designing and regularizing neural networks. How to engineer a good architecture is a hot topic in current research.
We remark that we only discuss how neural networks work, but not why. Their power can perhaps best be understood as an emergent phenomenon.1A simple example of an emergent phenomenon is the freezing of water. Clearly, one molecule of H2O is not water, but how many molecules are needed before it becomes a water droplet that can freeze? Similarly, one biological neuron cannot think. Put many together and we obtain the brain of a snail. Put a huge number together and we get a rat that can solve labyrinth puzzles, with yet more we have ’us’.
7.1 Single-neuron networks
The simplest neural network consists of a single neuron taking a single feature \(x\) as input and producing one prediction \(\hat y\). Fig. 7.1 shows the two steps. It does this by first computing the logit (5.1.3)
\begin{align*} z = ax+b, \end{align*}with the weight \(a\) and bias \(b\). Then it applies an activation function to obtain the output \(\hat y \in \Yset\). Throughout this chapter we use the sigmoid as an example,
\begin{align*} \hat y = \sigma(z), \qquad \sigma(z) = \rb{1+e^{-z}}^{-1}. \end{align*}The role of \(\sigma\) is to transform the unbounded logit \(z\in\R\) into a number between \(0\) and \(1\), so that \(\hat y\) can be interpreted as a probability. Thus, once \(a\) and \(b\) are known, prediction is just the calculation above. Note that the mechanism we describe below can be easily changed to other activation functions.

Figure 7.1: A single neuron computes a logit \(z\) from the input \(x\) and passes it through the activation function \(\sigma\) to obtain the response \(\hat y\).
For training we use the observations \((x,y)\) in the training set \(\Tset\). The goal is to solve the problem:
\begin{align*} \theta^{*} &\in \argmin_{\theta} \hat R_{\Tset}(a,b), & \theta &= (a,b), \tag{7.1.1} \end{align*}where the training risk is the average over the training samples,
\begin{align*} \hat R_{\Tset}(a,b) &= \frac{1}{|\Tset|}\sum_{(x,y)\in\Tset}\lscr(y, f_{\theta}(x)), & f_{\theta}(x)&=\hat y=\sigma(ax+b), \end{align*}and \(\lscr(y, \hat y)\) is the loss of the prediction \(\hat y\) on the single training sample \((x,y)\). Below we use squared-error loss
\begin{align*} \lscr(y,\hat y) = \frac{1}{2}\rb{y - \hat y}^{2}= \frac{1}{2}\rb{y - \sigma(ax+b)}^{2}. \end{align*}For hard labels \(y\in\set{0,1}\) and soft targets \(y\in[0,1]\), binary cross-entropy (5.1.5) is a natural choice for \(\lscr\).
In practice, we use the gradient descent tools of Section 5.1 to search for a parameter vector with low training risk. Because the objective may be nonconvex, gradient descent may reach a local minimum rather than a global one. The resulting parameter vector is then used for prediction: for a new predictor \(x\), the neuron returns \(\sigma(a^* x + b^*)\) as the prediction.
Regularization is important even for this single neuron network; however, these ideas apply more generally.
Early stopping uses a validation set to decide when to stop training. Split the available labeled data into a training set \(\Tset\) and a validation set \(\Vset\). The parameters are updated by gradient descent on \(\hat R_{\Tset}\) as before, but now, at each iteration \(k\), we store the validation risk
\begin{align*} \hat R_{\Vset}(\theta^{(k)}) = \frac{1}{|\Vset|}\sum_{(x,y)\in\Vset}\lscr(y, f_{\theta^{(k)}}(x)) \end{align*}together with the parameters \(\theta^{(k)}=(a^{(k)},b^{(k)})\). When the validation risk has failed to improve for a number of successive iterations, we stop iterating and use
\begin{align*} k^* \in \argmin_k \hat R_{\Vset}(\theta^{(k)}) \end{align*}to select \(\theta^{(k^*)}\) as the fitted parameter vector for prediction instead of the last parameter values obtained from training. This matters because the training risk can keep decreasing, while the validation risk can increase once the model begins to fit accidental details of \(\Tset\).
Weight decay copies the idea from ridge regression by including a penalty in the objective:
\begin{align*} \min_{a,b}\hat R_{\Tset,\lambda}(a,b), \qquad \hat R_{\Tset,\lambda}(a,b) = \hat R_{\Tset}(a,b) + \frac{\lambda}{2}a^2, \end{align*}where \(\lambda\geq 0\) is a tuning parameter.2In larger networks, the weight \(a\) is a matrix. Then we penalize the sum of the squared entries of the weight matrix, \(\norm{A}^2\), instead of just \(a^2\).
The bias \(b\) is often left out of the penalty. The reason is that \(a\) controls the steepness of \(\sigma(ax+b)\), while \(b\) controls where the steep part of the curve lies.3\(ax+b=0\) at \(x=-b/a\). Penalizing \(a\) discourages overly sharp changes in the fitted probabilities. Penalizing \(b\) would also discourage moving the steep part of the curve to where the data place it, which is usually not the intended effect.
The extra term in the objective contributes \(\lambda a\) to the derivative with respect to \(a\), so the update in gradient descent contains a small shrinkage toward zero. As \(\frac{\partial}{\partial a}\hat R_{\Tset,\lambda}(a,b)=\frac{\partial}{\partial a}\hat R_{\Tset}(a,b)+\lambda a\), the update for \(a\) is
\begin{align*} a^{(k+1)} &= a^{(k)} -\eta\left( \frac{\partial}{\partial a}\hat R_{\Tset}(a^{(k)},b^{(k)}) +\lambda a^{(k)} \right)\\ &= (1-\eta\lambda)a^{(k)} -\eta\frac{\partial}{\partial a}\hat R_{\Tset}(a^{(k)},b^{(k)}). \end{align*}The first term is a shrunken version of the old value \(a^{(k)}\), before the usual gradient of the training risk is added. For the decay term alone to reduce the magnitude of the old weight, we need \(|1-\eta\lambda|<1\). This condition by itself does not ensure convergence of the full update; gradient-descent convergence depends on the objective and step size, among other assumptions.4Wikipedia: Gradient descent.
7.2 Multi-neuron networks
A single neuron only gives shifted and stretched copies of one fixed nonlinearity, \(\sigma(ax+b)\). Neural networks become more flexible by combining many such transformations: some are placed in a sequence of layers, making the network deeper, and some are placed side by side, making a layer wider. Networks built from multiple layers, each of several neurons wide, are called multilayer perceptrons.
Making networks deeper and wider can improve prediction quality. Different hidden neurons can learn different useful patterns in the input, and later layers can combine patterns learned by earlier layers. However, if the network is made too large for the amount of data, the same flexibility can overfit by picking up accidental noise instead of structure.
We first study depth in the simplest case: a scalar stack with one neuron per layer. Then we make the network wider, and introduce more indices to cope with the extra complexity.
The simplest stack passes information from single neurons from one layer to the next, see Fig. 7.2. For instance, starting from the input \(x\), define
\begin{equation*} \begin{aligned} z_1 &= a_1x+b_1, & h_1 &= \phi_1(z_1),\\ z_2 &= a_2h_1+b_2, & h_2 &= \phi_2(z_2),\\ z_3 &= a_3h_2+b_3, & h_{3} &= \sigma(z_3), \\ \hat y &= h_{3}. \end{aligned} \tag{7.2.1} \end{equation*}Each \(a_i\) and \(b_i\), for \(i=1,2,3\), is a trainable parameter; \(\phi_1\) and \(\phi_2\) are activation functions, in the same role as \(\sigma\).5We discuss examples below. The intermediate values \(h_1\) and \(h_2\) are called hidden as the network only uses them internally. The \(z_i\) are the pre-activations; the last one, \(z_3\), is the logit. Since the signal flows only forward, from \(x\) through \(h_1\), \(h_2\) and \(h_{3}\) to \(\hat y\), without cycles or feedback, such a network is called feedforward. The value \(h_3\) of the last layer is not hidden, but we add it to simplify the derivations below considerably.

Figure 7.2: A scalar multilayer network.
This stack is richer than a single neuron because each layer transforms the signal once more, so a later layer works with features produced by the earlier layers rather than with the raw input.
Prediction is implemented as a forward pass. Given parameters \((a_i, b_i)\) for each neuron, compute for the predictor \(x\) the preactivations \(z_1, h_1, z_2, h_2, z_3\) in order of (7.2.1) and return \(\hat y=\sigma(z_3)\).
Training minimizes the training risk by (mini-batch) gradient descent, now over all six parameters \(\theta=(a_1,b_1,a_2,b_2,a_3,b_3)\). The new difficulty is computing the gradient. Backpropagation6Michael A. Nielsen, Neural Networks and Deep Learning is the systematic way to handle this: it is the chain rule, applied from the output back to the input.
First, apply a forward pass to one observation \((x,y)\) to compute the prediction \(f_\theta(x)=\hat y\), the loss \(\lscr(y, f_\theta(x))\), all pre-activations \(z_i\) and all hidden values \(h_i\). The computations below use these values.
Backpropagation starts at the output, that is, at the top of Fig. 7.2. Let
\begin{align*} \delta_{3} = \frac{\partial \lscr(y, h_{3})}{\partial h_3}. \tag{7.2.2} \end{align*}If we use square loss, \(\delta_{3} = (\hat y -y)\). As the parameters \(a_3\) and \(b_3\) enter the loss only through \(z_3=a_3h_2+b_3\), by the chain rule,
\begin{align*} \frac{\partial \lscr}{\partial a_3} &= \frac{\partial \lscr}{\partial h_{3}} \frac{\partial h_3}{\partial z_3} \frac{\partial z_3}{\partial a_3} = \delta_3 \sigma'(z_{3})h_2, & \frac{\partial \lscr}{\partial b_3} &= \frac{\partial \lscr}{\partial h_3} \frac{\partial h_3}{\partial z_3} \frac{\partial z_3}{\partial b_3} = \delta_3 \sigma'(z_{3}). \end{align*}Note that from the forward pass we have the numerical values of \(\delta_3\), \(\sigma'(z_3)\), and \(h_2\) at the given sample \((x,y)\). Thus, the RHSs are just numbers that we7That is, the computer. can compute.
Now step one layer back. From (7.2.1) and the chain rule8Another way is to consider \(\frac{\partial \lscr}{\partial z_2} = \frac{\partial \lscr}{\partial z_3} \frac{\partial z_3}{\partial z_2}\). In view of the residual connections we discuss below, this decomposition does not work well.
\begin{align*} \delta_2 &=\frac{\partial \lscr}{\partial h_2} = \frac{\partial \lscr}{\partial h_3} \frac{\partial h_3}{\partial h_2} = \delta_{3} \sigma'(z_3) a_{3} \tag{7.2.3} \end{align*}Since \(z_2=a_2h_1+b_2\), again by the chain rule,
\begin{align*} \frac{\partial \lscr}{\partial a_2} &= \frac{\partial \lscr}{\partial h_2} \frac{\partial h_2}{\partial z_2} \frac{\partial z_2}{\partial a_2} = \delta_2 \phi_2'(z_2) h_1, & \frac{\partial \lscr}{\partial b_2} &= \frac{\partial \lscr}{\partial h_2} \frac{\partial h_2}{\partial z_2} \frac{\partial z_2}{\partial b_2} = \delta_2 \phi_2'(z_2). \end{align*}One more step of the same kind yields
\begin{align*} \delta_1 &= \frac{\partial \lscr}{\partial h_2} \frac{\partial h_2}{\partial h_1} = \delta_2 \phi_2'(z_2) a_2, \tag{7.2.4} \end{align*}and by the same token
\begin{align*} \frac{\partial \lscr}{\partial a_1} &= \frac{\partial \lscr}{\partial h_1} \frac{\partial h_1}{\partial z_1} \frac{\partial z_1}{\partial a_1} = \delta_1 \phi_1'(z_1) x, & \frac{\partial \lscr}{\partial b_1} &= \frac{\partial \lscr}{\partial h_1} \frac{\partial h_1}{\partial z_1} \frac{\partial z_1}{\partial b_1} = \delta_1 \phi_1'(z_1). \end{align*}For the plain network above, note the pattern: One forward pass computes the \(z_i\) and hidden values \(h_i\), one backward pass computes each \(\delta_i\), and from these \(\partial_{a_i} \lscr\) and \(\partial_{b_i} \lscr\). Together they deliver the complete gradient \(\nabla_\theta\lscr(y, f_\theta(x))\) for the given sample \((x,y)\).
The parameter update is then the same as before, except that rather than updating from just one sample, we use a mini-batch \(\Bset\) to estimate the gradient with (5.1.6) and update the parameters through:
\begin{align*} \theta^{(k+1)} = \theta^{(k)} - \eta\, \nabla\hat R_{\Bset}(\theta^{(k)}) = \theta^{(k)} - \eta\, \frac{1}{|\Bset|} \sum_{(x,y) \in \Bset} \left.\nabla_{\theta} \lscr(y, f_{\theta}(x))\right|_{\theta=\theta^{(k)}}. \tag{7.2.5} \end{align*}For instance, for a mini-batch containing a single sample,
\begin{align*} a^{(k+1)}_{1} &= a^{(k)}_{1} - \eta \frac{\partial \lscr}{\partial a_1} = a^{(k)}_{1} - \eta \delta_1\phi_1'(z_1)x, & b^{(k+1)}_{1} &= b^{(k)}_{1} - \eta \frac{\partial \lscr}{\partial b_1} = b^{(k)}_{1} - \eta \delta_1\phi_1'(z_1). \end{align*}The other parameters follow in the same way.
Because the training risk of a neural network is not necessarily convex, two runs started from different initial parameter values may end at different local minima. A simple practical response is to train the same architecture from several random initializations and keep the run with the lowest validation risk.
Generalizing to networks with \(L\) layers, including the output layer, is straightforward. Thus there are \(L-1\) hidden layers.
Widening a layer places several neurons side by side in one layer, see Fig. 7.3. A neuron in a wide layer now receives several inputs, one from every neuron in the previous layer, so its weights carry three indices: \(A_i(j,k)\) is the weight that neuron \(j\) of layer \(i\) applies to its \(k\)-th input. Then hidden layer \(i\) computes the components of its pre-activation and hidden vector as
\begin{align*} z_i(j)&=\sum_{k=1}^{q_{i-1}} A_i(j,k)h_{i-1}(k) + b_i(j), & h_i(j) &= \phi_i(z_i(j)). \tag{7.2.6} \end{align*}Collecting the weights of layer \(i\) in the matrix \(A_i\in\R^{q_i\times q_{i-1}}\), whose \((j,k)\) entry is \(A_i(j,k)\), the hidden vector \(h_{i-1}\in \R^{q_{i-1}}\), and the biases in the vector \(b_i\in\R^{q_i}\), this becomes
\begin{align*} z_i &= A_i h_{i-1} + b_i, & h_i &=\phi_i(z_i), & i&=1,\ldots,L-1, \tag{7.2.7} \end{align*}where \(\phi_i\) is applied element-wise. The output is layer \(L\). It consists of one neuron, so \(q_L=1\), and it reads the last hidden layer,
\begin{align*} z_L &= A_Lh_{L-1} + b_L, & \hat y &= \sigma(z_L) \tag{7.2.8} \end{align*}with \(A_L\in\R^{1\times q_{L-1}}\) and \(b_L\in\R\). Thus the output layer has the same affine form as a hidden layer, with the output activation \(\sigma\).

Figure 7.3: A small multilayer perceptron.
Each layer has its own parameters \(A_i(j,k), b_i(j)\); thus
\begin{align*} \theta=(A_1,b_1,\ldots,A_L,b_L). \end{align*}Note how quickly \(\theta\) grows: layer \(l\) alone has \(q_l(q_{l-1}+1)\) parameters.
Including multiple features is now straightforward. When the input is a feature vector \(x\in\R^p\), just set \(h_0=x\) and \(q_0=p\), and all of (7.2.7) applies right away. Thus, moving from one to \(p\) features changes only the bookkeeping for the first layer; later layers are unaffected as they read only from the previous hidden layer.
Backpropagation carries over nearly unchanged. Again, there is one \(\delta\) per neuron, so \(\delta_i\in\R^{q_i}\) is a column vector, as are \(h_i\), \(z_i\) and \(b_i\). The output layer \(L\) has one neuron, so
\begin{align*} \delta_L = \frac{\partial \lscr}{\partial h_L}. \end{align*}The other backward recursions follow from the chain rule:
\begin{align*} \delta_i(j) &= \frac{\partial \lscr}{\partial h_i(j)} = \sum_{k}\frac{\partial \lscr}{\partial h_{i+1}(k)} \frac{\partial h_{i+1}(k)}{\partial h_i(j)} \\ &= \sum_{k} \delta_{i+1}(k) A_{i+1}(k,j) \phi_{i+1}'(z_{i+1}(k)), \end{align*}where we use (7.2.6) for the second partial derivative and \(h_{i+1}(k) = \phi_{i+1}(z_{i+1}(k))\) for the third. Now note that the summation over \(k\) is the \(j\)-th component of \(A_{i+1}^{\top}\rb{\delta_{i+1} \odot \phi_{i+1}'(z_{i+1})}\). With the Hadamard product9The Hadamard product, written as \(C = A \odot B\), of two matrices \(A\) and \(B\) with the same dimensions is the element-wise multiplication: \(C_{ij} = A_{ij} B_{ij}\). we can then write
\begin{align*} \delta_i = A_{i+1}^{\top}\rb{\delta_{i+1} \odot \phi_{i+1}'(z_{i+1})}, \qquad i = L-1,\ldots,1. \end{align*}Finally, the gradients of layer \(i=1,\ldots,L\) become
\begin{align*} \frac{\partial \lscr}{\partial A_i} &= \rb{\delta_i \odot \phi_i'(z_i)}\, h_{i-1}^{\top}, & \frac{\partial \lscr}{\partial b_i} &= \delta_i \odot \phi_i'(z_i), \end{align*}because
\begin{align*} \frac{\partial \lscr}{\partial A_i(j,k)} &= \frac{\partial \lscr}{\partial h_i(j)} \frac{\partial h_i(j)}{\partial z_i(j)} \frac{\partial z_i(j)}{\partial A_i(j,k)} = \delta_i(j)\phi_i'(z_i(j))h_{i-1}(k). \end{align*}These are the gradients for one observation. For a mini-batch, average each of these per-observation gradients over the batch before updating the parameters, just as in (7.2.5).
As an aside, in software these formulas need not be derived by hand: automatic differentiation records every elementary operation of the forward pass and applies the chain rule mechanically in reverse, which reproduces exactly the backpropagation computation above, for a network of any shape. This is what allows practitioners to change an architecture without redoing any calculus. Also, in real large networks, plain gradient descent is often replaced by specialized optimizers such as momentum methods, Adam, or AdamW.10Wikipedia: Stochastic gradient descent, Adam.
Multi-neuron networks can use other regularization techniques besides early stopping and weight decay.
Dropout is a technique that aims to prevent the network from relying too much on just a few neurons in a layer. The risk with \(q\) parallel neurons is that during training they can start to cooperate too closely so that just a few neurons in that layer become dominant, which in turn may lead to tuning that layer to accidental details of the training set. Dropout counters this by switching off neurons selected at random for each training sample, see Fig. 7.4. Like this, the network cannot rely on just a few neurons; in that sense, it should make networks more robust.

Figure 7.4: Dropout switches off hidden units during training.
Dropout is generally applied to each hidden layer separately; we discuss the details for one layer. Given the vector \(h_i\) of hidden layer \(i\), let \(\rho\in[0,1)\) be the dropout probability. For each sample in a mini-batch, independently draw a mask \(M_i=(M_i(1),\ldots,M_i(q_i))\) where
\begin{align*} M_i(j)\sim \Bern{1-\rho}, \quad j=1,\ldots,q_i \end{align*}are independent mask variables.11In deeper networks, use a separate mask for each dropout layer. Then, replace \(h_i\) by its randomly thinned version
\begin{align*} \tilde h_i = \frac{M_i\odot h_i}{1-\rho}, \end{align*}and use \(\tilde h_i\) instead of \(h_i\) in (7.2.7). Thus, each sample is processed by a differently thinned network. Since the model cannot rely on the same hidden units always being present, no single neuron can play too dominant a role.
The division by \(1-\rho\) is called inverted dropout. It keeps \(\E{\tilde h_i(j)}=h_i(j)\), so that in expectation, the activation remains equally strong during each training step.
Dropout is only applied during training. Once trained, the full network is used for prediction.
DropConnect is the same idea applied to individual weights instead of hidden units. For the weight matrix \(A_i\), take a mask matrix \(B_i\) of the same shape, with independent entries
\begin{align*} B_i(j,k)\sim \Bern{1-\rho}. \end{align*}For each sample in a mini-batch, independently draw a mask matrix \(B_i\), and replace \(A_i\) by its randomly thinned version
\begin{align*} \tilde A_i = \frac{B_i\odot A_i}{1-\rho}. \end{align*}By (7.2.6), the entry \(A_i(j,k)\) is the weight of the connection from neuron \(k\) in layer \(i-1\) to neuron \(j\) in layer \(i\). If \(B_i(j,k)=0\), the term \(A_i(j,k)h_{i-1}(k)\) drops out of \(z_i(j)\), so neuron \(j\) does not receive the output of neuron \(k\) in this update, while the other neurons of layer \(i\) still do. The weight itself is kept; a new mask is drawn for each sample. Dropout of \(h_{i-1}(k)\), in contrast, removes all outgoing connections of neuron \(k\) at once, which is the same as setting the whole column \(k\) of \(A_i\) to zero. As with dropout, the random masking is only used during training; once trained, the full weight matrix is used for prediction.

Figure 7.5: Freezing one layer keeps its weights fixed while the rest of the network trains.
Freezing12Wikipedia: Fine-tuning (deep learning). keeps selected parameters fixed during training, as in Fig. 7.5. The frozen neurons still compute their values in the forward pass, but their parameters are excluded from the gradient updates. This is a deliberate restriction on what the optimizer is allowed to change.
Freezing is a useful technique to enable transfer learning13Freezing is not the same as transfer learning; it is one common tool used when reusing a pretrained network for a new task.. Transfer learning is to reuse part of a network trained on one task for a related new task. A typical workflow is:
- Start from a network already trained on a large source data set.
- Keep its lower layers, because they often capture generic features.
- Add (or replace) an output layer for the new task.
- Train only this last layer.
- Optionally unfreeze some of the lower layers and continue training with small learning rate. This is known as fine-tuning.
Thus, by freezing the lower layers we can save computation and protect against overfitting when the new data set is small.
7.3 Further architectural aspects
Besides specifying the number and width of the layers, we must consider other aspects of a neural network: which activation functions to use, how many outputs the network should produce, and how information should flow14That is, how the neurons and the layers are connected. between distant parts of the network.
A first question is what a given architecture can represent at all. The universal approximation theorem15Wikipedia: Universal approximation theorem. gives an interesting answer: a one-hidden-layer network suffices, provided it is sufficiently wide, in the following sense. Take a network with one hidden layer (so \(L=2\)) and an identity output activation; then the network computes
\begin{align*} \hat f(x) = \sum_{j=1}^{q_1} A_2(1,j)\, \sigma\rb{\sum_{k=1}^{p} A_1(j,k)\,x(k) + b_1(j)} + b_2, \end{align*}i.e., a weighted sum of \(q_1\) shifted and stretched copies of the sigmoid function. The theorem says that for every continuous \(f\) on a compact domain and every \(\epsilon>0\) there are a width \(q_1\) and parameters \(A_1,b_1,A_2,b_2\) such that \(|\hat f(x)-f(x)|<\epsilon\) for all \(x\) in that domain. Thus, one hidden layer suffices, provided it is wide enough.
This is an existence result, not a guarantee about training. So the interesting question is not whether an architecture can represent a function, but which architectures reach a given accuracy with few parameters and can still be trained; this is the subject of current research.
Activation functions need not be sigmoid. Another common hidden-layer choice is the ReLU,16Wikipedia: Rectified linear unit.
\begin{align*} \phi(z)&=\max\set{0,z} & \phi'(0) &=0. \end{align*}This is useful because an active17That is, when \(z>0\). ReLU has derivative \(1\): it passes the gradient backward without shrinking it. A sigmoid hidden unit, by contrast, has derivative at most18\(\sigma'(z)=\sigma(z)\rb{1-\sigma(z)}\leq 1/4\) \(1/4\) and becomes almost flat for large positive or negative \(z\) which leads to the following problem. Recall the backpropagation recursion applied to the simple three-layer feedforward network above. For instance, the derivation in (7.2.2)--(7.2.4) shows that
\begin{align*} \delta_{1} = a_{2} a_{3}\phi_{2}'(z_{2}) \frac{\partial \lscr}{\partial \hat y} \sigma'(z_{3}). \tag{7.3.1} \end{align*}Each extra layer contributes one more weight and one more slope. So in a stack of, say, \(15\) sigmoid layers the slopes alone contribute a factor of at most \((1/4)^{15}\approx 10^{-9}\). Thus, when using sigmoids throughout, the the small activation derivatives can strongly attenuate gradients through many layers unless the weights compensate, leaving lower layers to learn slowly. This phenomenon is known as vanishing gradients.
ReLU does not suffer from this problem because its derivative is either 0 or 1.19The probability that a preactivation hits 0 is negligible.
A smooth variant of the ReLU is the GELU,20The Gaussian error linear unit, Wikipedia: Rectified linear unit, GELU. It is used in GPT-style language models.
\begin{align*} \phi(z)=z\,\P{Z\leq z},\qquad Z\sim\Norm{0,1}. \end{align*}For large positive \(z\) it behaves like the identity and for large negative \(z\) it tends to \(0\), as the ReLU does. It differs near the origin: the GELU is negative for every \(z<0\), with a minimum of about \(-0.17\) at \(z\approx-0.75\), so unlike the ReLU it is not monotone.
Output heads21Do not confuse these output heads with the attention heads used inside Transformer layers of large language models. are final output neurons, or final small output layers, for separate prediction tasks, see Fig. 7.6. So far, the network had just one output head: one \(\hat y\) for the input \(x\). However, often we can ask several related questions about the same input. For instance, does a patient have illness A and illness B? Each such question can be answered by its own head but, as Fig. 7.6 shows, all heads share the last hidden layer, \(h_{L-1}\).
For \(K\) such questions, take as logits and outputs
\begin{align*} z^{(r)} = b^{(r)}_{0} + \sum_{j=1}^{q_{L-1}} a^{(r)}(j) h_{L-1}(j), \qquad \hat y^{(r)} = \sigma(z^{(r)}), \qquad r=1,\ldots,K. \end{align*}Thus, each head has its own training parameters.

Figure 7.6: Each head is a separate output neuron reading the same hidden layer.
For training, this means that, instead of samples with an outcome \(y\), each observation has multiple targets \(y^{(1)},\ldots,y^{(K)}\), one for each supervised head. Training changes little: take as loss the sum of the \(K\) per-head losses and apply backpropagation as before.
Sharing information from lower layers is efficient: the features \(h_{L-1}(j)\) are learned once but used \(K\) times. Training on multiple labels per input is known as multi-task learning. Because the heads share the hidden layers, the gradient of the summed loss combines contributions from all tasks when updating those layers. If tasks share useful structure, this can encourage a representation that works across tasks and may improve generalization. If their requirements conflict, however, they can interfere.
Skip connections become important in deep networks. A skip connection lets a layer pass its input directly to a later layer. A residual connection22Wikipedia: Residual neural network. is the additive case of a skip connection: the skipped input and the learned update are merged by addition, not by a more general function. The formula below assumes consecutive hidden layers have the same width, so the addition is dimensionally valid; if their widths differ, the skip path needs a projection. So, instead of setting \(h_i = \phi_i(z_i)\) as in (7.2.1), the hidden values are given by
\begin{align*} h_i = h_{i-1} + \phi_i(z_i), \qquad z_i = A_i h_{i-1}+b_i, \end{align*}equivalently, \(\Delta h_i := h_i-h_{i-1} = \phi_i(z_i)\). Thus, in the forward pass, layer \(i\) adds the update \(\Delta h_i\) to the previous hidden vector.23This is close to an Euler step for a differential equation, where a new state is the old state plus a small change.

Figure 7.7: A residual connection sends a layer input directly to an addition node, where it is added to the layer update.
The effect of this change on training appears when we differentiate it: the derivative through the layer is
\begin{align*} \frac{\partial h_i}{\partial h_{i-1}} = I + \diag(\phi_i'(z_i))A_i, \end{align*}and the identity matrix \(I\) provides a direct path for the gradient back to earlier layers. This can help mitigate vanishing gradients, though it does not guarantee that gradients remain large at every layer, as they can still shrink or grow through the learned part of the network.
Residual connections help make networks of dozens or even hundreds of layers trainable. The first famous example was ResNet, an image-recognition network with residual blocks and over a hundred layers; the transformer blocks of large language models also wrap each sublayer in a residual connection.24Wikipedia: Residual neural network. Wikipedia: Transformer (deep learning architecture). Wikipedia: GPT-3. They also make each layer’s job easier: the layer only needs to learn a small correction to the identity map, not the whole transformation.
A skip connection need not skip just one layer. Examples include ResNet blocks, long U-Net connections, and DenseNet-style dense connections.25Wikipedia: Residual neural network, which also covers DenseNet. Wikipedia: U-Net.
7.4 Exercises
Conceptual 1.
What is a skip connection? Why are skip connections used in deep neural networks?
Solution
See the main text. A skip connection passes a layer’s input directly to a later layer. They give information and gradients a direct path through the network, making deep networks easier to train.
Conceptual 2.
What is dropout, and why is it used?
Solution
Dropout randomly switches off neuron outputs during training. This reduces reliance on particular neurons and helps prevent overfitting.
Conceptual 3.
For a neural network:
- What is DropConnect?
- How does DropConnect differ from dropout?
Solution
- DropConnect sets randomly chosen single weights \(A_i(j,k)\), each the connection from neuron \(k\) in layer \(i-1\) to neuron \(j\) in layer \(i\), to zero for one update.
- Dropout sets the output \(h_{i-1}(k)\) of a neuron to zero, which removes all outgoing connections of that neuron at once: the whole column \(k\) of \(A_i\). DropConnect instead removes individual connections.
Conceptual 4.
- What does it mean to freeze a layer?
- Why can freezing be useful in transfer learning?
Solution
- Freezing a layer keeps its parameters fixed during training: the layer still computes its outputs in the forward pass, but its parameters get no gradient updates.
- In transfer learning, the lower layers of a network trained on a large source data set are frozen and only the new output layer is trained. This reuses the features the lower layers learned, saves computation because no updates are computed for the frozen parameters, and reduces overfitting when the new data set is small, because fewer parameters are fitted to it. Afterwards some lower layers may be unfrozen for fine-tuning.
Conceptual 5.
What is early stopping, and how does it help prevent overfitting?
Solution
Early stopping ends training when validation risk stops improving, before later updates overfit the training data.
Conceptual 6.
What is weight decay, and what effect does it have on the fitted weights?
Solution
Weight decay adds a penalty proportional to the squared size of the weights to the objective, encouraging smaller weights.
Conceptual 7.
What is the vanishing-gradient problem?
Solution
Gradients can become very small when propagated backwards through many layers, so early layers learn extremely slowly.
Conceptual 8.
What is the difference between a residual connection and a general skip connection?
Solution
A residual connection is an additive skip connection: the input is added to the learned update. A general skip connection may combine them in another way.
Conceptual 9.
What is inverted dropout? Why use it?
Solution
During training, the surviving activations are divided by \(1-\rho\): \(\tilde h_i(j) = M_i(j)h_i(j)/(1-\rho)\) with \(M_i(j)\sim\Bern{1-\rho}\). For a given input and given parameters, \(h_i(j)\) is a fixed number and the mask is the only random quantity, so
\begin{align*} \E{\tilde h_i(j)} = \frac{\E{M_i(j)}\,h_i(j)}{1-\rho} = \frac{(1-\rho)\,h_i(j)}{1-\rho} = h_i(j). \end{align*}The pre-activation \(z_{i+1} = A_{i+1}\tilde h_i + b_{i+1}\) of the next layer is linear in \(\tilde h_i\), so its expectation is \(A_{i+1}h_i + b_{i+1}\), the value it takes in the full network. Hence the full network can be used at prediction time as it is. Without the division, \(\E{\tilde h_i(j)} = (1-\rho)h_i(j)\), and the next layer would see inputs \(1/(1-\rho)\) times larger at prediction time than on average during training; the weights \(A_{i+1}\) would then have to be multiplied by \(1-\rho\) before prediction. The match holds for the pre-activation only; after the nonlinearity \(\phi_i\), the expected output under dropout differs in general from the output of the full network.
Conceptual 10.
What is transfer learning? What is fine-tuning?
Solution
Transfer learning reuses part of a network trained on one task for a related new task. Typically, the lower layers of a network trained on a large source data set are kept and frozen, the output layer is replaced by one for the new task, and only this new layer is trained. Fine-tuning is the optional next step: some of the frozen lower layers are unfrozen and training continues with a small learning rate, so that these layers also adapt to the new task.
Conceptual 11.
What is a multilayer perceptron?
Solution
A multilayer perceptron is a network of \(L\) learned layers, consisting of \(L-1\) hidden layers followed by an output layer, where each layer consists of several neurons side by side. Each neuron of layer \(i\) receives the outputs of all neurons of layer \(i-1\), starting from the input \(h_0 = x\). Layer \(i\) computes \(h_i = \phi_i(A_i h_{i-1} + b_i)\), with weight matrix \(A_i\), bias vector \(b_i\) and activation \(\phi_i\) applied element-wise (7.2.7). The output layer has the same form with the output activation \(\sigma\) (7.2.8).
Conceptual 12.
According to the universal approximation theorem discussed in this chapter: a. How many hidden layers suffice to approximate any continuous real-valued function on a compact domain to arbitrary accuracy, provided the hidden layers can be sufficiently wide? State the hidden and output activations used in the theorem. Would a network with no hidden layer and the identity output activation give the same guarantee? Explain. b. Does the theorem guarantee that gradient descent will find suitable parameters, or that a network trained on finitely many samples will generalize well?
Solution
a. One hidden layer with sigmoid activations and an identity output activation suffices. Its width may depend on the target function and the desired accuracy. With no hidden layer and an identity output, the network is just an affine function of its input, which cannot approximate arbitrary continuous functions to arbitrary accuracy. Thus one hidden layer is the minimum for this general guarantee, though particular target functions may need none. This counts hidden layers only, not the output layer. b. No. The theorem guarantees the existence of suitable parameters and a sufficiently large width. It does not guarantee that gradient descent finds those parameters, or that training on finitely many samples produces accurate predictions on unseen inputs.
Conceptual 13.
A neural network has shared hidden layers and two separate output heads, each predicting a different target for the same input. Training minimizes the sum of the two heads’ losses. a. Which parameters are shared between the two prediction tasks, and which are specific to each task? b. How does each head’s loss contribute to the gradients for the shared parameters and for the parameters of each output head?
Solution
a. The weights and biases of the hidden layers are shared. Each output head has its own weights and bias, connecting the shared representation to its own prediction. b. For a shared parameter, the gradient of the total loss is the sum of the gradients from the two heads’ losses. Both tasks therefore influence the learned representation. A parameter belonging to one output head receives a gradient only from that head’s loss, since the other head’s prediction does not depend on it.
Conceptual 14.
Take the two-layer, single-neuron network with a ReLU hidden activation and the identity as output activation,
\begin{align*} z_1=a_1x+b_1,\qquad h_1=\text{ReLU}(z_1),\qquad z_2=a_2h_1+b_2,\qquad h_2=\hat y=z_2, \end{align*}and squared loss \(\lscr=(y-\hat y)^{2}/2\). Let \(\delta_i=\partial\lscr/\partial h_i\), with \(h_2=\hat y\). Backpropagation gives
\begin{align*} \delta_2 &= (\hat y-y), & \frac{\partial\lscr}{\partial a_2} &= \delta_2 h_1, & \frac{\partial\lscr}{\partial b_2} &= \delta_2,\\ \delta_1 &= \delta_2 a_2, & \frac{\partial\lscr}{\partial a_1} &= \delta_1\1{z_1>0}x, & \frac{\partial\lscr}{\partial b_1} &= \delta_1\1{z_1>0}. \end{align*}- Explain why a sample with \(z_1\leq 0\) can only change \(b_2\).
- Why is this a desirable effect, provided the training set contains samples in the neuron’s active region?
Solution
- If \(z_1\leq 0\), then \(h_1=0\) and \(\1{z_1>0}=0\). Thus \(\partial\lscr/\partial a_1=\delta_1\1{z_1>0}x=0\) and \(\partial\lscr/\partial b_1=\delta_1\1{z_1>0}=0\), while \(\partial\lscr/\partial a_2=\delta_2h_1=0\); only \(\partial\lscr/\partial b_2=\delta_2\) can differ from zero. For \(z_1<0\), sufficiently small changes of \(a_1\), \(b_1\) or \(a_2\) leave \(\hat y=b_2\) unchanged, so the loss is locally independent of these parameters. At \(z_1=0\), we use the convention \(\text{ReLU}'(0)=0\). If \(\hat y=y\), even the gradient for \(b_2\) is zero.
- The network predicts \(\hat y = a_2(a_1x+b_1)+b_2\) on the region where \(a_1x+b_1>0\), and the constant \(\hat y=b_2\) elsewhere. A sample outside the active region can only adjust \(b_2\); it does not pull on \(a_1\), \(b_1\) and \(a_2\), which shape the prediction inside the active region. So these parameters are fitted by the samples on which they have an effect. In a wide layer each hidden neuron has its own active region, and a sample updates only the neurons that are active at it, so different neurons can fit different parts of \(\Xset\). The condition on the training set is needed because a neuron learns only from the samples in its active region: if the region contains no samples, its parameters are never updated, which is the dead ReLU.
Conceptual 15.
What is a dead ReLU? Can a hidden neuron die in the same way when it uses the sigmoid or the GELU as activation?
Solution
A ReLU neuron is dead when its pre-activation satisfies \(z\leq 0\) for every sample in \(\Tset\). Then its output is \(0\) and its slope \(\text{ReLU}'(z)=0\) at every sample, so the gradient with respect to its incoming weights and bias vanishes. With fixed inputs to the neuron, ordinary gradient descent cannot change its incoming weights or bias, so it remains inactive. In a deeper layer, however, updates to earlier layers can change its inputs and potentially reactivate it.
With the sigmoid this cannot happen exactly, because \(\sigma'(z)=\sigma(z)\rb{1-\sigma(z)}>0\) for every \(z\). The neuron can, however, saturate: if \(\abs{z}\) is large for every sample, \(\sigma'(z)\) is close to \(0\) and the updates are very small, so the neuron learns very slowly.
The GELU has derivative
\begin{align*} \phi'(z) = \P{Z\leq z} + \frac{z}{\sqrt{2\pi}}e^{-z^{2}/2}, \end{align*}which is zero only at the minimum \(z\approx-0.75\). Unless every sample has its pre-activation exactly at this point, the activation derivative does not force the gradient to vanish for every sample. Other factors in the chain rule can still make the gradient zero. For large negative \(z\), however, \(\phi'(z)\) tends to \(0\), and a GELU neuron with \(z\) far below \(0\) for every sample learns very slowly, as a saturated sigmoid does.
Technical 16.
Carry out backpropagation for the two-layer, single-neuron network
\begin{align*} z_1=a_1x+b_1,\qquad h_1=\phi_1(z_1),\qquad z_2=a_2h_1+b_2,\qquad \hat y=\sigma(z_2), \end{align*}with squared loss \(\lscr=(y-\hat y)^2/2\).
Solution
Apply the chain rule from the output backwards, with \(\delta_i=\partial\lscr/\partial h_i\), where \(h_2=\hat y\). The result is
\begin{align*} \delta_2 &= (\hat y-y), & \frac{\partial\lscr}{\partial a_2} &= \delta_2\sigma'(z_2) h_1, & \frac{\partial\lscr}{\partial b_2} &= \delta_2\sigma'(z_2),\\ \delta_1 &= \delta_2 a_2\sigma'(z_2), & \frac{\partial\lscr}{\partial a_1} &= \delta_1\phi_1'(z_1)x, & \frac{\partial\lscr}{\partial b_1} &= \delta_1\phi_1'(z_1). \end{align*}Technical 17.
Carry out backpropagation for the two-layer, single-neuron network
\begin{align*} z_1=a_1x+b_1,\qquad h_1=\phi_1(z_1),\qquad z_2=a_2h_1+b_2,\qquad \hat y=\sigma(z_2), \end{align*}with the binary cross-entropy
\begin{align*} \lscr = -y\log\hat y-(1-y)\log(1-\hat y). \end{align*}Solution
Let \(h_2=\hat y\) and \(\delta_i=\partial\lscr/\partial h_i\). Differentiating the loss with respect to \(\hat y\),
\begin{align*} \frac{\partial\lscr}{\partial\hat y} = -\frac{y}{\hat y}+\frac{1-y}{1-\hat y} = \frac{\hat y-y}{\hat y(1-\hat y)}. \end{align*}Since \(\sigma'(z_2)=\hat y(1-\hat y)\), the factors cancel in the parameter gradients for the output layer:
\begin{align*} \delta_2 &= \frac{\partial\lscr}{\partial\hat y},\\ \frac{\partial\lscr}{\partial a_2} &= \delta_2\sigma'(z_2)h_1=(\hat y-y)h_1, & \frac{\partial\lscr}{\partial b_2} &= \delta_2\sigma'(z_2)=\hat y-y,\\ \delta_1 &= \delta_2 a_2\sigma'(z_2)=(\hat y-y)a_2, & \frac{\partial\lscr}{\partial a_1} &= \delta_1\phi_1'(z_1)x, & \frac{\partial\lscr}{\partial b_1} &= \delta_1\phi_1'(z_1). \end{align*}Technical 18.
Take the two-layer, single-neuron network
\begin{align*} z_1=a_1x+b_1,\qquad h_1=\phi_1(z_1),\qquad z_2=a_2h_1+b_2,\qquad \hat y=\sigma(z_2), \end{align*}with the loss \(\lscr=(y-\hat y)^{n}/2\) for an even integer \(n\geq 2\). a. Why must \(n\) be even? b. Derive the partial derivatives of \(\lscr\) with respect to \(a_1,b_1,a_2,b_2\) with the chain rule. Write each one as a product of local derivatives before simplifying. c. Which factors in these derivatives depend on \(n\)?
Solution
a. For even \(n\), \((y-\hat y)^{n}/2\geq 0\), with equality only if \(\hat y=y\), and a prediction that is too high by some amount gets the same loss as one that is too low by that amount. For odd \(n\) the loss is negative when \(\hat y>y\), so minimizing it would push \(\hat y\) as far above \(y\) as possible. b. The forward pass gives the numbers \(z_1,h_1,z_2,\hat y\). The local derivatives are
\begin{align*} \frac{\partial\lscr}{\partial\hat y} &= -\frac{n}{2}(y-\hat y)^{n-1} = \frac{n}{2}(\hat y-y)^{n-1}, & \frac{\partial\hat y}{\partial z_2} &= \sigma'(z_2), & \frac{\partial z_2}{\partial h_1} &= a_2, & \frac{\partial h_1}{\partial z_1} &= \phi_1'(z_1), \end{align*}where the sign flips because \(n-1\) is odd, together with \(\partial z_2/\partial a_2 = h_1\), \(\partial z_2/\partial b_2 = 1\), \(\partial z_1/\partial a_1 = x\) and \(\partial z_1/\partial b_1 = 1\). Starting at the output, let \(\delta_i=\partial\lscr/\partial h_i\), with \(h_2=\hat y\):
\begin{align*} \delta_2 &= \frac{\partial\lscr}{\partial\hat y} = \frac{n}{2}(\hat y-y)^{n-1}, \\ \frac{\partial\lscr}{\partial a_2} &= \delta_2\frac{\partial\hat y}{\partial z_2} \frac{\partial z_2}{\partial a_2} = \delta_2\sigma'(z_2)h_1, \qquad \frac{\partial\lscr}{\partial b_2} &= \delta_2\frac{\partial\hat y}{\partial z_2} \frac{\partial z_2}{\partial b_2} = \delta_2\sigma'(z_2), \\ \delta_1 &= \frac{\partial\lscr}{\partial h_1} = \delta_2\frac{\partial\hat y}{\partial z_2} \frac{\partial z_2}{\partial h_1} = \delta_2\, a_2\, \sigma'(z_2), \\ \frac{\partial\lscr}{\partial a_1} &= \delta_1\frac{\partial h_1}{\partial z_1} \frac{\partial z_1}{\partial a_1} = \delta_1\phi_1'(z_1)x, \qquad \frac{\partial\lscr}{\partial b_1} &= \delta_1\frac{\partial h_1}{\partial z_1} \frac{\partial z_1}{\partial b_1} = \delta_1\phi_1'(z_1). \end{align*}c. Only \(\partial\lscr/\partial\hat y\), which is \(\delta_2\), depends on \(n\). All other factors, and the step from \(\delta_2\) to \(\delta_1\), do not depend on the loss. For \(n=2\), \(\delta_2 = (\hat y-y)\), as in the squared-loss exercise above.
Technical 19.
Add a skip connection to a network of three single neurons:
\begin{align*} z_1&=a_1x+b_1, & h_1&=\phi_1(z_1), \\ z_2&=a_2h_1+b_2, & h_2&=h_1+\phi_2(z_2), \\ z_3&=a_3h_2+b_3, & \hat y&=\sigma(z_3), \end{align*}with squared loss \(\lscr=(y-\hat y)^{2}/2\). a. Derive the partial derivatives of \(\lscr\) with respect to all six parameters. b. Compare \(\delta_1=\partial\lscr/\partial h_1\) with its value in the same network without the skip connection, \(h_2=\phi_2(z_2)\). What happens to the updates of \(a_1\) and \(b_1\) when \(\phi_2'(z_2)\) is close to \(0\)?
Solution
a. Let \(h_3=\hat y\) and \(\delta_i=\partial\lscr/\partial h_i\). At the output, as before,
\begin{align*} \delta_3 &= (\hat y-y), & \frac{\partial\lscr}{\partial a_3} &= \delta_3\sigma'(z_3) h_2, & \frac{\partial\lscr}{\partial b_3} &= \delta_3\sigma'(z_3). \end{align*}The loss depends on \(h_2\) only through \(z_3\), so \(\partial\lscr/\partial h_2 = \delta_3\sigma'(z_3)a_3\), and
\begin{align*} \delta_2 &= \frac{\partial\lscr}{\partial h_2}, & \frac{\partial\lscr}{\partial a_2} &= \delta_2\phi_2'(z_2) h_1, & \frac{\partial\lscr}{\partial b_2} &= \delta_2\phi_2'(z_2). \end{align*}Now \(h_1\) reaches \(h_2\) along two paths: directly through the skip connection, and through \(z_2\). Hence \(\partial h_2/\partial h_1 = 1 + \phi_2'(z_2)\,a_2\), and
\begin{align*} \delta_1=\frac{\partial\lscr}{\partial h_1} = \delta_2\rb{1 + a_2\phi_2'(z_2)} = \delta_2 + \delta_2\phi_2'(z_2)a_2. \end{align*}Finally
\begin{align*} \frac{\partial\lscr}{\partial a_1} &= \delta_1\phi_1'(z_1)x, & \frac{\partial\lscr}{\partial b_1} &= \delta_1\phi_1'(z_1). \end{align*}b. Without the skip connection, \(\partial h_2/\partial h_1 = a_2\phi_2'(z_2)\) and \(\delta_1 = \delta_2 a_2\phi_2'(z_2)\). If \(\phi_2'(z_2)\approx 0\), for instance for a ReLU with \(z_2<0\) or a saturated sigmoid, then \(\delta_1\approx 0\) and the gradients for \(a_1\) and \(b_1\) vanish. With the skip connection, \(\delta_1 \approx \delta_2\): the term that comes through the direct path does not contain \(\phi_2'(z_2)\). If the other factors are nonzero, this allows the first layer to update.
Technical 20.
Take a chain of \(L-1\) single hidden neurons and one output neuron,
\begin{align*} h_0 &= x, & z_i &= a_i h_{i-1} + b_i, & h_i &= \phi_i(z_i), \qquad i=1,\ldots,L-1, \\ & & z_L &= a_Lh_{L-1} + b_L, & \hat y &= \sigma(z_L), \end{align*}with a differentiable loss \(\lscr(y,\hat y)\), let \(h_L=\hat y\) and write \(\delta_i=\partial\lscr/\partial h_i\). a. Show that \(\delta_i = \delta_{i+1}\,a_{i+1}\,\phi_{i+1}'(z_{i+1})\) for \(i=L-1,\ldots,1\), where \(\phi_L=\sigma\), and that \(\partial\lscr/\partial a_i=\delta_i\phi_i'(z_i)h_{i-1}\) and \(\partial\lscr/\partial b_i=\delta_i\phi_i'(z_i)\). b. Which values of the forward pass must be stored to compute all partial derivatives? c. Given \(\delta_L\), how many multiplications does it take to compute \(\delta_{L-1},\ldots,\delta_1\) with the recursion of part a? How many if each \(\delta_i\) is computed separately as a product of local derivatives from the output down to \(h_i\)?
Solution
a. The loss depends on \(h_i\) only through \(z_{i+1}\), and then through \(h_{i+1}\). By the chain rule,
\begin{align*} \delta_i = \frac{\partial\lscr}{\partial h_{i+1}} \frac{\partial h_{i+1}}{\partial z_{i+1}} \frac{\partial z_{i+1}}{\partial h_i} = \delta_{i+1}\,\phi_{i+1}'(z_{i+1})a_{i+1}. \end{align*}Since \(a_i\) and \(b_i\) enter only through \(z_i=a_ih_{i-1}+b_i\), \(\partial\lscr/\partial a_i = \delta_i\phi_i'(z_i)h_{i-1}\) and \(\partial\lscr/\partial b_i = \delta_i\phi_i'(z_i)\). b. The hidden values \(h_0=x,h_1,\ldots,h_{L-1}\), for the factors \(h_{i-1}\); the pre-activations \(z_1,\ldots,z_L\), for \(\phi_i'(z_i)\) and \(\sigma'(z_L)\); and \(\hat y\), for \(\partial\lscr/\partial\hat y\). c. Each step of the recursion multiplies \(\delta_{i+1}\) by \(a_{i+1}\) and by \(\phi_{i+1}'(z_{i+1})\), so \(2(L-1)\) multiplications in total. Computed separately,
\begin{align*} \delta_i = \delta_L\prod_{m=i+1}^{L} a_m\,\phi_m'(z_m) \end{align*}takes \(2(L-i)\) multiplications, so in total \(\sum_{i=1}^{L-1}2(L-i) = L(L-1)\). For \(L=10\) this is \(18\) against \(90\). The recursion needs fewer because \(\delta_{i+1}\) already contains the product of all factors above layer \(i\).
Technical 21.
Carry out backpropagation for the two-layer, single-neuron network with a ReLU hidden activation and the identity as output activation, \(\phi_2(z)=z\) and squared loss, so that
\begin{align*} z_1=a_1x+b_1,\qquad h_1=\text{ReLU}(z_1),\qquad z_2=a_2h_1+b_2,\qquad h_2=\hat y=z_2. \end{align*}For a sample with \(z_1\leq 0\), which parameter gradients must vanish, and which can be nonzero? What changes when \(z_1>0\)?
At initialization, what should you check about \(a_1x+b_1\) over the training samples to ensure the hidden neuron is not already inactive on every sample?
Solution
Since \(\phi_2\) is the identity, \(\partial\hat y/\partial z_2=1\), and
\begin{align*} \delta_2 &= (\hat y-y), & \frac{\partial\lscr}{\partial a_2} &= \delta_2 h_1, & \frac{\partial\lscr}{\partial b_2} &= \delta_2,\\ \delta_1 &= \delta_2 a_2, & \frac{\partial\lscr}{\partial a_1} &= \delta_1\1{z_1>0}x, & \frac{\partial\lscr}{\partial b_1} &= \delta_1\1{z_1>0}. \end{align*}For \(\delta_1\), the loss depends on \(h_1\) only through \(z_2\), so
\begin{align*} \delta_1 = \frac{\partial\lscr}{\partial h_2} \frac{\partial h_2}{\partial z_2} \frac{\partial z_2}{\partial h_1} = \delta_2 a_2. \end{align*}Since \(\text{ReLU}(z)=\max\set{0,z}\) equals \(0\) for \(z<0\) and \(z\) for \(z>0\), its derivative is \(0\) for \(z<0\) and \(1\) for \(z>0\), that is, \(\text{ReLU}'(z_1)=\1{z_1>0}\). The derivative is not defined at \(z_1=0\); the convention \(\text{ReLU}'(0)=0\) settles that case.
At a sample with \(z_1\leq 0\) we have \(h_1=0\) and \(\text{ReLU}'(z_1)=0\), so \(\partial\lscr/\partial a_1=\partial\lscr/\partial b_1= \partial\lscr/\partial a_2=0\). Only the gradient for \(b_2\) can be nonzero. For \(z_1>0\), the ReLU no longer forces any of these gradients to vanish: \(h_1=z_1>0\), \(\delta_1=(\hat y-y)a_2\), and the hidden activation derivative is \(1\). Individual gradients may still be zero because of other factors; for example, all vanish if \(\hat y=y\).
Check that at least one training sample has \(a_1x+b_1>0\) at initialization. Otherwise, \(z_1\leq 0\) for every sample in \(\Tset\), so under ordinary gradient descent \(a_1\) and \(b_1\) never change, \(z_1\) stays non-positive for all samples, and the hidden neuron is permanently stuck at \(0\). This is the dead ReLU. Having an active sample initially does not guarantee that the neuron remains active during training.
Technical 22.
For \(\delta_i=\partial\lscr/\partial h_i\), the backward recursion of a network with \(L\) learned layers, \(L-1\) hidden layers of widths \(q_1,\ldots,q_{L-1}\), and a one-neuron output layer \(L\), using column vectors, reads
\begin{align*} \delta_i = A_{i+1}^{\top}\rb{\delta_{i+1} \odot \phi_{i+1}'(z_{i+1})}, \qquad i = L-1,\ldots,1. \end{align*}a. Specify the dimensions of \(\delta_i\), \(\delta_{i+1}\), \(A_{i+1}\), \(z_{i+1}\) and \(\phi_{i+1}'(z_{i+1})\). b. Check that both products are well-defined. c. What do these dimensions become in the first step, \(i=L-1\)?
Solution
There is one \(\delta\) per neuron, so \(\delta_i\) has shape \(q_i\times 1\) and \(\delta_{i+1}\) has shape \(q_{i+1}\times 1\). The weight matrix of layer \(i+1\) maps the hidden vector \(h_i\in\R^{q_i}\) to the pre-activation \(z_{i+1}\in\R^{q_{i+1}}\), so \(A_{i+1}\in\R^{q_{i+1}\times q_i}\). Both \(z_{i+1}\) and \(\phi_{i+1}'(z_{i+1})\) have shape \(q_{i+1}\times 1\), since \(\phi_{i+1}\) is applied element-wise.
The Hadamard product \(\delta_{i+1}\odot\phi_{i+1}'(z_{i+1})\) has shape \(q_{i+1}\times 1\). Multiplication by \(A_{i+1}^{\top}\), with dimensions \(\rb{q_i\times q_{i+1}}\cdot\rb{q_{i+1}\times 1}\), gives a column vector of shape \(q_i\times 1\), matching \(\delta_i\).
In the first step \(i=L-1\), the output layer has one neuron, \(q_L=1\). Hence \(\delta_L\) has shape \(1\times 1\) and can be treated as a scalar, while \(A_L\) has shape \(1\times q_{L-1}\). Thus \(\delta_L\odot\phi_L'(z_L)\) is a scalar equal to \(\partial\lscr/\partial z_L\). Multiplication by \(A_L^{\top}\) gives the column of output weights scaled by this derivative. It has shape \(q_{L-1}\times 1\), matching \(\delta_{L-1}\).