# Shallow neural networks as Wasserstein gradient flows

## Motivation

Artificial neural networks (ANNs) consist of layers of artificial "neurons" which take in information from the previous layer and output information to neurons in the next layer. Gradient descent is a common method for updating the weights of each neuron based on training data. While in practice every layer of a neural network has only finitely many neurons, it is beneficial to consider a neural network layer with infinitely many neurons, for the sake of developing a theory that explains how ANNs work. In particular, from this viewpoint the process of updating the neuron weights for a shallow neural network can be described by a Wasserstein gradient flow.

## Single Layer Neural Networks

### Discrete Formulation

Let us introduce the mathematical framework and notation for a neural network with a single hidden layer.[1] Let ${\displaystyle D\subset \mathbb {R} ^{d}}$ be open . The set ${\displaystyle D}$ represents the space of inputs into the network. There is some unknown function ${\displaystyle f:D\rightarrow \mathbb {R} }$ which we would like to approximate. Let ${\displaystyle N\in \mathbb {N} }$ be the number of neurons in the hidden layer. Define

${\displaystyle F_{N}:D\times \Omega ^{N}\rightarrow \mathbb {R} ^{k}}$

be given by

${\displaystyle F_{N}(x,\omega _{1},\dots ,\omega _{N},\theta _{1},\dots ,\theta _{N})={\frac {1}{N}}\sum _{i=1}^{N}\omega _{i}h(\theta _{i},x)}$

where ${\displaystyle h}$ is a fixed activation function and ${\displaystyle \Omega ^{N}}$ is a space of possible parameters ${\displaystyle (\omega ,\theta )=(\omega _{1},\dots ,\omega _{N},\theta _{1},\dots ,\theta _{N})}$. The goal is to use training data to repeatedly update the weights ${\displaystyle \omega _{i}}$ and ${\displaystyle \theta _{i}}$ based on how close ${\displaystyle f_{N,\omega ,\theta }:=F_{N}(\cdot ,\omega _{1},\dots ,\omega _{N},\theta _{1},\dots ,\theta _{N})}$ is to the function ${\displaystyle f}$. More concretely, we want to find ${\displaystyle \omega ,\theta }$ that minimizes the loss function:

${\displaystyle l(f,f_{N,\omega ,\theta }):={\frac {1}{2}}\int _{D}|f(x)-f_{N,\omega ,\theta }(x)|^{2}dx}$

A standard way to choose and update the weights is to start with a random choice of weights ${\displaystyle {\bar {\omega }},{\bar {\theta }}}$ and perform gradient descent on these parameters. Unfortunately, this problem is non-convex, so the minimizer may not be achieved. It turns out in practice, neural networks are surprisingly good at finding the minimizer. A nicer minimization problem that may provide insight into how neural networks work is a neural network model with infinitely many neurons.

### Continuous Formulation

For the continuous formulation (i.e. when ${\displaystyle N=\infty }$), we rephrase the above mathematical framework. In this case, it no longer makes sense to look for weights ${\displaystyle \omega ,\theta }$ that minimize the loss function. We instead look for a probability measure ${\displaystyle \mu \in {\mathcal {P}}(\Omega )}$ such that

${\displaystyle f_{\mu }(x):=\int _{\Omega }\Phi (\xi ,x)d\mu (\xi )}$

minimizes the loss function:

${\displaystyle F(\mu ):={\frac {1}{2}}\int _{D}(f-f_{\mu })^{2}dx}$.

Here ${\displaystyle \Phi (\xi ,x)}$ is an activation function with parameter ${\displaystyle \xi =(\omega ,\theta )\in \Omega }$. We will in fact restrict choices of ${\displaystyle \mu }$ to probability measures with finite second moment, denoted ${\displaystyle {\mathcal {P}}_{2}(\Omega )}$. This is a small technicality to ensure that the Wasserstein metric is indeed a metric.

Note that by restricting choices of ${\displaystyle \mu }$ to probability measures of the form ${\displaystyle \mu _{N}={\frac {1}{N}}\sum _{i=1}^{N}\delta _{\xi _{i}}}$, the above minimization problem generalizes to case with finitely many neurons as well.

To avoid overfitting the network to the training data, a potential term is added the loss function. For the remainder of this article, we define the loss function ${\displaystyle F}$ to be:

${\displaystyle F(\mu ):={\frac {1}{2}}\int _{D}(f-(\int _{\Omega }\Phi (\xi ,x)d\mu (\xi )))^{2}dx+\int _{\Omega }V(\xi )d\mu (\xi )}$

for a convex potential function ${\displaystyle V:\Omega \rightarrow \mathbb {R} }$. Often we choose ${\displaystyle V(\xi )={\frac {\lambda }{2}}|\xi |^{2}}$. In fact, ${\displaystyle F}$ is convex (along linear interpolations), in contrast to the minimization function in the finite neuron case.

When ${\displaystyle X\subseteq \mathbb {R} ^{n}}$, the gradient flow of a differentiable function ${\displaystyle g:X\rightarrow \mathbb {R} }$ starting at a point ${\displaystyle x_{0}}$ is a curve ${\displaystyle x(t):[0,T)\rightarrow X}$ satisfying the differential equation

${\displaystyle {\frac {d}{dt}}x(t)=-\nabla g(x(t)),x(0)=x_{0}}$.

where ${\displaystyle \nabla g}$ is the gradient of g.[2]

Crucially, the gradient flow heads in the direction that decreases the value of ${\displaystyle g}$ the fastest. We would like to use this nice property of gradient flow in our setting with the functional ${\displaystyle F}$. However, it is not immediately straightforward how to do this, since ${\displaystyle F}$ is defined on the space of probability measures, rather than on ${\displaystyle \mathbb {R} ^{n}}$, so the usual gradient is not defined. Before we generalize the notion of gradient flow, recall that ${\displaystyle \nabla g}$ is the unique function from ${\displaystyle \mathbb {R} ^{n}}$ to ${\displaystyle \mathbb {R} ^{n}}$ such that

${\displaystyle \langle \nabla g,v\rangle =D_{v}g}$,

where ${\displaystyle D_{v}g}$ is the directional derivative of ${\displaystyle g}$ in the direction ${\displaystyle v}$. Motivated by this and using the Riemannian structure of the space of probability measures, we can define the a notion of gradient for our loss functional ${\displaystyle F}$.

Note that one can define gradient flows in a general Hilbert space.

We are looking for an element ${\displaystyle \nabla _{W_{2}}F(\mu )}$ in the tangent space of ${\displaystyle {\mathcal {P}}_{2}(\Omega )}$ at ${\displaystyle \mu }$ such that

${\displaystyle \langle \nabla _{W_{2}}F(\mu _{*}),{\frac {d}{dt}}\mu (t)|_{t=0}\rangle _{W_{2}}=\lim _{h\rightarrow 0}{\frac {F(\mu (h))-F(\mu _{*})}{h}}}$

for any absolutely continuous curve ${\displaystyle \mu (t)}$ in ${\displaystyle {\mathcal {P}}_{2}(\Omega )}$ with ${\displaystyle \mu (0)=\mu _{*}}$.

We claim that in fact

${\displaystyle \nabla _{W_{2}}F(\mu _{*})=-\nabla \cdot (\mu _{*}\nabla {\frac {\delta F}{\delta \mu _{*}}})}$

where ${\displaystyle {\frac {\delta F}{\delta \mu _{*}}}}$ is the first variation of ${\displaystyle F}$ at ${\displaystyle \mu _{*}}$ [3] (also called the functional derivative).

We provide a formal argument for this equality that makes the most sense when ${\displaystyle \mu }$ is absolutely continuous with respect to the Lebesgue measure. In this case, we can think of ${\displaystyle \mu }$ as a density function with ${\displaystyle d\mu (x)=\mu (x)dx}$. We can further assume that ${\displaystyle \mu }$ is in ${\displaystyle L^{2}(\Omega )}$. Therefore the same notion of gradient for ${\displaystyle L^{2}}$ exists for ${\displaystyle F}$, and in fact

${\displaystyle \nabla _{L^{2}}F(\mu )={\frac {\delta F}{\delta \mu }}}$

Thus by definition of ${\displaystyle L^{2}}$ gradient and ${\displaystyle L^{2}}$ inner product, we should have

${\displaystyle \lim _{h\rightarrow 0}{\frac {F(\mu (h))-F(\mu _{*})}{h}}=\langle {\frac {\delta F}{\delta \mu _{*}}},{\frac {d}{dt}}\mu (t)|_{t=0}\rangle _{L^{2}}}$
${\displaystyle =\int {\frac {\delta F}{\delta \mu _{*}}}{\frac {d}{dt}}\mu (t)|_{t=0}dx}$

by the continuity equation for ${\displaystyle \mu (t)}$ and divergence theorem,

${\displaystyle \int {\frac {\delta F}{\delta \mu _{*}}}{\frac {d}{dt}}\mu (t)|_{t=0}dx=-\int {\frac {\delta F}{\delta \mu _{*}}}(\nabla \cdot (v_{*}\mu _{*}))dx=\int \nabla {\frac {\delta F}{\delta \mu _{*}}}(v_{*}\mu _{*})dx=\int \nabla {\frac {\delta F}{\delta \mu _{*}}}v_{*}d\mu _{*}}$

where ${\displaystyle v_{*}}$ is the unique velocity vector corresponding to ${\displaystyle \mu _{*}}$ by the equivalence of absolutely continuous curves and solutions of the continuity equation. Now by the definition of the inner product structure of ${\displaystyle ({\mathcal {P}}_{2}(\mathbb {R} ^{d}),W_{2})}$, we have

${\displaystyle \int \nabla {\frac {\delta F}{\delta \mu _{*}}}v_{*}d\mu _{*}=\langle -\nabla \cdot (\mu \nabla {\frac {\delta F}{\delta \mu _{*}}}),{\frac {d}{dt}}\mu (t)|_{t=0}\rangle _{W_{2}}.}$

Therefore we have

${\displaystyle \nabla _{W_{2}}F(\mu _{*})=-\nabla \cdot (\mu \nabla {\frac {\delta F}{\delta \mu _{*}}}).}$

so the gradient flow for ${\displaystyle F}$ is an absolutely continuous curve of probability measure ${\displaystyle \mu _{t}}$ such that

${\displaystyle \partial _{t}\mu _{t}=\nabla \cdot (\mu _{t}\nabla {\frac {\delta F}{\delta \mu _{t}}})}$.

Now we actually compute this gradient flow for our loss functional ${\displaystyle F}$.

### Computing the Wasserstein Gradient Flow for F

First, we compute the first variation ( or functional derivative) of a function from ${\displaystyle {\mathcal {P}}(\Omega )}$ to ${\displaystyle \mathbb {R} }$ of ${\displaystyle F}$

${\displaystyle {\frac {\delta F}{\delta \mu }}(\mu _{*})=\int _{D}\Phi (\cdot ,x)[\int _{\Omega }\Phi ({\bar {\xi }},x)d\mu _{*}({\bar {xi}})-f(x)]dx+V}$

Therefore we have

${\displaystyle \nabla {\frac {\delta F}{\delta \mu }}(\mu _{*})=\int _{D}\nabla _{\xi }\Phi (\cdot ,x)[\int _{\Omega }\Phi ({\bar {\xi }},x)d\mu _{*}({\bar {xi}})-f(x)]dx+\nabla V.}$

Hence the Wasserstein gradient flow is

${\displaystyle \partial _{t}\mu _{t}={\rm {{div}(\mu _{t}\nabla {\frac {\delta F}{\delta \mu }}(\mu _{t}))}}}$.

## Open Problems

### Generalization

Generalization error is how well a model generalizes for a new data not in your training data set. In practice, gradient descent does generalize really well, but it is an open problem to provide a theoretical framework to guarantee generalization. Wang, Meng, Chen and Liu 2021 made progress on studying the implicit regularization of the algorithm, and provide a framework for convergence. [4]