          Universal Approximation of Mean-Field Models via Transformers


                             Shiba Biswal * 1 Karthik Elamvazhuthi * 1 Rishi Sonthalia * 2


                        Abstract                                     which are independent of the particles’ identities and are
     This paper investigates the use of transformers to              permutation-equivariant. Examples include biological enti-
     approximate the mean-field dynamics of interact-                ties (Lopez et al., 2012), robots (Elamvazhuthi & Berman,
     ing particle systems exhibiting collective behavior.            2019), traffic flow (Piccoli et al., 2009; Siri et al., 2021), and
     Such systems are fundamental in modeling phe-                   parameters in two-layer neural networks (Mei et al., 2019).
     nomena across physics, biology, and engineering,                A common approach to simplifying the analysis of such
     including opinion formation, biological networks,               systems is to consider the continuum limit as the number
     and swarm robotics. The key characteristic of                   of particles n → ∞, resulting in mean-field models rooted
     these systems is that the particles are indistin-               in statistical physics. Instead of specifying the dynamics
     guishable, leading to permutation-equivariant dy-               of each agent, the particles are modeled using probability
     namics. First, we empirically demonstrate that                  measures. This paper learns the mean-field dynamics of
     transformers are well-suited for approximating                  particles via particle trajectories using transformers.
     a variety of mean field models, including the                   Let Ω ⊂ Rd . Consider a vector field F : Ω × P(Ω) → Rd .
     Cucker-Smale model for flocking and milling,                    The following equation describes the general mean-field
     and the mean-field system for training two-layer                behavior of interacting particles evolving on Ω,
     neural networks. We validate our numerical ex-
     periments via mathematical theory. Specifically,                              dz
                                                                                      = F(z, µ), z(0) = z0 ∼ µ0 ,
     we prove that if a finite-dimensional transformer                             dt
     effectively approximates the finite-dimensional
                                                                     where z ∈ Ω denotes the state of a particle, µ ∈ P(Ω) is
     vector field governing the particle system, then
                                                                     the distribution of the particles at time t, and µ0 is the initial
     the L2 distance between the expected transformer
                                                                     distribution. The above equation can be approximated by
     and the infinite-dimensional mean-field vector
                                                                     a finite particle-level model. Let z = (z1 , . . . , zn ) ∈ Ωn
     field can be uniformly bounded by a function of
                                                                     represent the state of n particles, and consider the following,
     the number of particles observed during training.
     Leveraging this result, we establish theoretical                                        żi = F(zi , νzn ),
     bounds on the distance between the true mean-
     field dynamics and those obtained using the trans-              where the empiricalP  distribution of the n-particle system
                                                                                             n
     former.                                                         given by νzn := n1 i=1 δzi ∈ Dn (Ω). Several works
                                                                     learn the mean-field dynamics by learning the particle-level
                                                                     dynamics. Pham & Warin (2023) constructed a neural net-
1   Introduction                                                     work via binning as approximations of the vector field and
                                                                     proved approximation capabilities when restricted to dy-
The identification of dynamical system models for physical
                                                                     namics on measures that admit densities. The works of
processes is a fundamental application of machine learning
                                                                     Feng et al. (2024); Lu et al. (2019); Miller et al. (2023)
(ML). Of particular interest are systems of particles exhibit-
                                                                     present a kernel-based method for identifying the dynam-
ing collective behaviors—such as swarming, flocking, opin-
                                                                     ics of interacting particle systems. Whereas, Messenger
ion dynamics, and consensus. These systems involve a large
                                                                     & Bortz (2022) presents a form of the SINDy algorithm
number of particles or agents that follow identical dynamics,
                                                                     for identifying mean-field dynamics of interacting particle
   *
     Equal contribution 1 Theoretical Division, Los Alamos           systems. Kratsios et al. (2022) constructed a probabilis-
National Laboratory, Los Alamos, NM, USA 2 Boston Col-               tic transformer that can satisfy additional constraints via
lege, Boston, MA, USA. Correspondence to: Shiba Biswal               maps on measures. Additionally, Furuya et al. (2025) use a
<sbiswal@lanl.gov>, Rishi Sonthalia <sonthal@bc.edu>.                continuous version of transformers and attention (Vuckovic
Proceedings of the 42 nd International Conference on Machine         et al., 2020; Geshkovski et al., 2023) and provide universal
Learning, Vancouver, Canada. PMLR 267, 2025. Copyright 2025          approximation of measure theoretic maps. Similarly (Adu &
by the author(s).                                                    Gharesifard, 2024) have proven an approximation result for

                                                                 1
                              Universal Approximation of Mean-Field Models via Transformers

measure theoeretic maps arising from solutions of continuity        noted by ν ⊗n := ν × · · · × ν . The support of a measure
                                                                                     | {z }
equations on the sphere using a different architecture.                                     n times
                                                                    ν ∈ P(Rd ), denoted by supp(ν), is the smallest closed
This paper explores the use of transformers to approximate
                                                                    set S ⊂ Rd such that µ(Rd \ S) = 0. Given a mea-
the dynamical systems that govern the collective behavior of
                                                                    surable map X : Rd → Rd and a measure µ ∈ P(Rd ),
interacting particles with permutation-equivariant dynamics.
                                                                    the pushforward measure X# µ ∈ P(Rd ) is defined by
We compare the transformer against models from Pham &
                                                                    X# µ(A) := µ X −1 (A) for every Borel measurable set
Warin (2023) and Messenger & Bortz (2022) as well as
                                                                    A ⊂ Rd .
two additional permutation equivariant baselines models on
synthetic and real data from the Cucker-Smale model for             For a vector y ∈ Rd , the i-th component is denoted by yi .
swarming and milling. We prove theoretical guarantees for           The p-norm, p ∈ [1, ∞), and ∞-norm of y are, respectively,
                                                                             Pd
the transformer by lifting it from a sequence-to-sequence           ∥y∥pp = i=1 |yi |p , ∥y∥∞ = maxi |yi |. Boldface letters,
map to a map on measures, by taking the expectation of              such as z, denote elements in Rd×n , representing collection
a finite-dimensional transformer with respect to a product          of n vectors in Rd . The i, j-th element is denoted by zi,j .
measure. We refer to this as the expected transformer.              The corresponding p-norm (p ∈ [1, ∞)) and ∞-norm of
                                                                                                   Pd Pn
The main contributions are as follows:                              z are, respectively, ∥z∥pp =     i=1
                                                                                                                     p
                                                                                                           j=1 |zij | , ∥z∥∞ =
                                                                    maxi,j |zij |.
1. We empirically show that transformers approximate the
   vector field better compared to other network architec-          A function f : Rd×n → Rd×n is permutation equiv-
   tures (see Table 1).                                             ariant if for any permutation σ ∈ Sn , where Sn is
2. We define a continuum version of the transformer as an           the symmetric group on n elements, and for any x =
   expectation of finite-dimensional transformers (see (9)).        (x1 , . . . , xn ) ∈ Rd×n , we have f (xσ(1) , . . . , xσ(n) ) =
3. We establish approximation rate bounds of measure-                fσ(1) (x), . . . , fσ(n) (x) , where fi (x) denotes the i-th
   valued maps by this expected transformer (see Theo-              component of the output. We denote by C k (Rd ) the space
   rem 4.7).                                                        of k-times continuously differentiable functions on Rd . The
4. We mathematically show that the solution to the mean-            space of continuous functions with compact support is de-
   field model can be approximated by approximating the             noted by Cc (Rd ).
   vector field by the expected transformer (see Theo-
   rem 4.14 and Figure 2).
                                                                    2    Problem Formulation
The rest of the paper is organized as follows. Section 1.1
                                                                    We briefly introduced the problem in Section 1, we restate
defines the notation used in the paper. Section 2 defines the
                                                                    the equations again for clarity. Let Ω ⊂ Rd . Consider a
problem, Section 3 presents our numerical experiments, and
                                                                    vector field F : Ω × P(Ω) → Rd . The general mean-field
Section 4 presents our theoretical results.
                                                                    behavior of interacting particles evolving on Ω is given by
1.1   Notation                                                                    dz
                                                                                     = F(z, µ), z(0) = z0 ∼ µ0 ,                 (1)
Let Rd denote the d-dimensional Euclidean space, and let                          dt
Z+ denote the set of positive integers. The diameter of a           where z ∈ Ω denotes the state of each particle, µ ∈ P(Ω)
subset A ⊂ Rd is defined as diam(A) := sup{∥x − y∥ :                denotes the distribution of the particles, and z0 is the initial
x, y ∈ A} and let Br (z) be the closed ball of radius r > 0         state that is distributed according µ0 , the initial distribu-
centered at z ∈ Rd .                                                tion. The inter-particle interactions are modeled through µ;
We denote by P(Rd ) the set of all Borel probability mea-           specifically, the dynamics of each particle are influenced by
sures on Rd . Similarly, for a subset Ω ⊂ Rd , P(Ω) de-             the distribution of all other particles.
notes the set of Borel probability measures on Ω. The               Corresponding to (1), the continuity equation describes the
subset of probability measures ν with finite p-th moments,          evolution of the distribution µ:
                     p
                           1/p
                                is denoted by Pp (Rd ) :=
            R
 p (ν) := d Rd ∥x∥ dν(x)
M
                                                                              ∂µ
 ν ∈ P(R ) : Mp (ν) < ∞ . Let Pc (Rd ) be the set of                             + ∇z · (F(z, µ)µ) = 0, µ(0) = µ0 .              (2)
probability measures with compact support. The set of                         ∂t
empirical measures formed by finite sums of n Dirac-                For a finite final time τ > 0, µF : [0, τ ] → P(Ω) denotes
delta measures
         Pn     is denoted by Dn (Rd ) := {ν ∈ P(Rd ) :             the solution of the continuity equation (2) over the time
       1
ν = n i=1 δxi , xi ∈ Rd }. Similarly, the set Dn (Ω)                interval [0, τ ].
denotes the set of empirical measures on Ω ⊂ Rd . For
ν ∈ P(Rd ), the n-fold product measure on (Rd )n is de-             In this paper, we propose to use transformers to approximate
                                                                    the map in (1) and the system (2). However, transformers

                                                                2
                                Universal Approximation of Mean-Field Models via Transformers

are defined on sequences of vectors in Rd , whereas the map          Next, we consider the n-dimensional (finite) approximation
F is defined on Ω × P(Ω), an infinite-dimensional space.             of the model above.
Therefore, we consider a finite-dimensional approximation                        dxi
of (1) via a particle-level system. Specifically, consider                           = vi , 0 ≤ i ≤ n
                                                                                  dt
a n-particle system where the state of each particle i ∈                                    n                                    (6)
{1, . . . , n} is given by zi ∈ Ω. We assume that each zi is                     dvi   1X
                                                                                     =        ϕ(∥xi − xj ∥)(vj − vi )
independently sampled from the distribution µ ∈ P(Ω). Let                        dt    n j=1
z = (z1 , . . . , zn ) ∈ Ωn denote the collection of particle
states. The empirical distribution of the n-particle system is       To generate the data, we compute trajectories for 500 ran-
then given by                                                        dom initial conditions (xi (0), vi (0)), chosen uniformly at
                                                                     random from Ω = [0, 1] × [0, 1]. For each initial condition,
                            n
                         1X                                          we solve the differential equation (6) for n = 20 agents over
                νzn :=        δz ∈ Dn (Ω).                (3)        a time horizon [0, 100] using SciPy’s solve ivp. Hence,
                         n i=1 i
                                                                     for each initial condition, we obtain position xi (t) and ve-
The particle-level dynamics on Rd according to the map               locity vi (t), for each time step t.
defined in (1), for a single particle i, can be written as           Fish Milling Model The second example that we consider
                                                                     is the fish milling model (D’Orsogna et al., 2006) which
                żi = F(zi , νzn ), zi (0) ∼ µ0           (4)
                                                                     is a version of the Cucker-Smale model. In contrast to the
Note that the collection of random variables (zi ) is permu-         previous example, this example uses an actual experimental
tation equivariant because the joint distribution of (zi ) is        dataset (Katz et al., 2021). The dataset includes the position
invariant under any permutation of the indices.                      x ∈ R2 , velocity v ∈ R2 , and fish identifier for n = 300
                                                                     fish over 5000 time steps. While the experiment tracks 300
                                                                     fish, real-world data is incomplete at certain time points.
3     Numerical Simulations                                          Specifically, the positions of all fish are not available at all
In this section, we present experiments demonstrating that           times, and as the fish move in a milling pattern, some fish
transformers can learn mean-field dynamics and generalize            disappear and reappear. When a fish reappears, it is assigned
to cases involving more particles than those seen during             a new index, and its previous identity is lost. On average,
training. We also compare the transformer with Graph Neu-            only about 220 fish are observed at any given time. As a
ral Networks (GNNs) and prior models from Pham & Warin               result, we have 5000 data points, but the number of particles
(2023); Messenger & Bortz (2022).                                    varies at each time step.

                                                                     Data Next, we will describe how we assemble the data
3.1   Learning the Vector Field                                      from the Cucker-Smale and fish milling model for the ML
                                                                                                                  T         T
Our first goal focuses on learning the vector field F. To-           architectures. We note that F maps x v 7→ ẋ v̇ .
                                                                                               T                              T
wards this, in this experiment, we use two datasets: first, a
                                                                                                                   
                                                                     Let z(t) = x(t) v(t)             and ż(t) = ẋ(t) v̇(t) .
synthetic dataset generated from the Cucker-Smale model              From the simulations described above, we obtain the col-
(Cucker & Smale, 2007), and second, real data of fish                lection z(t) = (z1 (t), . . . , zn (t)), where each zi (t) =
milling (Katz et al., 2021).                                                      T
                                                                      xi (t) vi (t) . To construct ż(t), we use the centered-
Cucker-Smale Model The first example that we consider                difference formula:
is the well-studied 2d × n-dimensional Cucker-Smale (CS)                                      zi (t + ∆t) − zi (t − ∆t)
equation that models consensus of a n-agent system (Cucker                        żi (t) ≈                             ,
                                                                                                        2∆t
& Smale, 2007). For d = 2, he vector field F : R4 ×
                                                                     where ∆t is the time step. We assume that at each time step
P(R4 ) → R4 is given by
                                                                     t, we may know z(t) for only a subset of the particles;as
                 
                                  v
                                                                    is the case for the fish milling model. Moreover, we do
   F(x, v, µ) =      R                                ,              not know the functional form of F(z, νzn ). Let I(t) :=
                  − R4 ϕ(∥x − y∥)(v − u)dµ(y, u)
                                                        (5)          {i : zi (t), żi (t) are known}. and we redefine z(t) to mean
                      H
        ϕ(r) = 2            .                                        a collection of particle states for which we have data, and
                 (s + r2 )b                                          similarly for ż(t),
Where x ∈ R2 and v ∈ R2 denote the position and velocity              z(t) = {zi (t) : i ∈ I(t)} and ż(t) = {żi (t) : i ∈ I(t)}.
of each agent, respectively. Here, ϕ, a non-negative function,
is the interaction potential that determines the inter-agent         The data so obtained are split into an 80-20-20 split for
interaction, and H, s, b are parameters (set to 1 here).             training, validation, and testing.

                                                                 3
                                          Universal Approximation of Mean-Field Models via Transformers

                                   Model                                            Cucker-Smale                        Fish Milling
                                   Transformer                                      (1.9 ± 0.3) × 10−6                  (2.2 ± 0.0) × 10−2
                                   Cylindrical NN                                   (1.1 ± 0.4) × 10−3                  (2.7 ± 0.0) × 10−1
                                   GraphConv m = 3                                  (2.0 ± 0.1) × 10−4                  (1.2 ± 0.2) × 10−1
                                   GraphConv m = 20                                 (4.1 ± 3.6) × 10−3                  >1
                                   TransformerConv m = 3                            (2.8 ± 0.3) × 10−5                  (6.5 ± 1.7) × 10−2
                                   TransformerConv m = 20                           (3.3 ± 0.1) × 10−6                  (1.0 ± 0.3) × 10−1
                                   FNN                                              (6.7 ± 0.6) × 10−6                  N/A
                                   Kernel                                           (3.0 ± 0.1) × 10−5                  N/A

          Table 1. Table showing the mean-squared error in approximating the map Fn (14) for the different data and models.
                                                                                                                              1e 4

                                                                         0.14                                             5
                        0.14                     SGD                                               SGD
                                                                         0.12
                        0.12                     Transformer                                       Transformer            4
                                                                         0.10




                                                                                                                        T 2
                        0.10
               Train Loss




                                                                 Test Loss

                                                                         0.08
                                                                                                                          3
                        0.08




                                                                                                                        SGD
                        0.06                                             0.06                                             2
                        0.04                                             0.04
                                                                                                                          1
                        0.02                                             0.02
                        0.00                                             0.00                                             0
                               0   100   200   300   400   500                  0    100   200    300       400   500           0    100   200   300   400   500
                                         Iteration                                         Iteration                                       Iteration

Figure 1. Figure comparing training a two-layer neural network using gradient descent to update the weights and using a transformer
to update the weights. The solid line is the median value over 100 trials, while the shaded region is the interquartile range (25th-75th
percentile). Left: evolution of the training error during training. Center: evolution of the test error during training. Right: difference
between the parameters learned by gradient descent and the transformer.


Next, we will describe the ML architectures that we use                                          |ż(t)| is constant for all t (i.e., for the CS data), we evaluate
to approximate F. In particular, the transformer will be                                         a fully connected feedforward net and kernel regression
compared against four additional permutation equivariant                                         with sine, cosine, and polynomial basis. For both of these
baselines.                                                                                       methods, we concatenate the vectors in z(t) and ż(t) to
                                                                                                 create the training data.
Transformer In Appendix D, we define a transformer
network, T : Rn×d → Rn×k (k not necessarily equal to d)                                          Training Details and Hyperparameter Search For each
At each time step t, we provide the transformer with input                                       model and dataset, we conduct a hyperparameter search to
z(t) and have it predict ż(t). The model is trained using                                       optimize depth, width, and learning rate. We train the mod-
mean-squared error loss.                                                                         els using mini-batch Adam and a cosine annealing learning
                                                                                                 rate schedule. For kernel regression, we explore different
Graph Neural Network (GNN) Given a set of particles                                              numbers of basis functions for each type of basis function.
Z(t), let G3 (t) and G20 (t) represent the graphs based on                                       See Appendix E for more training details.1
the three nearest neighbors and the twenty nearest neighbors,
respectively. We use two graph neural networks (GNNs)                                            To account for the randomness introduced by initialization
that, at each time step t, take (Gm (t), z(t)) as input, where                                   and training, we conduct five trials for each hyperparameter
m is either 3 or 20, and aim to predict the node labels ż(t).                                   setting. The best hyperparameter configuration is selected
This forms a graph regression task where the target size                                         based on performance on the validation data.
depends on the number of nodes in the graph.
                                                                                                 Results Table 1 presents the mean-squared error for the
We employ two common GNN architectures: the Graph                                                different methods, across the two data sets. We observe that
Convolutional Network (GraphConv) (Morris et al., 2019)                                          the transformer has the lowest mean-squared error among
and TransformerConv (Shi et al., 2021). Note that, similar                                       all of the models. Therefore, we conclude (empirically) that
to GAT (Veličković et al., 2018), TransformerConv applies                                      transformers can effectively learn the vector field.
attention only to local neighborhoods.                                                                  1
                                                                                                    Code can be found at: https://github.com/
Other Baselines The final three baselines we consider are:                                       rsonthal/Mean-Field-Transformers
cylindrical nets from Pham & Warin (2023). Additionally, if

                                                                                            4
                                 Universal Approximation of Mean-Field Models via Transformers
                                                                                  1e 5                                                1e 5
3.2   Simulating Mean-Field Dynamics                                          8          x                                        8          y
                                                                                         u                                                   v
                                                                              6                                                   6
Our second goal is to approximate the solution to the con-




                                                                          Error




                                                                                                                              Error
tinuity equation (2). We use two synthetic datasets: the                      4                                                   4

Cucker-Smale dataset from Section 3.1, and the training                       2                                                   2
dynamics for a two-layer neural network (Mei et al., 2019).                   0                                                   0
                                                                                  0.0    0.5   1.0    1.5   2.0   2.5   3.0           0.0    0.5   1.0        1.5   2.0   2.5   3.0
                                                                                                     Time                                                    Time
Cucker-Smale After obtaining a transformer T that ap-
proximates the vector field F in (5) (as described in Sec-                 Figure 2. Figure comparing the true dynamics of the Cucker-
tion 3.1), we solve the differential equation (6) twice, using             Smaler model versus those obtained from a transformer. The
SciPy’s solve ivp function, for a new set of initial con-                  solid line is the median value over 100 trials, while the shaded
dition. First, with the true vector field F, and second, using             region is the interquartile range (25th-75th percentile).
the transformer T in lieu of F. We note that solving (2)                                                                                                 3


for Dirac valued initial distributions, δzi (0) , is equivalent to
solving (6) for initial condition zi (0).

Training 2-Layer Neural  Pn Network            Consider a two-layer




                                                                                                                                                         Time
                                       T
network f (x) =             i=1 ai σ(x   wi ).  Let θi = (ai , wi )
be the parameters (the states of this model) and Θ =
(θ1 , . . . , θn ). In this model, we treat each θi as a parti-                                                           Transformer Dynamics
                                                                                                                          True Dynamics
cle, with its distribution evolving according to the following                                                                                           0




continuity equation, as derived by Mei et al. (2019),
                                                                           Figure 3. Figure showing the trajectories of ten particles computed
                µ̇ = 2ξ(t)∇θ · (µ∇θ Ψ(θ, µ)).                  (7)         for the Cucker-Smale model using the true F versus the trans-
                                                                           former in lieu of F.
Here, ξ(t) depends on the learning rate schedule and

   Ψ(θ, µ) := −Ex,y yaσ(xT w)
                                  
                                                                           while the error in the velocity seems to plateau and even
                                                                           decrease slightly. Additionally, the initial interquartile range
               Z
            + Ex,y aâσ(xT w)σ(xT ŵ) dµ(â, ŵ).
                                          
                                                                           is small, but it grows over time.

Here, the vector field we wish to approximate is F(θ, µ) =                 Finally, we note that we trained the model with 20 particles
2ξ(t)∇θ Ψ(θ, µ). To generate the data, let X be a d times                  but simulated the model with 30 particles. Hence we see
1000 dimensional matrix with i.i.d. Gaussian entries. Let f                that the model can generalize to more particles than that
be a fixed two-layer teacher network with sigmoid activation               seen during training. This implies that the transformer is
(σ) and let Y = f (X). We then initialize 50 two-layer net-                able to learn the interaction kernel between the particles.
works with n hidden nodes, and train them for 500 epochs                   Next we simulate training a two-layer neural network us-
using the data X, Y . We use the MSE loss and gradient                     ing a Transformer. Figure 1 illustrates the training and test
descent to train the network. This gives us 2500 training                  loss as we train the network. The blue line represents the
data points (Θ, Θ̇). We set the hidden layer with n = 100                  model trained using GD, while the orange line corresponds
and use an input dimension of d = 10.                                      to the model trained with the transformer. Note that the
Once the transformer is trained, we test it as follows: We                 transformer model does not compute any gradients. We
reinitialize a two-layer network, providing its weights as                 trained models with one hundred different random initial-
input to the transformer. The transformer’s outputs are                    izations. The solid lines indicate the median and the shaded
then treated as gradients, which we use to perform gradient                regions represent the interquartile range. The right-most
descent. Finally, we compare the results with the dynamics                 plot in Figure 1 shows the Frobenius norm of the difference
obtained from performing true gradient descent.                            between the parameters learned using GD and the trans-
                                                                           former. The figure demonstrates that the maximum norm of
Results Figure 3 shows the evolution of 30 particles using                 the difference is at most 5 × 10−5 , even after 500 iterations.
the Cucker-Smale equations (6) along with the transformer                  Therefore, we observe that the transformer has learned to
approximation. Notably, the dynamics obtained using the                    approximate the dynamics of GD for a two-layer network.
transformer tracks the true dynamics near exactly. Figure 2
plots the 2-norm between the positions coordinates x, y and                4            Theoretical Results
velocities u, v. The figure indicates that the error is generally
quite small (< 10−4 ), although it increases over time. This               Our experiments demonstrate that the transformer can ap-
increase appears to be linear for the position coordinates,                proximate the finite-dimensional particle dynamics (4). In

                                                                      5
                                 Universal Approximation of Mean-Field Models via Transformers

this section, we will prove that the infinite-dimensional vec-            Definition 4.2 (1-Wasserstein Distance). Given two proba-
tor field F (1) can be approximated by the expected output                bility measures µ, ν ∈ Pp (Ω) on a metric space (Ω, ∥ · ∥2 ),
of a transformer, which we shall denote Tn (x, µ). Further,               where d is the metric on Ω, the 1-Wasserstein distance be-
we show that the solutions to the continuity equation (2) can             tween µ and ν is defined as
be approximated by the solutions to the following approxi-                                            Z
mate continuity equation, defined using Tn ,                                    W1 (µ, ν) = inf          ∥x − y∥2 dγ(x, y),      (12)
                                                                                             γ∈Π(µ,ν)
                                                                                                     Ω×Ω
          ∂µ
             + ∇z · (Tn (z, µ)µ) = 0, µ(0) = µ0 .              (8)
          ∂t                                                              where Π(µ, ν) denotes the set of all couplings (transport
                                                                          plans) γ on Ω × Ω with marginals µ and ν.
4.1    Lifting Transformers to the Space of Measures
                                                                          We now state the main assumptions required for our analysis:
Traditionally, transformers are defined on sequences of vec-              Assumption 4.3 (Regularity and Growth Conditions). As-
tors in Rd . However, the map we wish to approximate, F, is               sume that the vector field F : Ω × Pp (Ω) → Rd satisfies
defined on Ω × P(Ω). Therefore, we lift the standard trans-               the following conditions:
former to act on Ω×P(Ω) via an expectation operation. This                a) (Lipschitz Continuity) There exists a constant L such
will allow us to lift any transformer T : Ωn+1 → R(n+1)×d                    that for all x, y ∈ Ω and µ, ν ∈ Pp (Ω),
to a model Tn : Ω × P(Ω) → Rd .
Definition 4.1 (Expected Transformer). Given a transformer                      ∥F(x, µ) − F(y, ν)∥2 ≤ L (∥x − y∥2 + W1 (µ, ν)) .
T : Ωn+1 → R(n+1)×d and a prescribed sequence length n,                      This condition implies the following.
define the expected transformer Tn : Ω × P(Ω) → Rd by                     b) (Linear Growth) There exists a constant M > 0 such
                                                                             that for all x ∈ Ω and µ ∈ Pp (Ω),
Tn (x, µ) := Ez∼µ⊗n [(T ([x; z]))1 ] ,                         (9)
            Z                                                                         ∥F(x, µ)∥2 ≤ M (1 + ∥x∥2 + M1 (µ)) ,
          =     (T ([x; z1 , . . . , zn ]))1 dµ(z1 ) · · · dµ(zn ),                            R
               Ωn                                                               where M1 (µ) := Ω |y|dµ(y) is the first moment of µ.
where [x; z] denotes the concatenation of x and z =                       These assumptions are standard in the analysis of mean-field
(z1 , . . . , zn ) to form an input sequence of length n + 1,             models and differential equations in general and are needed
and (T ([x; z]))1 denotes the first output vector.                        to guarantee the uniqueness of solutions.
                                                                          Remark 4.4 (Lipschitz Implies Linear Growth). Since Ω
Some prior works (Geshkovski et al., 2023; Furuya et al.,
                                                                          is compact, Assumption 4.3a) implies Assumption 4.3b).
2025) have defined transformers T̂ : Ω × P(Ω) → Rd
                                                                          However, in some cases, we only need the weaker assump-
through a continuous version of self-attention Γ. For x ∈ Ω
                                                                          tion of Linear Growth, hence we explicitly state it.
and µ ∈ P(Ω), Γ is defined as
                                                                          Remark 4.5 (Example Models). These assumptions are sat-
                                                                          isfied by the Cucker-Smale model as well as the training
                            Z
                       1
    Γ(x, µ) := x +              Att ([x; y]) dµ(y), (10)                  2-layer neural network model. Specifically, Theorem 2 of
                   Z(x, µ) Ω
                                                                          Piccoli et al. (2009) shows that Cucker-Smale model satis-
where Z(x, µ) is a normalization factor and Att is the at-                fies Assumption 4.3a) and Lemma 2 of Mei et al. (2019)
tention layer (D.1). Then the transformer T̂ in Geshkovski                shows that the training 2-layer neural network model (7)
et al. (2023); Furuya et al. (2025) is defined as                         also satisfies Assumption 4.3a)
                                                                          To establish our approximation results for functions H :
      T̂ (x, µ) := FCξL ◦ΓθL ◦ · · · ◦ FCξ1 ◦Γθ1 (x),         (11)
                                                                          Ω × P(Ω) → Rd , we define the following norm.
where Γθj and FCξj are attention and feed-forward layers                  Definition 4.6. Given a function H : Ω × P(Ω) → Rd , we
with parameters θj and ξj , respectively.                                 define its norm by
                                                                                        ∥H∥∗ := sup sup ∥H(x, µ)∥2 .              (13)
4.2    Assumptions                                                                               x∈Ω µ∈P(Ω)

To state our result, we require some assumptions on the
map F in (1). The key assumption we make is that F                        4.3    Approximating the Vector Field F
is Lipschitz continuous with respect to µ, the probability                To approximate the vector field F, we define, for a fixed n,
measure. To formalize this, we require a metric on the space              the finite-dimensional map Fn : Ωn → Rd×n as
of probability measures P(Ω). A commonly used metric is
                                                                                    Fn (z) := F(z1 , νzn ) . . . F(zn , νzn )
                                                                                                                            
the p-Wasserstein distance.                                                                                                       (14)

                                                                      6
                              Universal Approximation of Mean-Field Models via Transformers

We now state our main result regarding the universal approx-        with the weak* topology. Then for all ε > 0, there exist l
imation of the mean-field vector field F by the expected            and parameters (θj , ξj )lj=1 such that
transformer Tn .
Theorem 4.7 (Universal Approximation). Let Ω ⊂ Rd be                   ∥T̂ (x, µ) − F ∗ (x, µ)∥2 ≤ ε, ∀(x, µ) ∈ Ω × P(Rd )
a compact set containing 0. Let F : Ω × P(Ω) → Rd
satisfy Assumption 4.3a) for a given p. Given a transformer         where the parameters θj , ξj depend on the dimension d.
T : Ωn+1 → R(n+1)×d let
                                                                    To compare with Theorem 4.9, we state the following corol-
                                                                    lary to Theorem 4.7.
             E := sup ∥T (z) − Fn+1 (z)∥2               (15)
                  z∈Ωn+1                                            Corollary 4.10. Let ε > 0 and n ≥ 1. Let Ω ⊂ Rd be
                                                                    a compact set containing 0. Let F : Ω × P(Ω) → Rd
Then, for q > p there exists a constant C(p, q, d), depending       satisfy Assumption 4.3a) for a given p. Then there exists
only on p, q, and d, such that for all n ≥ 1, the correspond-       a transformer T with depth Θ(1), one attention layer with
ing continuum version Tn : Ω × P(Ω) → Rd (9) satisfies              width Θ(d) such that the expected transformer Tn satisfies
                                                                  (16) with E = ε.
                                          1
 ∥Tn − F ∥∗ ≤ E + L diam(Ω)      p
                                             + CG(n, p, q) ,
                                         n+1                        While Corollary 4.10 and Theorem 4.9 are about two differ-
             
              √1            p > d/2, q ̸= 2p                       ent models, they share notable similarities while exhibiting
              n
             
                                                                    key differences. Both results feature Θ(d) width for the
 G(n, p, q) = √1n log(n + 1) p = d/2, q ̸= 2p (16)
                                                                   attention layers, independent of ε and n, and neither pro-
                 1                          d
             √              p < d/2, q ̸= d−p
             
                d p
                  n                                                 vides bounds for the width of feedforward layers. However,
                                                                    there are key differences. Our work leverages prior results
Remark 4.8. In the Theorem 4.7 above, we observe that               to establish a bound on network depth, which Furuya et al.
the approximation of infinite-dimensional maps F by finite-         (2025) does not. Moreover, we note that providing a bound
dimensional transformers T depends on two key quantities.           on the width of the feedforward network, in our case, is
First, it depends on how well T approximates the finite-            straightforward, owing to recent developments that provide
dimensional map Fn . This corresponds to the E term in              bounds on both width and depth (Augustine, 2024).
the bound (15). Due to universal approximation results for
                                                                    Further, we provide detailed error rates that depend on the
transformers, this term can be made arbitrarily small. For
                                                                    length of the sequence n that the transformer is trained on.
example, see Theorem 4.3 in Alberti et al. (2023).
                                                                    However, we note that Furuya et al. (2025) impose weaker
Second, the approximation depends on the convergence rate           assumptions for the map F.
of W1 (µ, νzn ), which, by Theorem 1 of Fournier & Guillin
(2015) is tight and depends on n, p, q, and d. Furthermore,         4.4   Approximating the Mean Field Dynamics
we observe that the stronger the regularity of the map F
(i.e., larger the constant p), the better                           We build upon our previous approximation results to prove
                                      theapproximation.           that the solution µTn (t) to the approximate continuity equa-
The best rates are obtained for p = d2 + 1 . In which case
the rate becomes independent of the dimension d. Conse-             tion (8) approximates the solution µF (t) to continuity equa-
quently, if we use longer sequences, we obtain an improved          tion (2). To formalize this, we first introduce an appropriate
approximation of the vector field F.                                notion of a solution to the continuity equation.
                                                                    Definition 4.11. Let µ ∈ C([0, τ ]; Pp (Rd )) be a measure-
We empirically verify Theorem 4.7 using the transformer             valued function, is called a Lagrangian solution of the con-
trained for the Cucker-Smale model in Section 3.1. We test          tinuity (2) if there exists X : [0, τ ] × Rd → Rd , referred to
five (x, y) ∈ [0.1]2 values and vary (u, v) over an 11 × 11         as the flow map, satisfies
grid. Using Gibbs sampling, we approximate the expected
transformer and compute the error with the true vector field                                  Z t
(5). Figure 4 shows the heat maps, with a maximum error                      X(t, x) = x +          F(X(s, x), µ(s))ds,       (17)
                                                                                               0
of 0.025, while the bound from Theorem 4.7 is at least 0.05.
                                                                    for all x ∈ Rd and µ(t) = X(t, ·)# µ0 for all t ∈ [0, τ ].
Comparison with Result From (Furuya et al., 2025):
The concurrent work (Furuya et al., 2025) proves the fol-           Existence and Uniqueness of Solutions Under Assump-
lowing approximation result for continuous maps F by the            tion 4.3a), it is known that there is a unique Lagrangian
continuum version of the transformer T̂ (11).                       solution corresponding to (2), see Proposition 4.8 in Cav-
Theorem 4.9. Let Ω ⊂ Rd be a compact set and F ∗ :                  agnari et al. (2022). As stated in Remark 4.5 the numerical
Ω × P(Rd ) → Rd be continuous, where P(Rd ) is endowed              examples considered in Section 3 satisfy this assumption.

                                                                7
                                Universal Approximation of Mean-Field Models via Transformers




Figure 4. Figure shows the error ∥Tn − F∥∗ for the CS model. Here (x, y) is held fixed while (u, v) is varied in a 11 × 11 grid.


However, to ensure that solutions of (2) can be approxi-               where µF and µTn are the solutions to (2) and (8), respec-
mated, we also need existence of unique solutions for the              tively, and the estimate (19) is independent of µ0 .
approximating continuity equation (8). Unfortunately, the              Remark 4.15. In essence, Theorem 4.14 states that by choos-
expected transformer might not be globally Lipschitz as re-            ing a large enough ball of radius R that covers the support
quired in Assumption 4.3a) since T [x, z] in (9) might not be          of µ0 , and selecting and appropriate K, as a function of R,
globally Lipschitz (Kim et al., 2021). Hence, the continuity           final time τ , and regularity constant M , we can ensure that
equation (8) with the expected transformer as the vector               if Tn approximates F on the ball of radius K, then Tn can
field may not have a unique Lagrangian solution in general.            be used to simulate the dynamics (2) over the time interval
The goal of the following theorem is to show that if Assump-           [0, τ ]. We observe that the error bound (19) grows expo-
tion 4.3b) holds locally on a sufficiently large set, then we          nentially. Therefore, a small approximation error δ for the
can prove existence of a unique solution to (8), even if the           vector field F implies a small approximation error (19) for
expected transformer is only locally Lipschitz. This will be           the solution of the continuity equation over [0, τ ]. However,
used later to show the ability of the expected transformer to          the bound (19) also depends on the regularity of F, namely
approximate solutions of the continuity equation (2), using            p and L . Therefore, the more regular the vector-field F,
its universal approximation property.                                  i.e., larger p and smaller L , the better the bound (19).
Theorem 4.12. Suppose µ0 ∈ Pc (Ω) is such that the sup-                We can combine Theorem 4.7 and Theorem 4.14 to obtain
port of µ0 is contained within BR (0) ⊂ Ω, for some R > 0.             the following corollary.
Additionally, let K satisfy K > (R + 2M τ )e3M τ . Further-
more, assume that                                                      Corollary 4.16. Suppose F satisfies Assumption 4.3a) for
                                                                       some p. Let the support of µ0 be contained within BR (0) ⊂
         ∥Tn (x, µ)∥2 ≤ M (1 + ∥x∥2 + M1 (µ)) ,             (18)       Ω, for some R > 0, and let K ∈ R be such that K > (R +
for all x ∈ BK (0) and all µ with support in BK (0). Then,             2M τ )e3M τ . Given a transformer T : Ωn+1 → R(n+1)×d
there exists a unique Lagrangian solution to the approximate           let E := sup ∥T (z) − Fn+1 (z)∥2 . If E is small enough
                                                                                z∈Ωn+1
continuity equation (8) such that supp µT (t) ⊆ BCt (0) for            and n ∈ Z+ is large enough, then
all t ∈ [0, τ ], where Ct = (R + 2M t)e3M t .
Remark 4.13. In the proof of Theorem 4.14 it will be shown               W1 (µF (t), µTn (t)) < 2(E + δ(n, K))t exp(2p L t)
that the bound (18) on the expected transformer directly                                                             
                                                                                                        1
follows from Assumption 4.3a) on F wheneverTn is close                 where δ(n, K) = L (2K)      p
                                                                                                        q−p G(n, p, q) , where
enough to F in the uniform norm.                                                                       n q
                                                                       G(n, p, q) is as per (16).
We are now ready to state our main theorem regarding the
approximation of mean-field dynamics using transformers.               5   Related Works
Theorem 4.14 (Mean Field Dynamics Approximation Us-
ing Transformers). Let ε > 0 be small enough and n ≥ 1.                In this section, we provide a more in-depth comparison with
Suppose that F satisfies Assumption 4.3a) for some p. As-              similar recent prior work Furuya et al. (2025); Kratsios &
sume that the support of µ0 is contained within BR (0)                 Furuya (2025). We highlight key differences below.
⊂ Ω, for some R > 0, and let K ∈ R be such that
                                                                       Role of Measure and Approximation Target: In this
K > (R + 2M τ )e3M τ . If the transformer Tn satisfies
                                                                       work the measure µ is an input argument representing the
the condition
                                                                       state distribution in a mean-field system, directly influenc-
                ∥Tn (x, µ) − F(x, µ)∥∗ < ε.                            ing the vector field F(z, µ). Specifically, this paper targets
                                                                       the approximation of the vector field F(z, µ) governing
for all z ∈ BK (0) and µ ∈ P(BK (0)). Then we have that
                                                                       mean-field dynamics and the subsequent approximation of
            W1 (µF (t), µTn (t)) ≤ εt exp(2L t)             (19)       the dynamical system’s evolution (solution to the continuity

                                                                   8
                              Universal Approximation of Mean-Field Models via Transformers

equation). We require the target vector field F(z, µ) to be          sults and numerical simulations, we established that trans-
Lipschitz continuous w.r.t. spatial and measure arguments            formers can be powerful tools for modeling and learning the
(using Wasserstein distance). This directly models physical          collective behavior of particle systems.
or biological system interactions. A key strength is its ap-
plicability to general Borel probability measures P(Ω) on a          Acknowledgment
compact set Ω.
                                                                     Shiba Biswal acknowledges partial support from the Labo-
Furuya et al. (2025) also has µ as input and aim to approx-
                                                                     ratory Directed Research and Development program of Los
imate general continuous in-context mappings Λ∗ (µ, x).
                                                                     Alamos National Laboratory (LANL) under Projects No.
They require Λ∗ to be continuous w.r.t. weak-star topol-
                                                                     20230338ER and No. 20250638DI, and from the U.S. De-
ogy (plus Lipschitz conditions on contexts for masked case).
                                                                     partment of Energy/Office of Science Advanced Scientific
Their focus is broad representational power.
                                                                     Computing Research Program.
For Kratsios & Furuya (2025) the measure µ is an input
argument to the target function f (µ, x). However, it’s re-          Impact Statement
stricted to the specific class of Permutation-Invariant Con-
texts within a geometrically constrained domain, aimed at            This paper presents work whose goal is to advance the field
analyzing general in-context function approximation.                 of Machine Learning. There are many potential societal
                                                                     consequences of our work, none which we feel must be
Map Definition and Transformer: Our work defines the                 specifically highlighted here.
map µ 7→ Tn (Measure to Vector Field) using the Expected
Transformer, derived by taking the expectation of a standard,
finite-sequence transformer T . This provides a practical            References
link between standard architectures and measure-theoretic            Adu, D. O. and Gharesifard, B. Approximate controllability
inputs.                                                                of continuity equation of transformers. IEEE Control
Furuya et al. (2025) define transformers directly on the               Systems Letters, 2024.
space of probability measures using a measure-theoretic              Alberti, S., Dern, N., Thesing, L., and Kutyniok, G. Sum-
formulation with continuous attention layers. Kratsios &               former: Universal approximation for efficient transform-
Furuya (2025) defines the map from finite vector spaces                ers. In Topological, Algebraic and Geometric Learning
to finite vector spaces. However, the input and outputs are            Workshops 2023, pp. 72–86. PMLR, 2023.
interpreted as measures on discrete sets.
                                                                     Augustine, M. T. A survey on universal approximation
Guarantees: This work provides quantitative bounds on                  theorems. arXiv preprint arXiv:2407.12895, 2024.
the vector field approximation error that explicitly show
convergence as the number of particles increases, linking            Cavagnari, G., Lisini, S., Orrieri, C., and Savaré, G. La-
the error to the quality of the underlying finite transformer.         grangian, eulerian and kantorovich formulations of multi-
Furthermore, it connects this to the approximation of the              agent optimal control problems: Equivalence and gamma-
system’s dynamics via Gronwall’s lemma.                                convergence. Journal of Differential Equations, 322:
                                                                       268–364, 2022. ISSN 0022-0396.
Kratsios & Furuya (2025) provides quantitative probabilis-
tic bounds on the output approximation error, dependent on           Cucker, F. and Smale, S. Emergent behavior in flocks. IEEE
the target function’s modulus of continuity and the domain’s           Transactions on automatic control, 52(5):852–862, 2007.
geometry, focusing on the network size needed for a given
precision. While Furuya et al. (2025) show a single trans-           D’Orsogna, M. R., Chuang, Y. L., Bertozzi, A. L., and
former architecture (with fixed dimensions/heads) works                Chayes, L. S. Self-propelled particles with soft-core
uniformly for an arbitrary number of input tokens (even                interactions: Patterns, stability, and collapse. Phys. Rev.
infinite) for a given precision ε.                                     Lett., 96:104302, Mar 2006. doi: 10.1103/PhysRevLett.
                                                                       96.104302. URL https://link.aps.org/doi/
                                                                       10.1103/PhysRevLett.96.104302.
6   Conclusion
                                                                     Elamvazhuthi, K. and Berman, S. Mean-field models in
This paper demonstrated the efficacy of transformer archi-             swarm robotics: A survey. Bioinspiration & Biomimetics,
tectures in approximating the mean-field dynamics of inter-            15(1):015001, 2019.
acting particle systems. We showed that finite-dimensional
transformer models can be lifted to approximate the infinite-        Feng, J., Kulick, C., Ren, Y., and Tang, S. Learning parti-
dimensional mean-field dynamics. Through theoretical re-               cle swarming models from data with gaussian processes.
                                                                       Mathematics of Computation, 93(349):2391–2437, 2024.

                                                                 9
                              Universal Approximation of Mean-Field Models via Transformers

Fournier, N. and Guillin, A. On the rate of convergence in             go neural: Higher-order graph neural networks. In Pro-
  Wasserstein distance of the empirical measure. Probabil-             ceedings of the AAAI conference on artificial intelligence,
  ity theory and related fields, 162(3):707–738, 2015.                 volume 33, pp. 4602–4609, 2019.

Furuya, T., de Hoop, M. V., and Peyré, G. Transform-                Pham, H. and Warin, X. Mean-field neural networks: learn-
  ers are universal in-context learners. In The Thirteenth             ing mappings on Wasserstein space. Neural Networks,
  International Conference on Learning Representations,                168:380–393, 2023.
  2025. URL https://openreview.net/forum?
  id=6S4WQD1LZR.                                                     Piccoli, B., Tosin, A., et al. Vehicular traffic: A review
                                                                       of continuum mathematical models. Encyclopedia of
Geshkovski, B., Letrouit, C., Polyanskiy, Y., and Rigollet,            complexity and systems science, 22:9727–9749, 2009.
  P. A mathematical perspective on transformers. arXiv
  preprint arXiv:2312.10794, 2023.                                   Shi, Y., Huang, Z., Feng, S., Zhong, H., Wang, W., and
                                                                       Sun, Y. Masked label prediction: Unified message
Katz, Y., Kolbjørn, T., Christos C, I., Huepe, C., and                 passing model for semi-supervised classification. In
  Iain D, C. Fish schooling data subset: Oregon state                  Proceedings of the Thirtieth International Joint Con-
  university. https://ir.library.oregonstate.                          ference on Artificial Intelligence, IJCAI-21, pp. 1548–
  edu/concern/datasets/zk51vq07c, 2021.                                1554, 2021. URL https://doi.org/10.24963/
                                                                       ijcai.2021/214.
Kim, H., Papamakarios, G., and Mnih, A. The Lipschitz
  constant of self-attention. In International Conference on         Siri, S., Pasquale, C., Sacone, S., and Ferrara, A. Freeway
  Machine Learning, pp. 5562–5571. PMLR, 2021.                         traffic control: A survey. Automatica, 130:109655, 2021.

Kratsios, A. and Furuya, T. Is in-context universality               Veličković, P., Cucurull, G., Casanova, A., Romero, A.,
  enough? MLPs are also universal in-context. arXiv                    Lio, P., and Bengio, Y. Graph attention networks. In
  preprint arXiv:2502.03327, 2025.                                     International Conference on Learning Representations,
                                                                       2018. URL https://openreview.net/forum?
Kratsios, A., Zamanlooy, B., Liu, T., and Dokmanic, I.                 id=rJXMpikCZ.
  Universal approximation under constraints is possible
  with transformers. In The Tenth International Conference           Vuckovic, J., Baratin, A., and des Combes, R. T. A math-
  on Learning Representations, 2022. URL https://                      ematical theory of attention. ArXiv, abs/2007.02876,
  openreview.net/forum?id=JGO8CvG5S9.                                  2020. URL https://api.semanticscholar.
                                                                       org/CorpusID:220363994.
Lopez, U., Gautrais, J., Couzin, I. D., and Theraulaz, G.
  From behavioural analyses to models of collective motion
  in fish schools. Interface focus, 2(6):693–707, 2012.

Lu, F., Zhong, M., Tang, S., and Maggioni, M. Nonparamet-
  ric inference of interaction laws in systems of agents from
  trajectory data. Proceedings of the National Academy of
  Sciences, 116(29):14424–14433, 2019.

Mei, S., Misiakiewicz, T., and Montanari, A. Mean-field
 theory of two-layers neural networks: dimension-free
 bounds and kernel limit. In Conference on learning the-
 ory, pp. 2388–2464. PMLR, 2019.

Messenger, D. A. and Bortz, D. M. Learning mean-field
 equations from particle data using WSINDy. Physica D:
 Nonlinear Phenomena, 439:133406, 2022.

Miller, J., Tang, S., Zhong, M., and Maggioni, M. Learning
 theory for inferring interaction kernels in second-order
 interacting agent systems. Sampling Theory, Signal Pro-
 cessing, and Data Analysis, 21(1):21, 2023.

Morris, C., Ritzert, M., Fey, M., Hamilton, W. L., Lenssen,
 J. E., Rattan, G., and Grohe, M. Weisfeiler and leman

                                                                10
                               Universal Approximation of Mean-Field Models via Transformers

A    Proof of Theorem 4.7
Theorem 4.7 (Universal Approximation). Let Ω ⊂ Rd be a compact set containing 0. Let F : Ω × P(Ω) → Rd satisfy
Assumption 4.3a) for a given p. Given a transformer T : Ωn+1 → R(n+1)×d let

                                                E := sup ∥T (z) − Fn+1 (z)∥2                                            (15)
                                                         z∈Ωn+1


Then, for q > p there exists a constant C(p, q, d), depending only on p, q, and d, such that for all n ≥ 1, the corresponding
continuum version Tn : Ω × P(Ω) → Rd (9) satisfies
                                                                                   
                                                                   1
                                    ∥Tn − F ∥∗ ≤ E + L diam(Ω)p        + CG(n, p, q) ,
                                                                  n+1
                                                
                                                   1
                                                
                                                 √
                                                 n             p > d/2, q ̸= 2p
                                                   1
                                    G(n, p, q) = √n log(n + 1) p = d/2, q ̸= 2p                                         (16)
                                                   1
                                                √              p < d/2, q ̸= d
                                                
                                                  d                               d−p
                                                         np



Proof.
                                                  Z
                  ∥F − Tn ∥∗ = sup sup F(x, µ) −      (T (x, z))1 dµ⊗n (z)
                                µ   x              Ωn
                                                                             2
                                       Z
                                                                        ⊗n
                             ≤ sup sup     ∥F(x, µ) − (T (x, z))1 ∥2 dµ (z)
                                µ   x   Ωn
                                       Z
                             = sup sup     ∥F(x, µ) − F(x, νzn ) + F(x, νzn ) − (T (x, z))1 ∥2 dµ⊗n (z)
                                µ   x   Ωn
                                       Z
                             ≤ sup sup     ∥F(x, µ) − F(x, νzn )∥2 dµ⊗n (z)
                                µ   x   Ωn
                                          Z
                                + sup sup    ∥F(x, νzn ) − (T (x, z))1 ∥2 dµ⊗n (z)        (∗)
                                        µ   x       Ωn

The second inequality follows from the standard triangle inequality. The first integral on the RHS can be bounded from
above as,
                                          Z
                                  sup sup     ∥F(x, µ) − F(x, νzn )∥2 dµ⊗n (z)
                                   µ   x   Ωn
                                          Z
                                    ≤ sup     sup ∥F(x, µ) − F(x, νzn )∥2 dµ⊗n (z)
                                       µ   Ωn  x
                                          Z
                                    = sup     L ∥µ − νzn ∥Wp dµ⊗n (z)
                                            µ       Ωn
                                        =L sup Ez∼µ⊗n [Wp (µ, νzn )]         (∗∗)
                                                µ


Recall that Mq (µ) denotes the q-moment of µ i.e. Mq (µ) := Ω |x|q dµ(x), then as per Theorem 1 of (Fournier & Guillin,
                                                                R

2015) there exists a constant C(p, q, d) (a function of p, q, d) such that, Ez∼µ⊗n [Wp (µ, νzn )] from (∗∗) can be bounded
                    p/q
from above by CMq (µ)G(n, p, q). We obtain:
                                Z
                      sup sup         ∥F(x, µ) − F(x, νzn )∥2 dµ⊗n (z) ≤ L sup CMqp/q (µ)G(n, p, q)
                       µ   x    Ωn                                            µ

                                                                       ≤ L C diam(Ω)p G(n, p, q),                       (20)

where we have used the fact that µ is a probability measure on Ω.

                                                                  11
                                   Universal Approximation of Mean-Field Models via Transformers

Next, we obtain an upper bound for the second integral in (∗).
                           Z
                   sup sup     ∥F(x, νzn ) − (T (x, z))1 ∥2 dµ⊗n (z)
                     µ   x  Ωn
                           Z
                     ≤ sup     sup ∥F(x, νzn ) − (Fn+1 (x, z1 , . . . , zn ))1 ∥2 dµ⊗n (z)
                         µ  Ωn   x
                           Z
                     + sup     sup ∥(Fn+1 (x, z1 , . . . , zn ))1 − (T (x, z))1 ∥2 dµ⊗n (z)           (∗ ∗ ∗)
                           µ       Ωn   x

Consider the first term in the expression above
                                    Z
                                sup     sup ∥F(x, νzn ) − (Fn+1 (x, z1 , . . . , zn ))1 ∥2 dµ⊗n (z)
                                 µ   Ωn x
                                           Z                                     
                                    = sup      sup F(x, νzn ) − F x, ν(x,z) n+1
                                                                                        dµ⊗n (z)
                                        µ   Ωn x                                      2
                                           Z                    
                                    ≤ sup      L W1 νzn , ν(x,z)
                                                            n+1
                                                                   dµ⊗n (z)
                                        µ   Ωn
                                           Z
                                                              1
                                    ≤ sup      L diam(Ω)p          dµ⊗n (z)
                                        µ   Ωn              n+1
                                                       1
                                    = L diam(Ω)p                                                                    (21)
                                                   n+1
Since we have assumed (15), the second term in (∗ ∗ ∗) evaluates to
                             Z
                        sup       sup ∥(Fn+1 (x, z1 , . . . , zn ))1 − (T (x, z))1 ∥2 dµ⊗n (z) = E.                 (22)
                               µ     Ωn     x


Putting together (20), (21), and (22), we get that
                                                                                          
                                                                                      1
                                    ∥Tn − F ∥∗ ≤ L diam(Ω)p CG(n, p, q) +                      + E.
                                                                                     n+1



B    Proof of Theorem 4.12
Proposition B.1. Suppose µ0 ∈ P(Ω) is such that the support of µ0 lies in BR (0), for some R > 0, and that there exists
a Lagrangian solution µF ∈ C([0, τ ]; Pp (Rd )) of (2). Additionally, suppose that F satisfies Assumption 4.3b). Then the
solution satisfies,
                                                 supp µF (t) ⊆ BCt (0)                                               (23)
for all t ∈ [0, τ ], where Ct = (R + 2M t)e3M t .

Proof. By definition of the Lagrangian solution (17),
                                                   Z t
                             ∥X(t, x)∥2 ≤ ∥x∥2 +        ∥F(X(s, x), µ(s))∥2 ds
                                                      0
                                                   Z t
                                        ≤ ∥x∥2 +        M (1 + ∥X(s, x)∥2 + M1 (µ(s))) ds                           (24)
                                                          0

Integrating both sides of (24) with respect to µ0 and noting that X(t, ·)# µ0 = µt , we get,
                                                                     Z t
                                          M1 (µ(t)) ≤ M1 (µ0 ) + M         (1 + 2M1 (µ(s))) ds
                                                                      0


                                                                12
                              Universal Approximation of Mean-Field Models via Transformers

Combining this with (24) itself we get,
                                                                      Z t
                     ∥X(t, x)∥2 + M1 (µ(t)) ≤ M1 (µ0 ) + M                  (2 + ∥X(s, x)∥2 + 3M1 (µ(s))) ds
                                                                       0

This implies
                                                                           Z t
                 ∥X(t, x)∥2 + M1 (µ(t)) ≤ (M1 (µ0 ) + 2M t) +                    3M (∥X(s, x)∥2 + M1 (µ(s))) ds.
                                                                            0
                                                                                                             Z t
We note that the above equation is in the integral form of Gronwall’s lemma (u(t) ≤ α(t) +                         β(s)u(s) ds) with α(t) =
                                                                                                              a
M1 (µ0 ) + 2M t and β(s) = 3M . Therefore, an application of Gronwall’s lemma gives us,

                                    ∥X(t, x)∥2 + M1 (µ(t)) ≤ (M1 (µ0 ) + 2M t))e3M t
                                                                 ≤ (R + 2M t)e3M t

Hence supp µF ⊆ BCt (0).
Theorem 4.12. Suppose µ0 ∈ Pc (Ω) is such that the support of µ0 is contained within BR (0) ⊂ Ω, for some R > 0.
Additionally, let K satisfy K > (R + 2M τ )e3M τ . Furthermore, assume that

                                          ∥Tn (x, µ)∥2 ≤ M (1 + ∥x∥2 + M1 (µ)) ,                                                       (18)

for all x ∈ BK (0) and all µ with support in BK (0). Then, there exists a unique Lagrangian solution to the approximate
continuity equation (8) such that supp µT (t) ⊆ BCt (0) for all t ∈ [0, τ ], where Ct = (R + 2M t)e3M t .

                                                                                                                               (n+1)
Proof. Let ϕ ∈ Cc∞ (R(n+1)d ) be a compactly support smooth function such that ϕ(x) = 1 for all x ∈ BR+Cτ (0). We
construct the function T̂ [x, z] = ϕ(x, z)T [x, z]. Let Tˆn be the expected transformer corresponding to T̂ . We check that Tˆn
satisfies Assumption 4.3a). Towards, this end we compute
                                                    Z                             Z
                       ˆ            ˆ
                     ∥Tn (x, µ) − Tn (y, ν)∥2 =                           ⊗n
                                                         (T̂ ([x; z]))1 dµ (z) −     (T̂ ([y; ẑ]))1 dν(ẑ)
                                                    Ωn                                      Ωn                         2

Let γ ∈ P(Rd × Rd ) be the optimal plan with marginals µ and ν that solves the optimization problem (12). Then
                                        Z                                  Z
             ∥T̂ (x, µ) − T̂ (y, ν)∥2 =         (T̂ ([x; z]))1 dγ(z, ẑ) −       (T̂ ([y; ẑ]))1 dγ(z, ẑ)
                                             Ωn ×Ωn                                      Ωn ×Ωn                            2
                                            Z
                                        =             (T̂ ([x; z]))1 − (T̂ ([y; ẑ]))1 dγ(z, ẑ)
                                             Ωn ×Ωn                                                    2

The function T̂ is globally Lipschitz for some Lipchitz constant L as it is a product of a compactly supported smooth
function ϕ and a locally Lipschitz function T . We can use this to conclude that
                                          Z                                X n Z
             ∥T̂ (x, µ) − T̂ (y, ν)∥2 ≤ L          ∥x − y∥2 dγ(z, ẑ) + L            ∥zi − ẑi ∥2 dγ(zi , ẑi )
                                             Ωn ×Ωn                                     i    Ωn ×Ωn
                                                           n Z
                                                           X
                                      = L∥x − y∥2 + L                           ∥zi − ẑi ∥2 dγ(zi , ẑi )
                                                             i    Ωn ×Ωn

                                      = L∥x − y∥2 + LnW1 (µ, ν)

Therefore, T̂ satisfies Assumption 4.3a). Then, Proposition 4.8 of Cavagnari et al. (2022) guarantees that there exists a
unique Lagrangian solution µT̂ (t) of (8) corresponding to T̂ . Moreover, we know from Proposition B.1 that the support
of the solution µT̂ (t) lies in BCt (0) for all t ∈ [0, τ ]. However, we note that T̂ (x, µ) = T (x, µ) for all x ∈ BCt (0) and
all µ ∈ P(Ω) with support in BCt (0). Therefore, µT̂ (t) = µT (t), the unique Lagrangian solution of (8) for the expected
transformer T .

                                                                 13
                               Universal Approximation of Mean-Field Models via Transformers

C    Proof of Theorem 4.14
Theorem 4.14 (Mean Field Dynamics Approximation Using Transformers). Let ε > 0 be small enough and n ≥ 1. Suppose
that F satisfies Assumption 4.3a) for some p. Assume that the support of µ0 is contained within BR (0) ⊂ Ω, for some
R > 0, and let K ∈ R be such that K > (R + 2M τ )e3M τ . If the transformer Tn satisfies the condition

                                                   ∥Tn (x, µ) − F(x, µ)∥∗ < ε.

for all z ∈ BK (0) and µ ∈ P(BK (0)). Then we have that

                                            W1 (µF (t), µTn (t)) ≤ εt exp(2L t)                                          (19)

where µF and µTn are the solutions to (2) and (8), respectively, and the estimate (19) is independent of µ0 .




Proof. Using the uniform norm approximation ∥Tn (x, µ) − F(x, µ)∥∗ < ε and Assumption 4.3b), we can conclude that

                                        ∥Tn (x, µ)∥2 ≤ (M + ε)(1 + ∥x∥2 + M1 (µ))

for all x ∈ Rd and µ ∈ P(BK (0)). Since K > (R + 2M τ )e3M τ , for ε > 0 small enough, and n large enough we can
conclude that, K > (R + 2(M + ε)τ )e3(M +ε)τ . From Theorem 4.12, there exists a Lagrangian solution for (8) such that

                                                       supp µTn (s) ⊆ BK (0)

for all s ∈ [0, τ ]. Due to Assumption 4.3 and Proposition 4.8 of (Cavagnari et al., 2022), there exists a unique Lagrangian
solution for (2). Once again, using Proposition B.1 we can conclude that

                                                       supp µF (s) ⊆ BK (0)

for all s ∈ [0, τ ]. Let X, Y be the flow maps associated with with respect the vector fields F and Tn , respectively.
From the definition of Lagrangian solutions we know that,

                                                             Z t
                                         X(t, x) = x +             F(X(s, x), µF (s))ds
                                                              0
                                                             Z t
                                         Y (t, x) = x +            Tn (Y (s, x), µTn (s))ds
                                                              0


for all t ∈ [0, τ ]. From this we get

                                               Z t                                    Z t
                    ∥Y (t, x) − X(t, x)∥2 =            Tn (Y (s, x), µTn (s))ds −           F(X(s, x), µF (s))ds
                                                   0                                   0                            2
                                               Z t                                    Z t
                                          ≤            Tn (Y (s, x), µTn (s))ds −           F(Y (s, x), µF (s))ds
                                                 0                                     0
                                              Z t                               Z t
                                                                    F
                                          +        F(Y (s, x), µ (s))ds −             F(X(s, x), µF (s))ds
                                               0                                  0                            2


                                                                   14
                                Universal Approximation of Mean-Field Models via Transformers

By triangle inequality of the ∥ · ∥2 norm,
                                     Z t                                            Z t
                                ≤                 Tn (Y (s, x), µTn (s))ds −               F(Y (s, x), µF (s))ds
                                             0                                        0                                    2
                                     Z t                                     Z t                                      
                               +             F(Y (s, x), µF (s))ds −                  F(X(s, x), µF (s))ds
                                         0                                      0                                 2
                                     Z t                                        Z t
                                ≤            Tn (Y (s, x), µTn (s))ds −                F(Y (s, x), µF (s))ds
                                     0                                          0                                     2
                                    Z t                                      Z t
                               +             F(Y (s, x), µF (s))ds −                 F(X(s, x), µF (s))ds
                                      0                                         0                                 2
                                     Z t                                        Z t
                                ≤            Tn (Y (s, x), µTn (s))ds −                F(Y (s, x), µTn (s))ds
                                     0                                            0                                    2
                                    Z t                                         Z t
                               +             F(Y (s, x), µTn (s))ds −                 F(Y (s, x), µF (s))ds
                                      0                                        0                                  2
                                    Z t                                      Z t
                               +             F(Y (s, x), µF (s))ds −                 F(X(s, x), µF (s))ds
                                      0                                         0                                 2

Using the fact that ∥ · ∥2 is convex, applying Jensen’s inequality yields,
                                    Z t                                          Z t
                                ≤                Tn (Y (s, x), µTn (s))ds −               F(Y (s, x), µTn (s))        ds
                                     0                                            0                               2
                                    Z t                                         Z t
                                +            F(Y (s, x), µTn (s))ds −                 F(Y (s, x), µF (s))         ds
                                     0                                         0                              2
                                    Z t                                      Z t
                                +            F(Y (s, x), µF (s))ds −                  F(X(s, x), µF (s))          ds
                                     0                                           0                           2


Using the fact that
                                                                  supp µTn (s) ⊆ BK (0)
we get,
                                                      Z t
                 ∥Y (t, x) − X(t, x)∥2 ≤                    εds
                                                       0
                                                      Z t                                      Z t
                                                  +         L W1 (µF (s), µTn (s))ds +               L ∥Y (s, x) − X(s, x)∥2 ds.
                                                       0                                         0

Integrating with respect to µ0 and noting that µ0 is a probability measure,

                      W1 (µF (t), µTn (t)) ≤ εt
                                             Z t                            Z t
                                           +     L W1 (µ (s), µ (s))ds +
                                                         F     Tn
                                                                                L W1 (µF (s), µTn (s))ds
                                              0                               0
                                                    Z t
                                           ≤ εt + 2     L W1 (µF (s), µTn (s))ds.
                                                                  0


Now, applying Gronwall’s inequality, we get,

                                                       W1 (µF (t), µTn (t)) ≤ εt exp(2L t)




                                                                           15
                              Universal Approximation of Mean-Field Models via Transformers

D     Transformer
Definition D.1 (Multi-Headed Self-Attention). Let X ∈ Rn×d be a matrix whose rows are n data points in Rd . Let
WQ , WK , WV ∈ Rd×d be learnable weight matrices. Define the query, key, and value matrices by

                                        Q = XWQ ,      K = XWK ,        V = XWV .

Let softmax denote the softmax function applied row-wise to a matrix. The self-attention head function AttHead : Rn×d →
Rn×d is defined as
                                                                      QK ⊤
                                                                           
                                        AttHead(X) := softmax          √       V.
                                                                         d
Let h ∈ Z+ be the number of attention heads. Let AttHead1 , . . . , AttHeadh be attention heads with their own weight
matrices, and let W0 ∈ Rhd×d be a learnable weight matrix. The multi-head self-attention layer Att : Rn×d → Rn×d is
defined as
                                Att(X) := [AttHead1 (X), . . . , AttHeadh (X)] W0 ,
where [·] denotes concatenation along the feature dimension.
Definition D.2 (Transformer Network). A transformer block Block : Rn×d → Rn×d is defined as

                                          Block(X) := X + FC (X + Att(X)) ,

where FC are feed-forward layers (position-wise fully connected layers), and ReLU is the rectified linear unit activation
function. The addition operations represent residual connections. Let L ∈ Z+ , and let Block1 , . . . , BlockL be transformer
blocks. A transformer network T : Rn×d → Rn×k is defined as a composition of transformer blocks followed by an output
network:
                                T (X) := FCout (BlockL ◦ BlockL−1 ◦ · · · ◦ Block1 (X)) ,
where FCout : Rn×d → Rn×k is a fully connected neural network applied position-wise.

E     Training
Here we provide the training details.

E.1   Learning the Vector Field
Hyperparameters We consider depths in {3, 4, 5},                  widths in {128, 256, 512},      and learning rates in
{0.0002, 0.0001, 0.001}.
For the kernel method, we use polynomial basis of {1, x, . . . , xd } and sine and cosine basis of
{sin(x), sin(2x), . . . , sin(kx)} and {cos(x), cos(2x), . . . , cos(kx). We search over d ∈ {2, 3, 4} and k ∈ {3, 4, 5}.

Training Details We used Adam with a cosine annealing decay rate for the step size. For the synthetics CS data, we used
a batch size of 500 and trained the model for 1000 epochs. For the fish milling data, we used a batch size of 1, and trained
the model for 10 epochs.

E.2   Simulating dynamics
For the simulating dynamics experiment, we also train a transformer to learning the training dynamics of a two layer neural
network. We fix the transformer to have a hidden dimension of 512 and 5 layers. We train the model for 250 epochs, using a
learning rate 0.0002, batch size of 1000, using Adam with cosine annealing.




                                                             16
