Multilayer Perceptron (MLP): Stacking Layers in a Neural Network
A neuron with a ReLU puts just one bend into a line. Put neurons side by side into a layer, stack layers on top of each other, and the bends multiply until the line can follow a curve. Change the number of layers and neurons and see how closely you can follow the ridge.
- POINT 1Neurons side by side are a layer. An input layer, hidden layers and an output layer, stacked, are a multilayer perceptron (MLP). The work of one layer: multiply by a matrix, add the biases, apply the activation function.
- POINT 2A wider layer has more bends and fits the curve better (one hidden layer, 1 → 3 → 8 → 24 neurons: error 22% → 16% → 5% → 1.4%). Stacking makes the bends grow even faster (6 neurons as 2 layers of 3 make 9 bends).
- POINT 3Gemma 3 4B has one MLP on each of its 34 floors. It widens 2,560 numbers to 10,240 and brings them back. About 69% of all the weights are in these MLPs.
TRY 1
Add layers and neurons to fit the curve
The dashed line is the curve to follow (a mountain ridge; its shape is an example). The golden line is the output of the network. Choose the number of hidden layers (1 to 4) and the neurons per layer (1 to 24): you get the result of really training a network of that shape. The 3D picture above is that network itself. Input at the bottom, output at the top; one ring is one layer, the threads are the weights.
Hidden layers (stacking)
Neurons per layer (side by side)
Activation function
Error 15.9%1 × 3 neurons
Results of the 20 starting points (green = the best fit, the one in the graph; red = stayed almost a straight line)
One hidden layer of 3 neurons: at most 3 bends. This one has 3 bends and an error of 15.9%. Add neurons, or stack layers.
| Ways to arrange 3 neurons | Weights | Bends | Best | Middle of 20 |
|---|---|---|---|---|
| 1 × 3 neurons | 10 | 3 | 15.9% | 19.3% |
| 3 × 1 neurons | 8 | 2 | 23.0% | 24.8% |
Every shape was trained under the same conditions (81 points of the curve, 6,000 corrections, 20 starting points). The graph and the 3D picture show the best of the 20. Error = the mean gap between output and curve, as a share of the mountain's height. The golden line, the error and the bends are computed on this page from the recorded weights. Training magnifies tiny rounding differences, so training again with another program gives slightly different numbers (the trends are the same).
TRY 2
Adding up lines with one bend
Inside the network with one hidden layer of 3 neurons. Each neuron makes a line with one bend (left). The output layer multiplies each by a weight and adds them: a line with 3 bends (right). Switch neurons off, or move the weights.
Each neuron's line, after its weight
The sum, and the ridge
−0.99 × neuron 1 + 1.11 × neuron 2 + −1.18 × neuron 3 + 0.42
Error 15.9%
These are the weights that training found (error 15.9%). Three neurons are not enough for three peaks.
The white circles on the left mark the bends (where a neuron's multiply-and-add is exactly 0). Where a bend sits is decided by the weights and biases of the hidden layer. Here you can only move the weights of the output layer.
TRY 3
Stacking a layer that folds
The extreme example from the video. A layer of 2 neurons folds the line back once and makes 1 peak. Each time the same layer is stacked, the number of peaks doubles. Move the number of layers.
1 layers, 2 neurons: 1 peaks (1 bends). To make the same shape with a single hidden layer you need 1 neurons, because one ReLU neuron can make only one bend.
This is a special example with weights set by hand, not a trained result (one layer: h1 = ReLU(2a), h2 = ReLU(2a − 1), next a = h1 − 2 × h2). The peaks and bends are really counted.
TRY 4
Wider or deeper; inside Gemma
Results of training from 20 different starting points under the same conditions (1 bead = 1 run, green = the best run, red = stayed almost a straight line). The first two rows have the same 145 weights. The next two have the same 8 neurons. The last one has no activation function.
| Layers × neurons | Activation | Weights | Best | Middle | Almost straight | The 20 runs |
|---|---|---|---|---|---|---|
| 1 × 48 | ReLU | 145 | 1.1% | 1.9% | 0 / 20 | |
| 4 × 6 | ReLU | 145 | 0.8% | 1.4% | 1 / 20 | |
| 1 × 8 | ReLU | 25 | 5.1% | 13.9% | 0 / 20 | |
| 4 × 2 | ReLU | 25 | 14.3% | 24.8% | 16 / 20 | |
| 3 × 8 | none | 169 | 24.8% | 24.8% | 20 / 20 |
With the same 145 weights, 4 layers of 6 (0.8%) did better on this mountain than 1 layer of 48 (1.1%). But when the layers are too thin, as with 4 layers of 2, 16 of the 20 runs stay almost a straight line. Without an activation function, any number of layers gives one straight line (error 24.8%).
The MLP on every floor of Gemma
2,560 → 10,240 → 2,560
| Matrix (its name in the file) | Job | Weights |
|---|---|---|
| ffn_gate | 2,560 → 10,240, GELU | 26,214,400 |
| ffn_up | 2,560 → 10,240 | 26,214,400 |
| ffn_down | 10,240 → 2,560 | 26,214,400 |
78,643,200 × 34 = 2,673,868,800
Gemma 3 4B's MLP widens 2,560 numbers 4 times, to 10,240, and brings them back to 2,560. Two matrices do the widening (gate and up); only the output of gate goes through GELU, it is multiplied by the output of up, and the third matrix, down, brings it back. Over 34 floors that is 102 matrices and 2,673,868,800 weights: about 69% of all the model's weights (3,880,099,328).
The names and sizes of the matrices come from the model file of Gemma 3 4B (GGUF); how they are used was checked in llama.cpp's source. This MLP has no biases. The rest of the weights are the embedding table and the mechanism that lets tokens look at each other (much later in the series).
CHECK
Check yourself
Three questions on what this episode covered.
Q1What is a hidden layer?
Q2One hidden layer of 6 ReLU neurons. How many bends can the output have at most?
Q3What does Gemma 3 4B's MLP do with its 2,560 numbers?
NEXT #29
The Softmax Function: Turning Logits into Probabilities
Next time: the softmax function. It turns the unruly numbers a network puts out (logits) into probabilities that add up to 100%.
The golden line, the error and the bends are computed in your browser. Nothing is sent to a server. The weights of the networks were really trained beforehand (the shape of the mountain is an example). Gemma's MLP was checked in the model file of Gemma 3 4B and in llama.cpp's source.
FINISH
COMMENTS
Comments & questions
Something unclear, something you want to know more about, or just your thoughts: write it here. We answer quickly, and other learners can reply too.
Loading…