X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
W1, b1 = np.array([[1, 1], [1, 1]]), np.array([0, -1])
w2, b2 = np.array([1, -2]), 0
H = np.maximum(X @ W1 + b1, 0) # hidden layer with ReLU
o = H @ w2 + b2 # output layer
for x, h, out in zip(X, H, o):
print(f'x = {x}, h = {h}, o = {out}')4.1 Multilayer Perceptrons
In Chapter 3, softmax regression scored each garment category by taking an affine weighted sum of the input pixels, \(o_j = \sum_{i=1}^d x_i w_{ij} + b_j\). That linear form bakes in two severe restrictions:
- Monotonicity: Increasing feature \(x_i\) always pushes \(o_j\) in the same direction (up if \(w_{ij} > 0\), down if \(w_{ij} < 0\)). Yet many real relationships are non-monotonic: human health risk rises whenever body temperature strays from \(37^\circ\textrm{C}\) in either direction.
- No feature interactions: Increasing \(x_i\) by one unit changes \(o_j\) by \(w_{ij}\) regardless of the values of every other coordinate. In an image, however, a bright pixel at \((13, 17)\) means completely different things depending on whether the neighboring pixels form a collar, a sleeve, or a handbag strap.
Figure 4.1.1 plots the temperature example. The best linear score rises with temperature throughout, so it rates a patient with hypothermia as the safest of all.
A multilayer perceptron (MLP) overcomes both limitations by inserting one or more hidden layers that transform raw inputs into learned intermediate representations, applying an affine map followed by a nonlinear activation function before a linear output layer makes the final prediction. In this section, we prove that no linear model can solve the four-point XOR problem, solve XOR explicitly with two ReLU hidden units, examine what functions shallow and deep networks can represent, and compare the activation functions used across modern deep learning.
4.1.1 Limits of Linear Models: The XOR Problem
The starkest possible demonstration of feature interaction uses just four data points: the corners of the unit square \(\{0, 1\}^2\) labeled by their exclusive-OR (XOR), where \(y = 1\) if and only if exactly one coordinate equals \(1\):
| \(x_1\) | \(x_2\) | \(y\) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Notice the interaction: flipping \(x_1\) from \(0\) to \(1\) increases \(y\) when \(x_2 = 0\), but decreases \(y\) when \(x_2 = 1\).
Proposition. For all \(w_1, w_2, b \in \mathbb{R}\), the linear decision rule “predict class 1 if \(w_1 x_1 + w_2 x_2 + b > 0\)” misclassifies at least one of the four XOR points.
Proof. Suppose for contradiction that some \(w_1, w_2, b\) classify all four corners correctly. The two class-1 points \((0, 1)\) and \((1, 0)\) require \(w_2 + b > 0\) and \(w_1 + b > 0\), whose sum gives \(w_1 + w_2 + 2b > 0\). Meanwhile, the two class-0 points \((0, 0)\) and \((1, 1)\) require \(b \leq 0\) and \(w_1 + w_2 + b \leq 0\), whose sum gives \(w_1 + w_2 + 2b \leq 0\), an immediate contradiction. \(\blacksquare\)
Geometrically, both diagonals of the unit square share the midpoint \((1/2, 1/2)\) (Figure 4.1.2), where any affine function equals the average of its values at either diagonal’s endpoints, so no straight line can ever separate the two diagonals.
4.1.3 Expressive Power
4.1.3.1 Universal Approximation
How rich is the class of functions computable by a single hidden layer (\(K = 2\))? On the real line (\(d = 1\)), each ReLU hidden unit \(a_j\,\mathrm{ReLU}(x - t_j)\) contributes a hinge that is flat to the left of its knot \(t_j\) and changes slope by \(a_j\) to the right (Figure 4.1.5). Summing \(h\) hinges plus a constant produces any continuous piecewise-linear polyline with \(h\) interior knots; since any continuous function on a closed bounded interval \([a, b]\) can be approximated uniformly to within any \(\epsilon > 0\) by a fine enough polyline, a single wide enough ReLU hidden layer approximates any continuous 1D function to arbitrary precision.
The universal approximation theorem generalizes this fact to arbitrary input dimension \(d\): a single-hidden-layer MLP can approximate any continuous function on any compact subset of \(\mathbb{R}^d\) to arbitrary uniform accuracy using any continuous non-polynomial activation function \(\phi\) (Cybenko 1989; Leshno et al. 1993) (see Exercise 5 for a 2D proof via ridge functions and the Stone–Weierstrass theorem).
4.1.3.2 Depth and Space Folding
If one hidden layer can already approximate anything, why build deep networks? Because universal approximation says nothing about how many units a single layer needs (which can grow exponentially in \(d\)), nor whether gradient descent can learn or generalize from finite data.
Depth achieves exponential parameter efficiency by composing space-folding maps (Telgarsky 2016). Consider the 1D tent function on \([0, 1]\) computed by two ReLU units:
\[g(z) = 2\,\mathrm{ReLU}(z) - 4\,\mathrm{ReLU}(z - 1/2), \tag{4.1.4}\]
which rises from \(0\) to \(1\) on \([0, 1/2]\) and falls back to \(0\) on \([1/2, 1]\) (Figure 4.1.6). Composing \(g\) with itself folds \([0, 1]\) in half at every layer: \(g \circ g\) has \(4\) linear pieces, \(g \circ g \circ g\) has \(8\), and the \(m\)-fold composition \(g^{\circ m}\), computed by \(m\) hidden layers of \(2\) ReLU units (\(2m\) total units), produces a sawtooth wave with \(2^m\) linear pieces. Representing that same \(2^m\)-piece sawtooth with a single hidden layer requires at least \(2^m - 1\) hidden units (\(1{,}023\) units in one layer vs. \(20\) units across \(10\) layers when \(m = 10\)).
4.1.4 Activation Functions
While almost any non-polynomial \(\phi\) suffices for universal approximation, optimization via gradient descent depends critically on the derivative \(\phi'(z)\). For hidden unit \(j\) with pre-activation \(z_j = \sum_i x_i w^{(1)}_{ij} + b^{(1)}_j\) and activation \(h_j = \phi(z_j)\), the chain rule scales every incoming weight gradient by the local slope \(\phi'(z_j)\):
\[\frac{\partial l}{\partial w^{(1)}_{ij}} = \frac{\partial l}{\partial h_j}\,\phi'(z_j)\,x_i. \tag{4.1.5}\]
Wherever \(\phi'(z_j) \approx 0\), gradient flow stalls. Figure 4.1.7 compares the four principal activations and their derivatives.
- ReLU and Leaky ReLU: \(\mathrm{ReLU}(z) = \max(z, 0)\) has derivative \(1\) for \(z > 0\) and \(0\) for \(z < 0\) (conventionally \(0\) at \(z = 0\)). Because its slope is identically \(1\) across the entire positive half-line, active ReLU units never attenuate gradients as \(z\) grows (Nair and Hinton 2010; Glorot et al. 2011). If a unit’s weights shift so that \(z < 0\) on every training input, however, its gradient vanishes permanently (a dead ReLU); leaky ReLU, \(\max(z, \alpha z)\) with \(\alpha \approx 0.01\), keeps a small positive slope for \(z < 0\) so dead units can recover.
- GELU and SiLU (Swish): The Gaussian error linear unit \(\mathrm{GELU}(z) = z\,\Phi(z)\) (where \(\Phi\) is the standard normal CDF) and its cousin \(\mathrm{SiLU}(z) = z\,\sigma(z)\) smooth out ReLU’s kink at the origin (passing through \(z = 0\) with slope \(1/2\) and a gentle negative dip near \(z \approx -0.75\)) and serve as the default activations in modern Transformers (Hendrycks and Gimpel 2016).
- Sigmoid and Tanh: The logistic sigmoid \(\sigma(z) = 1/(1 + e^{-z}) \in (0, 1)\) has derivative \(\sigma'(z) = \sigma(z)(1 - \sigma(z)) \leq 1/4\), while the zero-centered hyperbolic tangent \(\tanh(z) = (1 - e^{-2z})/(1 + e^{-2z}) = 2\sigma(2z) - 1 \in (-1, 1)\) (Exercise 3) has derivative \(1 - \tanh^2(z) \leq 1\). Both saturate to flat horizontal asymptotes (\(\phi'(z) \to 0\)) as \(|z|\) grows beyond \(\sim 3\), and multiplying \(\sigma'(z) \leq 1/4\) across \(K\) layers shrinks gradients by at least \(4^{-K}\) (vanishing gradients, analyzed in Section 4.4). Today, sigmoid and \(\tanh\) are used primarily as bounded gates and state squashers in recurrent architectures (Chapter 12).
4.1.5 Summary
Linear models are monotonic in every coordinate and cannot capture feature interactions such as XOR. Multilayer perceptrons overcome both limitations by inserting hidden layers \(\mathbf{H}^{(k)} = \phi(\mathbf{H}^{(k-1)}\mathbf{W}^{(k)} + \mathbf{b}^{(k)})\) before the linear output layer; without a nonlinear activation \(\phi\), stacked affine transformations collapse into a single linear map. A single sufficiently wide hidden layer with any continuous non-polynomial activation can approximate any continuous function on a compact set, whereas composing \(m\) hidden layers folds space repeatedly to create \(2^m\) linear regions with \(O(m)\) units. Among activation functions, rectifiers such as ReLU (\(\max(z, 0)\)) and GELU (\(z\Phi(z)\)) maintain unit or near-unit slope on positive inputs, avoiding the tail saturation that attenuates gradients through deep stacks of sigmoid or \(\tanh\) units.
4.1.6 Exercises
- Linear networks. Without activation functions, stacking layers composes affine maps into another affine map. Consider such a network with input dimension \(d\), output dimension \(q\), and hidden widths \(h_1, \ldots, h_{K-1}\). Show that it represents exactly the affine maps \(\mathbf{X}\mathbf{W} + \mathbf{b}\) whose weight matrix \(\mathbf{W}\) has rank at most \(\min(h_1, \ldots, h_{K-1})\). Use this result to give an example in which adding a hidden layer to a linear model strictly shrinks its function class.
- XOR by hand. Figure 4.1.4 shows a two-unit ReLU network that computes XOR by mapping the two inputs of class 1 to the same hidden point.
- Find weights for a two-unit ReLU network that also computes XOR but maps the two inputs of class 1 to distinct hidden points, and verify it on the four inputs.
- Show that no network with a single ReLU hidden unit and a linear output classifies XOR correctly, for any threshold applied to its output.
- Sigmoid and tanh. The two squashing functions are closely related.
- Show that \(\tanh(z) = 2\sigma(2z) - 1\).
- Show that for every MLP with tanh hidden activations there is an MLP of the same architecture with sigmoid hidden activations that computes the same function, and conversely. State how the weights and biases change.
- Depth and linear pieces. ● A one-hidden-layer ReLU network with \(h\) hidden units on the real line has at most \(h + 1\) linear pieces, while each composition with the two-unit tent of Figure 4.1.6 doubles the number of pieces. This problem makes the gain from depth exact for a network with three units per layer.
- Take three hidden units \(\mathrm{ReLU}(w_j x + b_j)\) with input weights \(w_1 = w_2 = 1\) and \(w_3 = -1\) and with joints \(-b_j/w_j\) at \(1/6\), \(2/6\), and \(4/6\). Choose output weights and an output bias so that the network’s output \(f\) takes the values \(0, 1, 0, 1, 0\) at \(x = 0, 1/6, 2/6, 4/6, 1\).
- Count the linear pieces of \(f \circ f\) on \([0, 1]\), and of the \(m\)-fold composition \(f \circ \cdots \circ f\). Explain, in terms of how composing with \(f\) folds the graph, why each composition multiplies the count rather than adding to it.
- The \(m\)-fold composition is computed by a network with \(m\) hidden layers of three units each. How many hidden units would a network with a single hidden layer need to represent the same function? Compare the parameter counts of the two networks.
- One hidden layer suffices. ● On the real line, a single hidden layer approximates a continuous function by a polyline, one hinge per joint. In the plane there are no joints to place, and the proof needs another idea: functions that vary along one direction only.
- Let \(\psi\) be continuous on \(\mathbb{R}\) and let \(\mathbf{w} \in \mathbb{R}^2\). Show that a single hidden layer approximates the ridge function \(\psi(\mathbf{w}^\top \mathbf{x})\), which is constant along every line orthogonal to \(\mathbf{w}\), uniformly on any compact subset of the plane.
- The Stone–Weierstrass theorem states the following. Let a set of continuous functions on a compact set be closed under sums, products, and scalar multiples, contain the constants, and contain, for any two distinct points, a function that takes different values at them. Then every continuous function on the compact set is a uniform limit of functions in the set. Show that the finite linear combinations of \(\cos(\boldsymbol{\omega}^\top \mathbf{x})\) and \(\sin(\boldsymbol{\omega}^\top \mathbf{x})\), with \(\boldsymbol{\omega} \in \mathbb{R}^2\), form such a set.
- Conclude that a single hidden layer approximates any continuous function on a compact subset of the plane.