Understanding the elegant math behind modern generative models
Modern generative AI relies on sophisticated mathematical frameworks to transform random noise into complex, structured data like images, text, and music. Two of the most powerful approaches in this domain are flow models and diffusion models. In this post, we'll explore the elegant mathematics that makes these models work, starting with flow models.
Flow models represent a fascinating approach to generative modeling where we define vector fields that transform simple distributions (like Gaussian noise) into complex data distributions (like images or music). Unlike their more famous cousins, diffusion models, flow models are fundamentally deterministic - given the same starting point, they'll always produce exactly the same result.
Someone gave me this analogy for flow models, which isn't perfect but is a good starting point: Imagine pouring a bucket of water (representing a simple probability distribution) onto a carefully sculpted landscape. The water flows along paths determined by the terrain (that we will later define as a vector field), eventually forming streams that lead towards our target probability distribution. Flow models work in a similar way - they define "probability currents" that guide simple distributions into complex ones through continuous transformations.
Just like how the slope of terrain determines which way water flows at any point, the vector field in a flow model tells each point in our probability distribution exactly which way to move.
Now let's translate this intuition into precise mathematics. At first, you might think of flow models as simple updates where
We can define a vector field as a function:
Let's break down what this means:
The notation
A trajectory is the path that a single point follows through our vector field over time. Mathematically, we define it as:
It's simply saying:
Note that
For example, in a 2D vector field, we can think of our starting point as a single point in 2D space, and our trajectory as the path that point follows through time. A point starting at (1,3) at t=0 might move to (2,4) at t=0.5, then to (3,5) at t=1. However, a different point starting at (1,4) at t=0 would follow a completely different trajectory because it has a different initial condition. This is why specifying
The relationship between our vector field and trajectories is formalized through an ordinary differential equation:
with the initial condition
This equation is the heart of flow models. Breaking it down:
While trajectories describe the path of individual points, the flow function captures the entire transformation all at once:
The flow function
The flow function is powerful because it encapsulates the complete transformation from our simple distribution to our complex target distribution. If we know
Flow models possess several crucial mathematical properties that make them uniquely powerful for generative modeling:
Given the same starting point
This determinism contrasts with diffusion models, which involve stochastic noise and don't guarantee the same output given the same input.
Flow models use invertible transformations, meaning we can trace the path backward from the target distribution to the source distribution. This bidirectional property is essential for both training and sampling.
Mathematically, this means we can define an inverse flow
The transformation occurs continuously over the time interval
This continuity allows us to "watch" the transformation process at any intermediate time step, which can be helpful for understanding and debugging these models.
While the mathematical theory of flow models operates in continuous time, practical implementation requires discrete numerical methods.
The key insight comes from the definition of a derivative. Recall that:
This is the formal definition of a derivative - the limit of the difference quotient as the time step
Here,
Rearranging the approximation by multiplying both sides by
This equation provides the discrete equivalent of our continuous ODE. It tells us how to compute the next position
If we ignore the remainder term
This approximation method works by:
Despite its simplicity, the Euler method works remarkably well for many applications, especially when the step size is sufficiently small.
If you've ever heard of Taylor series: The numerical approximation methods used in flow models have a deep connection to Taylor series expansion. The Taylor series provides a way to approximate functions using polynomial terms:
We basically calculate the rate of change of a given function at a specific point and use it to approximate the function at a nearby point. If the function was infinitely differentiable, we could use the Taylor series to exactly represent the function by infinitely many polynomial terms.
In our discrete formulation:
This corresponds exactly to a first-order Taylor approximation where:
This connection explains why numerical methods for solving ODEs work: they're essentially using truncated Taylor series to approximate the continuous evolution of the system. There are actually more sophisticated methods that use more terms of the Taylor series to get a more accurate approximation, but the Euler method is the simplest one and works surprisingly well.
Let's connect all these mathematical pieces to understand how flow models work in practice:
Initialization: We start by sampling points from a simple distribution (typically Gaussian noise) at time
Vector Field Definition: We define (or more commonly, learn) a vector field
Numerical Integration: We use numerical methods like Euler's method to approximate the ODE and push our samples forward through time:
Final Distribution: By time
For training, we typically use the inverse direction: starting with real data samples, we learn a vector field that pushes them back to a simple distribution (like a Gaussian). Once trained, we can sample from the simple distribution and push forward to generate new data samples. But this will be a topic for another post.
Now that we understand the deterministic approach of flow models, let's explore their more chaotic cousins: diffusion models. While flow models give us the same output every time for the same input, diffusion models embrace randomness to create more flexible and diverse generative processes.
The key insight behind diffusion models is simple yet powerful: real-world processes aren't perfectly predictable. By adding controlled randomness to our transformations, we can model more complex and varied data distributions.
Remember our flow model ODE:
Diffusion models extend this by adding a stochastic component, transforming our ordinary differential equation (ODE) into a stochastic differential equation (SDE):
This single equation captures the essence of diffusion models. Let's break it down:
The
The first term
The second term
Think of it like walking: the deterministic part tells you the general direction to head (toward your destination), while the stochastic part adds random stumbles and course corrections that make your path more varied.
Brownian motion is the mathematical foundation for the randomness in diffusion models. It's named after the botanist Robert Brown, who observed the random movement of particles suspended in fluid.
For any two time points
This means the change in Brownian motion between times s and t follows a normal distribution with:
The crucial insight is that variance scales with time:
This scaling property is what makes Brownian motion perfect for diffusion models - it ensures that our random process behaves consistently regardless of how we discretize time.
Just like flow models need numerical methods to implement continuous ODEs, diffusion models need the Euler-Maruyama method to handle SDEs.
For a small time step
The Brownian increment
In practice, we implement this by sampling from a standard normal distribution and scaling appropriately:
where
You might wonder: why
This comes from a fundamental property of variance scaling. When you multiply a random variable by a constant
Since we want our Brownian increment to have variance
Unlike flow models, running a diffusion model twice with the same input will produce different results due to the random components. This makes them excellent for generating diverse outputs.
The diffusion coefficient
The stochastic component allows diffusion models to "explore" the probability space more thoroughly than deterministic flow models. This exploration capability often leads to better coverage of complex data distributions.
Here's the complete algorithm for sampling from a diffusion model:
Require: Vector field
The key insight is that we're taking the deterministic Euler step from flow models and adding a carefully scaled random component at each iteration.
Diffusion models have become incredibly successful in generative AI because they strike a perfect balance between structure and randomness. The deterministic component ensures the generated samples follow the learned data distribution, while the stochastic component allows for exploration and diversity.
This mathematical framework explains why diffusion models can generate such diverse and high-quality samples - they're not just following a single deterministic path (trajectory) like flow models, but exploring a whole family of possible paths guided by both learned structure and controlled randomness.
So which approach reigns supreme? The answer isn't straightforward. Diffusion models currently dominate the commercial landscape, powering everything from Stable Diffusion to DALL-E, and have proven their reliability in real-world applications. However, recent advances in flow matching show that flow models can achieve superior performance in controlled studies, offering faster training and more efficient sampling paths. The most exciting development is that cutting-edge models like Stable Diffusion 3 and OpenAI's Sora are combining both approaches, using diffusion transformers with flow matching techniques. Rather than one approach replacing the other, the future of generative AI lies in hybrid methods that make use of the deterministic approach of flow models alongside the proven diversity and robustness of diffusion models.