All posts

The multilayer perceptron

/AI/4 min read

Stack layers of neurons with a bend between them and you can approximate any reasonable function. Each ReLU adds one kink: 4 of them fit a sine wave to within 0.21, 32 to within 0.0048. Inside every transformer, an MLP holds two-thirds of each layer's parameters.

The multilayer perceptron — MLP — is the original deep neural network and still one of the most used. It is a stack of fully connected layers: every neuron in one layer is connected to every neuron in the next, and each layer's output passes through a non-linear activation before the next layer sees it.

h₁ = activation(x W₁ + b₁) h₂ = activation(h₁ W₂ + b₂) y = h₂ W₃ + b₃

There is nothing else in it: no convolutions, no attention, no memory. Yet it can learn almost any input-output mapping, and it is a core component of every large language model.

What each layer adds

A single layer computes weighted sums, which can only describe flat relationships: straight lines, planes. The activation function is what adds shape. With ReLU — max(0, z) — each hidden neuron contributes a function that is zero on one side of a threshold and rises linearly on the other: a single kink.

Add those kinked pieces together with different thresholds and slopes and you can build any piecewise-linear curve. And piecewise-linear curves can follow any smooth curve as closely as you like, given enough pieces.

How many neurons a curve needs

Take one period of a sine wave. A hidden layer of N ReLU units can produce a curve with N straight segments — kinks at chosen points — and the best such curve follows the sine with an error that shrinks as segments are added:

ReLU unitsmaximum error
40.2105
80.0704
160.0188
320.0048

Each doubling of the units cuts the error by about four. That is the intuition behind the universal approximation theorem: a network with one hidden layer and enough units can approximate any continuous function on a bounded range to any desired accuracy.

The theorem says it is possible. It does not say the number of units is small, or that training will find the weights. For complicated functions, a single wide layer can need astronomically many units. Depth helps: a second layer can fold and combine the first layer's pieces, so deep MLPs represent many functions with far fewer total neurons than shallow ones.

EACH RELU IS ONE KINK 4 ReLUs · max error 0.21 16 ReLUs · max error 0.019 double the units, quarter the error — enough kinks follow any smooth curve
Grey: the target. Dark: the best piecewise-linear fit a layer of that many ReLUs can make.

Counting parameters

Every connection is a weight and every neuron has a bias, so a layer from a inputs to b outputs has a × b + b parameters. A classic MLP for 28 × 28 handwritten digits, with hidden layers of 256 and 128:

784×256 + 256 + 256×128 + 128 + 128×10 + 10 = 235,146 parameters

Almost all of them are in the first layer, because it connects to every pixel. That is the MLP's main weakness on images: it treats every pixel as an unrelated input, with no idea that neighbouring pixels belong together. A cat moved two pixels to the left is, to an MLP, an entirely new input. Convolutional networks were invented to fix exactly that.

The MLP inside every transformer

Each transformer layer has two parts. Attention moves information between positions in the sequence. Then an MLP — usually called the feed-forward network — processes each position on its own: expand the vector to about four times its width, apply an activation, project back down.

For a model of width 4,096:

part of one transformer layerparameters
attention (queries, keys, values, output)67M
MLP (4,096 → 16,384 → 4,096)134M

The MLP holds two-thirds of the layer's parameters, and therefore most of the model's. Research into what models store suggests much factual knowledge lives in these MLP weights: attention gathers the context, the MLP looks up and transforms what that context means. Mixture-of-experts models replace this one MLP with many smaller ones and route each token to a few — which is why "experts" are MLPs.

Training it

An MLP is trained like any network: forward pass, loss, backpropagation, gradient step. Practical concerns that apply to it directly:

  • Activation choice. ReLU and its smooth relatives (GELU, SiLU) train well in deep stacks; sigmoids saturate and slow learning in many layers.
  • Initialisation. Weights must be scaled to the layer width so signals neither vanish nor explode as they pass through.
  • Overfitting. With hundreds of thousands of parameters and modest data, an MLP can memorise; dropout, weight decay and early stopping keep it general.

What to take away

A multilayer perceptron is fully connected layers with non-linear activations between them. Each ReLU unit contributes one kink, and enough kinks follow any smooth curve: 4 units fit a sine wave to within 0.21, 32 to within 0.0048. Depth lets layers combine pieces efficiently.

MLPs struggle with structure like images on their own, but they are central to modern models: in a transformer, the per-position MLP holds about two-thirds of every layer's parameters.