Neural networks and the multilayer perceptron
Layers, activations and what a hidden unit does
Chapter 2 of 21 · I. Foundations · about 35 minutes. Builds on: 1. Functions, parameters and gradient descent.
From a line to a neuron
Section titled “From a line to a neuron”A line has one input and two parameters. With several inputs , a neuron computes a weighted sum plus a bias, then passes it through a fixed non-linear function called an activation:
The weights say how much each input matters; the bias shifts the threshold; bends the result so the neuron can respond differently to different ranges of .
Layers are matrix multiplications
Section titled “Layers are matrix multiplications”Put neurons side by side and stack their weight vectors as the rows of a matrix (shape ). The whole layer is then one matrix–vector product:
is applied to each element of the vector. A layer with inputs and outputs has parameters and costs about arithmetic operations (one multiply and one add per weight). GPUs are built to do exactly these matrix products, which is the reason this architecture won.
A multilayer perceptron (MLP) feeds the output of one layer into the next:
The intermediate vectors , are called hidden layers. In practice you process a whole batch at once by making a matrix with one column per example; the same equations hold.
Why the non-linearity is essential
Section titled “Why the non-linearity is essential”If were the identity, then = (), a single matrix. A hundred linear layers would be exactly as expressive as one. The non-linearity is what lets depth add power.
Common activation functions
Section titled “Common activation functions”| Name | Formula | Notes |
|---|---|---|
| ReLU | Simple; zero for negative , identity for positive . | |
| tanh | Smooth, output between and 1; saturates for large . | |
| GELU | , = Gaussian CDF | Smooth ReLU-like curve ; used in many earlier transformers. |
| SiLU / Swish | , | Smooth, slightly negative dip ; the activation inside modern gated MLPs (Chapter 7). |
What does a one-hidden-layer network compute?
Section titled “What does a one-hidden-layer network compute?”Take one input , a hidden layer of ReLU units, and a single output:
Each hidden unit is a “ramp”: zero until reaches , then rising with slope . The output is a weighted sum of ramps, so the function is piecewise linear with a bend wherever a ramp starts. With enough ramps you can approximate any continuous function on an interval to any accuracy. This is the content of the universal approximation theorem. It says expressiveness is available, not that gradient descent will find it or that it will generalize.
Try it. The network below starts with random ramps. Press “Train” and watch gradient descent (Chapter 1) bend them into the target. Turn on “show each hidden unit’s contribution” to see the individual ramps whose sum is the orange curve. Then try one hidden unit, or switch to tanh or SiLU.
Depth versus width
Section titled “Depth versus width”A wide shallow network can in principle approximate anything, but a deep network can often represent the same function with far fewer units, because each layer builds on the features produced by the previous one. Concretely, composing ReLU layers can create a number of linear pieces that grows exponentially with depth. In language models the depth is tens to over a hundred layers (36 in gpt-oss-120b, 61 in DeepSeek-V3, 126 in Llama 3 405B), and each layer contains an MLP like the one above.
The MLP inside a transformer
Section titled “The MLP inside a transformer”In a transformer, the MLP is applied independently to each token’s vector. The model width is called . The classic version expands the vector to 4d, applies the activation, and projects back to :
That is weights per layer (ignoring biases), roughly two thirds of all non-embedding parameters. Chapter 7 introduces the gated version used by current models and Chapter 14 splits this block into many “experts”.
Check yourself
Section titled “Check yourself”How many parameters does a layer mapping 4096 inputs to 14336 outputs have (no bias)?
, about 58.7 million. (Those are the dimensions of one MLP matrix in Llama 3 8B.)
Why does a network with only linear activations not benefit from depth?
A product of matrices is a matrix, so any stack of linear layers collapses to a single linear map.
Where do the bends of a ReLU network sit?
At each hidden unit’s kink, , where crosses zero.
Papers and books
Section titled “Papers and books”- Chapter 6 (deep feedforward networks).
- Chapters 3–4 give a very clear geometric picture of shallow and deep networks as piecewise-linear functions.
- Introduces GELU.
- Introduces Swish (SiLU).