The Multilayer Perceptron

Feed-forward networks with hidden layers, built and trained from scratch. The forward pass in matrix form, the loss, gradient descent and minibatches, backpropagation derived with the chain rule and worked by hand on one tumor, gradients checked by finite differences, why initial weights must be random, a NumPy network compared in 30 seeded splits with logistic regression and scikit-learn's MLPClassifier on two moons, breast cancer and wine data, overfitting in a 74,000-parameter network and early stopping, and the universal approximation theorem with what it does not promise.

Backpropagation computes every gradient of a feed-forward network in one backward pass. Each unit's error is $\delta_i^{(l-1)} = g'(z_i^{(l-1)})\sum_j w_{ij}^{(l)}\delta_j^{(l)}$, and each weight's gradient is the error at its destination times the activation at its source. A NumPy version agrees with finite differences to about ten digits. Over 30 seeded datasets, one hidden layer of 8 ReLU units beat logistic regression on two moons (median test accuracy 0.9358 against 0.8614). On breast cancer (0.9649 against 0.9737) and wine data (0.9722 for both), a hidden layer gained nothing; both are linearly separable on their training parts. Without early stopping, a 73,985-parameter network overfit mostly in its log loss. One hidden layer can approximate any continuous function on a compact set, but that theorem promises nothing about the number of units, training or generalization.

Two hundred points in the plane form two interleaving crescents, the "two moons" of scikit-learn's make_moons, with enough noise that the crescents overlap a little. No straight line separates them, so logistic regression, whose boundary is a line, classifies a median of 86.1% of fresh test points correctly over 30 simulated datasets. A network with one hidden layer of eight units, trained on the same points, bends its boundary around the crescents and classifies 93.6% correctly. On the breast cancer biopsies used throughout this collection, the same kind of network does no better than logistic regression, and by some measures slightly worse. Both results are typical, and this article explains both.

A multilayer perceptron (MLP) is a feed-forward network with one or more hidden layers of units between the inputs and the output, each unit applying a nonlinear activation function to a weighted sum of the layer before (Introduction to Artificial Neural Networks describes the structure). The name is historical. The units are not perceptrons, whose step activation gives no gradient, and the network is not trained by the perceptron rule of The Single-Layer Perceptron. It is trained by gradient descent, with the gradients computed by backpropagation. This article derives backpropagation, checks it numerically, builds a network in NumPy, and compares it with logistic regression and with scikit-learn's implementation on real data.

The forward pass

Write the training data as a matrix $X$ with one row per example and one column per input, $n$ rows and $p$ columns. A network with $L$ layers of weights has, for each layer $l = 1, \ldots, L$, a weight matrix $W^{(l)}$ of shape $n_{l-1} \times n_l$, whose column $j$ holds the incoming weights of unit $j$, and a bias vector $\mathbf{b}^{(l)}$ of length $n_l$. Here $n_0 = p$ and $n_l$ is the number of units in layer $l$. Starting from $A^{(0)} = X$, each layer computes

$$Z^{(l)} = A^{(l-1)} W^{(l)} + \mathbf{1}\,\mathbf{b}^{(l)\mathsf{T}}, \qquad A^{(l)} = g\big(Z^{(l)}\big),$$

where $\mathbf{1}$ is a column of $n$ ones (adding the bias to every row) and the activation $g$ is applied to every entry. Row $i$ of $Z^{(l)}$ holds the weighted sums of layer $l$ for example $i$, and row $i$ of $A^{(l)}$ its activations. The last layer replaces $g$ with the output function: a sigmoid for two classes, giving one probability per example, a softmax for $K$ classes, giving a row of $K$ probabilities, or the identity for a numerical outcome (see Nonlinear Activation Functions). Computing every layer in turn, from the inputs to the output, is the forward pass. Processing all examples as rows of one matrix turns the work into a few matrix products, which is how every practical implementation runs.

The loss

Training chooses the weights and biases, collected into one long vector $\boldsymbol{\theta}$, to make a loss small on the training data. For two classes with labels $y_i \in \{0, 1\}$ and predicted probabilities $p_i$, the usual loss is the mean cross-entropy,

$$L(\boldsymbol{\theta}) = -\frac{1}{n}\sum_{i=1}^{n} \big[\,y_i \ln p_i + (1 - y_i) \ln (1 - p_i)\,\big],$$

the negative log-likelihood of logistic regression divided by $n$. For $K$ classes it is $-\frac{1}{n}\sum_i \ln p_{i, y_i}$, the mean negative log of the probability given to the true class, and for a numerical outcome it is usually the mean squared error. Cross-Entropy and Kullback–Leibler Divergence explains the name cross-entropy and proves that this loss is an average negative log-likelihood. Loss Functions for Classification and Loss Functions for Regression discuss these criteria; Hastie, Tibshirani and Friedman (2009, §11.4) and James, Witten, Hastie and Tibshirani (2021, §10.2) use the same ones for networks. With a softmax output and the cross-entropy, the output layer of a network is a multinomial logistic regression whose inputs are the last hidden layer's activations.

Gradient descent

The loss of a network with hidden layers has no closed-form minimum, and unlike the log-likelihood of logistic regression it is not convex, so it has multiple solutions (James et al., 2021, §10.7) and can have many local minima (Hastie et al., 2009, §11.5.5). Some of the multiplicity is exact: permuting the units of a hidden layer, together with their incoming and outgoing weights, leaves the function unchanged, so every solution comes with many equivalent copies (Worked problem 3 proves this and gives a lower bound on their number). Networks are fitted by gradient descent. The gradient $\nabla L(\boldsymbol{\theta})$, the vector of partial derivatives of the loss with respect to every parameter, points in the direction in which the loss increases fastest. A small step the other way,

$$\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta\, \nabla L(\boldsymbol{\theta}),$$

decreases the loss if the learning rate $\eta$ is small enough (James et al., 2021, eq. 10.27). Repeated, the steps descend until the gradient is nearly zero, at a minimum or a flat region. Too small a learning rate makes progress slow; too large a one overshoots and can diverge, as the experiment with dying units in Nonlinear Activation Functions shows.

Computing the exact gradient needs a pass over all $n$ examples. Stochastic gradient descent instead estimates it from a small random minibatch of examples, takes a step, and moves on to the next minibatch; one pass through the whole training set in minibatches is an epoch (James et al., 2021, §10.7.2). The steps are noisier but far cheaper, so many more of them can be taken. The network in this article uses minibatches of 32, shuffled afresh each epoch, and a fixed learning rate. Refinements such as momentum and the Adam optimizer, which adapt the step to the history of the gradients, are what most libraries use; The Bigram Language Model trains with Adam in PyTorch.

Backpropagation

Gradient descent needs the derivative of the loss with respect to every weight in every layer. Backpropagation computes all of them with one pass backward through the network, using the chain rule and the quantities already stored by the forward pass. Rumelhart, Hinton and Williams (1986) presented it as "a new learning procedure" that "repeatedly adjusts the weights of the connections in the network so as to minimize a measure of the difference between the actual output vector of the net and the desired output vector"; Hastie et al. (2009, bibliographic notes to Ch. 11) credit the algorithm to Werbos (1974) and Parker (1985) as well as to Rumelhart, Hinton and Williams.

One unit at a time

Consider one example and a unit $j$ in layer $l$, with weighted sum $z_j^{(l)} = \sum_i a_i^{(l-1)} w_{ij}^{(l)} + b_j^{(l)}$ and activation $a_j^{(l)} = g(z_j^{(l)})$. Define the unit's error

$$\delta_j^{(l)} = \frac{\partial L}{\partial z_j^{(l)}},$$

how much the loss would change per unit change in the unit's weighted sum. Two applications of the chain rule give everything else.

From errors to weight gradients. The weight $w_{ij}^{(l)}$ affects the loss only through $z_j^{(l)}$, and $\partial z_j^{(l)} / \partial w_{ij}^{(l)} = a_i^{(l-1)}$, so

$$\frac{\partial L}{\partial w_{ij}^{(l)}} = \delta_j^{(l)}\, a_i^{(l-1)}, \qquad \frac{\partial L}{\partial b_j^{(l)}} = \delta_j^{(l)}.$$

The gradient of a weight is the error at its destination times the activation at its source.

From errors in one layer to errors in the layer below. The weighted sum $z_i^{(l-1)}$ of unit $i$ in the layer below affects the loss through its activation $a_i^{(l-1)}$, which feeds every unit $j$ of layer $l$. Summing over those paths,

$$\delta_i^{(l-1)} = \sum_j \frac{\partial L}{\partial z_j^{(l)}}\,\frac{\partial z_j^{(l)}}{\partial a_i^{(l-1)}}\,\frac{\partial a_i^{(l-1)}}{\partial z_i^{(l-1)}} = \Big(\sum_j w_{ij}^{(l)}\,\delta_j^{(l)}\Big)\, g'\big(z_i^{(l-1)}\big).$$

Each unit's error is the weighted sum of the errors of the units it feeds, using the same weights as the forward pass but run backward, times the slope of its own activation. These are the back-propagation equations of Hastie et al. (2009, eq. 11.15).

At the output. The recursion needs a starting point. For a sigmoid output with the cross-entropy loss, $L = -[y \ln p + (1 - y)\ln(1 - p)]$ with $p = \sigma(z)$, and since $\sigma'(z) = p(1 - p)$,

$$\delta^{(L)} = \frac{\partial L}{\partial p}\,\frac{\partial p}{\partial z} = \left(-\frac{y}{p} + \frac{1 - y}{1 - p}\right) p\,(1 - p) = p - y.$$

For a softmax output with the cross-entropy, the same cancellation gives the vector $\mathbf{p} - \mathbf{y}$, with $\mathbf{y}$ the one-hot vector of the true class, and for an identity output with the loss $\tfrac12(\hat{y} - y)^2$ it gives $\hat{y} - y$ (the derivations are in Nonlinear Activation Functions and Logistic Regression). In all three, the output error is simply prediction minus target.

In matrix form

For a minibatch of $n$ examples and the mean loss, the same equations become matrix products. Let $\Delta^{(l)}$ be the $n \times n_l$ matrix of errors, row $i$ for example $i$. Then

$$\Delta^{(L)} = \tfrac{1}{n}\,(P - Y), \qquad \frac{\partial L}{\partial W^{(l)}} = A^{(l-1)\mathsf{T}}\,\Delta^{(l)}, \qquad \frac{\partial L}{\partial \mathbf{b}^{(l)}} = \Delta^{(l)\mathsf{T}}\,\mathbf{1},$$

$$\Delta^{(l-1)} = \big(\Delta^{(l)}\, W^{(l)\mathsf{T}}\big) \odot g'\big(Z^{(l-1)}\big),$$

where $P$ and $Y$ hold the predictions and targets, the factor $1/n$ comes from averaging the loss, the bias gradient sums each column of $\Delta^{(l)}$ over the examples, and $\odot$ multiplies entry by entry. The matrix $A^{(l-1)\mathsf{T}}\Delta^{(l)}$ has the shape of $W^{(l)}$, and its entry $(i, j)$ is $\sum_{\text{examples}} a_i^{(l-1)} \delta_j^{(l)}$, the one-unit formula summed over the minibatch.

The algorithm is therefore: run the forward pass, storing every $Z^{(l)}$ and $A^{(l)}$; compute $\Delta^{(L)}$ at the output; run the recursion down to layer 1, computing each layer's weight and bias gradients as its error matrix becomes available; then update all the weights with one gradient descent step. The backward pass costs about the same as the forward pass, a few matrix products per layer, which is why gradients for networks with millions of weights are affordable. The backward pass works from the output toward the inputs, so the gradients near the output are computed first; the update itself is applied to all layers together, once every gradient is known.

Worked by hand

Take a network with two inputs, two ReLU hidden units and a sigmoid output, and one example: tumor 0 of the breast cancer data (Wolberg, Mangasarian, Street and Street, 1993), malignant, so $y = 1$, with its mean radius and mean texture standardized over all 569 tumors, $\mathbf{x} = (1.0971, -2.0733)$. The weights are chosen by hand: hidden unit 1 has weights 1 and 0.5 from the two inputs and bias 0; hidden unit 2 has weights −0.5 and 1 and bias −1; the output has weights 1.5 and −2 from the hidden units and bias −0.5.

Forward. The hidden weighted sums are

$$z_1 = 1(1.0971) + 0.5(-2.0733) + 0 = 0.0604, \qquad z_2 = -0.5(1.0971) + 1(-2.0733) - 1 = -3.6219.$$

The ReLU passes the first and zeroes the second: $a_1 = 0.0604$, $a_2 = 0$. The output's weighted sum is $z = 1.5(0.0604) - 2(0) - 0.5 = -0.4094$, so $p = \sigma(-0.4094) = 0.3991$, and the loss is $-\ln 0.3991 = 0.9187$. The network gives this malignant tumor a 40% chance of malignancy.

Backward. The output error is $\delta = p - y = -0.6009$. The output weights' gradients are this error times the hidden activations, $(-0.6009 \times 0.0604,\; -0.6009 \times 0) = (-0.0363, 0)$, and the output bias's is −0.6009. The hidden errors are the output error times the output weights times the ReLU slopes, which are 1 for the active unit and 0 for the inactive one:

$$\delta_1 = 1.5 \times (-0.6009) \times 1 = -0.9014, \qquad \delta_2 = -2 \times (-0.6009) \times 0 = 0.$$

The hidden weights' gradients are these errors times the inputs: for hidden unit 1, $(1.0971 \times -0.9014,\; -2.0733 \times -0.9014) = (-0.9889, 1.8689)$, and bias gradient −0.9014; for hidden unit 2, all zero. A negative gradient means increasing the weight lowers the loss, so the step will raise the weight from radius to unit 1 and lower the weight from texture, both of which raise $z_1$ for this smooth, large tumor, and raise the output weight from unit 1. Hidden unit 2 is off for this tumor and gets no gradient from it at all: a ReLU unit learns only from the examples that activate it.

The 2-2-1 network on tumor 0. Forward values are in white: hidden unit 1 is barely active, hidden unit 2 is switched off by its ReLU, and the output gives probability 0.3991 to a malignant tumor. Backward errors δ = ∂L/∂z are in yellow: the output's error p − y flows back through the weight 1.5 to hidden unit 1, and nothing flows to hidden unit 2.
The 2-2-1 network on tumor 0. Forward values are in white: hidden unit 1 is barely active, hidden unit 2 is switched off by its ReLU, and the output gives probability 0.3991 to a malignant tumor. Backward errors δ = ∂L/∂z are in yellow: the output's error p − y flows back through the weight 1.5 to hidden unit 1, and nothing flows to hidden unit 2.

A single gradient step with learning rate 0.5 changes the prediction for this tumor from 0.3991 to 0.9871 and the loss from 0.9187 to 0.0129 (Worked problem 1 carries the step through). A learning rate that large on a single example is only for illustration; in training, each step averages the gradient over a minibatch, and the many tumors pull the weights in different directions.

Checking the gradients

A backpropagation implementation with a mistake in it often still trains, slowly or badly, so the gradients should be checked against a definition that is hard to get wrong. The central finite difference perturbs one parameter $\theta_k$ by a small $h$ in each direction and measures the change in the loss,

$$\frac{\partial L}{\partial \theta_k} \approx \frac{L(\theta_k + h) - L(\theta_k - h)}{2h},$$

with an error proportional to $h^2$. It needs two forward passes per parameter, far too slow for training but fine for a check on a small network. For the hand-worked example, with $h = 10^{-6}$ in double precision, the nine backpropagated gradients agree with the finite differences to within $1.3 \times 10^{-10}$. For the network code used below, checked on 20 tumors and on 20 wines:

Network Parameters Largest absolute difference Relative difference
30-8-4-1, tanh 289 $9.3 \times 10^{-11}$ $1.8 \times 10^{-10}$
30-8-4-1, ReLU 289 $1.2 \times 10^{-10}$ $1.6 \times 10^{-10}$
13-8-3 softmax, tanh 139 $2.8 \times 10^{-10}$ $4.4 \times 10^{-10}$

The relative difference is the largest absolute difference divided by the largest gradient size. Agreement to about ten digits is what rounding allows with this $h$; an error in the code typically shows up as a disagreement in the first or second digit of some gradients. For ReLU networks, a check can fail spuriously if a weighted sum lies within $h$ of zero, where the function has a kink.

Starting values

The weights must start at different random values. If every hidden unit in a layer starts with the same weights, every unit computes the same output, receives the same error, and gets the same update, so the units stay identical forever and the layer acts as one unit. A 30-16-1 tanh network with every weight started at 0.05 was trained for 200 epochs on the breast cancer data: at the end, the largest difference between any two hidden units' weights was exactly zero, and a network with a single hidden unit, carrying the sum of the sixteen output weights, gave the same predictions to within $7 \times 10^{-16}$. Starting all weights at zero is worse still for ReLU units: every hidden weighted sum is zero, the ReLU's slope there is taken as zero, and the hidden layer's gradient is exactly zero, so it never moves. Hastie et al. (2009, §11.5.1) note both points: exact zero weights give "zero derivatives and perfect symmetry, and the algorithm never moves", while starting with large weights "often leads to poor solutions." They recommend random values near zero, so that sigmoid-like units start in their nearly linear range and the network starts close to a linear model and becomes nonlinear as the weights grow. How near zero matters more as networks deepen: the activation functions article measures the effect and derives the Glorot and He initializations, which the network below uses.

The inputs should be standardized for the same reason. The scale of the inputs sets the effective scale of the first layer's weights, and with inputs of mean zero and standard deviation one, a single range of starting weights suits every input (Hastie et al., 2009, §11.5.3). The breast cancer measurements range from values below 0.03 (the standard error of the fractal dimension) to areas above 4,000; unstandardized, a few large inputs would dominate every weighted sum.

A network from scratch

The network used below is about 110 lines of NumPy, not counting comments, in tools/experiments/data-science/nnet.py in the site's repository. Its core is the forward pass, the backward pass and the training loop:

import numpy as np

def forward(params, X, g):
    A, Z = [X], []
    for l, (W, b) in enumerate(zip(params["W"], params["b"])):
        Z.append(A[-1] @ W + b)
        if l < len(params["W"]) - 1:
            A.append(g(Z[-1]))
    P = 1 / (1 + np.exp(-Z[-1]))         # sigmoid output (two classes)
    return P, A, Z

def backward(params, A, Z, P, y, g_prime):
    n = len(y)
    D = (P - y.reshape(-1, 1)) / n       # output errors, mean loss
    grads_W, grads_b = [], []
    for l in reversed(range(len(params["W"]))):
        grads_W.insert(0, A[l].T @ D)
        grads_b.insert(0, D.sum(axis=0))
        if l > 0:
            D = (D @ params["W"][l].T) * g_prime(Z[l - 1])
    return grads_W, grads_b

def train(params, X, y, g, g_prime, lr=0.05, epochs=100, batch=32, seed=0):
    rng = np.random.default_rng(seed)
    for _ in range(epochs):
        order = rng.permutation(len(y))
        for start in range(0, len(y), batch):
            idx = order[start:start + batch]
            P, A, Z = forward(params, X[idx], g)
            gW, gb = backward(params, A, Z, P, y[idx], g_prime)
            for l in range(len(gW)):
                params["W"][l] -= lr * gW[l]
                params["b"][l] -= lr * gb[l]
    return params

The version in the repository adds a softmax output for more than two classes, a numerically stable sigmoid, the initialization schemes, and early stopping: after each epoch it computes the loss on a separate validation set, keeps a copy of the weights from the epoch with the lowest validation loss, stops when that loss has not improved for a set number of epochs (the patience), and returns the copy.

Where hidden layers help: two moons

For each of 30 seeds, 200 training points, 200 validation points and 5,000 test points were drawn from make_moons with noise 0.25. Logistic regression (no penalty) was fitted to the training points, and networks with one hidden layer of 2, 8 or 64 ReLU units were trained by minibatch gradient descent (learning rate 0.1, He initialization), with early stopping on the validation points (patience 100 epochs, at most 3,000).

Model Test accuracy: median (IQR)
Logistic regression 0.8614 (0.8584–0.8626)
Network, 2 hidden units 0.8640 (0.8587–0.8731)
Network, 8 hidden units 0.9358 (0.9328–0.9395)
Network, 64 hidden units 0.9359 (0.9326–0.9375)
Decision boundaries on the first of the 30 two-moons datasets, with the 200 training points; the titles give that dataset's test accuracy and the median over all 30. Logistic regression draws a line. Two ReLU units can only bend the line slightly, and here they barely bend it at all. Eight units fold the boundary around the tips of the crescents; sixty-four give a similar, somewhat smoother fold.
Decision boundaries on the first of the 30 two-moons datasets, with the 200 training points; the titles give that dataset's test accuracy and the median over all 30. Logistic regression draws a line. Two ReLU units can only bend the line slightly, and here they barely bend it at all. Eight units fold the boundary around the tips of the crescents; sixty-four give a similar, somewhat smoother fold.

Two hidden units are not enough: a ReLU network's boundary is made of straight pieces, roughly one bend for each unit, and the median result with two units was barely better than logistic regression's line. Eight units were enough to follow the crescents. Sixty-four did no better and no worse, although they have nearly eight times as many parameters (257 against 33), because early stopping halted them before they could fit the noise in 200 points; the next section shows what happens without it.

Where they do not: breast cancer and wine

The breast cancer data have 569 tumors and 30 measurements. In each of 30 splits (seeds 0 to 29), the tumors were divided at random, stratified by diagnosis, into 341 for training, 114 for validation and 114 for testing, and standardized using the training part. Three models were fitted to the training part: the NumPy network with one hidden layer of 16 ReLU units (513 parameters; learning rate 0.05, patience 50, at most 1,000 epochs, early stopping on the validation part); logistic regression with scikit-learn's default L2 penalty; and scikit-learn's MLPClassifier with one hidden layer of 16 units, up to 1,000 epochs, and its other settings at their defaults (ReLU, the Adam optimizer, a small L2 penalty, and stopping when the training loss stops improving). The network stopped at a median of epoch 73 (IQR 48 to 140).

Model Test accuracy: median (IQR) Test log loss: median (IQR)
NumPy network, 30-16-1 0.9649 (0.9561–0.9803) 0.0858 (0.0693–0.1166)
Logistic regression 0.9737 (0.9649–0.9825) 0.0764 (0.0680–0.1050)
scikit-learn MLPClassifier, 16 units 0.9737 (0.9649–0.9803) 0.0763 (0.0606–0.1206)

Compared split by split with the NumPy network, logistic regression was more accurate in 13 splits, less accurate in 8 and tied in 9, and had lower log loss in 20 of the 30, by a median of 0.0084. scikit-learn's network was more accurate in 15, less in 7, tied in 8, with lower log loss in 17. On 114 test tumors, one tumor is 0.9 percentage points of accuracy, and the differences between the three models are smaller than the differences between splits.

The wine data (Aeberhard and Forina, 1992) have 178 wines, 13 chemical measurements and three cultivars, so the network ends in a softmax over three outputs (13-16-3, 275 parameters). With 106 wines for training, 36 for validation and 36 for testing in each split, all three models had a median test accuracy of 0.9722 (IQR 0.9722 to 1.0000), one wrong wine in 36; logistic regression and the NumPy network tied on accuracy in 19 of the 30 splits. The median test log losses were 0.0747 for the NumPy network, 0.0686 for logistic regression and 0.0654 for scikit-learn's network.

These outcomes fit what is known of the two datasets. Both are linearly separable on their training parts once standardized: the perceptron article found this for all 30 breast cancer training sets, and a linear program finds each wine cultivar separable from the other two in all 30 wine training parts. So logistic regression's boundary is already close to the best one these data allow, and a likely reason the network gains nothing is that its extra flexibility buys only extra variance. With a few hundred examples, the variance of a network's fitted probabilities shows in the log loss before it shows in the accuracy. The two-moons problem is the opposite case: abundant data from a boundary that is far from linear.

Overfitting and early stopping

A network with enough units can fit its training data almost perfectly, noise included, which is the high-variance end of the bias-variance tradeoff. To see it, a much larger network, 30-256-256-1 with 73,985 parameters, more than 200 for each of the 341 training tumors, was trained on the same 30 breast cancer splits for 1,000 epochs without stopping, recording the loss on the training and validation parts after every epoch.

Training and validation cross-entropy for the 30-256-256-1 network on the first breast cancer split, both axes logarithmic. The two fall together for about ten epochs. The training loss then keeps falling toward zero, while the validation loss levels off, reaches its minimum at epoch 38 and creeps upward for the rest of the run.
Training and validation cross-entropy for the 30-256-256-1 network on the first breast cancer split, both axes logarithmic. The two fall together for about ten epochs. The training loss then keeps falling toward zero, while the validation loss levels off, reaches its minimum at epoch 38 and creeps upward for the rest of the run.

On the first split, the validation loss reached its lowest value, 0.0423, at epoch 38, when the training loss was 0.0165; by epoch 1,000 the training loss was 0.0002 and the validation loss had risen to 0.0515. Over the 30 splits:

Quantity At the epoch of lowest validation loss At epoch 1,000
Epoch median 23.5 (IQR 12.3–42.5) 1,000
Training loss 0.0002 (0.0002–0.0003)
Validation loss 0.0709 (0.0414–0.1091) 0.1446 (0.0587–0.2531)
Test log loss 0.0840 (0.0602–0.1153) 0.1430 (0.0808–0.1933)
Test accuracy 0.9737 (0.9649–0.9803) 0.9649 (0.9561–0.9737)

The test log loss was higher at the end than at the best validation epoch in 25 of the 30 splits, by a median of 0.0509. Test accuracy changed much less: it was higher at the end in 7 splits, lower in 8 and the same in 15. Overfitting here shows mostly in the probabilities. The fully trained network is as often right as the stopped one, but it is confidently right and sometimes confidently wrong, and the log loss charges heavily for confident mistakes. Judging the network by accuracy alone would have hidden most of the damage.

Early stopping trains while watching a validation loss and keeps the weights from the epoch where it was lowest. Hastie et al. (2009, §11.5.2) describe it as one of the original ways of avoiding overfitting in neural networks, and note that since training starts from nearly linear small weights, stopping early "has the effect of shrinking the final model toward a linear model." The stopping epoch is chosen on the validation data, which makes the validation loss at that epoch an optimistic estimate, the winner's curse described in Training, Validation, and Test Sets; that is why the table judges the result on test data that played no part in the choice. The other common remedies limit the weights directly. Weight decay adds a penalty $\lambda \sum w^2$ to the loss, the network analogue of ridge regression (Hastie et al., 2009, §11.5.2), which Ridge and Lasso Regression derives for linear models, and is the alpha parameter of scikit-learn's networks. Dropout sets a random fraction of the units to zero at each training step so that no unit can rely on particular others (James et al., 2021, §10.7.3); Scaling the Shakespeare GPT applies it in a transformer. Reducing the number of units or layers lowers the capacity to overfit, as for any model. A smaller learning rate slows training down, which with a fixed number of epochs acts like stopping earlier, but on its own it delays overfitting rather than preventing it.

Universal approximation

How flexible can a network with one hidden layer be? Cybenko (1989) proved that it can approximate any continuous function on the unit cube as closely as desired. In his Theorem 2, for any continuous sigmoidal activation $\sigma$ (one that tends to 1 as its input goes to $+\infty$ and to 0 as it goes to $-\infty$), sums of the form

$$G(\mathbf{x}) = \sum_{j=1}^{N} \alpha_j\, \sigma(\mathbf{y}_j^{\mathsf{T}}\mathbf{x} + \theta_j)$$

are dense in the continuous functions on $[0, 1]^p$: for any continuous $f$ and any $\varepsilon > 0$ there is such a sum with $|G(\mathbf{x}) - f(\mathbf{x})| < \varepsilon$ for every $\mathbf{x}$ in the cube. That sum is exactly a network with one hidden layer of $N$ sigmoid units and a linear output. Hornik, Stinchcombe and White (1989) obtained related results at about the same time. Hornik (1991) extended the result to any continuous, bounded and nonconstant activation, on any compact set of inputs (and, for approximation in $L^p$ norms rather than uniformly, to any bounded and nonconstant one), and Leshno, Lin, Pinkus and Schocken (1993) gave the cleanest condition: a network with a locally bounded, piecewise continuous activation can approximate any continuous function "if and only if the network's activation function is not a polynomial." ReLU qualifies. The same paper stresses that the bias terms are needed; without them the result fails.

For ReLU units in one dimension, an approximation can be written down directly. The function

$$f_N(x) = f(0) + \sum_{j=0}^{N-1} c_j \max(0,\; x - j/N)$$

is piecewise linear with a bend at each knot $j/N$, and choosing $c_0$ as the slope of the first piece and each later $c_j$ as the change of slope at knot $j/N$ makes it pass through $f$ at all $N + 1$ knots. It is a network with one hidden layer of $N$ ReLU units with input weight 1 and biases $-j/N$. For $f(x) = \sin(2\pi x)$ on $[0, 1]$:

Hidden units $N$ 2 4 8 16 32 64
Largest error 1.0000 0.2105 0.0704 0.0188 0.0048 0.0012

(With $N = 2$ every knot falls on a zero of the sine, so the network is identically zero, and the error is the sine's full height.) The error falls by about four each time $N$ doubles, as the standard bound for linear interpolation, $\max|f''|/(8N^2) = (2\pi)^2/(8N^2)$, predicts (Worked problem 2 derives it); at $N = 64$ the two agree to three decimal places.

Networks of 4 and 16 ReLU units built to pass through sin(2πx) (dashed) at equally spaced knots (dots). Each unit adds one bend. Four units give a zigzag with largest error 0.211; sixteen follow the curve to within 0.019.
Networks of 4 and 16 ReLU units built to pass through sin(2πx) (dashed) at equally spaced knots (dots). Each unit adds one bend. Four units give a zigzag with largest error 0.211; sixteen follow the curve to within 0.019.

The theorem is often quoted as if it explained why networks work, and it promises much less than that. It is a statement about existence. It does not say how many hidden units are needed; Cybenko himself wrote that "we suspect quite strongly that the overwhelming majority of approximation problems will require astronomical numbers of terms," because of the curse of dimensionality. The construction above suggests why, for constructions of this kind: a grid of knots fine enough to follow a function in one dimension needs $N$ units, and a grid of the same spacing in $p$ dimensions has $N^p$ cells, each needing its own piece. It does not say that gradient descent will find the approximating weights from a random start. And it says nothing about learning from data: a network flexible enough to represent any function is flexible enough to represent the noise in a finite sample, which is the overfitting of the previous section. Why large networks that fit their training data exactly can still predict well is a question the classical generalization bounds do not answer either (Why Classical Bounds Do Not Explain Deep Networks); the Library's note on Superposition, Memorization, and Double Descent describes one study of how a small network moves from memorizing to generalizing as its training set grows. In practice, networks with several layers of moderate width are easier to train than one very wide layer; James et al. (2021, §10.2) put it that one large hidden layer can approximate most functions in theory, but "the learning task of discovering a good solution is made much easier with multiple layers each of modest size."

Worked problems

The course notes behind this unit hold no assignments, so these problems extend the article on its own examples. The script checks every number.

Problem 1 (the gradient step on tumor 0, carried through). Apply one gradient descent step with learning rate 0.5 to the hand-worked 2-2-1 network, and recompute the forward pass to confirm the new prediction of 0.9871.

Solution. Each parameter moves by $-0.5$ times its gradient. Hidden unit 1's weights become $1 - 0.5(-0.9889) = 1.4945$ and $0.5 - 0.5(1.8689) = -0.4345$, and its bias $0 - 0.5(-0.9014) = 0.4507$. Hidden unit 2 had zero gradients and keeps its weights $(-0.5, 1)$ and bias $-1$. The output weights become $1.5 - 0.5(-0.0363) = 1.5181$ and $-2$ (unchanged, its gradient was 0), and the output bias $-0.5 - 0.5(-0.6009) = -0.1995$. Forward again:

$$z_1 = 1.4945(1.0971) - 0.4345(-2.0733) + 0.4507 = 2.9910, \qquad z_2 = -3.6219 \text{ (unchanged)},$$

so $a_1 = 2.9910$ and $a_2 = 0$, and the output's weighted sum is $1.5181 \times 2.9910 - 0.1995 = 4.3413$, giving $p = \sigma(4.3413) = 0.9871$ and loss $-\ln 0.9871 = 0.0129$. (The intermediate values are rounded for display; the script carries full precision.) Most of the gain came through hidden unit 1, whose weighted sum rose from 0.0604 to 2.9910: its own weights moved in the direction that raises $z_1$ for this tumor, and its bias rose too. The step is large because the gradient is large: the output error $p - y = -0.6009$ is a confident mistake, and the cross-entropy passes it back undiminished. Hidden unit 2 stays off for this tumor, and nothing in this step can turn it on.

Problem 2 (the error of the ReLU interpolant). Prove the bound $\max|f''|/(8N^2)$ for the linear interpolant on knots spaced $h = 1/N$ apart, and compare it with the errors in the table.

Solution. Fix one interval $[a, a + h]$ and a point $x$ strictly inside it. Let $L$ be the line through $(a, f(a))$ and $(a + h, f(a + h))$, and $e(t) = f(t) - L(t)$, which vanishes at both ends. Define

$$G(t) = e(t) - e(x)\,\frac{(t - a)(t - a - h)}{(x - a)(x - a - h)}.$$

$G$ vanishes at $t = a$, $t = a + h$ and $t = x$. By Rolle's theorem $G'$ vanishes at a point between $a$ and $x$ and at another between $x$ and $a + h$, and by Rolle's theorem again $G''$ vanishes at some $\xi$ between them. Since $L'' = 0$ and the quadratic's second derivative is 2,

$$0 = G''(\xi) = f''(\xi) - \frac{2\,e(x)}{(x - a)(x - a - h)}, \qquad\text{so}\qquad e(x) = \frac{f''(\xi)}{2}\,(x - a)(x - a - h).$$

The product $(x - a)(a + h - x)$ is largest at the midpoint, where it equals $h^2/4$, so $|e(x)| \leq \max|f''|\,h^2/8$. For $f(x) = \sin(2\pi x)$, $\max|f''| = (2\pi)^2$ and $h = 1/N$, which gives the bound. Its values for $N = 2, 4, 8, 16, 32, 64$ are 1.2337, 0.3084, 0.0771, 0.0193, 0.0048 and 0.0012, against the measured errors 1.0000, 0.2105, 0.0704, 0.0188, 0.0048 and 0.0012. The bound is nearly attained once $N$ is large, because the sine's peak at $x = 1/4$ is then a knot (for $N = 4, 8, \ldots, 64$), and the two intervals beside it have their midpoints within $h/2$ of it, where $|f''|$ is nearly its maximum $(2\pi)^2$ and nearly constant across the short interval; for small $N$ the knots fall where $|f''|$ is far from its maximum. The bound needs $f''$ to exist and be bounded; it says nothing about a target with a corner between two knots.

Problem 3 (symmetries of the weights). Prove that a hidden layer started with identical units stays identical under gradient descent, and show that every 30-16-1 network shares its function with a very large number of other weight vectors.

Solution. Identical units stay identical. Suppose that in a network with one hidden layer, every hidden unit $j$ has the same incoming weights $\mathbf{w}_j = \mathbf{w}$, the same bias $b_j = b$ and the same outgoing weight $v_j = v$. Then for every example all hidden units have the same weighted sum $z_j = z$ and activation $a_j = g(z)$. Their errors are $\delta_j = v\,\delta^{\text{out}}\,g'(z)$, the same for every $j$, and their gradients, $\delta_j \mathbf{x}$ for the incoming weights, $\delta_j$ for the bias and $\delta^{\text{out}} a_j$ for the outgoing weight, are the same too, summed over any minibatch. One step therefore changes every unit by the same amount, and the units are identical again after it. By induction on the steps they are identical forever, and the layer computes what one unit with outgoing weight $16v$ would. That is the experiment's finding: after 200 epochs from all weights 0.05 the largest difference between units was exactly zero. The argument also needs the outgoing weights to start equal; random input weights alone break the symmetry.

Equivalent weights. Reordering the 16 hidden units, each carrying its incoming weights, bias and outgoing weight, changes no output, so a weight vector with no two hidden units equal has at least $16! = 20{,}922{,}789{,}888{,}000$ arrangements computing the same function. For tanh units there is more: since $\tanh(-z) = -\tanh(z)$, negating a unit's incoming weights, bias and outgoing weight also changes nothing, which gives at least $2^{16} \times 16!$, about $1.37 \times 10^{18}$, equivalent weight vectors when no two units are equal or opposite and none has all its weights zero. These are lower bounds: the page does not prove that there are no other equivalent weight vectors, and there can be others (a unit whose outgoing weight is zero may have any incoming weights). For ReLU units, $\operatorname{ReLU}(cz) = c\operatorname{ReLU}(z)$ for $c > 0$, so multiplying a unit's incoming weights and bias by $c$ and dividing its outgoing weight by $c$ changes nothing, and each solution lies on a continuum of equivalent ones. None of this is a failure of training: it means that a network's individual weights are not identifiable, and that only the function they compute can be compared between runs.

Open questions

  • Does weight decay close the log-loss gap on the breast cancer data? The NumPy network's median test log loss was 0.0858 against logistic regression's 0.0764. Repeating the 30-split comparison with a penalty $\lambda \sum w^2$, $\lambda$ chosen on the validation part, would show whether the gap is variance that a penalty removes, as for ridge regression, or a cost of the hidden layer that remains.
  • Is depth easier than width at the same parameter count? James et al. (2021, §10.2) say that several layers of modest size make the learning task "much easier" than one wide layer. On the two-moons problem, networks of one, two and three hidden layers with about 257 parameters each, trained from 30 seeds with the same early stopping, would test the claim on data where the target boundary is known.
  • Does test loss rise and fall again as the hidden layer widens without early stopping? The 30-256-256-1 network overfits in its probabilities. Sweeping the width of a single hidden layer from 1 to 1,024 units, trained to a fixed small training loss on the 341 training tumors, would show whether the breast cancer data give the double descent that Why Classical Bounds Do Not Explain Deep Networks and The Bias-Variance Tradeoff discuss.

Beyond this article

The multilayer perceptron is the base on which more specialized networks are built. Convolutional networks share weights across the positions of an image; recurrent networks carry a state along a sequence; transformers, used in language models, combine attention layers with small MLPs applied at every position. What those MLP layers compute is a question the Library's reading notes follow: A Mathematical Framework for Transformer Circuits reads small transformers exactly by leaving the MLP layers out, Softmax Linear Units changes the MLP activation function to make the neurons easier to interpret, and Towards Monosemanticity decomposes the 512 ReLU neurons of a one-layer transformer's MLP into more interpretable features with a sparse autoencoder. The Language Models from Scratch collection builds one, a character-level GPT, step by step in PyTorch, where automatic differentiation computes the backward pass that is written out by hand here. Its Assembling the Transformer Block adds residual connections and layer normalization, two further techniques that keep gradients usable through many layers. The Academics section's Neuroevolution topic trains networks without gradients at all, by evolving their weights and structure.

This article closes the unit on neural networks. The collection's next unit turns from single flexible models to combinations of models, beginning with Introduction to Ensemble Models, which establishes when an average or a vote of several models beats the best of them.

In Python

scikit-learn's MLPClassifier and MLPRegressor implement the multilayer perceptron with backpropagation:

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.2, stratify=data.target, random_state=0)

model = make_pipeline(
    StandardScaler(),
    MLPClassifier(hidden_layer_sizes=(16,), activation="relu", alpha=1e-4,
                  max_iter=1000, random_state=0))
model.fit(X_train, y_train)
print(model.score(X_test, y_test))          # 0.9649

mlp = model[-1]
print(mlp.n_iter_)                          # 391 epochs
print(mlp.loss_curve_[-1])                  # training loss after the last epoch

hidden_layer_sizes lists the sizes of the hidden layers only, so (16,) is one hidden layer of 16 units and (16, 8) two layers; the input and output sizes come from the data. alpha is the weight decay penalty. The standardization step sits inside the pipeline so that it is fitted on the training data only. By default the network trains until the training loss stops improving, here after 391 epochs, which does nothing to prevent overfitting.

scikit-learn's own early stopping needs care. With early_stopping=True, it sets aside 10% of the training data and stops when the score on it has not improved for n_iter_no_change epochs (10 by default). That score is the validation accuracy, recorded in validation_scores_, not the validation loss, so it watches decisions rather than probabilities, and the overfitting seen above in the log loss can pass unnoticed. On a set of 46 tumors, accuracy also moves in coarse steps and can sit still for ten epochs while the network is still learning, especially with the small default learning rate of the Adam optimizer (0.001). On this split, turning early stopping on with the other settings unchanged stopped training after 37 epochs at a test accuracy of 0.8772, against 0.9649 without it; with n_iter_no_change=50 it ran 140 epochs and reached 0.9298. The early stopping in the NumPy network above watches the validation loss, with a patience of 50 epochs, for this reason. (These defaults and the accuracy-based validation score were checked against the scikit-learn 1.9.1 documentation on 23 November 2026; the numbers were computed with scikit-learn 1.6.1.)

The numbers in this article, including the hand-worked pass, the gradient checks, the three comparisons, the overfitting experiment and the ReLU approximations, are computed by tools/experiments/data-science/multilayer-perceptron.py in the site's repository, using tools/experiments/data-science/nnet.py.

References

  • Aeberhard, S., and Forina, M. (1992). Wine [Data set]. UCI Machine Learning Repository. https://doi.org/10.24432/C5PC7J record · 8 pages
  • Cybenko, G. (1989). Approximation by superpositions of a sigmoidal function. Mathematics of Control, Signals, and Systems, 2(4), 303–314. record · 2 pages
  • Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction (2nd ed.). Springer. §§11.3–11.5. record · 39 pages
  • Hornik, K. (1991). Approximation capabilities of multilayer feedforward networks. Neural Networks, 4(2), 251–257. record
  • Hornik, K., Stinchcombe, M., and White, H. (1989). Multilayer feedforward networks are universal approximators. Neural Networks, 2(5), 359–366. Cited through Cybenko (1989) and Leshno et al. (1993); not read for this article. record
  • James, G., Witten, D., Hastie, T., and Tibshirani, R. (2021). An Introduction to Statistical Learning with Applications in R (2nd ed.). Springer. Ch. 10. record · 39 pages
  • Leshno, M., Lin, V. Ya., Pinkus, A., and Schocken, S. (1993). Multilayer feedforward networks with a nonpolynomial activation function can approximate any function. Neural Networks, 6(6), 861–867. Read in the working-paper version, Stern School of Business IS-92-13, New York University (1992); the quoted abstract checked against the journal version. record
  • Parker, D. B. (1985). Learning-Logic: Casting the Cortex of the Human Brain in Silicon. Technical Report TR-47, Center for Computational Research in Economics and Management Science, MIT. Cited by attribution (Hastie et al., 2009). record · 2 pages
  • Rumelhart, D. E., Hinton, G. E., and Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323, 533–536. record · 3 pages
  • scikit-learn developers (2026). MLPClassifier. scikit-learn 1.9.1 documentation. https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html (checked 23 November 2026). record
  • Werbos, P. J. (1974). Beyond Regression: New Tools for Prediction and Analysis in the Behavioral Sciences. PhD thesis, Harvard University. Cited by attribution (Hastie et al., 2009). record · 2 pages
  • Wolberg, W., Mangasarian, O., Street, N., and Street, W. (1993). Breast Cancer Wisconsin (Diagnostic) [Data set]. UCI Machine Learning Repository. https://doi.org/10.24432/C5DW2B record · 15 pages

Reviewed: verified, 23 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 9 revisions