Optimal Transport in Generative Modeling
Optimal transport finds the cheapest way to morph one pile of probability into another, which gives generative models a stable distance and a straight training path between noise and data.
Why Does This Exist?
Generative models must turn noise into data, but plain divergences misbehave. KL divergence explodes when the model puts zero mass where data lives, and it ignores geometry: shifting a generated image by one pixel can look identical yet score badly. Training needs a distance that stays finite when supports differ and grows with actual moving effort.
Optimal transport supplies it. It asks how much work it takes to rearrange one distribution into another, mass times distance, minimized over all pairings. That minimum is the Wasserstein distance, and it trains Wasserstein GANs, flow matching, and rectified flows. This page covers the transport problem and its 1D solution.
The one idea you need first
A coupling pairs every unit of mass in distribution with a destination in . Many couplings exist; the cost of one is the average distance traveled under it. The optimal coupling is the cheapest pairing, and its cost is the distance between the distributions. Probability distributions carry the background.
Think of It Like This
Moving sand piles with wheelbarrows
Two sand piles sit in a yard: a round heap by the fence (noise) and a castle-shaped target (data). Any reshaping moves grains along paths, and each trip costs effort times distance. The cheapest full plan is optimal transport; its total bill is the Wasserstein distance.
Crossing paths waste effort, so the best plan never crosses: grains keep their order. Where the analogy stops: probability mass splits fractionally across destinations, which sand grains cannot, and entropy-regularized plans deliberately blur the assignment to compute faster.
How It Actually Works
For distributions and with cost , often , the -Wasserstein distance is:
In plain words: ranges over couplings with marginals and , the expectation totals the moving bill, and the minimum picks the cheapest plan. The choice gives the earth mover distance; gives smoother gradients and straight-line displacement paths used in flow matching.
In one dimension the answer is closed form: sort both samples and pair them in order. For empirical samples and , . No linear program needed. In higher dimensions the Sinkhorn algorithm adds a small entropy bonus that makes the problem solvable by repeated row and column rescaling.
Worked example
Move mass from to , one unit each, cost is absolute distance.
Sorted pairing: costs 2, costs 1. Total , average . Crossed pairing: costs 3, costs 2, total , average . The sorted plan wins by , and no other pairing exists for two points. The optimal coupling keeps order, exactly as the sand picture predicts.
Code
import numpy as np
def w1_sorted(x: np.ndarray, y: np.ndarray) -> float: xs = np.sort(x) # optimal 1D plan pairs order statistics ys = np.sort(y) return float(np.mean(np.abs(xs - ys))) # mean quantile gap
x = np.array([0.0, 4.0])y = np.array([2.0, 3.0])print(w1_sorted(x, y)) # -> 1.5The output 1.5 matches the hand pairing. Try shuffling first and the sort inside still recovers the cheap plan, which is the entire 1D algorithm.
Watch Out For
Using Wasserstein where samples are too few
estimated from 50 samples in 100 dimensions mostly measures sampling noise, not distribution gap. The value stays large even for identical distributions because empirical clouds rarely overlap in high dimensions. Teams then chase a distance that cannot fall.
Fix it by using thousands of samples, reporting the sliced or entropic variant, or pairing the number with a fixed validation set. Never quote raw from a minibatch.
Forgetting the cost defines the distance
with squared Euclidean cost punishes long moves quadratically and yields straight paths, while with absolute cost tolerates them. Swapping costs mid-project changes rankings of models without changing any sample. The number is meaningless without the cost beside it.
Always state and the ground metric when you report the distance.
The Quick Version
- Optimal transport is the cheapest pairing of two probability masses, and its cost is the Wasserstein distance.
- In 1D the optimum sorts and pairs in order; the example costs 1.5.
- Sinkhorn adds entropy to make high-dimensional transport solvable by iterative rescaling.
- Flow matching trains along the straight displacement paths that plans trace.
- Always name the cost and use enough samples, since minibatch is mostly noise.