TL;DR
An affine coupling layer is one invertible step inside a normalizing flow. It splits its input vector into two parts, copies the first part unchanged, and multiplies the second part by a scale and adds a shift. Both the scale and the shift are computed from the first part by any function you like. Because the first part passes through untouched, the layer can be undone exactly and its determinant costs almost nothing to compute.
Builds on: Normalizing Flow, Bijective Function · Leads to: Density Estimation Using Real NVP
Where it's used
Exact density models of images. The RealNVP paper by Dinh, Sohl-Dickstein and Bengio (2016) stacked affine coupling layers into a model that assigns an exact log-likelihood to an image. Each layer is cheap to invert, so the same network can also generate images by running backwards.
Fast generation and scoring. Because the inverse costs about as much as the forward pass, a model built from coupling layers can sample new data and score existing data at similar speed. Most other generative models are fast at one of the two and slow at the other. Later flow models for image generation reused this layer, including the Glow model of 2018.
Anomaly detection. A flow built from coupling layers gives every input an exact log-density. A sensor reading whose log-density is far below the typical range can be flagged as unusual, and the score comes from a single forward computation rather than an approximation.
How it works
Take a two-dimensional input . The layer keeps and changes , using two functions (scale) and (shift) that read . To keep the arithmetic exact, choose , so that the scale equals , and . Start with the input .
- Keep the first part. The output's first coordinate is . Nothing happens to it, and this is the part that lets the layer be undone.
- Compute the scale from the kept part. . Check: the scale depends only on , not on .
- Compute the shift from the kept part. . Check: the shift also depends only on .
- Scale and shift the second part. . Check: becomes , and then is .
- Read off the output. The layer maps to .
- Undo the layer. Given , the first coordinate is copied back, so . Then . The original input is recovered. Check: no derivative of or was needed, only their values.
- Find the stretch. The layer stretches the area of the input by . A small region of inputs maps to a region twice as large, which is the factor that the density formula needs.
flowchart LR A["Input x1, x2"] --> B[Keep x1 as y1] A --> C["Compute s(x1) and t(x1)"] C --> D["y2 = x2 times exp(s) plus t"] B --> E["Output y1, y2"] D --> E E --> F["Inverse: x2 = (y2 minus t) times exp(minus s)"]
- scale exp(s(x1)) = 2^x1
The math
The layer
Let be the input and split it at an index into and . Let and be functions from to . The output is
Here is the element-wise product, so each entry of the second part is multiplied by its own scale, and is applied entry by entry.
Read it in words: the first part is copied, and the second part is multiplied by and then shifted by . The word affine means exactly this: a scale followed by a shift. The two functions and read only the first part. They are not required to be invertible, and in practice they are neural networks.
The stretch: the Jacobian
Write the Jacobian of the map with its blocks in order. The top-left block is the identity, because equals . The top-right block is zero, because the first output does not depend on the second input. The bottom-right block is the diagonal matrix . The bottom-left block contains derivatives of and , but it sits in the lower-left corner and does not affect the determinant. The Jacobian is therefore block triangular, and its determinant is the product of the diagonal blocks:
The determinant is the exponential of the sum of the scales. Summing the entries of is cheap, and no derivative of or is needed.
Undoing the layer: the inverse
Solving the two equations backwards gives
The inverse uses and as functions, evaluated at the kept part, and never inverts them. So and can be arbitrarily complicated.
The density: pulling the point back
A flow scores an input by pulling it back to the base variable and applying the change of variables formula. For one coupling layer with base density ,
The sign is negative because the layer stretches space by , which spreads the probability out and lowers its density.
Stacking layers
One layer leaves untouched, so a single layer cannot change the first part at all. The fix is to alternate the split. For example, one layer updates the second half, and the next layer keeps that half and updates the first. Each layer adds its log-determinant term, so after layers
Here each is the scale of layer , evaluated at the input that layer receives.
Worked examples
Use the layer from the walkthrough with and . Take the input .
- The kept part is .
- The scale is .
- The shift is .
- The second output is .
- Output: .
- Inverse: . The input is recovered.
- Determinant: , so the stretch is one half. A region of inputs shrinks to half its area on the way through.
Interpretation: a negative value of gives a scale below one, so the layer compresses the second coordinate there. The layer is only ever a rescaling of the second part, controlled by the first.
Let the base density be the standard two-dimensional Gaussian, . Use the same layer as in the walkthrough. The point comes from the base point .
- The base log-density at is .
- The layer's scale at the kept coordinate is .
- The log-determinant term is .
- The density at is .
- Cross-check: the density at is the base density at divided by the stretch factor 2. Dividing by 2 subtracts from the log, the same step as line 3.
Interpretation: the point is far from the origin, so its log-density is very negative, as expected. The score is exact. Nothing was approximated, and the layer did not need to invert or .
Consider a grayscale image, which has 784 pixel values. A checkerboard mask of the kind used for images updates half the pixels, which is 392 values, and keeps the other half. Suppose that, for a trained layer, the scale values average across the 392 updated pixels. The numbers here are invented for illustration.
- Each updated pixel is multiplied by its own scale, so the log-determinant of this layer is the sum of 392 scale values.
- The sum is .
- So the layer changes the log-density of the image by , which is a factor of about in density.
- A second layer uses the complementary mask, which updates the other 392 pixels. If its scales also average , it contributes another to the log-determinant.
- The two layers together contribute a total log-determinant term of .
Interpretation: a flow built from many layers changes the log-density by a large amount, and each layer's contribution must be computed correctly for training to work. Coupling layers make that computation a sum over the updated pixels, which is cheap even for a large image.
Try it: a third layer also updates 392 pixels with average scale . What is the total log-determinant term for the three layers? Answer: .
Common mistakes and tradeoffs
It is tempting to think that a coupling layer can map any input to any output. In fact it changes only half the coordinates, and each change is a scale and a shift. The flexibility comes from stacking layers with alternating splits and from the complicated functions and .
It is tempting to think that the inverse requires inverting or . In fact the inverse uses the values and , computed on the kept part, and the functions are never inverted.
It is tempting to think that the determinant needs the full Jacobian. In fact the Jacobian is block triangular, so its determinant is the product of the scale terms.
It is tempting to think that large scales are harmless. In fact an unbounded can make training unstable. Practical models bound , for example with a hyperbolic tangent, to keep the scales in a safe range.
When to use it: a flow where both sampling and exact scoring must be fast, and where the data has structure that a split can exploit, such as the spatial layout of an image.
When not to use it: a model that needs one fully flexible map in a single step, or data without a natural split of coordinates.
Alternatives: autoregressive flows, which give a more flexible map at the cost of slower sampling; continuous-time flows, which need numerical solvers; and variational models such as the variational autoencoder, which do not give an exact density.
History
Normalizing flows were developed as a way to get exact densities from invertible maps. Early work by Rezende and Mohamed in 2015 showed how to build flexible densities this way.
In 2014 Dinh, Krueger and Bengio introduced NICE, which used additive coupling layers. An additive layer shifts one part by a function of the other part and has determinant one, so it preserves volume. That was simpler but could not stretch space.
The 2016 RealNVP paper by Dinh, Sohl-Dickstein and Bengio added the scale term , which let the layer change volume while keeping the determinant cheap. That addition is the affine part of the name, and it made the layer useful for image models.
Recap
- A coupling layer copies one part of its input and transforms the other part by a scale and a shift, both computed from the copied part.
- The inverse is exact and needs only the values of and , never their inverses.
- The determinant is of the sum of the scales, which is cheap to compute.
- A flow needs many layers with alternating splits, so every coordinate is eventually transformed.
- Read next: Density Estimation Using Real NVP, which stacks these layers into an image model, and variational autoencoders for a different way of modelling the same data.
Check yourself
QuestionWhy can the coupling layer's inverse be computed without inverting the functions and ?Show answer
The inverse formula only evaluates and at the kept part, which is unchanged by the layer. Those functions are called as ordinary functions, so they can be any network, with no need to invert them.
QuestionA coupling layer has and . For the input , compute the output .Show answer
The kept part is . The scale is , and the shift is . So . The output is .
QuestionWhy does a single coupling layer not give a fully flexible map, and how is this fixed?Show answer
A single layer leaves one part of its input exactly as it was, so only half the coordinates change, and each change is a scale and a shift. Stacking layers with alternating splits, so that every coordinate is updated by some layer, gives a much more flexible map while keeping each layer simple to invert and to score.
QuestionA flow has two coupling layers. The first has average log-scale across 100 updated coordinates, and the second has average log-scale across 100 updated coordinates. What is the total log-determinant term, and how does it change the log-density?Show answer
The first layer contributes , and the second contributes . The total is . The log-density of the output is the base log-density minus , so the density is smaller than the base density by a factor of .
Sources
- Dinh, Sohl-Dickstein and Bengio, "Density estimation using Real NVP" (2016): https://arxiv.org/abs/1605.08803
- Dinh, Krueger and Bengio, "NICE: Non-linear Independent Components Estimation" (2014): https://arxiv.org/abs/1410.8516
- Normalizing flow, Wikipedia: https://en.wikipedia.org/wiki/Normalizing_flow