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
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.
Linear maps can't bend; switches can
The collapse, and the MLP that escapes it
Building |x| from two ReLU units
Check the collapse numerically
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
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.
Bytewise
All courses