Flow-based Models
Generative models differ in how they relate to the density of the data . GANs are implicit density models: they do not compute or optimize any likelihood, they only learn to sample through an adversarial game. VAEs are likelihood-based models, but their marginal likelihood is intractable and is optimized via a lower bound (the ELBO). Flow-based models, on the other hand, allow this density to be computed exactly and tractably.
As we already saw, a typical generative model with latent variables is defined by:
and computing is intractable because it is practically impossible to iterate over all values of .
Flow-based models approach this problem through invertible transformations of distributions. There are two major families: classical normalizing flows, which apply a discrete sequence of invertible transformations, and continuous normalizing flows (CNF), where the transformation is defined as the solution of an ordinary differential equation (ODE); we will look at the latter further on.
A good estimate of makes it possible to efficiently carry out many tasks, such as sampling new, unobserved but realistic data points (data generation), inferring latent variables, or filling in incomplete data samples. Their use for detecting out-of-distribution (OOD) data has also been explored.
Normalizing flows
Since we use backpropagation to train deep learning models, we expect the posterior probability distribution to be simple enough that its derivative can be computed easily and efficiently. This is why the Gaussian distribution is usually used in latent-variable generative models, even though most real-world distributions are much more complicated than the Gaussian.
In a normalizing flow, to approximate the real distribution we start from a simple distribution , to which a series of invertible transformations is applied to turn it into a complex one . The variable is repeatedly replaced by the new to finally obtain a probability distribution matching the final target variable.

Both the forward transformation and its inverse can be computed exactly. This makes density estimation possible. To do this, two things must be taken into account:
- The density of the inversely transformed sample: to obtain the transformed sample, the sequence of inverse transformations is applied to the original sample. Then, the density of this transformed sample is evaluated under the original simple distribution. That is, we check whether the resulting transformation is a Gaussian.
- The change of volume due to the transformations: as the transformations are carried out, the sample space is distorted. The change of volume is calculated by multiplying the absolute values of the determinants of the Jacobian matrices of each transformation.
By multiplying these two values (the density of the transformed sample and the change of volume), we obtain the density of the original sample under the complex distribution.
Applications
The most direct applications of normalizing flows are:
- Density estimation: to compute the exact density of the data (the one assigned by the model). They can be applied to fit multimodal densities to observed data. They can also be used as hybrid models that model the joint density of inputs and targets , unlike classification models, which only model , and density models, which only model . This is useful for tasks such as anomaly detection.
- Data generation: for different data modalities, including images, video, audio, text, and structured objects such as graphs.
- Inference: instead of blindly trying combinations in a simulator or digital twin until finding which parameters are compatible with an observed result, the flow learns to take the reverse path: starting from an observation, it approximates the posterior distribution over the parameters that could have generated it, without needing to run through the simulator thousands of times. This approach saves months of computation in fields such as cosmology[alsing2019fast] and computational neuroscience[goncalves2019training].
Basic linear algebra concepts
Jacobian matrix
Given a function mapping an -dimensional input vector to an -dimensional output vector, the matrix of all the first-order partial derivatives of this function is called the Jacobian matrix:
It can be understood as a translator from one vector space to another.

Determinant
The absolute value of the determinant (which only exists for square matrices) can be thought of as a measure of "how much multiplication by the matrix expands or contracts space."
The determinant of a square matrix detects whether it is invertible:
-
if then it is not invertible (a singular matrix with linearly dependent rows or columns; or any row or column that is all 0);
-
if , then is invertible.
The determinant of a product is equivalent to the product of the determinants: .
Change of variables theorem
When we transform a random variable, its probability does not change, but the space it lives in can stretch or compress.
Imagine a simple variable with known density , for example a Gaussian. Now we apply an invertible function:
Since is invertible, if we know a value of we can also recover the value of that produced it:
The question is: if we know the density in the original space, , how do we compute the density in the new space, ?
At first glance, one might think it is:
This tells us where comes from, BUT the transformation may have changed the size of the regions of space.
In one dimension, we can think of it in terms of small intervals. A little piece around has length . After applying the transformation, that piece becomes another one around with length .
The probability inside both pieces must be the same:
Rearranging:
And since :
The absolute value appears because a density cannot be negative. If the function flips the axis, for example from left to right, the derivative can be negative, but the size-change factor remains positive.

In several dimensions the same thing happens, but the "little pieces" are no longer intervals, but small areas, volumes, or hypervolumes. This is where the Jacobian determinant comes in:
Where is the Jacobian matrix of the inverse function. Its determinant measures how much the volume locally expands or contracts when going from back to .
This is also often written using the forward transformation :
This form is equivalent. If stretches the volume by a factor of , then the density is divided by . If it compresses it by a factor of , then the density is multiplied by .
Application to normalizing flows
A normalizing flow does not perform a single transformation, but a chain of invertible transformations:
We normally start with a simple distribution:
Then we apply transformations until we reach a sample that looks like real data:
This is used to generate data: we sample a simple and progressively transform it to obtain .
But the interesting thing about flows is that we can also take the reverse path to compute the exact density of a data point :
Once we have , its density is easy to compute because it belongs to the initial distribution. All that remains is to correct for all the volume changes that occurred along the way.
For a single transformation:
Applying the change of variables theorem:
This equation says:
- the density after the transformation depends on the density before the transformation
- if the transformation expands the space, the density decreases
- if the transformation compresses the space, the density increases
As we mentioned in the GAN chapter, in machine learning we usually work with logarithms of probabilities because:
- products of many small numbers tend to zero
- products become sums, which are easier to optimize
Taking logarithms:
If we repeat this for the transformations of the flow, we obtain:
This is the central formula of normalizing flows.
It reads as follows:
- : the log-density of the real data point we want to evaluate
- : the log-density of the corresponding point in the simple distribution
- : the accumulated correction for all the volume changes introduced by the transformations
The path traveled by the random variables is the flow. The complete chain of successive distributions is what we call a normalizing flow.
Models based on Normalizing flows
Some popular normalizing flow architectures are Real NVP[dinh2016density], Masked Autoregressive Flows[papamakarios2017masked], Glow[kingma2018glow] (by Kingma author of the original VAE paper and creator of the Adam optimizer), and SurVAE[nielsen2020survae].
The differences between these architectures lie in the transformations they apply and in specific network designs, but they all share the common goal of transforming a simple distribution into a more complex one.
Advantages and disadvantages
Flow Matching
As we have seen, normalizing flows build the model as a chain of invertible transformations:
Flow Matching starts from a similar idea, but instead of asking:
What invertible transformations can I design to turn noise into data?
it asks:
What direction should each point follow to move from a simple distribution to the target distribution?
In this way, what Flow Matching learns is a velocity field.
Imagine we have many noise points, for example samples from a Gaussian, and many real points, for example images. We want to gradually move the noise points until they end up resembling the real data.
To describe this movement we introduce a time variable:
When , we are at the simple distribution:
When , we want to be at the data distribution:
In between we have intermediate points . The model learns a function:
This function receives two things:
- the current point
- the current time
And returns a vector indicating where that point should move at that instant.

Training
To train the model we need to know what a good velocity would be at different intermediate points. To do this, we can pair a noise point with a real data point and draw a line between them:
This equation represents an interpolation where:
- if , then
- if , then
- if is between 0 and 1, then is between the noise and the data
If the path is a straight line, the velocity that leads from to is:
Therefore, during training we can do the following:
- Take a noise sample
- Take a real data point
- Choose a random time between 0 and 1
- Construct the intermediate point
- Ask the network to predict the correct velocity
The loss function can be written as:
The idea is fairly direct: the network predicts an arrow, which is penalized if it points in a direction different from the one it should.

Sample generation
Once the model is trained, generating a sample consists of solving an ordinary differential equation (ODE):
This roughly means: "update following the velocity predicted by the network at each instant."
In practice:
- We sample an initial noise point
- We use the network to know where to move it
- We take a small step in that direction
- We repeat the process from to
At the end we obtain , which should look like a sample from the data distribution.
Difference from Normalizing Flows
Normalizing flows and Flow Matching share the same intuition: both transform a simple distribution into a complex distribution. The difference lies in how they do it.
In normalizing flows:
- we use a finite sequence of invertible transformations
- we compute how the density changes with Jacobian determinants
- we can evaluate the density assigned by the model
In Flow Matching:
- we learn a continuous velocity field
- we generate data by following a trajectory from noise to data
- we do not need to manually design invertible layers with easy determinants
This is why Flow Matching is appealing: it allows the use of more flexible neural networks and training them with a relatively simple regression loss.
Continuous Normalizing Flows
Until now, when we talked about Normalizing Flows we were referring to discrete flows. However, there are also continuous flows, where, instead of applying a chain of discrete layers, the transformation is modeled as a continuous process in time governed by an ODE.
The problem is that training these flows required "simulating" the ODE forward and backward within each optimization step (known as simulation-based training or the use of Neural ODEs), which is computationally exhausting and very slow.
In this sense, the training reformulation proposed by Flow Matching (direct, linear probability paths and a simple regression loss) makes it possible to solve this problem during training: it is no longer necessary to simulate the ODE at every optimization step.
Optimal Transport
Once the central idea of Flow Matching is understood, it makes sense to talk about Optimal Transport as a way of choosing better paths between the initial distribution and the data distribution.
The intuition behind Optimal Transport is to transform one distribution into another at the lowest possible cost.
In Flow Matching this comes up when we construct paths between noise points and real points. If we pair points at random, the trajectories can cross a lot or take unnecessary detours. If we use the idea of optimal transport, we try to pair points in a more coherent way.
This is why variants such as Optimal Transport Flow Matching or OT-CFM appear in the literature. The original paper Flow Matching for Generative Modeling[lipman2022flow] already highlights the use of optimal-transport-based paths as a particularly interesting option, and later works such as Improving and Generalizing Flow-Based Generative Models with Minibatch Optimal Transport[tong2023improving] use optimal transport on mini-batches to build more useful pairings during training.
What matters is not to confuse the levels:
- Flow Matching: learns a velocity field that moves samples from noise to data.
- Optimal Transport: can help define more efficient paths or pairings for training that velocity field.
Relationship with diffusion models
Flow Matching is closely related to diffusion models, because both describe a process that connects noise with data.
In diffusion, one usually learns to invert a stochastic process that adds noise. The same model can parameterize noise, score, , or velocity, and these parameterizations can be converted into one another for a given noise path. For example, for the linear path used in Flow Matching, with :
Given and any one of the four quantities (noise , score , , or velocity ), the others can be obtained without retraining the model. Other (nonlinear) noise paths give rise to analogous relationships but with different coefficients, as we will see in the next chapter.
In Flow Matching, the velocity field that transports samples from noise to data is learned directly.
Therefore, both frameworks are closely related: they differ in the objective and in how the path is specified, as we will see in the next chapter.