Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 4: Neural Network Foundations

Universal approximation

P4.1135 min●●○ IntermediateReviewed Dec 2, 2026

Why this matters for machine learning

A network with one hidden layer can come as close as you like to any continuous curve on an interval, so learning functions with networks is not ruled out in advance. Knowing what the guarantee leaves out shows what is still hard.

  • Every transformer block contains a wide one-hidden-layer network that is applied to each token. (P7.7: The transformer block, coming soon)
  • Choosing between a wider and a deeper network rests on what width alone can and cannot do. (P4.23: Hyperparameter tuning)
  • Why large networks both fit their data and generalise is a separate question that this theorem does not answer. (P4.24: Modern generalization puzzles)

Before this: Multi-layer perceptrons, Activation functions, Linear regression

After this lesson you can

  • Build a ramp from two ReLU units and a hat from three, with the weights written out and the values checked by hand
  • Turn a function on an interval into a one-hidden-layer ReLU network that passes through chosen points, and compute its largest error for x2x^2 and a sine
  • State the universal approximation theorem with its conditions, and say what it does not promise about size, training and generalisation
  • Explain with the sawtooth why a deep network can need far fewer units than a one-hidden-layer network

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 f(x)=x2f(x)=x^2 on [0,1][0,1]. Four ReLU units get within 0.01560.0156 of it everywhere, and sixteen get within 0.0010.001. 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.

  1. Can a network of this shape get close to the function? (Do good weights exist?)
  2. Can training find those weights? (P4.7 to P4.10.)
  3. 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 ∣f(x)−f^(x)∣|f(x)-\hat f(x)| over the whole interval is at most some small number ε\varepsilon, and we may pick ε\varepsilon 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 x2x^2 exactly. It can only come as close as you ask.

Block one: two ReLUs make a ramp

One ReLU unit with weight 11 and bias −p-p is ReLU(x−p)\mathrm{ReLU}(x-p): zero until x=px=p, then rising with slope 11 (P4.4). Subtract a second one that starts later, at q>pq>p:

ramp(x)=1q−p(ReLU(x−p)−ReLU(x−q)).\begin{aligned} \mathrm{ramp}(x)=\frac1{q-p}\Bigl(&\mathrm{ReLU}(x-p)\\ &-\mathrm{ReLU}(x-q)\Bigr). \end{aligned}

Before pp both are zero. Between pp and qq only the first is on, so the sum rises. After qq both are on, they rise at the same rate, and the difference stays constant. The division by q−pq-p makes that constant equal to 11. With p=0.4p=0.4 and q=0.6q=0.6 the weights are 55 and −5-5:

xxramp value
0.30
0.40
0.50.5
0.61
0.81

It is a soft step: flat at 00, a ramp up to 11, flat at 11. Narrow the ramp and the weights grow (q−p=0.02q-p=0.02 gives weights 5050 and −50-50), 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 p<m<qp<m<q with gap h=m−p=q−mh=m-p=q-m:

hat(x)=1h(ReLU(x−p)−2 ReLU(x−m)+ReLU(x−q)).\begin{aligned} \mathrm{hat}(x)=\frac1h\Bigl(&\mathrm{ReLU}(x-p)\\ &-2\,\mathrm{ReLU}(x-m)\\ &+\mathrm{ReLU}(x-q)\Bigr). \end{aligned}

The first unit makes the rise. At mm the second unit turns on with weight −2-2, which cancels the rise and then reverses it, so the curve falls. At qq the third unit adds back one unit of slope, so the total slope returns to 00 and the curve stays flat at 00. The result is a hat: zero, a straight climb to a peak of height 11 at mm, a straight fall, zero again.

With p=0.25p=0.25, m=0.5m=0.5, q=0.75q=0.75 and h=0.25h=0.25, the weights are 44, −8-8, 44:

xxhat value
00
0.250
0.3750.5
0.51
0.6250.5
0.750
10

This is the tent of P4.3. The tent T(x)=2 ReLU(x)−4 ReLU(x−0.5)T(x)=2\,\mathrm{ReLU}(x)-4\,\mathrm{ReLU}(x-0.5) on [0,1][0,1] is the hat with points 00, 0.50.5, 11: its third unit, 2 ReLU(x−1)2\,\mathrm{ReLU}(x-1), only matters to the right of 11, 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 ff on [a,b][a,b] and nn equal steps of width h=(b−a)/nh=(b-a)/n. The points ti=a+iht_i=a+ih are the knots. Put one hat on each knot, with height f(ti)f(t_i) at its own knot and zero at the neighbouring knots. Then add them:

f^(x)=∑i=0nf(ti) hati(x).\hat f(x)=\sum_{i=0}^{n} f(t_i)\,\mathrm{hat}_i(x).

At the knot tit_i only hat ii is nonzero, so f^(ti)=f(ti)\hat f(t_i)=f(t_i) exactly. Between two knots only two hats are on, and their sum is a straight line joining the two heights. So f^\hat f is the connect-the-dots curve through the points (ti,f(ti))(t_i,f(t_i)).

It is also a one-hidden-layer ReLU network. Neighbouring hats share ReLU units, so after collecting terms there are only nn units, ReLU(x−t0),…,ReLU(x−tn−1)\mathrm{ReLU}(x-t_0),\dots,\mathrm{ReLU}(x-t_{n-1}), each with input weight 11 and bias −ti-t_i, and the output weights are easy to read off:

  • the bias of the output unit is f(t0)f(t_0);
  • 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 f(x)=x2f(x)=x^2 on [0,1][0,1] and n=4n=4, so h=0.25h=0.25 and the knots are 00, 0.250.25, 0.50.5, 0.750.75, 11. The heights are 00, 0.06250.0625, 0.250.25, 0.56250.5625, 11. The four pieces have slopes 0.250.25, 0.750.75, 1.251.25, 1.751.75, so the first weight is 0.250.25 and the slope changes are 0.50.5, 0.50.5, 0.50.5. The network is

f^(x)=0.25 ReLU(x)+0.5 ReLU(x−0.25)+0.5 ReLU(x−0.5)+0.5 ReLU(x−0.75).\begin{aligned} \hat f(x)={}&0.25\,\mathrm{ReLU}(x)\\ &+0.5\,\mathrm{ReLU}(x-0.25)\\ &+0.5\,\mathrm{ReLU}(x-0.5)\\ &+0.5\,\mathrm{ReLU}(x-0.75). \end{aligned}

At the knot x=0.5x=0.5 it gives 0.125+0.125=0.250.125+0.125=0.25, which is 0.520.5^2. Halfway between two knots, at x=0.375x=0.375, it gives 0.25×0.375+0.5×0.125=0.156250.25\times0.375+0.5\times0.125=0.15625, while 0.3752=0.1406250.375^2=0.140625. The gap is 0.0156250.015625, the largest in the interval. (The slopes of x2x^2 go up by the same 0.50.5 at every knot because its second derivative is the constant 22, and 2×0.25=0.52\times0.25=0.5.) 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.

0.20.40.60.81−1−0.50.51x
  • Target (dashed): sin(2πx)
  • Network, 4 units (solid, circles at the knots when 16 or fewer)

Largest error: 0.2105 Mean error: 0.1364 Error at x = 0.5: 0

The knots are the equally spaced points 0, 1/4, …, 1. Connect the dots sets the output weights so the network passes exactly through the target at every knot. Errors are measured on 801 equally spaced points. The steep and bumpy curves are made up.

Add neurons; the approximation improves. The target is dashed and the network is solid blue, with a circle at each knot when there are 16 units or fewer. Connect the dots sets the output weights so the network passes through the target at every knot; Least squares keeps the same knots and fits the output weights to 801 sample points. The steep and bumpy curves are made up. Errors are measured on 801 equally spaced points.

Things to try, in order:

  1. Choose sin and Connect the dots (the widget uses sin⁡(2πx)\sin(2\pi x) on [0,1][0,1], the same shape as sin⁡\sin on [0,2π][0,2\pi]). At 2 units the largest error is 1.00001.0000: the knots 00, 0.50.5, 11 all sit on zeros of the sine, so the network is a flat line at 00. At 4, 8, 16 and 32 units the largest error is 0.21050.2105, 0.07040.0704, 0.01880.0188 and 0.00480.0048. Each doubling divides it by about 3.03.0, 3.73.7 and 3.93.9, heading towards the 44 of the x2x^2 row below.
  2. Choose corner and set 10 units. The error is zero (to rounding): a knot falls exactly on the corner at 0.30.3, and the target is straight on each side. At 4 units there is no knot at 0.30.3 and the error is 0.08000.0800. A network does best when its kinks are where the target bends.
  3. Choose steep and compare 4, 8 and 16 units: 0.30290.3029, 0.18370.1837 and 0.06530.0653. The curve is nearly a jump near 0.50.5, so the pieces there matter most, and it takes 32 units to get under 0.0170.017.
  4. 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.
  5. Press Least squares on sin with 8 units. The mean error drops from 0.03300.0330 to 0.01370.0137 and the largest from 0.07040.0704 to 0.05310.0531. 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 0.01100.0110, a little above the 0.01000.0100 of connecting the dots.

Why the error can always be made small

Every example above is an instance of one short argument. Let ff be continuous on the interval [a,b][a,b]. A continuous function on a closed interval is uniformly continuous: for any ε>0\varepsilon>0 there is a width δ>0\delta>0 such that any two points closer than δ\delta have ff-values closer than ε\varepsilon. Choose nn so that h≤δh\le\delta. A point xx between two neighbouring knots has f^(x)\hat f(x) equal to a weighted average of the two knot values, and both are within ε\varepsilon of f(x)f(x). An average of two numbers that are each within ε\varepsilon of f(x)f(x) is also within ε\varepsilon. So ∣f^(x)−f(x)∣≤ε|\hat f(x)-f(x)|\le\varepsilon everywhere, using nn ReLU units.

For smooth functions the rate is known too. If ff has a second derivative, the largest error of connect-the-dots is at most h28max⁡∣f′′∣\frac{h^2}{8}\max|f''| (the full form is in the Math tab). For x2x^2, f′′=2f''=2 and the largest error is exactly h24\frac{h^2}{4}, reached midway between knots:

pieces nnerror for x2x^2error for sin⁡\sin
20.06251.0000
40.0156250.2105
80.0039060.0704
160.0009770.0188
320.0002440.0048

Each row down doubles the units. The error for x2x^2 is divided by exactly 4. The sine ratios from 4 pieces onward are 3.03.0, 3.73.7 and 3.93.9, 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 f′′f'' 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 xx, 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 ff.

The theorem

The construction above is for one input and the ReLU. The general statements cover many inputs and many activations. In words:

Let KK be a closed and bounded set of inputs in Rd\mathbb{R}^d, let ff be a continuous function on KK, and let ε>0\varepsilon>0. Then, for a suitable fixed activation σ\sigma (the conditions are listed next), there is a number NN of hidden units, with weights wk\mathbf{w}_k, biases bkb_k and output weights ckc_k, such that

∣f(x)−∑k=1Nck σ(wk⋅x+bk)∣<ε\left|f(\mathbf{x})-\sum_{k=1}^{N}c_k\,\sigma(\mathbf{w}_k\cdot\mathbf{x}+b_k)\right|<\varepsilon

for every x\mathbf{x} in KK. (A constant output offset is allowed: with wk=0\mathbf{w}_k=\mathbf{0} and bk=1b_k=1 a ReLU unit outputs the constant 11.)

Which activations count has been widened step by step:

  • Cybenko (1989) proved it for any continuous sigmoidal activation (one that tends to 00 at −∞-\infty and to 11 at +∞+\infty), 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 w⋅x+b\mathbf{w}\cdot\mathbf{x}+b of degree at most mm is itself a polynomial of degree at most mm, so it can never get arbitrarily close to sin⁡x\sin x on an interval, however many units are used. The one-input ReLU construction above is a complete proof for d=1d=1. For d>1d>1 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. NN can be very large. The grid idea needs a knot spacing hh in each of dd inputs, so for nn steps per input there are ndn^d cells, and each cell needs at least one unit’s worth of work. With n=16n=16 and d=10d=10 that is 1610=1,099,511,627,77616^{10}=1{,}099{,}511{,}627{,}776 cells, about 1.1×10121.1\times10^{12}. 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 KK. 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 ε\varepsilon 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 00 to 11 at x=0.5x=0.5, 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 TT from 2 units. Feed the output of one tent into another, and again. The sawtooth after kk copies has 2k2^k straight pieces and costs 2k2k units. The table, from the code cell below, counts the pieces and compares them with what one hidden layer needs.

copies kkdeep unitspiecesone layer needs
1221
3687
6126463

The right-hand column comes from a fact about one hidden layer: each ReLU unit on a line adds at most one kink, so mm units give at most m+1m+1 pieces. To equal a sawtooth with 2k2^k pieces, a one-hidden-layer network needs at least 2k−12^k-1 units. The deep network needs 2k2k.

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 k3k^3 layers that cannot be approximated, in the average-gap (integral of ∣f−g∣|f-g|) sense and to a stated accuracy, by networks with only about kk layers unless those have a number of units that grows like 2k2^k. 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 ti=a+iht_i=a+ih with h=(b−a)/nh=(b-a)/n, and let si=f(ti+1)−f(ti)hs_i=\dfrac{f(t_{i+1})-f(t_i)}{h} be the slope of piece ii for i=0,…,n−1i=0,\dots,n-1. The connect-the-dots network is

f^(x)=f(t0)+∑i=0n−1ci ReLU(x−ti),c0=s0,ci=si−si−1.\begin{aligned} \hat f(x)&=f(t_0)\\ &\quad+\sum_{i=0}^{n-1}c_i\,\mathrm{ReLU}(x-t_i),\\ c_0&=s_0,\qquad c_i=s_i-s_{i-1}. \end{aligned}

Each hat is

hati=1h(ReLU(x−ti−1)−2 ReLU(x−ti)+ReLU(x−ti+1)),\begin{aligned} \mathrm{hat}_i=\frac1h\Bigl(&\mathrm{ReLU}(x-t_{i-1})\\ &-2\,\mathrm{ReLU}(x-t_i)\\ &+\mathrm{ReLU}(x-t_{i+1})\Bigr), \end{aligned}

so collecting the terms of ∑if(ti) hati\sum_i f(t_i)\,\mathrm{hat}_i gives, for each interior unit, the second difference of the heights:

ci=f(ti+1)−2f(ti)+f(ti−1)h.c_i=\frac{f(t_{i+1})-2f(t_i)+f(t_{i-1})}{h}.

If ff has a continuous second derivative on [a,b][a,b], then on each piece

∣f(x)−f^(x)∣≤h28max⁡[a,b]∣f′′∣.\lvert f(x)-\hat f(x)\rvert\le\frac{h^2}{8}\max_{[a,b]}\lvert f''\rvert.

For f(x)=x2f(x)=x^2 this gives h24\frac{h^2}{4}, and it is reached at the midpoint of each piece. In the least-squares mode the n+1n+1 numbers f(ti)f(t_i) become free parameters viv_i found by solving the normal equations of P3.A2 for the hat features.

Code

The same 8-unit network for sin⁡\sin on [0,2π][0,2\pi] 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 25

The 25 parameters are 88 input weights, 88 biases, 88 output weights and 11 output bias. Only the last 99 were free in the construction; training would adjust all 2525.

Try it in Python

The first cell builds the network for sin⁡\sin and x2x^2 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.

Build the network and measure its error

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The sawtooth: pieces from tents in a row

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 1.00001.0000, 0.21050.2105, 0.07040.0704 and 0.01880.0188 for the sine, and 0.0625000.062500, 0.0156250.015625, 0.0039060.003906 and 0.0009770.000977 for x2x^2, equal to h2/4h^2/4. The least-squares fit of the 8 sine units lowers the largest error from 0.07040.0704 to 0.05310.0531 and the mean from 0.03300.0330 to 0.01370.0137. The second prints 22, 44, 88, 1616, 3232 and 6464 pieces from 22, 44, 66, 88, 1010 and 1212 units.

Quiz

Check your understanding

  1. A theorem says that for every continuous function ff on a closed interval and every ε>0\varepsilon>0, a one-hidden-layer ReLU network exists whose largest error is below ε\varepsilon. What does that tell you?

  2. Three ReLU units are combined as 4 ReLU(x−0.25)−8 ReLU(x−0.5)+4 ReLU(x−0.75)4\,\mathrm{ReLU}(x-0.25)-8\,\mathrm{ReLU}(x-0.5)+4\,\mathrm{ReLU}(x-0.75). What does this function look like?

  3. You want a network that equals the tent TT with knot heights 00, 11, 00 at the points 00, 0.50.5, 11 on [0,1][0,1], using units ReLU(x)\mathrm{ReLU}(x) and ReLU(x−0.5)\mathrm{ReLU}(x-0.5) and a zero bias. What should the two output weights be?

  4. A one-hidden-layer ReLU network on a line has mm units. At most how many straight pieces can its curve have, and what does that mean for a sawtooth with 2k2^k pieces?

  5. You approximate f(x)=x2f(x)=x^2 on [0,1][0,1] by connecting the dots at equally spaced knots with n=20n=20 pieces. What is the largest error?

  6. The target is a step: 00 for x<0.5x<0.5 and 11 for x≥0.5x\ge0.5. Why can no ReLU network keep the largest error small on the whole interval?

Exercises

  1. With the ramp 5 ReLU(x−0.4)−5 ReLU(x−0.6)5\,\mathrm{ReLU}(x-0.4)-5\,\mathrm{ReLU}(x-0.6), what is the value at x=0.55x=0.55, and what weights would make the ramp rise from 00 to 11 between 0.450.45 and 0.550.55?
    Show answerAt 0.550.55: 5×0.15−0=0.755\times0.15-0=0.75. For a ramp between 0.450.45 and 0.550.55 the width is 0.10.1, so the weights are 1010 and −10-10: 10 ReLU(x−0.45)−10 ReLU(x−0.55)10\,\mathrm{ReLU}(x-0.45)-10\,\mathrm{ReLU}(x-0.55).
  2. Write a hat of height 11 whose peak is at x=2x=2 and whose feet are at x=1x=1 and x=3x=3. What is its value at 1.51.5 and at 44?
    Show answerThe gap is h=1h=1, so the weights are 11, −2-2, 11: ReLU(x−1)−2 ReLU(x−2)+ReLU(x−3)\mathrm{ReLU}(x-1)-2\,\mathrm{ReLU}(x-2)+\mathrm{ReLU}(x-3). At 1.51.5 it is 0.50.5. At 44 it is 3−4+1=03-4+1=0.
  3. How large is the largest error when connecting the dots for x2x^2 on [0,1][0,1] with 32 pieces, and how many pieces would bring it below 0.00010.0001?
    Show answerh=132h=\frac1{32}, so the error is h24=14096≈0.000244\frac{h^2}{4}=\frac{1}{4096}\approx0.000244. We need 14n2<0.0001\frac{1}{4n^2}<0.0001, that is n2>2500n^2>2500, so n>50n>50: 51 pieces are enough (at n=50n=50 the error is exactly 0.00010.0001).
  4. Write f(x)=∣x−0.5∣f(x)=|x-0.5| on [0,1][0,1] as a one-hidden-layer ReLU network with 4 equal pieces. What is its largest error?
    Show answerThe knots are 00, 0.250.25, 0.50.5, 0.750.75, 11 and the heights are 0.50.5, 0.250.25, 00, 0.250.25, 0.50.5. The slopes are −1-1, −1-1, 11, 11, so the bias is 0.50.5 and the weights are −1-1, 00, 22, 00: f^(x)=0.5−ReLU(x)+2 ReLU(x−0.5)\hat f(x)=0.5-\mathrm{ReLU}(x)+2\,\mathrm{ReLU}(x-0.5). A knot sits on the corner, so the error is 00.
  5. 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 answer2×4=82\times4=8 units, 24=162^4=16 pieces, and at least 16−1=1516-1=15 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.

Key takeaways

  • One ReLU is a hinge. Two give a ramp that then goes flat, and three give a hat: zero, then a peak, then zero. A hat is a small localized bump, and adding scaled hats is enough to follow any curve on an interval.
  • Place hats at equally spaced points, scale each to the height of the target there, and add them. The sum is the target's connect-the-dots curve, and it is exactly a one-hidden-layer ReLU network with nn units whose output weights are the changes of slope.
  • The error falls as the points get closer. For x2x^2 on [0,1][0,1] with nn pieces the largest error is exactly 14n2\frac1{4n^2}, so doubling the units divides it by 4. Any continuous function on an interval can be approximated within any ε\varepsilon this way, because a continuous function on an interval is uniformly continuous.
  • The universal approximation theorems (Cybenko 1989; Hornik, Stinchcombe and White 1989; Hornik 1991; Leshno, Lin, Pinkus and Schocken 1993) extend this to several inputs and to other activations. They say the weights exist. They do not say how many units are needed, that training will find them, or that the network will generalise.
  • Depth can be exponentially cheaper than width: kk tents in a row need 2k2k units for 2k2^k pieces, while one hidden layer needs at least 2k−12^k-1 units to match them exactly.
Found a mistake? Tell us