Chapter 10 Universal Approximation

In our motivation of SRM in chapter 9, we talked about wanting to use an ℋ so big that the approximation error infh∈ℋL𝒟⁢(h)−Lbayes is zero. What kinds of ℋ satisfy that?

To keep things simple, we’ll think about 𝒴⊆ℝ for this chapter. We’re also going to assume for simplicity that 𝒳 is compact. For intuition, sets like {x∈ℝd:∥x∥≤R} are compact; ℝd isn’t, and neither is {x∈ℝd:∥x∥<R} .

Definition 10.1.
111There are many definitions of compactness which are equivalent for metric spaces (but not necessarily more general topological spaces).

A metric space 𝒳 is totally bounded if for every η>0 , its covering number Nηcov⁢(𝒳) (definition 4.8) is finite. A metric space which is totally bounded and complete (definition B.9) is called compact.

If 𝒳 is a subset of ℝd and we’re using the Euclidean metric, being complete is the same thing as being a closed set, and being totally bounded is the same as being bounded. That result is known as the Heine-Borel theorem.

One example would be the set of all functions 𝒳→𝒴 . This way leads to a million mathematical counterexamples of being able to do even super basic things like computing expectations, let alone being able to learn.

A milder set to target is the set of all continuous functions. If there’s a continuous function achieving the Bayes error, then this immediately guarantees that the approximation error would be zero.

Definition 10.2.

For a compact metric space 𝒳 , C⁢(𝒳) denotes the Banach space of continuous functions 𝒳→ℝ , with norm given by ∥f∥∞=supx∈𝒳|f⁢(x)| .

Recall that if f and g are elements of a function space and a∈ℝ , we have that a⁢f+g is the function mapping x to a⁢f⁢(x)+g⁢(x) . So, ∥f−g∥∞=supx∈𝒳|f⁢(x)−g⁢(x)| is one possible distance metric on functions.

The following result suggests that this is a reasonable (if strict) way to calculate distances between functions.

Proposition 10.3.

Suppose that ℓ⁢(h,(x,y))=ly⁢(h⁢(x)) for ly:ℝ→ℝ . Then L𝒟 is (𝔼y∼𝒟y∥ly∥Lip) -Lipschitz with respect to ∥h−g∥∞ .

Proof.

We have that

|L𝒟⁢(h)−L𝒟⁢(g)|=|𝔼(x,y)∼𝒟ly⁢(h⁢(x))−𝔼(x,y)∼𝒟ly⁢(g⁢(x))|≤𝔼(x,y)∼𝒟|ly⁢(h⁢(x))−ly⁢(g⁢(x))|≤𝔼(x,y)∼𝒟∥ly∥Lip|h(x)−g(x)|≤(𝔼(x,y)∼𝒟∥ly∥Lip)∥h−g∥∞.∎

10.1 Denseness

Even if the “target function” isn’t continuous, the approximation error could still be zero.

Example 10.4.

Consider 𝒳=[−1,1] , 𝒟x being arbitrary on 𝒳 , and the true labels being determined by the discontinuous function y=𝟙⁡(x>0) . Although this function isn’t in C⁢(𝒳) , you can get arbitrarily close to it, e.g. by taking the continuous functions

fσ⁢(x)={0if ⁢x≤0x/σif ⁢0≤x≤σ1if ⁢x≥σ.

The absolute loss ly⁢(y^)=|y−y^| is

L𝒟⁢(fσ)=𝔼[(1−xσ)⁢𝟙⁡(x∈(0,σ))]≤Pr⁡(x∈(0,σ)).

As σ→0 , for every 𝒟 we necessarily have Pr⁡(x∈(0,σ))→0 , and thus L𝒟⁢(fσ)→0 . As such infh∈C⁢(𝒳)L𝒟⁢(h)=0 – even though if 𝒟x is continuous with positive density on [0,ε] , there is no h∈C⁢(𝒳) with L𝒟⁢(h)=0 . The approximation error is still zero regardless.

C⁢(𝒳) can approximate many interesting function classes. We can frame this with the following definition from metric topology:

Definition 10.5.

Let 𝒢⊆ℱ for some metric space ℱ . We say that 𝒢 is dense in ℱ with respect to the metric ρ if, for every f∈ℱ , infg∈𝒢ρ⁢(g,f)=0 .

That is, for every f∈ℱ that isn’t in 𝒢 , you need to be able to get arbitrarily close to f with points in 𝒢 . A canonical example is that the set of rational numbers is dense in the set of real numbers.

Proposition 10.6.

Suppose that ℋ is dense in ℱ with respect to ∥⋅∥∞ , and use loss ℓ⁢(h,(x,y))=ly⁢(h⁢(x)) with finite 𝔼y∼𝒟y∥ly∥Lip . Then infh∈ℋL𝒟⁢(h)=inff∈ℱL𝒟⁢(f) .

Proof.

Let M=𝔼y∼𝒟y∥ly∥Lip<∞ . Choose (f1,f2,…) to be a sequence in ℱ such that L𝒟⁢(fi)→inff∈ℱL𝒟⁢(f) . For each fi , choose hi∈ℋ such that ∥fi−hi∥∞≤1/i , which is possible because ℋ is dense in ℱ . Then, by proposition 10.3, |L𝒟⁢(hi)−L𝒟⁢(fi)|≤M⁢∥hi−fi∥∞≤Mi→0 , and thus (L𝒟⁢(hi)) converges to the same point as (L𝒟⁢(fi)) . ∎

10.2 Universal Approximators

Definition 10.7.

We call a hypothesis class ℋ 222There are many variants of universality (see e.g. [SFL11]); this is a reasonable baseline that’s essentially what they call c -universal. of functions 𝒳→ℝ universal if ℋ∩C⁢(𝒳) is dense in C⁢(𝒳) with respect to ∥⋅∥∞ .

The following property is known as separating compact sets. It establishes that thresholding functions in a universal hypothesis class can shatter any set, so that VCdim⁡(sgn∘ℋ)=∞ . It also implies that the Rademacher complexity is infinite.

Proposition 10.8.

Let V,W⊂𝒳 be disjoint compact sets,333Finite sets are compact. and let ℋ be universal. Choose any a>0 . Then there exists an h∈ℋ such that h⁢(x)>a for all x∈V , and h⁢(x)<−a for all x∈W .

Proof.

If either set is empty, the result is trivial. Otherwise, define ρV⁡(x)=minv∈V⁡ρ⁡(x,v) , and likewise ρW . Since the sets are compact, we can use just min instead of inf , and they’ll still be well-defined continuous functions in C⁢(𝒳) . Since the sets are compact and disjoint, if ρV⁡(x)=0 then ρW⁡(x)>0 , and vice versa. Thus the following g is well-defined and continuous:

g⁢(x)=2⁢a⁢ρW⁡(x)−ρV⁡(x)ρW⁡(x)+ρV⁡(x).

If x∈V , then ρV⁡(x)=0 , and so g⁢(x)=2⁢a for x∈V . Likewise, g⁢(x)=−2⁢a for x∈W . Thus, any h∈ℋ with ∥h−g∥∞<a will satisfy the property we want. Since g is continuous and ℋ is dense in C⁢(𝒳) , such an h must exist. ∎

This result implies that, at least for binary classifiers, it’s impossible to PAC-learn a universal ℋ . Depending on the ℋ , though, we may be able to nonuniformly learn it with SRM or similar algorithms. If we use a decomposition ℋ=ℋ1∪ℋ2∪⋯ for ℋ1⊂ℋ2⊂⋯ , then even if the approximation error in all of ℋ is zero, the approximation error in ℋk might not be. As we consider ℋk for increasing k , we trade off higher estimation error for lower approximation error.

There might be some particular ℋk where we can achieve zero approximation error (if there’s some h∈ℋ achieving the minimal loss, 444Well-specified doesn’t imply realizable; you might have infh∈ℋL𝒟⁢(h)>0 . also called the well-specified setting).

Even when the approximation error of ℋ is zero, though, we may only have the approximation error of ℋk going to zero as k increases, without ever reaching it for any k . (This is called a misspecified setting.) This would be true e.g. in example 10.4 with ℋk={f:∥f∥Lip≤k} and an 𝒟x with positive density on [0,ε] .

10.3 Universal approximation of neural networks

As you may have heard before (probably invoked in somewhat mystical ways), classes of neural networks are universal.

A feedforward neural network (or multilayer perceptron, MLP) is a function defined hierarchically as

f⁢(x)=f(D)⁢(x)f(k)⁢(x)=σk⁢(Wk⁢f(k−1)⁢(x)+bk)f(0)⁢(x)=x,

where Wk∈ℝdk′×dk−1 , bk∈ℝdk′ , and σk:ℝdk′→ℝdk ; usually, dk=dk′ . Typically σD⁢(z)=z , while intermediate hidden layers use nonlinear activations. Many common choices are componentwise, such as ReLU⁡(z)=max⁡{z,0} , tanh , or sigmoid⁡(z)=11+exp⁡(−z) . Other choices include softmax⁡(z)=(exp⁡(zj))j/∑jexp⁡(zj) , max pooling, attention operators, and so on.

On A3, you bounded the Rademacher complexity for some such networks, with some assumptions on σk , 𝒟 , bounds on Wk , and that bk=0 . (There are [slightly] better bounds than this one; we’ll talk about this a bit soon.) Your bound didn’t explicitly depend on the number of parameters, just on their norms.

It’s worth noting now that neural networks are usually trained via stochastic gradient descent, but this non-convex optimization can be difficult: in general, it’s NP-hard, even to optimize a single ReLU unit with square loss ([GKM+21]). We’ll talk more about optimization soon.

10.3.1 Constructive proofs

The following result is easy to understand, and extremely simple, but is indicative of universal approximation results in general.

Theorem 10.9.

Let g:[0,1]→ℝ be M -Lipschitz. For any ε>0 , there exists a network f such that ∥f−g∥∞≤ε , where the network has one hidden layer with ReLU activations of width N=⌈M/ε⌉ , and one linear output unit.

Proof.

First off, if M=0 , then we can indeed do it with a hidden layer of width zero: the linear output unit just outputs the constant g⁢(0) . Otherwise, we know M>0 .

We’re going to construct a piecewise-linear approximation f to the function g . To do this, it’ll be simplest if we extend g slightly past its domain [0,1] : let g′:[0,∞)→ℝ be g′⁢(t)={g⁢(t)if ⁢t∈[0,1]g⁢(1)if ⁢t≥1 . We have ∥g′∥Lip=∥g∥Lip .

We’ll make f⁢(bi)=g′⁢(bi) for each bi=i⁢εM , where i∈{0,…,N} : 555 g f 0 b1 bi bN x

b0=0,b1=εM,⋯,bN−1=(⌈Mε⌉−1)⁢εM<1,bN=⌈Mε⌉⁢εM≥1.

We can make our piecewise-linear approximation if we start the function at f⁢(0)=g′⁢(0) and then have a slope on each [bi,bi+1) of

mi=g′⁢(bi+1)−g′⁢(bi)bi+1−bi=Mε⁢(g′⁢(bi+1)−g′⁢(bi)).

We can get that with the desired architecture by letting m−1=0 and using simply

f⁢(x)=g⁢(0)+∑i=0N−1(mi−mi−1)⁢ReLU⁡(x−bi),

i.e. the hidden layer has each neuron with weight 1 and bias −bi , and the weights to the single output neuron are mi−mi−1 with bias g⁢(0) .

How good of an approximation is this f ? Well, g′ is M -Lipschitz, and since f is piecewise-linear its Lipschitz constant is the max of the slope of any of its pieces:

∥f∥Lip=maxi⁡|g′⁢(bi+1)−g′⁢(bi)|bi+1−bi≤M.

Thus ∥f−g′∥Lip≤2⁢M . We also know that f−g′ equals 0 on each bi , and any input in [0,1] will have a bi no more than ε2⁢M away. Thus, as in chapter 4,

∥f−g∥∞=supx∈[0,1]|(f−g)⁢(x)|≤2⁢M⋅ε2⁢M=ε.∎

Here’s a similar result in ℝd :

Theorem 10.10.

Let g:[0,1]d→ℝ be continuous. For any ε>0 , 666 δ exists for any ε , since continuous functions on compact domains are uniformly continuous, and ∥⋅∥2 and ∥⋅∥∞ are equivalent. choose δ>0 such that ∥x−x′∥∞≤δ implies |g⁢(x)−g⁢(x′)|≤ε . Then there is a three-layer ReLU network f with 𝒪⁡(1δd) ReLU nodes satisfying ∫[0,1]d|f⁢(x)−g⁢(x)|⁢dx≤2⁢ε .

Proof (sketch).

Approximate the continuous g by a piecewise-constant h , with pieces given by hyper-rectangles. Construct a two-layer ReLU net to (approximately) check whether the input x is in each hyper-rectangle. Put those networks side-by-side as the first two layers of f , so that the second hidden layer is (approximately) an indicator vector of which hyper-rectangle x is in. Use a linear readout layer to set any value on those pieces. For details, see Theorem 2.1 of [TEL21]. ∎

Notice the curse of dimensionality: the size of the network depends exponentially on the dimension, which for deep learning is typically at least hundreds, perhaps millions or more. This isn’t just a proof artifact; it’s necessary to approximate arbitrary continuous functions. The construction also needs really large weights, and has a really bad Lipschitz constant; it also only gives an L1 approximation bound, not sup-norm like before.

10.3.2 Non-constructive bound via Stone-Weierstrass

We can actually get a sup-norm bound with only one hidden layer a different way, using the celebrated Stone-Weierstrass approximation theorem from analysis.

Theorem 10.11 (Stone-Weierstrass, special case).

Let 𝒳 be a compact metric 777More generally, 𝒳 can be compact Hausdorff. space. Suppose ℱ is a set of functions from 𝒳→ℝ such that:

  • •

    Each f∈ℱ is continuous: ℱ⊆C⁢(𝒳) .

  • •

    For each x∈𝒳 , there is at least one f∈ℱ with f⁢(x)≠0 .

  • •

    For all f,g∈ℱ and α∈ℝ , α⁢f+g∈ℱ and f⁢g=(x↦f⁢(x)⁢g⁢(x))∈ℱ . 888 ℱ is an algebra.

  • •

    For each x≠x′∈𝒳 , there is at least one f∈ℱ with f⁢(x)≠f⁢(x′) . 999 ℱ separates points.

Then ℱ is dense in C⁢(𝒳) with respect to ∥⋅∥∞ .

You may have heard of the Weierstrass theorem, which shows that polynomial functions are dense in C⁢([a,b]) ; this is a generalization. The rough idea of the proof is to first show that the closure of ℱ is also closed under maxima and minima, use point separation to interpolate a target function at each two points, then use maxima and minima to build those local interpolants into a global approximation.

Proposition 10.12.

ℱexp is dense in C⁢(𝒳) , where 𝒳⊂ℝd is compact and

ℱexp={x↦∑i=1mai⁢exp⁡(⟨wi,x⟩):m≥1;w1,…,wm∈ℝd;a1,…,am∈ℝ}.

Notice that ℱexp is a set of one-hidden-layer neural networks with exponential hidden activations and unbounded width.

Proof.

We just need to show it satisfies the conditions of Stone-Weierstrass. The first two are clear. For f⁢(x)=∑i=1mai⁢exp⁡(⟨wi,x⟩) and g⁢(x)=∑i=1m′ai′⁢exp⁡(⟨wi′,x⟩) , we have

α⁢f+g=(x↦∑i=1m(α⁢ai)⁢exp⁡(⟨wi,x⟩)+∑i=1m′ai′⁢exp⁡(⟨wi′,x⟩))∈ℱexp
f⁢g=(x↦∑i=1m∑j=1m′ai⁢aj′⁢exp⁡(⟨wi+wj′,x⟩))∈ℱexp.

To show ℱexp separates x1 and x2 , consider f⁢(x)=exp⁡(⟨x1−x2,x⟩) , so that

f⁢(x1)f⁢(x2)=exp⁡(∥x1∥2−⟨x2,x1⟩)exp⁡(⟨x1,x2⟩−∥x2∥2)=exp⁡(∥x1∥2−2⁢⟨x1,x2⟩+∥x2∥2)=exp⁡(∥x1−x2∥2),

which is one iff x1=x2 ; thus if x1≠x2 , f⁢(x1)≠f⁢(x2) . ∎

Proposition 10.13 (([HSW89])).

Let 𝒳⊂ℝd be compact, and σ:ℝ→ℝ be continuous with limz→−∞σ⁢(z)=0 , limz→∞σ⁢(z)=1 . Then ℱσ is dense in C⁢(𝒳) , where ℱσ is defined as

ℱσ={x↦∑i=1mai⁢σ⁢(⟨wi,x⟩+bi):m≥1;wi∈ℝd;ai,bi∈ℝ}.
Proof (sketch).

For any continuous target g , first find an f0∈ℱexp such that ∥f0−g∥∞≤ε/2 , possible by proposition 10.12. Now, find some coefficients such that

exp⁡(z)≈∑jcj⁢σ⁢(tj⁢z+bj)

is sufficiently accurate so that replacing each exp⁡(⟨wi,x⟩) in f0 by ∑jcj⁢σ⁢(tj⁢⟨wi,x⟩+bj) , we find an f∈ℱσ such that ∥f−f0∥∞≤ε/2 . ∎

More generally, this general approach works if σ is anything (locally bounded and piecewise continuous) that’s not (almost everywhere) a polynomial ([LLP+93]). (A shallow network with fixed-degree polynomial activations is itself a polynomial of fixed degree.) These are for shallow, wide networks, but if you use a deep, narrow network you can get away even with (non-affine) polynomial activations ([KL20]).

There are also a variety of other results. Maybe most important is an infinite-width construction of [BAR93]; also see Section 3 of [TEL21] or Section 9.3 of [BAC25].

10.4 Circuit complexity

We won’t go into depth on this perspective, but it’s definitely worth knowing it exists. [SB14], Chapter 20 overview the general basic results, but the standard classic text is [PAR94]. The short version:

  • •

    Two-layer networks with threshold activations can represent all functions from {±1}d→{±1} . Since computers always represent things as binary strings, that’s pretty powerful.

  • •

    But, it takes exponential width to do that.

  • •

    But, for any Boolean function that can be computed with maximal runtime T , there exists a network of size 𝒪⁡(T2) that implements that function.

There’s also recent work on relating circuit complexity to ResNets, Transformers, and chain of thought ([MS23, CHI25, SHT24, AHR+25, JAC26]).

10.5 Interpretation

“Neural networks can do anything!!”

(You don’t hear “decision trees can do anything!!” as often, but it’s just as true….)

These results mean that, for any (continuous) function (on a compact domain) that we’d like to approximate, there is some neural net that can closely approximate that behaviour. Continuous functions also aren’t a huge limit, as in example 10.4. So, there is some neural network that can approximate “what’s the next bit in the response of a very smart human to a Unicode string of length at most 128,000 bytes.” But we can only guarantee that there’s such a network that’s very large (in parameter count and also weight norm). There’s also a really really big decision tree that can do that.

So, does ERM in a large enough hypothesis class, or SRM, or whatever other learning algorithm, necessarily generalize? Maybe not.

Also, for neural networks ERM is NP-hard; does gradient descent approximate it well? Maybe not.

But, are these constructions with enormous norms indicative of the actual norm required for functions we care about? Maybe not.

One way to help answer these questions is to characterize what kinds of functions have large norms. This is mostly beyond the scope of this course, but the typical traditional scheme is based on functions in Sobolev classes; [BAC25] has a bunch of material on this. There’s also recent work on, say, constructing Transformers to do some particular task, as an existence proof of approximation for that task (rather than universally).