DeepONets, their error decomposition and covariance-operator projections
DefinitionDeepONetError_BasicObjects of Sections 2 and 3 of Lanthaler, Mishra and Karniadakis (arXiv:2102.09618v3).
- ReLU networks (Section 2.1, eqs. (2.1)-(2.3)). A ReLU network is with affine maps , , , , and applied componentwise.
- DeepONets (Section 2.2, eqs. (2.7)-(2.10), Definition 2.4). For sensors the encoder is . The approximator is a ReLU network . Trunk functions induce the reconstructor . The DeepONet is . When the trunk functions come from a ReLU network , they are viewed as elements of .
- Data (Definition 2.1). and are bounded measurable sets carrying Lebesgue measure. is a normed space (the theorems add completeness and separability as separate assumptions) with a continuous linear injection , and is a Borel probability measure on with . A Borel set with consists of elements whose image in has a representative that is continuous on . Finally is Borel measurable with .
- Errors (eqs. (2.11), (3.1)-(3.3)). The quantities below take values in :
- total error: ;
- encoding error: ;
- approximation error: ;
- reconstruction error: , with in the paper.
- Lipschitz and Hölder constants (Section 3.2.1). and , both with values in .
- Measures on a Hilbert space (Section 3.3). means a probability measure with . Its mean is . The uncentered covariance operator is and the covariance operator is . A spectral decomposition consists of an orthonormal sequence and a non-increasing sequence with for all . The span of the eigenvectors with eigenvalue in a set is also defined.
- Projection errors (eqs. (3.13), (3.16)). . A linear subspace with is optimal if it minimizes this error among -dimensional linear subspaces. An affine subspace is optimal if it minimizes the error among affine subspaces whose direction is -dimensional.
- Optimal projector and reconstruction (eqs. (3.19), (3.21)). For linearly independent , the dual basis is , and the projector is . The optimal trunk functions are and .
Formalization Note The output space is identified with its image in , as in Section 2.2 of the paper; every result uses only the norm on the output side. Eigenvalues are indexed from , so the paper's is lam (k-1) and the tail is ∑' k, lam (k + p). Square roots and integrals of nonnegative quantities are taken in . The function toL2 returns for a function that is not square integrable on ; it is used only for ReLU networks on a compact , which are always square integrable there.
import Mathlib
/-!
Lanthaler, Mishra and Karniadakis, *Error estimates for DeepOnets: A deep learning framework in
infinite dimensions*, arXiv:2102.09618v3 (2022): ReLU networks (Section 2.1), DeepONets and the
data of the approximation problem (Section 2.2, Definitions 2.1 and 2.4, eqs. (2.7)-(2.11)), the
encoding, approximation and reconstruction errors (Section 3.2.1, eqs. (3.1)-(3.3)), Lipschitz and
Hoelder constants (Section 3.2.1), covariance operators and projection errors (Sections 3.2.2 and
3.3, eqs. (3.8), (3.13)-(3.18)) and the optimal projector and reconstruction (Section 3.4,
eqs. (3.19)-(3.23)).
-/
open MeasureTheory
noncomputable section
namespace DeepONetError
/-- Eq. (2.2): the ReLU activation `sigma(z) = max(z, 0)`. -/
def relu (z : ℝ) : ℝ := max z 0
/-- Section 2.1, eqs. (2.1)-(2.3): a ReLU network `C_K ∘ sigma ∘ C_{K-1} ∘ ... ∘ sigma ∘ C_1`
from `ℝ^din` to `ℝ^dout` with `K = L + 1` affine maps `C_k z = W_k z + b_k` and `L = K - 1 ≥ 1`
hidden layers. The layer widths are `width 0 = din, width 1, ..., width (L+1) = dout`; the affine
map number `k` (counting from `0`) goes from `ℝ^(width k)` to `ℝ^(width (k+1))`. -/
structure ReLUNet (din dout : ℕ) where
/-- The number `L = K - 1` of hidden layers. -/
L : ℕ
/-- The paper requires `1 < K`. -/
one_le_L : 1 ≤ L
/-- The layer widths `d_1, ..., d_{K+1}` (here indexed from `0`). -/
width : ℕ → ℕ
width_zero : width 0 = din
width_last : width (L + 1) = dout
/-- The weight matrices `W_k`. -/
W : (k : ℕ) → Matrix (Fin (width (k + 1))) (Fin (width k)) ℝ
/-- The bias vectors `b_k`. -/
b : (k : ℕ) → Fin (width (k + 1)) → ℝ
/-- Eq. (2.1): the output `sigma ∘ C_k ∘ ... ∘ sigma ∘ C_1 (x)` of the first `k` layers of a ReLU
network (the input itself for `k = 0`). -/
def ReLUNet.hidden {din dout : ℕ} (N : ReLUNet din dout) (x : Fin din → ℝ) :
(k : ℕ) → Fin (N.width k) → ℝ
| 0 => fun i => x (Fin.cast N.width_zero i)
| k + 1 => fun i => relu ((N.W k).mulVec (N.hidden x k) i + N.b k i)
/-- Eq. (2.1): the function `ℝ^din → ℝ^dout` realized by a ReLU network; the last affine map
`C_K` is not followed by an activation. -/
def ReLUNet.eval {din dout : ℕ} (N : ReLUNet din dout) (x : Fin din → ℝ) : Fin dout → ℝ :=
fun i => (N.W N.L).mulVec (N.hidden x N.L) (Fin.cast N.width_last.symm i) +
N.b N.L (Fin.cast N.width_last.symm i)
/-- The Hilbert space `L^2(U)` for a set `U ⊆ ℝ^n` with Lebesgue measure (Section 2.2). -/
abbrev L2 {n : ℕ} (U : Set (Fin n → ℝ)) : Type := Lp ℝ 2 (volume.restrict U)
open Classical in
/-- A function on `ℝ^n` viewed as an element of `L^2(U)` (Section 2.2: a trunk net restricted to
the bounded set `U` is square integrable there; the value `0` is only a default for functions
that are not square integrable on `U`). -/
def toL2 {n : ℕ} (U : Set (Fin n → ℝ)) (f : (Fin n → ℝ) → ℝ) : L2 U :=
if h : MemLp f 2 (volume.restrict U) then h.toLp f else 0
/-- Eq. (2.9): the reconstructor induced by the functions `tau_0, ..., tau_p`,
`R(alpha) = tau_0 + sum_{k=1}^p alpha_k tau_k`. Here `tau k.succ` is the paper's `tau_{k+1}`. -/
def trunkRec {F : Type*} [AddCommGroup F] [Module ℝ F] {p : ℕ} (tau : Fin (p + 1) → F)
(a : EuclideanSpace ℝ (Fin p)) : F :=
tau 0 + ∑ k : Fin p, a k • tau k.succ
/-- Eq. (2.10) and Definition 2.4: the DeepONet `N = R ∘ A ∘ E` on continuous input functions
`u ∈ C(D)`, with sensors `x_1, ..., x_m ∈ D` (encoder (2.7), `E(u) = (u(x_1), ..., u(x_m))`),
an approximator ReLU network `A : ℝ^m → ℝ^p` (eq. (2.8)) and a trunk ReLU network
`tau : ℝ^n → ℝ^(p+1)` (Section 2.2), with values in `L^2(U)`. -/
def deepONet {d n m p : ℕ} {Dset : Set (Fin d → ℝ)} (U : Set (Fin n → ℝ)) (x : Fin m → Dset)
(Apx : ReLUNet m p) (trunk : ReLUNet n (p + 1)) (u : C(Dset, ℝ)) : L2 U :=
trunkRec (fun k => toL2 U (fun y => trunk.eval y k))
(WithLp.toLp 2 (Apx.eval (fun j => u (x j))))
/-- Definition 2.1 (data for the DeepONet approximation problem), with the output space `Y`
identified with its image in `L^2(U)` as in Section 2.2: `D ⊆ ℝ^d` and `U ⊆ ℝ^n` are bounded
(measurable) sets, `iota : X → L^2(D)` is a continuous linear embedding, `mu` is a Borel probability
measure on `X` with finite second moment, there is a Borel set `A` with `mu(A) = 1` consisting of
(elements whose image in `L^2(D)` has a representative that is) continuous functions on `D`, and
`G` is Borel measurable with `∫ ‖G(u)‖²_{L^2(U)} dmu < ∞`. -/
def IsDeepONetData {d n : ℕ} (Dset : Set (Fin d → ℝ)) (U : Set (Fin n → ℝ)) {X : Type*}
[NormedAddCommGroup X] [NormedSpace ℝ X] [MeasurableSpace X] [MeasurableSpace (L2 U)]
(iota : X →L[ℝ] L2 Dset) (mu : Measure X) (A : Set X) (G : X → L2 U) : Prop :=
Bornology.IsBounded Dset ∧ MeasurableSet Dset ∧ Bornology.IsBounded U ∧ MeasurableSet U ∧
Function.Injective iota ∧ IsProbabilityMeasure mu ∧ (∫⁻ u, ‖u‖ₑ ^ 2 ∂mu) < ⊤ ∧
MeasurableSet A ∧ mu A = 1 ∧
(∀ u ∈ A, ∃ f : (Fin d → ℝ) → ℝ, ContinuousOn f Dset ∧
((iota u : L2 Dset) : (Fin d → ℝ) → ℝ) =ᵐ[volume.restrict Dset] f) ∧
Measurable G ∧ (∫⁻ u, ‖G u‖ₑ ^ 2 ∂mu) < ⊤
/-- Eq. (2.11): the DeepONet error `(∫_X ‖G(u) - N(u)‖²_{L^2(U)} dmu(u))^{1/2}`, valued in
`[0, ∞]`. -/
def totalErr {X F : Type*} [MeasurableSpace X] [NormedAddCommGroup F] (mu : Measure X)
(G N : X → F) : ENNReal :=
(∫⁻ u, ‖G u - N u‖ₑ ^ 2 ∂mu) ^ ((1 : ℝ) / 2)
/-- Eq. (3.1): the encoding error `(∫_X ‖D(E(u)) - u‖²_X dmu(u))^{1/2}` of an encoder `E` and a
decoder `D`. -/
def encErr {X : Type*} [MeasurableSpace X] [NormedAddCommGroup X] {m : ℕ} (mu : Measure X)
(E : X → EuclideanSpace ℝ (Fin m)) (Dec : EuclideanSpace ℝ (Fin m) → X) : ENNReal :=
(∫⁻ u, ‖Dec (E u) - u‖ₑ ^ 2 ∂mu) ^ ((1 : ℝ) / 2)
/-- Eq. (3.2): the approximation error
`(∫_{ℝ^m} ‖A(y) - P(G(D(y)))‖²_{ℓ^2} d(E_# mu)(y))^{1/2}`. -/
def approxErr {X F : Type*} [MeasurableSpace X] {m p : ℕ} (mu : Measure X) (G : X → F)
(E : X → EuclideanSpace ℝ (Fin m)) (Apx : EuclideanSpace ℝ (Fin m) → EuclideanSpace ℝ (Fin p))
(Dec : EuclideanSpace ℝ (Fin m) → X) (Proj : F → EuclideanSpace ℝ (Fin p)) : ENNReal :=
(∫⁻ y, ‖Apx y - Proj (G (Dec y))‖ₑ ^ 2 ∂(mu.map E)) ^ ((1 : ℝ) / 2)
/-- Eq. (3.3): the reconstruction error `(∫ ‖R(P(v)) - v‖² dnu(v))^{1/2}` of a reconstructor `R`
and a projector `P` with respect to a measure `nu` on the output space (in the paper
`nu = G_# mu`). -/
def recErr {F : Type*} [NormedAddCommGroup F] [MeasurableSpace F] {p : ℕ} (nu : Measure F)
(R : EuclideanSpace ℝ (Fin p) → F) (Proj : F → EuclideanSpace ℝ (Fin p)) : ENNReal :=
(∫⁻ v, ‖R (Proj v) - v‖ₑ ^ 2 ∂nu) ^ ((1 : ℝ) / 2)
/-- Section 3.2.1: the Lipschitz constant `Lip(F) = sup_{u,u'} ‖F(u) - F(u')‖ / ‖u - u'‖`, valued
in `[0, ∞]` (pairs with `u = u'` contribute `0`). -/
def lipConst {α β : Type*} [EMetricSpace α] [EMetricSpace β] (f : α → β) : ENNReal :=
⨆ x, ⨆ y, edist (f x) (f y) / edist x y
/-- Section 3.2.1 and the proof of Theorem 3.3 (Appendix C.2): the Hoelder constant
`Lip_alpha(G : A → L^2(U)) = sup_{u,u' ∈ A} ‖G(u) - G(u')‖ / ‖u - u'‖^alpha` of `G` on `A`,
valued in `[0, ∞]`. -/
def holderConst {α β : Type*} [EMetricSpace α] [EMetricSpace β] (alpha : ℝ) (f : α → β)
(A : Set α) : ENNReal :=
⨆ x ∈ A, ⨆ y ∈ A, edist (f x) (f y) / edist x y ^ alpha
section Hilbert
variable {H : Type*} [NormedAddCommGroup H] [InnerProductSpace ℝ H] [MeasurableSpace H]
/-- Definition 2.1 and Section 3.3: `nu ∈ P_2(H)`, a probability measure with finite second
moment `∫ ‖v‖² dnu(v) < ∞`. -/
def IsP2 (nu : Measure H) : Prop :=
IsProbabilityMeasure nu ∧ (∫⁻ v, ‖v‖ₑ ^ 2 ∂nu) < ⊤
/-- Section 3.2.2: the mean `E[v] = ∫ v dnu(v)`. -/
def mean (nu : Measure H) : H := ∫ v, v ∂nu
/-- Eq. (3.14): the uncentered covariance operator `w ↦ ∫ ⟨v, w⟩ v dnu(v)`, i.e.
`∫ v ⊗ v dnu(v)`. -/
def uncovOp (nu : Measure H) (w : H) : H := ∫ v, inner ℝ v w • v ∂nu
/-- Eqs. (3.8) and (3.18): the covariance operator
`w ↦ ∫ ⟨v - E[v], w⟩ (v - E[v]) dnu(v)`, i.e. `∫ (v - E[v]) ⊗ (v - E[v]) dnu(v)`. -/
def covOp (nu : Measure H) (w : H) : H :=
∫ v, inner ℝ (v - mean nu) w • (v - mean nu) ∂nu
/-- Proposition 3.15 (and the eigenvalue convention of Theorems 3.6, 3.8, 3.10, 3.12, 3.14):
`Gamma = sum_k lam_k (phi_k ⊗ phi_k)` with an orthonormal family `phi` and decreasing `lam`, i.e.
`Gamma w = sum_k lam_k ⟨phi_k, w⟩ phi_k` for every `w`. Then `lam_0 ≥ lam_1 ≥ ...` are the
eigenvalues of `Gamma` repeated according to multiplicity (the paper's `lam_1 ≥ lam_2 ≥ ...`,
indexed here from `0`). -/
def IsSpectralDecomp (Gamma : H → H) (lam : ℕ → ℝ) (phi : ℕ → H) : Prop :=
Orthonormal ℝ phi ∧ Antitone lam ∧
∀ w, HasSum (fun k => (lam k * inner ℝ (phi k) w) • phi k) (Gamma w)
/-- Theorems 3.10 and 3.12: the span of the eigenvectors of `Gamma` whose eigenvalue lies in
`S`; for `S = (c, ∞)` or `[c, ∞)` this is the direct sum of the corresponding eigenspaces
`E_j`. -/
def eigSpan (Gamma : H → H) (S : Set ℝ) : Submodule ℝ H :=
Submodule.span ℝ {v | ∃ c ∈ S, Gamma v = c • v}
/-- Eqs. (3.13) and (3.16): the projection error `∫ inf_{w ∈ S} ‖v - w‖² dnu(v)` of a set `S`
(a linear or affine subspace), valued in `[0, ∞]`. -/
def projErr (nu : Measure H) (S : Set H) : ENNReal :=
∫⁻ v, Metric.infEDist v S ^ 2 ∂nu
/-- Theorem 3.10: `V` is an optimal `p`-dimensional subspace, i.e. `dim V = p` and `V` minimizes
the projection error among all `p`-dimensional linear subspaces. -/
def IsOptimalSubspace (nu : Measure H) (p : ℕ) (V : Submodule ℝ H) : Prop :=
Module.finrank ℝ V = p ∧
∀ W : Submodule ℝ H, Module.finrank ℝ W = p → projErr nu (V : Set H) ≤ projErr nu (W : Set H)
/-- Theorem 3.12: `V0` is an optimal `p`-dimensional affine subspace, i.e. its direction has
dimension `p` and `V0` minimizes the projection error among all affine subspaces whose direction
has dimension `p`. -/
def IsOptimalAffine (nu : Measure H) (p : ℕ) (V0 : AffineSubspace ℝ H) : Prop :=
Module.finrank ℝ V0.direction = p ∧
∀ W : AffineSubspace ℝ H, Module.finrank ℝ W.direction = p → projErr nu (V0 : Set H) ≤ projErr nu (W : Set H)
/-- The Gram matrix `(⟨v_i, v_j⟩)_{i,j}` of a finite family. -/
def gram {p : ℕ} (v : Fin p → H) : Matrix (Fin p) (Fin p) ℝ := fun i j => inner ℝ (v i) (v j)
/-- Lemma 3.13: the dual basis `v*_k = sum_j (Gram^{-1})_{kj} v_j` of linearly independent
`v_1, ..., v_p`, the unique elements of their span with `⟨v_l, v*_k⟩ = delta_{lk}`. -/
def dualBasis {p : ℕ} (v : Fin p → H) (k : Fin p) : H :=
∑ j, (gram v)⁻¹ k j • v j
/-- Eq. (3.19): the projector associated with the reconstructor of `tau_0, ..., tau_p`,
`P(u) = (⟨u - tau_0, tau*_1⟩, ..., ⟨u - tau_0, tau*_p⟩)`. -/
def optProj {p : ℕ} (tau : Fin (p + 1) → H) (u : H) : EuclideanSpace ℝ (Fin p) :=
WithLp.toLp 2 (fun k => inner ℝ (u - tau 0) (dualBasis (fun j => tau j.succ) k))
/-- Eq. (3.21) and Proposition 3.15: the functions of the optimal reconstruction,
`hat tau_0 = E[v]` and `hat tau_k = phi_k` (the paper's `phi_k`, `k = 1, ..., p`, is `phi (k-1)`
here). -/
def optTrunk {p : ℕ} (nu : Measure H) (phi : ℕ → H) (k : Fin (p + 1)) : H :=
Fin.cases (motive := fun _ => H) (mean nu) (fun j : Fin p => phi j) k
end Hilbert
end DeepONetError
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
relu. This is the function given by
ReLUNet. For natural numbers and , an object of this type consists of the following data:
- a natural number with ;
- a width function with and ;
- for every , a real matrix and a bias vector .
No widths are required to be positive, so any width, including or , may be . The widths for , and the matrices and biases with index , are arbitrary. They are part of the data but play no role in the evaluation defined below.
ReLUNet.hidden. For a network as above and an input , the vectors are defined recursively for all :
where is applied componentwise. Here uses the identification .
ReLUNet.eval. The function realized by is
using the identification . This is a composition of affine maps, for . The activation is applied after each of the first affine maps but not after the last one.
L2. For , denotes the real Hilbert space of (almost-everywhere equivalence classes of) square-integrable real functions on , with respect to Lebesgue measure restricted to . is not assumed measurable here. For a non-measurable , the restricted measure is the restriction to a measurable hull of .
toL2. For and any function , the element is defined by cases:
- if is almost-everywhere strongly measurable and (with respect to the restricted Lebesgue measure), then is the equivalence class of ;
- otherwise .
trunkRec. Let be any real vector space, , and . For (Euclidean space), define
deepONet. The ingredients are:
- natural numbers ;
- a set and a set , with no boundedness, measurability or non-emptiness assumed;
- sensor points ;
- a ReLU network (as in
ReLUNet); - a ReLU network with components ;
- a function that is continuous on (with the subspace topology).
The output is the element of
Here is the map from toL2, so any component that is not square-integrable on contributes the zero element. The input ranges over continuous functions on , not over an abstract space .
IsDeepONetData. The setting is:
- sets and ;
- a real normed space (not assumed complete) carrying an arbitrary -algebra;
- an arbitrary -algebra on ; neither -algebra is required to be the Borel one;
- a continuous linear map , a measure on , a set and a map .
The predicate holds exactly when all of the following hold:
- is bounded and Lebesgue measurable.
- is bounded and Lebesgue measurable.
- is injective.
- is a probability measure.
- , as an extended-valued lower integral.
- is measurable and .
- For every , there is a function , continuous on , such that the chosen representative of equals almost everywhere on .
- is measurable with respect to the given -algebras.
- .
totalErr. Let be a measurable space, a normed abelian group, a measure on , and arbitrary maps. Define
where the integral is the extended-valued lower integral, so no measurability is required.
encErr. Let be a measurable space that is also a normed abelian group, a measure on , and and arbitrary maps. Define
approxErr. The data are a measurable space , a set , a measure on , and arbitrary maps
- ,
- ,
- ,
- ,
- .
Define
Here is the push-forward of to with its Borel -algebra. If is not almost-everywhere measurable, the push-forward is by convention the zero measure, and then this quantity equals .
recErr. Let be a normed abelian group with a measure on an arbitrary -algebra, and let and be arbitrary maps. Define
lipConst. For a map between extended metric spaces, define
computed in with the following conventions:
- , so pairs with contribute ;
- , so pairs at infinite distance contribute ;
- the supremum over an empty space is .
holderConst. For an arbitrary real number (no restriction to ), a map between extended metric spaces, and a subset of the domain, define
This is computed in , where equals , or according as , or . As a result, pairs with always contribute . The supremum is if .
In the remaining declarations, is a real inner product space, not assumed complete, carrying an arbitrary -algebra (not necessarily Borel).
IsP2. For a measure on , means:
- is a probability measure, and
- , as a lower integral.
mean. For a measure on , is the Bochner integral. By convention it equals in either of these cases:
- the identity map is not Bochner-integrable with respect to ;
- is not complete.
uncovOp. For a measure on and , define
as a Bochner integral. It equals when the integrand is not integrable or is not complete.
covOp. For a measure on , let (as in mean, including its zero conventions). For , define
as a Bochner integral, with the same zero conventions.
IsSpectralDecomp. For an arbitrary (not necessarily linear) map , a real sequence , and a sequence in , the predicate states all of the following:
- is an orthonormal family indexed by all of . This is impossible when is finite-dimensional, so the predicate is never satisfied there.
- .
- For every , the series is unconditionally summable with sum .
Nothing requires the following:
- the to be nonnegative or to tend to ;
- the family to be complete.
eigSpan. For and a set , this is the linear span (algebraic, not closed) of
Zero vectors are allowed in this set. If the span is .
projErr. For a measure on and any set , define
This is a lower integral. The infimum over is .
IsOptimalSubspace. For a measure on , , and a linear subspace , the predicate states:
- , and
- for every linear subspace with .
Here is defined to be for infinite-dimensional subspaces. Consequently, for both and the competitors may be either or infinite-dimensional.
IsOptimalAffine. For a measure on , , and an affine subspace , the predicate states:
- the direction space of has equal to , and
- for every affine subspace whose direction has equal to .
The empty affine subspace has direction , hence . Infinite-dimensional directions also have . So for the candidates and competitors include the empty set, single points, and affine subspaces with infinite-dimensional direction.
gram. For , this is the real matrix
dualBasis. For (not assumed linearly independent), define
Here is the matrix inverse when , and the zero matrix otherwise. In particular, for all whenever the are linearly dependent.
optProj. For and , define
where is the dualBasis of . Thus if are linearly dependent.
optTrunk. For a measure on and a sequence in , this is the family given by
The mean is taken as in mean, including its zero conventions.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.