Multilayer Perceptrons

Dive into Deep Learning · §4.1

Multilayer Perceptrons: Hidden Layers & Nonlinearity
XOR & feature interactions · hidden representations · universal approximation · depth folding · activation derivatives

Two Structural Limits of Linear Models

Why Linearity Fails

  • Monotonicity in every coordinate: Increasing feature x_i always pushes o_j = \sum_i x_i w_{ij} + b_j in the same direction, yet real quantities like health risk vs. body temperature rise on both sides of 37^\circ\textrm{C}.
  • Zero feature interactions: Increasing x_i by 1 shifts o_j by w_{ij} regardless of every other feature’s value, whereas a bright pixel’s meaning depends on whether its neighbors form a collar or a sleeve.
  • Learned intermediate features: Enumerating multi-pixel interactions across d = 784 pixels by hand is intractable—hidden layers learn them directly from data.

The XOR Problem: Four Points No Line Can Separate

Feature Interactions

  • Direction reversal on \{0, 1\}^2: Flipping x_1 from 0 to 1 increases y = x_1 \oplus x_2 when x_2 = 0, but decreases y when x_2 = 1.
  • Algebraic proof of impossibility: Class 1 at (0, 1), (1, 0) requires w_1 + w_2 + 2b > 0, whereas class 0 at (0, 0), (1, 1) requires w_1 + w_2 + 2b \le 0—a contradiction. \blacksquare
  • Shared diagonal midpoint: Both diagonals intersect at (1/2, 1/2), so every linear classifier misclassifies at least one corner.

Hidden Layers: Affine Maps Followed by Nonlinearity

The MLP Architecture

An MLP overcomes linearity by inserting one or more hidden layers that transform a minibatch \mathbf{X} \in \mathbb{R}^{n \times d} into learned representations \mathbf{H} \in \mathbb{R}^{n \times h} before the output layer:

\mathbf{H} = \phi\left(\mathbf{X}\mathbf{W}^{(1)} + \mathbf{b}^{(1)}\right) \mathbf{O} = \mathbf{H}\mathbf{W}^{(2)} + \mathbf{b}^{(2)}

  • First-layer parameters: \mathbf{W}^{(1)} \in \mathbb{R}^{d \times h} and broadcasted row bias \mathbf{b}^{(1)} \in \mathbb{R}^h compute pre-activations \mathbf{Z} \in \mathbb{R}^{n \times h}.
  • Elementwise activation \phi: Applies a scalar nonlinearity (such as \mathrm{ReLU}(z) = \max(z, 0)) entry by entry.
  • Output layer: \mathbf{W}^{(2)} \in \mathbb{R}^{h \times q} and \mathbf{b}^{(2)} \in \mathbb{R}^q map hidden features \mathbf{H} to class logits \mathbf{O} \in \mathbb{R}^{n \times q}.

Why Stacking Linear Layers Without φ Collapses

The Role of Nonlinearity

\mathbf{O} = \mathbf{X}\underbrace{\mathbf{W}^{(1)}\mathbf{W}^{(2)}}_{\mathbf{W}} + \underbrace{\mathbf{b}^{(1)}\mathbf{W}^{(2)} + \mathbf{b}^{(2)}}_{\mathbf{b}}

  • Zero gain in expressive power: Without \phi, any stack of linear layers multiplies out to a single d \times q affine map that still fails on XOR.
  • Low-rank bottleneck: If h_k < \min(d, q), then \mathrm{rank}(\mathbf{W}) \le h_k, strictly shrinking the linear model class.
  • Nonlinearity blocks associativity: Elementwise \mathrm{ReLU}(z) = \max(z, 0) prevents (\mathbf{X}\mathbf{W}^{(1)})\mathbf{W}^{(2)} from factoring into \mathbf{X}(\mathbf{W}^{(1)}\mathbf{W}^{(2)}).

Solving XOR Explicitly with Two ReLU Hidden Units

Constructive Example

With h = 2 ReLU hidden units, we can solve XOR in closed form by having both units read the coordinate sum s = x_1 + x_2 with thresholds at 0 and 1:

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}')
x = [0 0], h = [0 0], o = 0
x = [0 1], h = [1 0], o = 1
x = [1 0], h = [1 0], o = 1
x = [1 1], h = [2 1], o = 0
  • Unit 1 (h_1 = \mathrm{ReLU}(x_1 + x_2)): Tracks the sum s \in \{0, 1, 2\} across all four corners.
  • Unit 2 (h_2 = \mathrm{ReLU}(x_1 + x_2 - 1)): Remains 0 on (0, 0), (0, 1), (1, 0) and turns on (h_2 = 1) only at (1, 1), so o = h_1 - 2h_2 subtracts 2 and folds (1, 1) back down to 0.

Alternative XOR Weights and Single-Unit Limits

Constructive Example · Uniqueness

  • Distinct hidden points: Setting h_1 = \mathrm{ReLU}(x_1 - x_2) and h_2 = \mathrm{ReLU}(x_2 - x_1) maps (1, 0) \mapsto (1, 0) and (0, 1) \mapsto (0, 1), while collapsing both class-0 corners (0, 0) and (1, 1) to (0, 0) so o = h_1 + h_2 solves XOR cleanly.
  • Why 1 ReLU hidden unit cannot solve XOR: A single unit h_1 = \mathrm{ReLU}(w_1 x_1 + w_2 x_2 + b) is monotonic along (w_1, w_2) and can only carve out a single halfspace—so h \ge 2 is minimal.

Geometry of XOR: Folding Space Until It Is Separable

Representation Learning

Visualizing the input space (x_1, x_2) alongside the learned hidden space (h_1, h_2) reveals how hidden layers classify non-separable sets:

  • Input space (left): The network carves out the diagonal strip 1/2 < x_1 + x_2 < 3/2 by intersecting two halfspaces.
  • Hidden space (right): The mapping (x_1, x_2) \mapsto (h_1, h_2) folds the unit square along x_1 + x_2 = 1, collapsing both class-1 corners (0, 1) and (1, 0) onto the single hidden point (1, 0).
  • Linear readout: In hidden space, the single straight line h_1 - 2h_2 = 1/2 separates class 1 from class 0 cleanly.

Parameter Scaling and Deep Compositional Hierarchies

From 2 Layers to K Layers

  • General 1-hidden-layer parameter count: (d + 1)h + (h + 1)q = dh + h + hq + q parameters and dh + hq multiply-adds per example.
  • First-layer dominance on Fashion-MNIST (d = 784, h = 256, q = 10): \mathbf{W}^{(1)}, \mathbf{b}^{(1)} hold 785 \times 256 = 200{,}960 of the 203{,}530 parameters, nearly 99\% of the model.
  • Adding a second hidden layer (256 \to 256): Adds 257 \times 256 = 65{,}792 parameters (269{,}322 total).

1D Universal Approximation: A Sum of ReLU Hinges

Expressive Power · Width

What functions can a single hidden layer (K = 2) represent? On the real line (d = 1), each ReLU unit a_j\,\mathrm{ReLU}(x - t_j) acts as a hinge at knot t_j:

  • Piecewise-linear polylines: Unit j is 0 for x \le t_j and changes the total slope by a_j for x > t_j. Summing h hinges plus an output bias constructs any continuous polyline with h interior knots (h + 1 linear segments).
  • Uniform approximation on [a, b]: Because any continuous function on a closed bounded interval can be approximated within any \epsilon > 0 by a fine enough polyline, a wide enough single ReLU layer approximates any continuous 1D function.

The Universal Approximation Theorem and Its Limits

Expressive Power · Guarantees vs. Reality

  • Universal Approximation (Cybenko, 1989; Leshno et al., 1993): For any continuous non-polynomial \phi and continuous f on compact K \subset \mathbb{R}^d, a 1-hidden-layer MLP g(\mathbf{x}) = \sum_{j=1}^h v_j\phi(\mathbf{w}_j^\top\mathbf{x} + b_j) + b^{(2)} satisfies \sup_{\mathbf{x} \in K}|f(\mathbf{x}) - g(\mathbf{x})| < \epsilon.
  • Exponential width curse: Matching oscillating functions in \mathbb{R}^d with a single hidden layer can require h = \Omega(\epsilon^{-d}) units.
  • No learnability guarantee: Proves good weights exist, not that SGD finds them or generalizes from finite samples.

Why Depth Matters: Composing Folds Multiplies Pieces

Expressive Power · Depth Efficiency

Whereas adding units to a single hidden layer adds hinges one by one (h + 1 linear pieces), stacking layers composes space-folding maps and multiplies the number of linear pieces (Telgarsky, 2016):

  • The 2-ReLU tent map: On [0, 1], two ReLU units compute the symmetric tent function: g(z) = 2\,\mathrm{ReLU}(z) - 4\,\mathrm{ReLU}(z - 1/2)
  • Exponential folding across depth: Each composition with g folds [0, 1] in half, doubling the linear segments: g^{\circ m} has 2^m linear pieces using only m hidden layers of 2 units (2m units total).
  • 10 layers vs. 1 layer: At m = 10, a deep network of 20 ReLU units produces 2^{10} = 1{,}024 pieces; matching it with one hidden layer requires 2^{10} - 1 = 1{,}023 units.

Activation Functions and Gradient Flow

Activation Functions · Overview

While any non-polynomial \phi suffices for universal approximation, training via gradient descent depends on the derivative \phi'(z), which gates every incoming weight gradient by the chain rule:

\frac{\partial l}{\partial w^{(1)}_{ij}} = \frac{\partial l}{\partial h_j}\,\phi'(z_j)\,x_i

  • Rectifying activations (top): ReLU keeps slope 1 for positive pre-activations and GELU a slope close to 1 once z \gtrsim 1, allowing gradients to pass undiminished across deep stacks.
  • Squashing activations (bottom): Sigmoid and \tanh compress \mathbb{R} into bounded intervals, so their derivatives \phi'(z) \to 0 saturate in both tails (|z| \gtrsim 3).

Rectifying Activations: ReLU, Leaky ReLU, and GELU

Activation Functions · Non-Saturating

Modern deep networks default to rectifying nonlinearities on hidden layers because they never saturate on positive inputs:

  • ReLU (\mathrm{ReLU}(z) = \max(z, 0)): Derivative is 1 for z > 0 and 0 for z < 0 (0 at z = 0 by convention). Evaluates in a single comparison and passes gradients without attenuation on active units.
  • Dead ReLU pitfall & Leaky ReLU: If a large gradient step pushes a unit’s weights so that z_j < 0 on every training example, \phi'(z_j) = 0 zeroes out its weight gradients permanently. Leaky ReLU, \max(z, \alpha z) with \alpha \approx 0.01, retains a small negative-half slope so inactive units can recover.
  • GELU (z\,\Phi(z)) and SiLU (z\,\sigma(z)): Weight z by the standard normal CDF \Phi(z) (or logistic sigmoid \sigma(z)), smoothing ReLU’s kink at z = 0 (where \mathrm{GELU}'(0) = 1/2) with a slight negative dip near z \approx -0.75. Standard in modern Transformers.

Squashing Activations: Sigmoid, Tanh, and Saturation

Activation Functions · Bounded Gates

Classical neural networks used smooth S-curves that squash \mathbb{R} into (0, 1) or (-1, 1), but suffer from severe gradient attenuation across depth:

  • Logistic sigmoid: \sigma(z) = \frac{1}{1 + e^{-z}} \in (0, 1) has derivative \sigma'(z) = \sigma(z)(1 - \sigma(z)) \le 1/4 (peaking at z = 0). Even near z = 0, stacking K = 10 sigmoid layers shrinks gradients by at least 4^{-10} \approx 10^{-6}.
  • Hyperbolic tangent: \tanh(z) = \frac{1 - e^{-2z}}{1 + e^{-2z}} = 2\sigma(2z) - 1 \in (-1, 1) is zero-centered with derivative 1 - \tanh^2(z) \le 1 at z = 0, avoiding the 1/4 penalty near the origin.
  • Tail saturation at |z| = 4: At z = \pm 4, \sigma'(\pm 4) \approx 0.018 and \tanh'(\pm 4) \approx 0.0013—both squash gradients by >98\%. Today, \sigma and \tanh serve as probability outputs or multiplicative gates in LSTMs/GRUs rather than deep hidden activations.

Quantitative Comparison of Activation Derivatives

Activation Functions · Head-to-Head

  • Positive tail (z = +4): ReLU, Leaky ReLU, and GELU all maintain unit slope (\phi'(+4) = 1.000), whereas sigmoid (0.018) and \tanh (0.0013) attenuate gradients by >98\%.
  • Origin (z = 0): Sigmoid peaks at \sigma'(0) = 0.25 (4\times shrinkage per layer even without saturation); \tanh'(0) = 1.00 and \mathrm{GELU}'(0) = 0.50.
  • Negative tail (z = -4): ReLU is 0 (dead if z < 0 across all inputs); Leaky ReLU retains slope 0.01 for recovery.

Concept Check: Parameters, Pieces, and Gradient Survival

Check

Test your command of MLP mechanics on three quick calculations:

  1. Two-hidden-layer parameter count: On Fashion-MNIST (d = 784, q = 10), how many total parameters does an MLP with two hidden layers of width 256 (784 \to 256 \to 256 \to 10, with biases) have? What if all \phi are removed?
  2. Depth vs. width pieces: For a 1D input and output, how many linear pieces can 12 ReLU units create in 1 hidden layer of 12 units vs. 6 hidden layers of 2 units?
  3. 5-layer slope product: Across a chain of 5 hidden units with unit weights (w = 1), what fraction of the upstream gradient survives if every unit sits at z = 0 under sigmoid vs. z = 4 under ReLU?

Answers:

  1. Parameters: 785 \times 256 + 257 \times 256 + 257 \times 10 = 200{,}960 + 65{,}792 + 2{,}570 = 269{,}322. Without \phi, the entire network collapses to a single 784 \times 10 affine map (7{,}850 effective parameters).
  2. Pieces: One layer of 12 units produces at most 12 + 1 = 13 pieces; 6 layers of 2 units (g^{\circ 6}) produce 2^6 = 64 pieces.
  3. Gradient survival: Sigmoid at z = 0 retains (1/4)^5 = 1/1024 \approx 0.1\%; ReLU at z = 4 retains 1^5 = 100\%.

Summary: Multilayer Perceptrons & Activation Functions

Key Takeaways

Concept / Operation Mathematical Form Key Property / Complexity Deep Learning Role
1-Hidden-Layer MLP \mathbf{H} = \phi(\mathbf{X}\mathbf{W}^{(1)} + \mathbf{b}^{(1)}), \mathbf{O} = \mathbf{H}\mathbf{W}^{(2)} + \mathbf{b}^{(2)} (d + 1)h + (h + 1)q parameters Learns nonlinear feature interactions (e.g., XOR, image parts)
Linear Collapse (\phi = \mathrm{id}) \mathbf{O} = \mathbf{X}(\mathbf{W}^{(1)}\mathbf{W}^{(2)}) + \mathbf{b} Rank \le \min(d, h, q) Proves nonlinearity \phi is mandatory in every hidden layer
Universal Approximation \sum_{j=1}^h v_j\,\phi(\mathbf{w}_j^\top\mathbf{x} + b_j) + b^{(2)} Approximates any continuous f on compact K \subset \mathbb{R}^d Guarantees existence for K = 2, but width h can grow as \Omega(\epsilon^{-d})
Depth Space-Folding g^{\circ m}(z) with 2-ReLU tent g 2^m pieces with 2m units (O(m) vs. O(2^m)) Explains why deep compositions achieve exponential parameter efficiency
ReLU / GELU \max(z, 0) / z\,\Phi(z) \phi'(z) = 1 for z \gg 0; \phi'(z) = 0 for z \ll 0 Default hidden-layer activations; prevents tail gradient saturation
Sigmoid / Tanh \frac{1}{1+e^{-z}} / 2\sigma(2z) - 1 \sigma'(0) = 1/4, \tanh'(0) = 1; \phi'(\pm 4) < 0.02 Bounded gates, binary probabilities, and recurrent state squashers