Normalizing Flows: Generative Models That Can Be Run Backwards

A Generator With an Exact Likelihood

Most generative models make a trade. A GAN produces sharp images but cannot tell you how probable any particular image is. A variational autoencoder gives you a number, but only a lower bound on the true likelihood. Normalizing flows refuse that trade. They generate samples and they also compute the exact probability density of any input, and they do it with one network.

The trick is invertibility. A flow takes a sample from a simple distribution, usually a standard Gaussian, and pushes it through a chain of transformations until it looks like data. Because every transformation can be undone, you can also take a real image, run the chain backwards, and land on the exact Gaussian point that would have produced it. Once you know that point, its probability under the Gaussian is a formula you learned in a statistics course.

The price of this property is architectural. Every layer must be invertible, and you must be able to compute how much it stretches or squeezes space. Most of the history of flows is the story of finding layers that satisfy both conditions without being too weak to model anything interesting.

The Change of Variables Formula

Suppose a function f maps a data point x to a latent point z. If f stretches a small region of space, probability mass gets spread thinner, so the density drops. If it compresses a region, the density rises. The amount of stretching at a point is measured by the determinant of the Jacobian, the matrix of all partial derivatives of f. The log-likelihood of x is the log-density of z under the Gaussian plus the log of the absolute value of that determinant.

That equation is the whole training objective. You feed real data through the network, compute the two terms, and maximize their sum by gradient descent. There is no discriminator and no bound with a gap you cannot measure.

The difficulty is computational. The determinant of a general D by D matrix costs on the order of D cubed operations. For a 256 by 256 color image, D is close to 200,000. Computing that determinant once per image per training step is out of the question, so flows are built from layers whose Jacobians have a special structure, most often triangular, where the determinant is the product of the diagonal.

Coupling Layers

The idea of building densities by composing simple invertible maps had appeared in statistics papers by Esteban Tabak and colleagues around 2010 and 2013. The version that made it practical for deep learning came from Laurent Dinh, who introduced NICE in 2014 and then, with Jascha Sohl-Dickstein and Samy Bengio, Real NVP, published at ICLR 2017. Around the same time, Danilo Rezende and Shakir Mohamed used flows at ICML 2015 to make the approximate posteriors of variational inference more flexible, and the name «normalizing flow» spread from that paper.

The coupling layer in Real NVP works like this. Split the input into two halves. Leave the first half unchanged. Use it as input to an ordinary neural network, which outputs a scale and a shift, and apply them to the second half: multiply by the exponential of the scale, add the shift. To invert the layer, you take the unchanged first half, recompute the same scale and shift, subtract and divide. The inner network never needs to be inverted, so it can be as large and nonlinear as you like.

The Jacobian of this layer is triangular, and its log-determinant is the sum of the scale outputs. One layer only changes half the variables, so the next layer swaps the roles of the halves. Stack enough of these and every dimension gets transformed many times, each step cheap to compute in both directions.

Glow and the 1×1 Convolution

In 2018, Diederik Kingma and Prafulla Dhariwal at OpenAI published Glow at NeurIPS. Real NVP had mixed the two halves between layers with fixed permutations. Glow replaced them with an invertible 1×1 convolution, a learned linear map across channels. Since it acts only on the channel dimension, its determinant is cheap, and the paper parameterized the weight matrix through an LU decomposition to make it cheaper still. Glow also dropped batch normalization in favor of a simpler «actnorm» layer with per-channel scale and bias.

The likelihoods improved. On 32 by 32 ImageNet, test bits per dimension went from 4.28 with Real NVP to 4.09 with Glow. What drew more attention were the faces. Trained on CelebA-HQ at 256 by 256 pixels, Glow produced realistic portraits, and because each face had an exact latent code, you could interpolate between two people or move along a direction associated with smiling or age.

Autoregressive Flows

A second family takes its structure from autoregressive models. In a masked autoregressive flow (Papamakarios, Pavlakou and Murray, NeurIPS 2017), each dimension is transformed using a scale and shift computed from all the dimensions that come before it. The Jacobian is again triangular. Evaluating the density of a data point is fast and parallel, because all the earlier values are known. Sampling is slow, because each dimension has to wait for the previous ones.

The inverse autoregressive flow, introduced by Kingma and collaborators in 2016, flips the arrangement. Sampling becomes parallel and density evaluation becomes sequential. Neither design is fast in both directions, so the choice depends on whether you mostly evaluate densities or mostly draw samples.

Case: Parallel WaveNet

The second option went into production at DeepMind. The original WaveNet produced speech with high quality but generated audio one sample at a time, at 24,000 samples per second, which was far too slow to serve. In Parallel WaveNet (van den Oord and colleagues, ICML 2018), the team trained an inverse autoregressive flow to imitate a pretrained WaveNet, a form of distillation where the slow autoregressive model is the teacher and the flow is the student.

Because the flow generates all samples of a waveform in parallel, the paper reports generation more than 20 times faster than real time, and a speedup of more than 1000 times over the original WaveNet, with no measurable loss in quality. The flow’s own weakness, slow likelihood evaluation, did not matter, because the student was trained by comparing its samples with the teacher’s judgments rather than by maximizing likelihood on recordings.

Case: Sampling Molecules and Lattices

Physics has a problem that flows fit well. Many systems have a known energy function, and the probability of a configuration follows the Boltzmann distribution, proportional to the exponential of minus the energy. Knowing the formula does not mean you can sample from it. Molecular dynamics and Markov chain Monte Carlo move in small steps and can spend enormous compute time stuck in one valley before crossing to another.

Frank Noé, Simon Olsson, Jonas Köhler and Hao Wu published «Boltzmann generators» in Science in 2019. Their flow learns a map from a Gaussian to the configuration space of a molecular system and produces independent samples in one shot. Since the flow’s density is exact, each sample can be reweighted by the ratio between the true Boltzmann probability and the flow’s probability, so the results stay unbiased even when the flow is imperfect. They demonstrated it on condensed matter systems and a small protein.

Lattice field theory took the same route. Michael Albergo, Gurtej Kanwar and Phiala Shanahan proposed in 2019 to use a Real NVP flow as the proposal distribution in a Metropolis sampler for a scalar field theory, with an accept or reject step correcting any error. Their motivation was critical slowing down, where standard samplers for lattice QCD become very expensive near the continuum limit. Later work by the same group built flows that respect gauge symmetry by construction. In both fields, the exact density is what makes the correction step possible.

From Discrete Layers to Continuous Flows

Coupling and autoregressive layers are restrictive. Each one has to be designed so its Jacobian stays triangular. In 2018, Ricky Chen and colleagues proposed neural ordinary differential equations, and FFJORD (Grathwohl and colleagues, ICLR 2019) used them to define a continuous normalizing flow. Instead of a stack of layers, a network defines a velocity field, and a point moves along it from noise to data over continuous time. The change in log-density becomes an integral of the trace of the Jacobian, which FFJORD estimated with a random projection trick (Hutchinson’s estimator), removing the need for triangular structure.

The cost moved to the ODE solver, which made training slow. Flow matching, by Yaron Lipman and colleagues at ICLR 2023, removed most of that cost. Rather than simulating the ODE during training, the network regresses directly onto a target velocity along a chosen path between noise and data, in the simplest case a straight line. The resulting models are close cousins of diffusion models, and the paper showed that diffusion paths are special cases of its framework. Stable Diffusion 3, described by Patrick Esser, Robin Rombach and colleagues at ICML 2024, is trained with this straight-line formulation, called rectified flow.

A Final Observation

The exact likelihood turned out to be less useful in one place than people expected. In 2019, Eric Nalisnick and colleagues at DeepMind trained Glow on CIFAR-10, a set of photos of animals and vehicles, and found that it gave higher likelihood to SVHN, a set of house numbers it had never seen. The pattern repeated for other dataset pairs and for VAEs and PixelCNNs as well. Their analysis tied it to simple statistics of the images, like their variance, rather than their content. A precise number for how probable an image is did not mean the model knew whether the image belonged to its world.

Where flows have held up is in settings where the density is used for computation rather than as a judgment of meaning: reweighting a molecular sample, correcting a Markov chain, comparing against a known energy. That is a narrower job than the one flows were first pitched for, and their descendants in image generation have mostly dropped strict invertibility. In physics, where an unbiased answer is worth more than a pretty one, being able to run the generator backwards is still the property people need.

By: Max Johnson B.

Comentarios

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *