AlgoPlusAlgoPlus
Learn/Machine Learning
Lesson

Multilayer Perceptron

Stacked layers of neurons with non-linear activations learn curved functions a single line never could.

8 min read Watch it move Build it

A multilayer perceptron (MLP) is the simplest deep network: a stack of layers, each a row of neurons. Every neuron multiplies its inputs by learned weights, adds a bias, and passes that sum through a non-linear activation. Feed numbers in one end, read the answer off the other — that's a forward pass. The layers in the middle, the *hidden* layers, are what let the network invent its own features and bend its decision surface into almost any shape.

What one neuron computes

A single neuron is just a weighted sum followed by a bend: a = activation(w·x + b). The weights w say how much each input matters; the bias b shifts the threshold. On its own that's a linear classifier — a straight cut. The power comes from stacking many of them and bending each layer's output.

function layer(x, W, b, act) {
  // x: input vector, W: weight matrix, b: bias vector
  return W.map((row, j) => {
    let sum = b[j];
    for (let i = 0; i < x.length; i++) sum += row[i] * x[i];
    return act(sum);            // the non-linear bend
  });
}
// forward pass = run layer after layer
const h = layer(x, W1, b1, relu);
const y = layer(h, W2, b2, softmax);

Why the hidden layers matter

  1. 1The input layer just holds the raw features — one slot per number.
  2. 2Each hidden layer reads the previous layer's outputs and builds new, combined features from them.
  3. 3The output layer turns the final features into the prediction (one number for regression, a probability per class for classification).
  4. 4Stacking non-linear layers is what lets the MLP model curved relationships, not just straight cuts.
Without the activation, depth is an illusion
Stack two *purely linear* layers and the math collapses: W2(W1 x) = (W2 W1) x, a single linear layer. The non-linear activation between layers is the only reason depth buys you anything — remove it and a 10-layer net is no stronger than one line.
Universal approximation
A wide enough MLP with one hidden layer can approximate *any* continuous function to arbitrary precision. In practice we go deeper rather than wider, because depth reuses features and reaches the same accuracy with far fewer neurons.
OperationTimeSpace
Forward pass (per example) · one multiply-add per connectionO(weights)O(neurons)
Check yourself
What breaks if you remove the non-linear activations from an MLP?