P4.3 stacked tents to make a zig-zag. This lesson asks the opposite question: with one hidden layer and enough ReLU units, which curves can you build? Take on . Four ReLU units get within of it everywhere, and sixteen get within . The same recipe works for any continuous curve you can draw on an interval, and that fact is called universal approximation.
The question, stated carefully
There are three different questions about a network and a target function, and this lesson answers only the first.
- Can a network of this shape get close to the function? (Do good weights exist?)
- Can training find those weights? (P4.7 to P4.10.)
- Will the network also be right on data it has not seen? (P3.A4, P4.24.)
“Close” needs a definition. Here it means a uniform error: the largest gap over the whole interval is at most some small number , and we may pick as small as we like by adding units. It does not mean equal. A network of ReLU units is made of straight pieces, so it cannot equal exactly. It can only come as close as you ask.
Block one: two ReLUs make a ramp
One ReLU unit with weight and bias is : zero until , then rising with slope (P4.4). Subtract a second one that starts later, at :
Before both are zero. Between and only the first is on, so the sum rises. After both are on, they rise at the same rate, and the difference stays constant. The division by makes that constant equal to . With and the weights are and :
| ramp value | |
|---|---|
| 0.3 | 0 |
| 0.4 | 0 |
| 0.5 | 0.5 |
| 0.6 | 1 |
| 0.8 | 1 |
It is a soft step: flat at , a ramp up to , flat at . Narrow the ramp and the weights grow ( gives weights and ), so the curve looks more and more like a jump. It never becomes a true jump, because every piece of a ReLU network is a straight line of finite slope.
Block two: three ReLUs make a hat
Now use three units at equally spaced points with gap :
The first unit makes the rise. At the second unit turns on with weight , which cancels the rise and then reverses it, so the curve falls. At the third unit adds back one unit of slope, so the total slope returns to and the curve stays flat at . The result is a hat: zero, a straight climb to a peak of height at , a straight fall, zero again.
With , , and , the weights are , , :
| hat value | |
|---|---|
| 0 | 0 |
| 0.25 | 0 |
| 0.375 | 0.5 |
| 0.5 | 1 |
| 0.625 | 0.5 |
| 0.75 | 0 |
| 1 | 0 |
This is the tent of P4.3. The tent on is the hat with points , , : its third unit, , only matters to the right of , so P4.3 left it out. P4.3 fed the tent into another tent. Here we put hats side by side instead.
Add hats to follow a curve
Pick a function on and equal steps of width . The points are the knots. Put one hat on each knot, with height at its own knot and zero at the neighbouring knots. Then add them:
At the knot only hat is nonzero, so exactly. Between two knots only two hats are on, and their sum is a straight line joining the two heights. So is the connect-the-dots curve through the points .
It is also a one-hidden-layer ReLU network. Neighbouring hats share ReLU units, so after collecting terms there are only units, , each with input weight and bias , and the output weights are easy to read off:
- the bias of the output unit is ;
- the weight on the first unit is the slope of the first piece;
- every later weight is the change of slope where that piece meets the one before.
Each ReLU unit adds one kink, and the weight tells how much the slope changes at it.
A worked example: a parabola with 4 pieces
Take on and , so and the knots are , , , , . The heights are , , , , . The four pieces have slopes , , , , so the first weight is and the slope changes are , , . The network is
At the knot it gives , which is . Halfway between two knots, at , it gives , while . The gap is , the largest in the interval. (The slopes of go up by the same at every knot because its second derivative is the constant , and .) The error is largest midway between knots because that is the farthest point from any dot.
Add neurons and watch the error fall
The widget builds this network for four target curves. The slider sets the number of hidden ReLU units, which is also the number of straight pieces. The dashed line is the target and the solid blue line is the network. Show hats draws the faint hats whose sum is the network.
Things to try, in order:
- Choose sin and Connect the dots (the widget uses on , the same shape as on ). At 2 units the largest error is : the knots , , all sit on zeros of the sine, so the network is a flat line at . At 4, 8, 16 and 32 units the largest error is , , and . Each doubling divides it by about , and , heading towards the of the row below.
- Choose corner and set 10 units. The error is zero (to rounding): a knot falls exactly on the corner at , and the target is straight on each side. At 4 units there is no knot at and the error is . A network does best when its kinks are where the target bends.
- Choose steep and compare 4, 8 and 16 units: , and . The curve is nearly a jump near , so the pieces there matter most, and it takes 32 units to get under .
- Press Show hats on bumpy and add units. Each hat is one unit’s worth of local control, so wiggles that are narrower than a piece are missed until a knot lands near them.
- Press Least squares on sin with 8 units. The mean error drops from to and the largest from to . The network still has the same 8 ReLU units with the same kinks. Only the output weights changed, fitted by the least-squares method of P3.A2. Least squares minimises the squared error, so it does not promise a smaller largest error: for the corner at 4 units its mean error is , a little above the of connecting the dots.
Why the error can always be made small
Every example above is an instance of one short argument. Let be continuous on the interval . A continuous function on a closed interval is uniformly continuous: for any there is a width such that any two points closer than have -values closer than . Choose so that . A point between two neighbouring knots has equal to a weighted average of the two knot values, and both are within of . An average of two numbers that are each within of is also within . So everywhere, using ReLU units.
For smooth functions the rate is known too. If has a second derivative, the largest error of connect-the-dots is at most (the full form is in the Math tab). For , and the largest error is exactly , reached midway between knots:
| pieces | error for | error for |
|---|---|---|
| 2 | 0.0625 | 1.0000 |
| 4 | 0.015625 | 0.2105 |
| 8 | 0.003906 | 0.0704 |
| 16 | 0.000977 | 0.0188 |
| 32 | 0.000244 | 0.0048 |
Each row down doubles the units. The error for is divided by exactly 4. The sine ratios from 4 pieces onward are , and , approaching 4 (the first row, 2 pieces, is a flat line, so it is not a fair start). A corner or a steep part has a large near it, so it needs many more units than a gentle curve of the same height.
Go deeper Weierstrass: the same idea with polynomials
The oldest result of this kind is the Weierstrass approximation theorem (1885): any continuous function on a closed interval can be approximated uniformly, to any accuracy, by a polynomial. A polynomial is built from one kind of piece, powers of , and a network is built from another kind, ReLU or sigmoid units. The statement has the same shape: enough pieces of this type, with the right weights, get as close as you like. The practical story differs. Raising the degree of a polynomial can make it wiggle between the points (P3.A3), while the hat construction cannot overshoot between its knots, because each piece is a straight line between two values of .
The theorem
The construction above is for one input and the ReLU. The general statements cover many inputs and many activations. In words:
Let be a closed and bounded set of inputs in , let be a continuous function on , and let . Then, for a suitable fixed activation (the conditions are listed next), there is a number of hidden units, with weights , biases and output weights , such that
for every in . (A constant output offset is allowed: with and a ReLU unit outputs the constant .)
Which activations count has been widened step by step:
- Cybenko (1989) proved it for any continuous sigmoidal activation (one that tends to at and to at ), with inputs in the unit cube.
- Hornik, Stinchcombe and White (1989) proved it for a broader class of “squashing” activations and a wider choice of what counts as close.
- Hornik (1991) showed that a continuous activation that is bounded and not constant is enough for continuous functions on closed and bounded sets. Without continuity, a bounded and not constant activation still gives approximation in the average sense.
- Leshno, Lin, Pinkus and Schocken (1993) closed the question for a wide class of activations that includes every continuous one: a one-hidden-layer network can approximate every continuous function on a closed and bounded set if and only if the activation is not a polynomial.
ReLU is continuous and is not a polynomial, so it qualifies. A polynomial activation does not: a sum of polynomials in of degree at most is itself a polynomial of degree at most , so it can never get arbitrarily close to on an interval, however many units are used. The one-input ReLU construction above is a complete proof for . For the standard proofs are different and say only that suitable weights exist; they do not build them.
What the theorem does not say
- It does not say how many units. can be very large. The grid idea needs a knot spacing in each of inputs, so for steps per input there are cells, and each cell needs at least one unit’s worth of work. With and that is cells, about . This counts the cells of one simple scheme, not a proven minimum number of units for every function, but it shows that guarantees stated for “enough units” can hide enormous numbers.
- It does not say training will find the weights. The theorem is an existence statement. Gradient descent on a non-convex loss (P4.3) may or may not reach them.
- It does not say the network will be right on new data. A network can match the target on every training point and still be far from it between them (P3.A3, P4.24). The theorem is about a known function, not a finite sample.
- It promises nothing outside the set . The ReLU network above is a straight line beyond its last knot. The guarantee holds only on the closed and bounded set you chose.
- It needs a continuous target for a uniform guarantee. Near a jump, a continuous network cannot stay within a small on both sides: it must climb through the gap. The mean error and the error away from the jump can still be made small (see the Go-deeper box on discontinuities).
- It does not say one hidden layer is the best design. Next, an example where it is a poor one.
Go deeper Jumps and other ways to measure the error
For a target with a jump, such as a step from to at , the connect-the-dots network must climb across at least one piece, so just left of the jump its error is large for any finite number of units. If you measure the error as the average gap, the climb takes a shrinking sliver of the interval, and the average error can be made as small as you like. Theorems that use such weaker notions of closeness cover broader classes of functions than the continuous ones (Hornik, Stinchcombe and White, 1989). For a classifier this is the useful sense: the exact value on the boundary rarely matters. The widget shows the same thing with the steep curve: the error in the steep part falls only when a knot lands there.
Depth versus width
If one wide layer is enough in principle, why stack layers? Because a deep network can be much more economical. P4.3 built the tent from 2 units. Feed the output of one tent into another, and again. The sawtooth after copies has straight pieces and costs units. The table, from the code cell below, counts the pieces and compares them with what one hidden layer needs.
| copies | deep units | pieces | one layer needs |
|---|---|---|---|
| 1 | 2 | 2 | 1 |
| 3 | 6 | 8 | 7 |
| 6 | 12 | 64 | 63 |
The right-hand column comes from a fact about one hidden layer: each ReLU unit on a line adds at most one kink, so units give at most pieces. To equal a sawtooth with pieces, a one-hidden-layer network needs at least units. The deep network needs .
This is a statement about one function made by hand. Telgarsky (2016) proved a much stronger result of the same kind: there are networks with a constant number of units per layer and about layers that cannot be approximated, in the average-gap (integral of ) sense and to a stated accuracy, by networks with only about layers unless those have a number of units that grows like . Training does not usually find such a perfect zig-zag, and whether real tasks look like these functions is a question for experiments (P4.23). The theorem of the last section is about what is possible; this section shows that “possible with one layer” and “affordable with one layer” are different things.
More than one input
Everything so far used one input. With two inputs, a ReLU unit is a ramp along one direction and constant across it, so a single unit cannot make a localized bump the way three units make a hat on a line. A bump takes several units pointing in different directions, and following a curved surface takes many such bumps. The theorem still holds, and the number of units a surface needs rises quickly with the number of inputs, as the count above showed. This is only a preview. Deeper networks, which fold the plane repeatedly, often get the same detail with fewer units (P4.3); how they do it, and what a trained network actually learns, come in the lessons on training and architecture.
Depth: three ways to see it
Intuition
Think of building a skyline from tent-shaped Lego pieces. Each tent stands on one spot and says “the curve is this high here”. Tents next to each other overlap exactly at their feet, so the skyline you get is the connect-the-dots outline through the tops. More tents give a finer outline. The theorem says that, for a continuous curve on an interval, the outline can be made as faithful as you want. It does not say how many tents the skyline of a very rough or very high-dimensional shape will need, or how to find where to put them.
Math
Let with , and let be the slope of piece for . The connect-the-dots network is
Each hat is
so collecting the terms of gives, for each interior unit, the second difference of the heights:
If has a continuous second derivative on , then on each piece
For this gives , and it is reached at the midpoint of each piece. In the least-squares mode the numbers become free parameters found by solving the normal equations of P3.A2 for the hat features.
Code
The same 8-unit network for on as a PyTorch model with the weights set by hand. This block is a static listing, not a runnable cell.
import torch
n = 8
knots = torch.linspace(0, 2 * torch.pi, n + 1)
vals = torch.sin(knots)
slope = (vals[1:] - vals[:-1]) / (knots[1] - knots[0])
c = torch.cat([slope[:1], slope[1:] - slope[:-1]])
net = torch.nn.Sequential(
torch.nn.Linear(1, n), torch.nn.ReLU(), torch.nn.Linear(n, 1))
with torch.no_grad():
net[0].weight.fill_(1.0) # unit k is ReLU(x - kink)
net[0].bias.copy_(-knots[:-1])
net[2].weight.copy_(c[None, :])
net[2].bias.fill_(float(vals[0]))
x = torch.linspace(0, 2 * torch.pi, 4001)[:, None]
err = (net(x)[:, 0] - torch.sin(x[:, 0])).abs()
print(f"max error {err.max().item():.4f}")
print(f"parameters {sum(p.numel() for p in net.parameters())}")Output from a local run with PyTorch 2.9.1:
max error 0.0704
parameters 25The 25 parameters are input weights, biases, output weights and output bias. Only the last were free in the construction; training would adjust all .
Try it in Python
The first cell builds the network for and with 2, 4, 8 and 16 units, prints the largest errors, and compares connect-the-dots with a least-squares fit of the same units. The second counts the pieces of the sawtooth. Both use NumPy only.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Running the first cell gives largest errors of , , and for the sine, and , , and for , equal to . The least-squares fit of the 8 sine units lowers the largest error from to and the mean from to . The second prints , , , , and pieces from , , , , and units.
Quiz
Check your understanding
A theorem says that for every continuous function on a closed interval and every , a one-hidden-layer ReLU network exists whose largest error is below . What does that tell you?
The theorem is an existence statement about weights. It does not say that training reaches them, because the loss is not convex (P4.3). It promises nothing outside the interval, where the ReLU network is a straight line. And it gives an error below , not zero: a network of straight pieces does not equal exactly.
Three ReLU units are combined as . What does this function look like?
The slope is , then , then , then , which is a hat. A ramp that goes up and stays flat needs only two units. The slope after is , not , because the three weights sum to zero. At the value is , not : the weight is a slope change, not a height.
You want a network that equals the tent with knot heights , , at the points , , on , using units and and a zero bias. What should the two output weights be?
The left piece has slope and the right piece has slope . The weight on the later unit is the change, , because the second unit adds to the slope already there. Using the slopes themselves as weights, and , would give a final slope of . The heights and and the weights and belong to other functions.
A one-hidden-layer ReLU network on a line has units. At most how many straight pieces can its curve have, and what does that mean for a sawtooth with pieces?
Each ReLU unit contributes one kink, so kinks cut the line into pieces. Matching pieces then takes units, while tents in a row use only . The count is the growth of depth, not of one layer. Pieces cannot be added for free, because each one needs its own kink.
You approximate on by connecting the dots at equally spaced knots with pieces. What is the largest error?
The error of connecting the dots is largest midway between two knots and equals for with , which is . The error is of order , not , so and are far too large. Smoothness lowers the error but does not make straight pieces equal to a parabola.
The target is a step: for and for . Why can no ReLU network keep the largest error small on the whole interval?
A ReLU network is made of straight pieces joined without a jump, so at the jump it has to climb through values in between, and just next to the jump the gap to the step stays large. The slope of the step is not the issue, and ReLU networks can take any value. The theorem concerns continuous functions, not polynomials. In the average sense the error can still be made small.
Exercises
- With the ramp , what is the value at , and what weights would make the ramp rise from to between and ?
Show answer
At : . For a ramp between and the width is , so the weights are and : . - Write a hat of height whose peak is at and whose feet are at and . What is its value at and at ?
Show answer
The gap is , so the weights are , , : . At it is . At it is . - How large is the largest error when connecting the dots for on with 32 pieces, and how many pieces would bring it below ?
Show answer
, so the error is . We need , that is , so : 51 pieces are enough (at the error is exactly ). - Write on as a one-hidden-layer ReLU network with 4 equal pieces. What is its largest error?
Show answer
The knots are , , , , and the heights are , , , , . The slopes are , , , , so the bias is and the weights are , , , : . A knot sits on the corner, so the error is . - A deep network has 4 tents in a row. How many units does it use, how many pieces does its curve have, and at least how many units does a one-hidden-layer network need to produce the same curve exactly?
Show answer
units, pieces, and at least units for one hidden layer.
Next: P4.12 asks how to choose the starting weights, because a network that can represent a function still has to be trained from a starting point. P4.23 returns to the choice between wide and deep.