Optimal Transport for Generative Models
The theory of optimal transport applied to flow models: Wasserstein distances, Monge and Kantorovich formulations, Brenier's theorem, Sinkhorn algorithm, and how OT coupling improves flow matching.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Monge Problem
- Kantorovich Relaxation
- Wasserstein Distance
- Brenier's Theorem
- Entropic Regularization & Sinkhorn
- Mini-Batch OT for Flow Matching
- Dynamic Optimal Transport
- Wasserstein Gradient Flows
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- State and compare the Monge and Kantorovich formulations of OT.
- Define the Wasserstein- distance and its metric properties.
- State Brenier's theorem and its connection to convex functions.
- Derive the Sinkhorn algorithm for entropic OT.
- Apply mini-batch OT to improve flow matching training.
Notation
- — source and target distributions
- — transport map
- — coupling (joint distribution)
- — Wasserstein- distance
- — entropic regularization strength
Core Intuition
Optimal transport asks: what's the cheapest way to move mass from one distribution to another? For generative models, this means finding the most efficient mapping from noise to data. OT maps follow straight, non-crossing paths — exactly what flow matching needs for fast generation. The Wasserstein distance gives a geometrically meaningful metric between distributions, unlike KL divergence.
Optimal Transport
The Monge Problem
Monge (1781): Find a transport map pushing to that minimizes total cost:
Constraint: means for any set : .
Problem: The Monge map may not exist (e.g., if is a Dirac mass and is not).
Kantorovich Relaxation
Kantorovich (1942): Relax to probabilistic couplings:
where .
Advantages over Monge:
- Always has a solution (linear program over convex set).
- Allows mass splitting (one source point can go to multiple targets).
- Duality theory available.
Kantorovich dual:
where .
Wasserstein Distance
Definition:
Properties:
- is a metric on probability distributions.
- Metrizes weak convergence (unlike KL which can be infinite).
- For : connected to displacement convexity and gradient flows.
for Gaussians (closed form):
Brenier's Theorem
Theorem (Brenier, 1991): If is absolutely continuous and , then the optimal transport map exists, is unique, and is the gradient of a convex function:
for some convex .
Implication: The OT map is curl-free (irrotational). Paths don't "twist" — they follow the gradient of a potential. This is why OT maps give straight, non-crossing paths.
For generative models: If we could find the Brenier map from to , we'd have the optimal one-step generator. But computing it exactly is intractable for complex distributions.
Entropic Regularization & Sinkhorn
Entropic OT: Add entropy to make the problem smooth:
where .
Solution form: .
Sinkhorn algorithm (discrete case): Given cost matrix , kernel :
Iterate until convergence. Transport plan: .
Cost: per iteration (matrix-vector products). Converges in iterations.
Mini-Batch OT for Flow Matching
Problem: Full OT between and is intractable. But we can approximate within each training batch.
Mini-batch OT (Tong et al., 2023):
- Sample batch: , .
- Compute cost matrix: .
- Solve assignment: (Hungarian, ).
- Train on paired batch: .
Effect on training:
- Without OT: paths cross, velocity is multi-valued, need many sampling steps.
- With mini-batch OT: paths approximately non-crossing within each batch, straighter learned flow.
Quality improvement: 2-5 FID points better, especially at few sampling steps.
Dynamic Optimal Transport
Benamou-Brenier formulation: OT as a fluid dynamics problem:
subject to the continuity equation with .
Connection to flow matching: The OT velocity field gives the kinetic-energy-minimizing flow. This is exactly what we want: minimum-energy paths = straight paths = few-step generation.
Wasserstein Gradient Flows
Many processes in ML can be viewed as gradient flows in Wasserstein space:
where is a functional (energy).
Examples:
- : converges to target (Langevin dynamics).
- (entropy): heat equation.
- : displacement interpolation.
Common Pitfalls
Pitfall 1. Applying full OT (Hungarian algorithm) to large batches. is expensive; use Sinkhorn with for .
Pitfall 2. Setting entropic regularization too large. The coupling becomes too blurry (close to independent); OT benefit vanishes. Use – relative to cost scale.
Pitfall 3. Assuming mini-batch OT equals global OT. It's an approximation; pairs within a batch don't guarantee global optimality. Use larger batches for better approximation.
Summary
- Monge problem: Find cheapest map from to .
- Kantorovich relaxation: Probabilistic couplings; always solvable.
- Wasserstein distance: Geometry-aware metric on distributions.
- Brenier's theorem: OT map = gradient of convex function (no crossing).
- Sinkhorn: Efficient entropic OT via alternating scaling.
- Mini-batch OT improves flow matching by reducing path crossing.
- Dynamic OT connects to minimum-energy flows.
Exercises
Exercise 1. Compute between and using the closed-form Gaussian formula.
Exercise 2. Implement Sinkhorn for two empirical distributions of 100 points each in 2D. Visualize the transport plan.
Exercise 3. Prove that the Brenier map between two 1D distributions is the quantile function composition: .
Exercise 4. For mini-batch OT with : compare the computational cost of Hungarian vs Sinkhorn (50 iterations) algorithms.
Exercise 5. Show that the displacement interpolation satisfies the continuity equation with constant velocity field .