Simple Algorithm
Deep Learning · Representation · 24 min

What Networks Can Represent

Universal approximation, width versus depth, and what representing is not.

A network with enough neurons can trace any continuous curve as closely as you like. That is a theorem, and in one dimension you can prove it with a pencil. The interesting questions start right after it: how many neurons, arranged how deep, and why a network that can represent a function may still never learn it.

What you’ll learn

  • Prove universal approximation in one dimension by building a ReLU network from hat functions, with the error bound K/(2N)K/(2N) for a KK-Lipschitz target, and state the general theorems of Cybenko, Hornik and Leshno et al.
  • Count linear pieces: at most m+1m+1 for one hidden layer of mm ReLUs on a line and at most (m+1)L(m+1)^L for LL such layers, and use the sawtooth to show that depth can be exponentially cheaper than width.
  • State the minimal width at which deep narrow ReLU networks become universal, and what counts of linear regions say about networks in higher dimension.
  • Separate three questions that are often confused (do good weights exist, does training find them, do the data pin them down) and explain inductive bias as the set of functions an architecture makes cheap.

Any curve from hat functions

The universal approximation question asks: for which activation functions can a network with one hidden layer approximate every continuous function on a bounded region, to any accuracy, given enough units? Neurons, Layers & Activations showed that one hidden layer of ReLUs computes any continuous piecewise-linear function of one variable. Approximation is then one step away: replace ff by the piecewise-linear function that agrees with it on a grid.

DerivationUniversal approximation on an interval, with an error bound
  1. g(x)=∑i=0Nf(ti) ϕi(x),ϕi(x)=ReLU⁡(x−tih+1)−2ReLU⁡(x−tih)+ReLU⁡(x−tih−1)g(x)=\sum_{i=0}^{N}f(t_i)\,\phi_i(x),\qquad \phi_i(x)=\operatorname{ReLU}\Big(\frac{x-t_i}{h}+1\Big)-2\operatorname{ReLU}\Big(\frac{x-t_i}{h}\Big)+\operatorname{ReLU}\Big(\frac{x-t_i}{h}-1\Big)

    Let f:[0,1]→Rf:[0,1]\to\R be KK-Lipschitz: ∣f(x)−f(y)∣≤K∣x−y∣\abs{f(x)-f(y)}\le K\abs{x-y}. Cut [0,1][0,1] into NN equal pieces with knots ti=iht_i=ih, h=1/Nh=1/N, and build the interpolant gg from hat functions centered on the knots:

  2. g(x)=f(0)+∑i=0N−1(si−si−1)ReLU⁡(x−ti)(0≤x≤1)g(x)=f(0)+\sum_{i=0}^{N-1}\big(s_i-s_{i-1}\big)\operatorname{ReLU}(x-t_i)\qquad(0\le x\le1)

    Each ϕi\phi_i is a tent of height 1 at tit_i that falls to 0 at the neighboring knots, so g(ti)=f(ti)g(t_i)=f(t_i) and gg is linear between knots. Neighboring hats share their kinks, so the whole sum needs only NN ReLUs on [0,1][0,1]. With sis_i the slope of gg on the piece [ti,ti+1][t_i,t_{i+1}] and s−1=0s_{-1}=0:

  3. ∣f(x)−g(x)∣≤(1−λ)∣f(x)−f(ti)∣+λ∣f(x)−f(ti+1)∣≤2λ(1−λ) Kh≤Kh2=K2N\abs{f(x)-g(x)}\le(1-\lambda)\abs{f(x)-f(t_i)}+\lambda\abs{f(x)-f(t_{i+1})}\le2\lambda(1-\lambda)\,Kh\le\frac{Kh}{2}=\frac{K}{2N}

    On the piece [ti,ti+1][t_i,t_{i+1}] write x=ti+λhx=t_i+\lambda h with λ∈[0,1]\lambda\in[0,1]. Then g(x)g(x) is the weighted average (1−λ)f(ti)+λf(ti+1)(1-\lambda)f(t_i)+\lambda f(t_{i+1}), and the Lipschitz bound applies to each part. The worst point is the middle of a piece, λ=12\lambda=\tfrac12:

  4. sup⁡[0,1]∣f−g∣≤ω(h)(f continuous),sup⁡[0,1]∣f−g∣≤h28max⁡[0,1]∣f′′∣(f twice differentiable)\sup_{[0,1]}\abs{f-g}\le\omega(h)\quad(f\text{ continuous}),\qquad \sup_{[0,1]}\abs{f-g}\le\frac{h^2}{8}\max_{[0,1]}\abs{f''}\quad(f\text{ twice differentiable})

    For a merely continuous ff the same average gives ∣f−g∣≤ω(h)\abs{f-g}\le\omega(h), where ω\omega is the modulus of continuity of ff; it tends to 00 with hh because ff is uniformly continuous on [0,1][0,1]. That proves universal approximation for ReLU networks in one dimension. For a twice-differentiable ff the classical interpolation bound is sharper: halving hh quarters the error.

Predict before you look

You approximate f(x)=sin⁡2πxf(x)=\sin 2\pi x on [0,1][0,1] by its piecewise-linear interpolant. Using max⁡∣f′′∣=4π2\max\abs{f''}=4\pi^2 and the bound h2max⁡∣f′′∣/8h^2\max\abs{f''}/8, how many equal pieces N=1/hN=1/h guarantee an error of at most 0.010.01?

Show the answer

23 pieces

You need N2≥4π2/(8×0.01)≈493N^2\ge4\pi^2/(8\times0.01)\approx493, so N≥22.2N\ge22.2: 23 pieces, which is 23 ReLUs. The Lipschitz bound with K=2πK=2\pi would ask for N≥2π/0.02≈315N\ge2\pi/0.02\approx315, so smoothness buys a lot. The bound is nearly tight: at N=16N=16 the true error is 0.01880.0188 against a bound of 0.01930.0193.

Polynomials fail for a simple reason: with σ(z)=zk\sigma(z)=z^k every unit is a polynomial of degree kk in x\mathbf x, and so is any sum of them, so no number of units escapes the polynomials of degree kk. Every non-polynomial activation works, which is why expressivity is not the reason to choose one activation over another (Activation Functions in Depth).

The theorems are existence results. They do not say how many units are needed, and in dd dimensions the grid idea is expensive: a grid of spacing hh has (1/h)d(1/h)^d cells. For a general Lipschitz function on [0,1]d[0,1]^d the number of parameters needed for accuracy ε\varepsilon grows exponentially with dd, about ε−d/2\varepsilon^{-d/2} up to logarithmic factors even for the deepest ReLU networks (Yarotsky, 2018). This curse of dimensionality lifts only for special classes. For functions of finite Barron norm, one hidden layer of mm sigmoidal units reaches squared error O(1/m)\order{1/m} in any dimension (Barron, 1993).

Counting pieces: width versus depth

One hidden layer suffices, given enough units. Depth earns its place through economy, and in one dimension you can see why by counting linear pieces.

DerivationHow many pieces can a ReLU network make?
  1. pout≤p+m p=(m+1) pp_{\text{out}}\le p+m\,p=(m+1)\,p

    Feed a scalar xx into a layer of mm ReLUs whose inputs are piecewise linear in xx with pp pieces. On each piece every pre-activation is affine in xx, so each ReLU can add at most one breakpoint per piece, mpmp new breakpoints in all:

  2. p≤m+1(one hidden layer),p≤(m+1)L(L hidden layers of width m)p\le m+1\quad(\text{one hidden layer}),\qquad p\le(m+1)^L\quad(L\text{ hidden layers of width }m)

    The input itself has one piece. So one hidden layer of mm units gives at most m+1m+1 pieces (its output layer is affine and adds none), and LL hidden layers of width mm give at most (m+1)L(m+1)^L. Adding units adds pieces; composing layers multiplies them.

  3. T∘k=T∘⋯∘T⏟k times has 2k linear pieces on [0,1],T∘k(j/2k)={0j even1j oddT^{\circ k}=\underbrace{T\circ\cdots\circ T}_{k\text{ times}}\ \text{has }2^k\text{ linear pieces on }[0,1],\qquad T^{\circ k}\big(j/2^k\big)=\begin{cases}0 & j\text{ even}\\ 1 & j\text{ odd}\end{cases}

    Composition can actually reach exponential growth. The tent map T(x)=2ReLU⁡(x)−4ReLU⁡(x−12)T(x)=2\operatorname{ReLU}(x)-4\operatorname{ReLU}(x-\tfrac12) maps [0,1][0,1] onto itself twice, up and back down. Composing it folds the interval again and again, and at the grid points the values alternate between 0 and 1:

  4. sup⁡[0,1]∣g−T∘k∣<12 ⟹ g has ≥2k−1+1 pieces ⟹ m≥2k−1 units in one hidden layer\sup_{[0,1]}\abs{g-T^{\circ k}}<\tfrac12\ \Longrightarrow\ g\text{ has}\ \ge2^{k-1}+1\text{ pieces}\ \Longrightarrow\ m\ge2^{k-1}\text{ units in one hidden layer}

    A deep network computes T∘kT^{\circ k} exactly with 2k2k units in kk layers of width 2. One hidden layer needs at least 2k−12^k-1 units to compute it exactly, and at least 2k−12^{k-1} even to approximate it within 12\tfrac12. If sup⁡∣g−T∘k∣<12\sup\abs{g-T^{\circ k}}<\tfrac12, then gg is below 12\tfrac12 at even grid points and above at odd ones. A linear piece of gg containing three consecutive grid points would have to rise and then fall, so each piece holds at most two of the 2k+12^k+1 grid points:

Predict before you look

How many linear pieces does T∘10T^{\circ 10}, the tent map composed 10 times, have on [0,1][0,1]?

Show the answer

1024 pieces

Each composition doubles the pieces: 210=10242^{10}=1024. A deep network computes it with 20 ReLUs in 10 layers of width 2. A single hidden layer needs at least 1,023 units to compute it exactly, one per breakpoint, and at least 512 to stay within 12\tfrac12 of it everywhere.

This is the simplest depth separation. Telgarsky (2016) made it general: for every kk there are networks with Θ(k3)\Theta(k^3) layers of constant width that any network with O(k)\order{k} layers can approximate only with Ω(2k)\Omega(2^k) units. Composition also helps with smooth functions. On [0,1][0,1], x2=x−∑s≥1T∘s(x)/4sx^2=x-\sum_{s\ge1}T^{\circ s}(x)/4^s, so stopping the sum after mm terms approximates x2x^2 within 4−(m+1)4^{-(m+1)} using O(m)\order{m} layers (Yarotsky, 2017). Then xy=12((x+y)2−x2−y2)xy=\tfrac12\big((x+y)^2-x^2-y^2\big) gives products, and products give polynomials. That construction is the approximation engine of Deep Networks as Statistical Estimators.

In dd dimensions the pieces become convex polytopes, the linear regions. One hidden layer of mm ReLUs cuts Rd\R^d with mm hyperplanes, which makes at most ∑i=0d(mi)\sum_{i=0}^{d}\binom mi regions, a polynomial of degree dd in mm. With LL layers of width n≥dn\ge d, Montúfar, Pascanu, Cho & Bengio (2014) constructed networks with Ω(⌊n/d⌋(L−1)d nd)\Omega\big(\lfloor n/d\rfloor^{(L-1)d}\,n^d\big) regions, exponential in depth. Typical networks are far more modest. Hanin & Rolnick (2019) proved that at initialization the average number of regions in a fixed input region is bounded by roughly the total number of neurons raised to the input dimension, independently of depth, and found the bound tight during training as well. The maximum shows what depth can do, not what training does.

How narrow can a deep network be?

Turn the classical theorem around: fix the width and let the depth grow without limit. Is a very deep, very narrow ReLU network universal? Only above a threshold width, and the threshold is now known almost exactly:

SettingResultSource
one hidden layer of any width; any non-polynomial activationuniversal, uniformly on compact setsLeshno et al. (1993)
ReLU, every hidden layer of width ≤din\le d_{\text{in}}, any depthnot universal for continuous f:[0,1]din→Rf:[0,1]^{d_{\text{in}}}\to\RHanin & Sellke (2017)
ReLU, width din+1d_{\text{in}}+1, unbounded depthuniversal for continuous f:[0,1]din→Rf:[0,1]^{d_{\text{in}}}\to\R, uniformly; width din+doutd_{\text{in}}+d_{\text{out}} suffices for vector outputsHanin & Sellke (2017)
ReLU, width din+4d_{\text{in}}+4, unbounded depthuniversal for integrable functions on Rdin\R^{d_{\text{in}}} in L1L^1; width dind_{\text{in}} is notLu, Pu, Wang, Hu & Wang (2017)
ReLU, approximation in LpL^p, 1≤p<∞1\le p<\inftyminimal width exactly max⁡{din+1, dout}\max\{d_{\text{in}}+1,\ d_{\text{out}}\}Park, Yun, Lee & Shin (2021)
So bounded width with unbounded depth is universal once the width exceeds the input dimension, and at width dind_{\text{in}} or below it is not, however deep the network. The ReLU-only LpL^p answer does not carry over to uniform approximation, where the exact minimal width for vector outputs is still being pinned down.
Check your understanding

A ReLU network maps inputs in [0,1]3[0,1]^3 to a scalar, and every hidden layer has the same width ww. Which statement about making it arbitrarily deep is correct?

  1. Width w=3w=3 suffices: depth can make up for anything once the width matches the input
  2. No constant width suffices; the width must grow as the accuracy improves
  3. Width w=4w=4 suffices to approximate every continuous function uniformly, given enough depth
  4. Width w=1w=1 suffices, since a deep enough chain of single ReLUs can bend any way it likes
Show the answer

C. Width w=4w=4 suffices to approximate every continuous function uniformly, given enough depth

The minimal width for uniform approximation of continuous scalar functions on [0,1]din[0,1]^{d_{\text{in}}} is exactly din+1=4d_{\text{in}}+1=4 (Hanin & Sellke, 2017). Below it, depth does not help.

Representing is not learning

A universal approximation theorem answers the first of three questions, and the other two are harder:

  1. Existence. Do weights exist that approximate the target? For a continuous target and enough units, yes.
  2. Optimization. Will training find them? Nothing guarantees it. Deciding whether a network of just three threshold units can fit a given labeled dataset is NP-complete (Blum & Rivest, 1992), and gradient descent on a nonconvex loss promises only to go downhill.
  3. Generalization. Do the training data single out weights that work on new data? A network that fits nn points can usually fit the same points with random labels, so a good fit proves nothing by itself (The Generalization Puzzle).

Counting parameters against data points is therefore a weak guide in both directions. Image classifiers with tens of millions of parameters are trained on ImageNet's 1.28 million images and still generalize, and the curve above shows that the worst place to be is often where parameters and data points balance. Among the many functions that fit the data, which one training returns is decided by the architecture, the optimizer and the data. Much of the rest of this course is about that choice.

Inductive bias: what is cheap to represent

If nearly every architecture is universal, why does the choice matter so much? Universality is about unlimited resources; learning happens with a fixed budget of parameters, data and compute. An architecture's inductive bias is the set of functions it makes cheap: representable with few parameters, and easy for gradient descent to reach from few examples.

ArchitectureStructure built inFunctions it makes cheap
multilayer perceptroncomposition of layers, nothing about the input's layoutcompositions of simple pieces; low frequencies before high ones (Spectral Bias & Fourier Features)
convolutional networklocality and weight sharing across positionstranslation-equivariant maps of images and audio, built from local patterns (Convolutional Networks & Vision)
recurrent network, state space modelone update rule applied at every time stepfunctions of sequences of any length that summarize the past in a fixed-size state (Recurrent Networks & Sequence Models)
transformerattention: interactions between positions weighted by contentfunctions in which any token may depend on any other, selected by content (Transformers)
graph neural networkaggregation over neighbors with shared weightsfunctions of graphs that ignore how the nodes are numbered (Graph Neural Networks)
Each row trades generality for efficiency. A convolutional layer represents only a sliver of the linear maps a dense layer can, but it is the sliver images need, at a tiny fraction of the parameters. Deriving architectures systematically from the symmetries of the data is the program of geometric deep learning (Bronstein, Bruna, Cohen & Veličković, 2021).
Check your understanding

An MLP and a convolutional network are both universal approximators. What does choosing the convolutional network for images actually buy?

  1. The ability to represent functions of images that no MLP can represent
  2. A guarantee that the trained network generalizes
  3. Nothing in principle; only the training speed differs
  4. Translation-respecting functions become cheap: few parameters and learnable from far fewer examples
Show the answer

D. Translation-respecting functions become cheap: few parameters and learnable from far fewer examples

Both can represent the same functions given enough units. Weight sharing and locality make the functions images need cost few parameters and few examples, and those costs, not representability, decide what training finds with a fixed budget.

Key takeaways

  • One hidden layer approximates any continuous function on a compact set exactly when the activation is not a polynomial. In one dimension, NN ReLUs interpolating on a grid give error at most K/(2N)K/(2N) for a KK-Lipschitz target.
  • Adding units adds linear pieces and composing layers multiplies them: one layer of mm ReLUs makes at most m+1m+1 pieces on a line, LL layers at most (m+1)L(m+1)^L. The tent map composed kk times takes 2k2k units in depth but at least 2k−12^{k-1} in one layer, even to approximate.
  • Deep narrow ReLU networks are universal exactly when their width exceeds the input dimension: din+1d_{\text{in}}+1 for continuous scalar functions.
  • Existence of good weights, finding them, and generalizing from data are separate questions. Parities are easy to represent but hard for gradient descent, and a network that interpolates its data can still predict badly.
  • Architectures differ less in what they can represent than in what they make cheap. That inductive bias is why convolution, recurrence, attention and message passing exist.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

You approximate a 3-Lipschitz function on [0,1][0,1] by its piecewise-linear interpolant on NN equal pieces. What is the smallest NN for which the bound K/(2N)K/(2N) guarantees an error of at most 0.010.01?

  1. N=300N=300
  2. N=150N=150
  3. N=15N=15
  4. N=1500N=1500
Show the answer

B. N=150N=150

3/(2N)≤0.013/(2N)\le0.01 gives N≥150N\ge150, so 150 ReLUs. The factor 12\tfrac12 comes from the worst point being in the middle of a piece, where both knots are h/2h/2 away.

Question 2

A ReLU network on a scalar input has four hidden layers of 3 units each. By the counting argument, what is the largest number of linear pieces its output can have?

  1. 1212
  2. 1313
  3. 8181
  4. 256256
Show the answer

D. 256256

Each layer of mm units multiplies the number of pieces by at most m+1=4m+1=4, so four layers give at most 44=2564^4=256. A single hidden layer with the same 12 units makes at most 13.

Question 3

With which activation can networks with one hidden layer not approximate every continuous function on [0,1]2[0,1]^2, however wide they are?

  1. ReLU
  2. tanh
  3. ϕ(z)=z3\phi(z)=z^3
  4. ϕ(z)=sin⁡z\phi(z)=\sin z
Show the answer

C. ϕ(z)=z3\phi(z)=z^3

By Leshno et al. (1993), a locally bounded, piecewise continuous activation gives universal approximation exactly when it is not a polynomial. With z3z^3, every unit and every sum of units is a polynomial of degree at most 3.

Question 4

A network with one hidden layer of width 10d10d can represent the parity of any subset of dd input bits, yet gradient training on a random parity makes no progress for d=30d=30. Why?

  1. The gradient is almost the same whichever parity is the target, so it carries almost no information about which one it is
  2. The network only approximates parities, and the approximation error swamps the signal
  3. At d=30d=30 the network has too few parameters to store the answer
  4. The network overfits the training set before it can find the parity
Show the answer

A. The gradient is almost the same whichever parity is the target, so it carries almost no information about which one it is

Shalev-Shwartz, Shamir & Shammah (2017) showed that the gradient's variance over the 2d2^d possible parities is at most G2/2dG^2/2^d. Expressivity is not the problem; the training signal is.

Question 5

Deep ReLU networks of constant width ww approximate continuous functions [0,1]10→R[0,1]^{10}\to\R in the uniform norm, with unlimited depth. What is the smallest width that works?

  1. w=10w=10
  2. w=14w=14
  3. w=11w=11
  4. w=20w=20
Show the answer

C. w=11w=11

Hanin and Sellke (2017) showed the minimal width is exactly din+1=11d_{\text{in}}+1=11: width 11 suffices with enough depth, and width 10 fails at any depth.

Go deeper

Further reading

  • S. J. D. Prince, Understanding Deep Learning (MIT Press, 2023), ch. 3 "Shallow neural networks" and ch. 4 "Deep neural networks".
  • M. Leshno, V. Y. Lin, A. Pinkus & S. Schocken, "Multilayer feedforward networks with a nonpolynomial activation function can approximate any function," Neural Networks 6(6) (1993).
  • M. Telgarsky, "Benefits of depth in neural networks," COLT (2016).
  • B. Hanin & M. Sellke, "Approximating continuous functions by ReLU nets of minimal width," arXiv:1710.11278 (2017).
  • S. Shalev-Shwartz, O. Shamir & S. Shammah, "Failures of gradient-based deep learning," ICML (2017).