All courses

MLPs and why nonlinearities matter

Show that stacked linear layers collapse into a single linear map. Explain how an elementwise nonlinearity between them lets an MLP represent complex functions.

Builds on: Weights, parameters and layers

▶ Watch this lesson Start the whole course

What the animation says

Why not just stack?

A layer takes its input and multiplies it by a matrix of weights. Follow one number through two layers: times two, then times three. Double the input, and every output doubles too. Linear maps stretch and mix, but they never bend. So here is the tempting idea: depth is power, so stack a hundred of these layers. What can a hundred stacked linear layers compute?

The collapse

Only what one layer can. Here is why, biases included. Two layers: x times W one, plus b one. Then times W two, plus b two. Multiply it out: x times the product W one W two, plus a fixed bias. That product is one matrix. Four by sixteen times sixteen by four is four by four. The wide middle vanishes. In numpy, this line prints True. Now put a ReLU between the two layers. Does the merge survive?

A switch makes a hinge

It can't merge. With ReLU in between, this line prints False. ReLU takes the max of zero and its input: negatives become zero, positives pass through. So each hidden unit becomes a hinge: flat on one side, sloped on the other. That is a multi-layer perceptron: multiply by W one, apply ReLU to each unit separately, multiply by W two. Width d goes out to d-f-f hidden hinges, and back to d.

|x| from two hinges

Let's build a bend from just two hinges. One input number. W one is one and minus one. W two adds the units up. Take x equal to minus three. The hidden units get minus three and three. ReLU zeroes the minus three and keeps the three. Add them up: three, the absolute value of minus three. Now x equals two. Which hidden unit stays on?

Hinges make any shape

Unit one passes two, unit two is zeroed, so the output is two. Again, the absolute value. Plot every input and you get a V: a real bend, built from two hinges. Remove the ReLU and the weights merge: one minus one is zero. A flat line. With more hinges, placed at different points, the sum can trace almost any continuous curve on a bounded range. Stacked linear layers collapse into one. The nonlinearity between them is where the power comes from.

Why not just stack matrix multiplies?

A layer multiplies its input by a matrix of weights. If depth is what gives a network power, why not stack 100 matrix multiplies and stop there? It turns out 100 such layers can compute **nothing** that a single layer can't. So what exactly does the small nonlinear function between layers add, and why does it let the network represent complicated functions?

Linear maps can't bend; switches can

A linear map can stretch, rotate and mix its inputs, but it never bends: doubling the input doubles the output. Composing two linear maps gives another linear map, so a stack of them collapses into one. Insert an elementwise switch like ReLU, $\max(0,\cdot)$, and each hidden unit becomes a **hinge**: flat on one side, sloped on the other. Adding many hinges placed at different points draws bent, piecewise-linear shapes.

The collapse, and the MLP that escapes it

Two linear layers on a row vector $x$, with biases $b$: $$(xW_1+b_1)W_2+b_2 = x\,(W_1W_2)+(b_1W_2+b_2)$$ That is one layer with weight $W_1W_2$. A multi-layer perceptron (MLP) puts an elementwise nonlinearity $\phi$ in between: $$\text{MLP}(x)=\phi(xW_1+b_1)\,W_2+b_2$$ With $x$ of width $d$, $W_1\in\mathbb R^{d\times d_{ff}}$ makes $d_{ff}$ hidden units, $\phi$ acts on each one separately, and $W_2\in\mathbb R^{d_{ff}\times d}$ maps back to width $d$. Because of $\phi$, $W_1$ and $W_2$ can no longer be merged into one matrix.

Building |x| from two ReLU units

Scalar input, $W_1=[1,\,-1]$, $W_2=[1;\,1]$, no biases. Take $x=-3$: $xW_1=[-3,\,3]$. ReLU gives $[0,\,3]$. Multiplying by $W_2$ gives $0+3=3=|{-3}|$. Take $x=2$: $[2,-2]\to[2,0]\to 2$. So $\text{ReLU}(x)+\text{ReLU}(-x)=|x|$, a function with a bend. Without the ReLU: $W_1W_2=1-1=0$, so the output is $0$ for every $x$.

Check the collapse numerically

The same claims with random weights: the plain stack equals one merged matrix, while the ReLU version does not. Then the two-unit $|x|$ network runs on three inputs.
import numpy as np
rng = np.random.default_rng(0)
x = rng.normal(size=(5, 4))               # 5 rows, d=4
W1, W2 = rng.normal(size=(4, 16)), rng.normal(size=(16, 4))
print(np.allclose((x @ W1) @ W2, x @ (W1 @ W2)))   # True: collapses
relu = lambda u: np.maximum(0, u)
print(np.allclose(relu(x @ W1) @ W2, x @ (W1 @ W2)))  # False
xs = np.array([[-3.], [-1.], [2.]])
print(relu(xs @ np.array([[1., -1.]])) @ np.array([[1.], [1.]]))  # 3,1,2

Remember this

Stacked linear layers multiply out into a single matrix (plus a bias), so depth alone adds nothing. An elementwise nonlinearity between layers turns each hidden unit into a hinge. Summing enough hinges can approximate essentially any continuous function on a bounded range, and that is where an MLP gets its power.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.