The Multilayer Perceptron
Stacking linear layers with a nonlinearity between them removes the limitation that stopped the perceptron. We build the multilayer perceptron in explicit matrix form (the forward pass, its dimensions, a worked XOR network with concrete weights) and prove why the nonlinearity is essential: without it the deepest stack collapses to a single hyperplane.
╌╌╌╌
The linear model failed XOR because a single hyperplane cannot carve up an interleaved space. The multilayer perceptron (MLP) fixes this by inserting a layer of neurons between the input and the output and passing their combined signal through a nonlinearity before the next layer. The result is a stack of affine maps interleaved with elementwise nonlinearities, the canonical deep feedforward network.1
The architecture
An MLP is a stack of fully connected layers. Layer takes the vector below it, applies an affine map (a weight matrix plus a bias), and passes the result componentwise through a nonlinear activation :
with the convention and a final layer usually left linear so the output can take any value a regression target or logit needs. Writing the four layer equations out shows the chain explicitly,
Stack as many hidden layers as you like; the count is the network's depth, the number of units in a layer its width.
The forward pass in matrix form
The whole computation is a sequence of matrix–vector products. The layer widths determine every shape: the weight matrix maps the layer below into the layer above, so it has one row per output unit and one column per input unit.
It is convenient to name the pre-activation separately from the post-activation ; the split is what backpropagation later differentiates through. The dimension chain must telescope, each layer's output width becoming the next layer's input width, or the products are undefined:
Reading off the figure: each blue box is a matrix multiply whose shape is printed beneath it, and the bare box leaves the width untouched. The forward pass is the literal traversal of this chain.
- 1
- 2for to do
- 3affine map, shape
- 4if then
- 5elementwise nonlinearity
- 6else
- 7linear output layer
- 8return
In practice the input is not one vector but a batch of examples stacked into a matrix (the row-major, PyTorch-shaped convention), and the layer becomes with the bias broadcast across rows.2 The same matrix multiply that processes one example processes the whole batch at once, the reason a GPU is the natural host for the forward pass.3
For example, track the shapes through the ––– net with a batch of . The input is ; layer right-multiplies by , so the batch dimension rides untouched through every product:
The bias is added to each of the rows of by broadcasting — the library copies the length- vector down the batch axis rather than materializing a bias. Every intermediate is a matrix whose first axis is the batch and whose second axis is the layer width, and the widths telescope exactly as in the single-example chain.
A forward pass, traced with numbers
For example, take a –– net with tanh hidden units and a linear output, feed it , and read every intermediate off the algorithm above. The parameters are
Layer 1. The pre-activation is one dot product per hidden unit, and squashes each into :
| unit | value | ||
|---|---|---|---|
Layer 2. The output is a single linear readout , a weighted sum of the three hidden activations:
Nothing here is more than a dot product and an elementwise squash, repeated once per layer; the whole net is that pattern scaled up to hundreds of units. Notice unit has saturated close to (its pre-activation sits well into tanh's flat tail), a detail that will matter when backpropagation multiplies by the small derivative there.
Counting parameters
Every entry of every and is a free parameter. A layer mapping width into width contributes weights plus biases, so the total is
For a concrete ––– classifier (the shape of a small MNIST net), the count is dominated by the first layer, where the wide input meets the first hidden layer:
| Layer | Shape | Weights | Biases | Params |
|---|---|---|---|---|
| Total |
Why the nonlinearity is non-negotiable
Drop the activation and the whole tower collapses. A composition of affine maps is itself affine; multiply two layers and the product is one weight matrix times the input plus one bias:
Induction extends this to any depth: stacked linear layers compute exactly what one linear layer computes.
A hundred stacked linear layers therefore still compute a single hyperplane and still fail XOR. The nonlinearity is the only reason depth adds expressivity: it lets each layer transform the space nonlinearly before the next one acts, so interleaved classes can be separated.
For example, take two linear layers, , and , , with no activation between them. Their composite has the single equivalent weight and bias
Check on : layer by layer, and then ; the collapsed form gives , the same value. The two-layer stack is exactly the single map . With a nonlinearity between the layers, no such single matrix exists.
A network that computes XOR
Here is a fully explicit –– MLP with ReLU hidden units, , that computes XOR exactly.4 The hidden layer has two units; the output weights place a on the second, the cancellation that carves out the interior:
Tracing all four inputs through the algorithm above verifies the claim. The pre-activation is , the hidden vector is , and the output is :
| XOR | ||||
|---|---|---|---|---|
The last row is the key case: when both inputs are on, would wrongly fire, but switches on and its weight cancels the surplus exactly. The ReLU is essential: it clips to on the input so the second unit stays silent there.
The geometry: intersecting half-planes
Each ReLU unit is a half-plane detector: it outputs on one side of the line and grows linearly on the other. Unit switches on above ; unit switches on above and, through its weight, subtracts twice as fast. Writing , the output is
which is positive precisely on the strip — between the line through and the line through . That strip contains the two XOR-positive corners (where ) and excludes the two negatives, which sit exactly on its boundary lines ( and ).
The two red corners (the XOR-positive points and ) sit inside the shaded strip; the two blue corners sit exactly on its boundary lines, where . Composition has turned two flat half-plane cuts into a bounded slab, a non-convex labelling no single hyperplane could produce.
Hidden space makes XOR linear
The strip picture lives in the input space. The dual picture lives in the hidden space , where the ReLU layer has re-coordinated the four points so that a single output line separates them:
This is the central idea of deep learning: instead of fitting a complicated boundary in the input space, learn a representation in which a simple boundary works. The hidden layers build the representation; the output layer is the same linear model from the last lesson, now applied in coordinates where it succeeds.
Depth versus width
The XOR net used width (two hidden units) to gain expressivity; it could also have used depth. Both knobs increase the function class, but they trade off differently, and the difference is a theorem rather than a heuristic.
| Knob | Increases | Cost | Effect on the function |
|---|---|---|---|
| Width | units per layer | params | more linear regions per layer |
| Depth | layers | params per layer | regions multiply across layers |
A shallow-but-wide net can represent any of these functions in principle (that is the universal approximation theorem), but often only at the cost of exponentially many units. A deep net produces the same number of linear regions by composition: each layer folds the regions of the layer below, so region count grows multiplicatively with depth and only additively with the parameters that buy it.5
The next lessons fill in the remaining pieces: which nonlinearity (activations), how the weights get learned (backpropagation), and how expressive the stack is (universal approximation).
What a layer became
Chollet presents the MLP as a stack of Dense layers, the standard picture
around 2015. Two developments since changed what a layer
is without changing
the affine-plus-nonlinearity unit.
Residual connections let the stack go deep. The affine-collapse argument shows depth is useless without a nonlinearity; a second obstacle is that training a very deep plain stack degrades even when it should not, because the gradient weakens and the identity map becomes hard to represent. He, Zhang, Ren & Sun's residual network (2015) adds a shortcut so each block computes instead of , where is the usual affine-plus-nonlinearity. The block now only has to learn the residual correction on top of the input it already has, and the shortcut gives the backward pass a gradient path that skips the weight layers entirely. That single change took trainable depth from tens of layers to hundreds and is now standard in every deep architecture.
The MLP is the compute inside the Transformer. The dominant architecture of the last decade, the Transformer (Vaswani et al., 2017), interleaves attention with a position-wise feed-forward network applied identically to every token: a two-layer MLP, , typically widening the hidden dimension by before projecting back. Roughly two-thirds of a Transformer's parameters live in these MLP blocks; attention mixes information across tokens while the MLP does the per-token nonlinear computation this lesson built. Every idea here — the affine map, the elementwise nonlinearity, the shape telescoping — is what that block runs.
Even the mixing can be an MLP. Tolstikhin et al.'s MLP-Mixer (2021) dropped attention entirely and showed a network built only from MLPs — one MLP mixing across spatial positions, another mixing across channels — reaches competitive image-classification accuracy. Given enough scale and the right connectivity, the stack of affine maps and nonlinearities is a competitive architecture in its own right.6
Takeaways
- An MLP composes affine maps with an elementwise nonlinearity between them; the forward pass is the literal traversal with weight shapes that must telescope.
- Without , the stack collapses to a single affine map (affine-collapse theorem); depth buys nothing, and the nonlinearity is the entire reason it helps.
- A concrete –– ReLU net with , , computes XOR exactly; the second unit's weight cancels the surplus on the input.
- Geometrically, two ReLU units carve intersecting half-planes whose strip is the XOR-positive region; equivalently, the hidden layer maps the four points into a space where one line separates them.
- Parameter count is , dominated by the layer facing the widest input; depth vs width both add expressivity, but depth multiplies linear regions while costing only additively in parameters.
Footnotes
- Goodfellow, Deep Learning, Ch. 6 — Deep Feedforward Networks: the MLP as a composition of affine maps and elementwise nonlinearities, learning hidden representations rather than hand-designed features. ↩
- Chollet, Deep Learning with Python, Ch. 2 — The Mathematical Building Blocks: tensor operations, broadcasting, and the
Denselayer as . ↩ - Stevens, Deep Learning with PyTorch, Ch. 5–6 — batched tensors
(N, features)flow throughnn.Linearas one matrix multiply, the form a GPU executes in parallel. ↩ - Goodfellow, Deep Learning, §6.1 — Example: Learning XOR: the explicit two-unit ReLU network that solves the canonical non-linearly-separable problem a single perceptron cannot. ↩
- Goodfellow, Deep Learning, §6.4 — Architecture Design: depth multiplies the number of linear regions a piecewise-linear network can express, an exponential advantage over width. ↩
- Primary sources: He, Zhang, Ren & Sun,
Deep Residual Learning for Image Recognition
(CVPR 2016) for the residual block ; Vaswani et al.,Attention Is All You Need
(NeurIPS 2017) for the position-wise feed-forward MLP inside each Transformer layer; Tolstikhin et al.,MLP-Mixer: An all-MLP Architecture for Vision
(NeurIPS 2021) for an attention-free network built from MLPs alone. ↩
╌╌ END ╌╌