Multi-Layer Perceptron
A hidden layer invents new coordinates, and in those coordinates one straight line is enough. Depth is a change of representation, not extra parameter room.
Why Does This Exist?
Two pressure mats guard a warehouse doorway, one inside and one outside, and the alert should fire when exactly one mat is pressed. A single perceptron cannot draw that boundary. The alert cases sit on opposite corners of a square with the quiet cases on the other diagonal, and no straight line splits that pattern. That wall is where the last page ended.
Clearing it takes two extra units. Give the first the weights and the second , then clip both at zero. The first unit stays silent unless only the inside mat is pressed; the second unless only the outside one is. Each detects a pattern, an asymmetry between the mats, instead of reading one input.
Now describe the four situations using only what those two units report. Both quiet gives , and both pressed also gives . Inside only gives , outside only gives . The alert cases sit at two separate corners while both quiet cases collapse onto the origin, so one straight line splits them: alert when the two numbers sum above 0.5. Nothing about the doorway changed. The coordinates did.
This page covers the hidden layer as a change of coordinates. The single unit underneath is on the perceptron page, choosing the clipping function is on the activation functions page, and how the weights get learned is backpropagation.
The one idea you need first. A linear model draws one straight boundary. If the classes interleave so no straight cut separates them, no linear model of those inputs can succeed, however long it trains. The way out is better inputs, not a better straight line.
Think of It Like This
Folding the paper before you cut it
Four dots on a sheet of paper: two dark ones on one diagonal, two light ones on the other. Your scissors only cut straight, and you need dark on one side and light on the other. Flat on the table it cannot be done. Every straight cut strands a dot on the wrong side.
So fold. Crease the sheet along the diagonal through both dark dots. The light dots land on top of each other, out at matching distances, with both dark dots sitting on the crease itself. One straight cut parallel to the fold finishes the job.
The scissors never bent. The paper moved. That is a hidden layer: it folds the input space, and each crease sits where one unit switches from off to on. More layers mean more creases, which is how a boundary grows as intricate as the data demands while every piece of it stays flat.
Where the analogy stops: you choose where to crease the paper, while a network learns its folds from data through training. And paper folds are sharp and exact, while learned bends are placed approximately, wherever the loss tells them to sit.
How It Actually Works
One layer, three operations
Multiply, shift, bend:
is the vector leaving layer , is what arrived from below, is that layer's weight matrix, is its bias vector, and is the nonlinearity, applied to each entry on its own. Chain layers and the last output is the prediction.
Shapes are where afternoons go missing, so here is one network in full. Tile 64 mats across the warehouse floor and sort each moment into four outcomes: empty, a person walking, a pallet jack, a stack of boxes, in batches of 128 moments:
- The input batch is 128 rows by 64 columns.
- is 64 by 32 with 32 biases, so layer one passes on 128 by 32.
- is 32 by 16 with 16 biases, so layer two passes on 128 by 16.
- is 16 by 4 with 4 biases, so the output is 128 by 4, one score per outcome per moment.
The batch dimension rides through untouched, and every matrix reads the width below it and writes the width above it. Total parameters: . That is the entire shape rule, and checking it catches most wiring bugs before they run.
The nonlinearity is load-bearing
Remove and stack two layers. You get , and matrix multiplication is associative, so that equals : one matrix, one linear layer. Depth with nothing between the layers buys literally nothing. In the doorway network the product of the two weight matrices is the zero matrix, so the version without clipping answers "no alert" to all four cases forever. Which to use is its own page.
What depth actually buys
One wide hidden layer can approximate any continuous function on a bounded region to any accuracy you name. That theorem settles one thing: such a network exists. It says nothing about whether gradient descent finds it from your initialization, how many units "wide enough" turns out to be, or how much data pins those parameters down.
Depth wins anyway, for a statable reason. Targets built in stages need far fewer units arranged in layers than flattened into one wide layer. On the floor grid: layer one finds pressure edges, layer two assembles outlines, layer three names them. A single wide layer cannot share that staged work, so it memorizes every outline at every position and the unit count explodes.
Code
Hand-set weights, so the only variable is the clipping. Same parameters, same architecture, one line apart.
import numpy as np
X = np.array([[0.0, 0], [0, 1], [1, 0], [1, 1]]) # inside mat, outside matW1 = np.array([[1.0, -1.0], [-1.0, 1.0]]) # unit 1: inside only. unit 2: outside onlyW2 = np.array([[1.0], [1.0]]) # either asymmetry means someone stands there
def alerts(data: np.ndarray, nonlinear: bool) -> np.ndarray: hidden = data @ W1 if nonlinear: hidden = np.maximum(hidden, 0.0) # drop this line and the matrices merge return (hidden @ W2 - 0.5).ravel() > 0
print("with clipping ", alerts(X, True).astype(int))# -> with clipping [0 1 1 0]print("without clipping", alerts(X, False).astype(int))# -> without clipping [0 0 0 0]print("collapsed to", (W1 @ W2).ravel())# -> collapsed to [0. 0.]With clipping the four cases read exactly right. Without it the two matrices collapse into the zero mapping and the model says nothing to anything.
Watch Out For
Depth with no activation between the layers
The symptom is a network that matches linear regression on the same features to three decimals and stays there while you add width, add layers, and train ten times longer. Nothing errors. The loss falls, then flattens exactly where a closed-form linear fit would have landed in one step.
Check that an activation sits between every pair of consecutive linear layers. In hand-rolled code the usual cause is computing the activation and discarding the result, or applying it only after the final layer, where it bends nothing underneath. One deliberate exception: a narrow linear projection that only cuts parameter count is still one linear map, and that is fine when intended.
Reading universal approximation as a guarantee
The symptom is a single hidden layer widened to thousands of units instead of adding a second layer, then the data getting blamed when the loss plateaus. "A network can approximate any function" is an existence claim: it cannot be violated and it cannot help you. The promised weights may sit where the optimizer never reaches, and the width they need can grow exponentially with the input dimension.
Read the curves for what they say about capacity. Training error near zero means capacity to spare and a generalization problem, so more width is the wrong lever. Depth is the lever for staged structure.
The Quick Version
- A layer is a matrix multiply, a bias shift, and a nonlinearity applied entry by entry. An MLP chains several of them.
- The hidden layer builds coordinates where the answer is linearly separable, and the output layer is an ordinary linear model in that new space.
- Exclusive-or needs two hidden units, each detecting an asymmetry pattern rather than reading one input.
- Without the nonlinearity, any stack of linear layers collapses into a single matrix, here the zero mapping.
- The batch dimension rides through untouched, and each matrix reads the width below and writes the width above.
- Universal approximation promises that good weights exist, not that training finds them or that the width is affordable.