Neural networks · lesson 3 of 10
Parts in this lesson
Arelu(x + 1) - 2 * relu(x) + relu(x - 1)a hat made of three ReLU units
Bnp.linalg.lstsq(H, target(xs), rcond=None)3.5: the best output weights for fixed kinks
Cy = tent(y)one more layer: the number of pieces doubles

What networks can draw

Last updated

How much can a network with one hidden layer represent? The surprising answer, proved around 1990, is: almost anything, given enough units. This lesson builds that answer out of ReLU kinks you can add one at a time, states the universal approximation theorem carefully, and then looks at the three things it doesn't promise, which matter more in practice: how many units, whether training finds them, and why depth helps.

By the end of this lesson you can

  • Build a bent line from ReLU units, and explain how more units make closer fits
  • State what the universal approximation theorem promises, and what it doesn’t
  • Show that a network that fits its training range can still be wrong outside it
  • Explain, with an example, why a deep network can draw shapes a shallow one needs far more units for

ReLU units make kinks

Take a network with one input x, one hidden layer of ReLU units, and one output with no activation. Hidden unit k computes ReLU⁡(wkx+bk): a line that is flat at 0 until wkx+bk=0, then rises (or falls). That point is the unit's kink. The output adds the units up with weights vk:

yˆ=c+∑k=1mvkReLU⁡(wkx+bk)

A sum of bent lines is a bent line: straight pieces joined at the kinks. Three units already make a hat, up from −1 to 0 and down to 1:

kinks.ipynb

The first unit starts rising at −1; the second, with weight −2, turns the rise into a fall at 0; the third levels it off at 1.

Adding kinks makes any curve

Hats can be placed anywhere, at any height and width. So a curve can be drawn as a row of hats, like a join-the-dots picture: the more units, the more dots, and the closer the fit. Try it: the output weights are chosen by least squares (lesson 3.5) for kinks spread evenly across the range.

Kink builderone hidden layer of ReLU units
dashed: the target · ink: the network · colours: each unit's piecex = −3x = 3

3 units: 3 kinks, so up to 3 straight pieces. The biggest gap between the network and the curve is 1.1. Too few kinks to follow the wiggles.

The same in NumPy, for the curve sin⁡3x+0.3x on [−3,3]:

kinks.ipynb

2 and 4 units can't follow three wiggles: the biggest gap is over 1. 8 units get within 0.48, 16 within 0.095, 32 within 0.022. Doubling the units roughly quarters the gap, and nothing stops you going on.

The theorem

That observation has a theorem behind it, proved for sigmoid units by George Cybenko in 1989 and extended by Kurt Hornik in 1991, and for any activation that isn't a polynomial, ReLU included, by Leshno and others in 1993. Informally:

A network with one hidden layer and enough units can approximate any continuous function on a bounded region as closely as you like.

This is the universal approximation theorem. "As closely as you like" means: pick any gap ε>0, and there is some number of units, and some weights, that keep the network within ε of the function everywhere in the region. The step 2 figure is the idea of the proof for one input; with several inputs, the hats become bumps.

It's a reassuring result. It says a network's shape isn't what limits it: the functions you want are in there somewhere.

It says nothing about learning

Read the theorem again for what it leaves out:

  • How many units. "Enough" can be astronomically many, especially with many inputs.
  • Whether training finds the weights. It says the weights exist, not that gradient descent will reach them.
  • What happens outside the region. It's about a bounded region, the one the data covers.

All three show up on the same curve. Here scikit-learn's MLPRegressor learns its weights from 200 points:

kinks.ipynb

Even 64 learned units only get within 0.867 of the curve, while step 2's 32 hand-placed kinks got within 0.022: the good weights exist, and training didn't find them. And at x=6, outside the training range, the networks say 2.26, 5.98 and 10.02 when the truth is 1.05. A ReLU network carries on in a straight line past its last kink, so the more confidently it fitted, the further off it can go. The theorem promised nothing there, and nothing was delivered.

Depth against width

If one layer is enough in principle, why do real networks have many? Because some shapes cost far fewer units when layers are stacked. Here's a famous example (Telgarsky, 2016). The tent, made of 2 ReLU units, maps [0,1] onto itself: up from 0 to 1, then back down. Feed its output into another tent, and the result goes up and down twice; again, four times. Each layer folds the line in half, like folding paper:

Tent foldeach layer: 2 ReLU units
0110

1 layer, 2 units: 2 straight pieces. One hidden layer would need at least 1 units for the same zigzag.

kinks.ipynb

With 2 units per layer, every layer doubles the number of straight pieces: 12 units in 6 layers make 64 pieces. One hidden layer needs a kink per piece, so 63 units or more, for the same zigzag; 30 layers would need about a billion. Deeper networks can reuse what earlier layers computed, which a single wide layer can't.

That's the honest case for depth: not that shallow networks can't, but that deep ones can far more cheaply, for the kinds of structure real data has. Training deep networks well is Stage 7.

Check what you learned

A one-hidden-layer ReLU network with 10 units has one input. At most how many straight pieces can its output have?
What does the universal approximation theorem guarantee?
In the step 5 notebook, change range(1, 7) to range(1, 9) and run it. What does the last line say?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning