Search

Search papers and lessons

Affine Coupling Layer

Level 4 · 13 min read

An affine coupling layer keeps half of its input unchanged and scales and shifts the other half by amounts computed from the unchanged half, so it stays easy to invert and cheap to score.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Calculus
  • Discrete math
  • Generative models
  • Inference
  • ReadReady to readNot yet

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 x=(x1,x2)x = (x_1, x_2). The layer keeps x1x_1 and changes x2x_2, using two functions ss (scale) and tt (shift) that read x1x_1. To keep the arithmetic exact, choose s(x1)=x1ln⁡2s(x_1) = x_1 \ln 2, so that the scale exp⁡(s(x1))\exp(s(x_1)) equals 2x12^{x_1}, and t(x1)=3x1t(x_1) = 3x_1. Start with the input x=(1,4)x = (1, 4).

  1. Keep the first part. The output's first coordinate is y1=x1=1y_1 = x_1 = 1. Nothing happens to it, and this is the part that lets the layer be undone.
  2. Compute the scale from the kept part. exp⁡(s(1))=21=2\exp(s(1)) = 2^1 = 2. Check: the scale depends only on x1x_1, not on x2x_2.
  3. Compute the shift from the kept part. t(1)=3×1=3t(1) = 3 \times 1 = 3. Check: the shift also depends only on x1x_1.
  4. Scale and shift the second part. y2=4×2+3=11y_2 = 4 \times 2 + 3 = 11. Check: 44 becomes 88, and then 8+38 + 3 is 1111.
  5. Read off the output. The layer maps (1,4)(1, 4) to (1,11)(1, 11).
  6. Undo the layer. Given (1,11)(1, 11), the first coordinate is copied back, so x1=1x_1 = 1. Then x2=(11−t(1))×exp⁡(−s(1))=(11−3)/2=4x_2 = (11 - t(1)) \times \exp(-s(1)) = (11 - 3)/2 = 4. The original input is recovered. Check: no derivative of ss or tt was needed, only their values.
  7. Find the stretch. The layer stretches the area of the input by exp⁡(s(x1))=2\exp(s(x_1)) = 2. 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)"]
−2−1.5−1−0.500.511.5201234x1 (the kept input)scale applied to x2
  • scale exp(s(x1)) = 2^x1
The scale depends only on x1. It is always positive, so the map from x2 to y2 can always be reversed by dividing by it.

The math

The layer

Definition: Affine coupling layer

Let x∈RDx \in \mathbb{R}^D be the input and split it at an index d<Dd \lt D into x1:dx_{1:d} and xd+1:Dx_{d+1:D}. Let ss and tt be functions from Rd\mathbb{R}^d to RD−d\mathbb{R}^{D-d}. The output yy is

y1:d=x1:d,yd+1:D=xd+1:D⊙exp⁡(s(x1:d))+t(x1:d).y_{1:d} = x_{1:d}, \qquad y_{d+1:D} = x_{d+1:D} \odot \exp\big(s(x_{1:d})\big) + t(x_{1:d}).

Here ⊙\odot is the element-wise product, so each entry of the second part is multiplied by its own scale, and exp⁡\exp is applied entry by entry.

Read it in words: the first part is copied, and the second part is multiplied by exp⁡(s)\exp(s) and then shifted by tt. The word affine means exactly this: a scale followed by a shift. The two functions ss and tt 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 y1:dy_{1:d} equals x1:dx_{1:d}. 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 diag⁡(exp⁡(s(x1:d)))\operatorname{diag}\big(\exp(s(x_{1:d}))\big). The bottom-left block contains derivatives of ss and tt, 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:

det⁡∂y∂xT=∏jexp⁡(s(x1:d)j)=exp⁡(∑js(x1:d)j).\det \frac{\partial y}{\partial x^{\mathsf{T}}} = \prod_{j} \exp\big(s(x_{1:d})_j\big) = \exp\Big(\sum_j s(x_{1:d})_j\Big).

The determinant is the exponential of the sum of the scales. Summing the entries of ss is cheap, and no derivative of ss or tt is needed.

Undoing the layer: the inverse

Solving the two equations backwards gives

x1:d=y1:d,xd+1:D=(yd+1:D−t(y1:d))⊙exp⁡(−s(y1:d)).x_{1:d} = y_{1:d}, \qquad x_{d+1:D} = \big(y_{d+1:D} - t(y_{1:d})\big) \odot \exp\big(-s(y_{1:d})\big).

The inverse uses ss and tt as functions, evaluated at the kept part, and never inverts them. So ss and tt can be arbitrarily complicated.

The density: pulling the point back

A flow scores an input yy by pulling it back to the base variable x=f−1(y)x = f^{-1}(y) and applying the change of variables formula. For one coupling layer with base density pxp_x,

log⁡py(y)=log⁡px(x)−∑js(x1:d)j,x=f−1(y).\log p_y(y) = \log p_x(x) - \sum_j s(x_{1:d})_j, \qquad x = f^{-1}(y).

The sign is negative because the layer stretches space by exp⁡(∑s)\exp(\sum s), which spreads the probability out and lowers its density.

Stacking layers

One layer leaves x1:dx_{1:d} 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 LL layers

log⁡py(y)=log⁡px(x)−∑ℓ=1L∑jsℓ(⋅)j.\log p_y(y) = \log p_x(x) - \sum_{\ell=1}^{L} \sum_j s_\ell\big(\cdot\big)_j.

Here each sℓs_\ell is the scale of layer ℓ\ell, evaluated at the input that layer receives.

Worked examples

Example
One layer, one input, by hand

Use the layer from the walkthrough with s(x1)=x1ln⁡2s(x_1) = x_1 \ln 2 and t(x1)=3x1t(x_1) = 3 x_1. Take the input x=(−1,6)x = (-1, 6).

  1. The kept part is y1=−1y_1 = -1.
  2. The scale is exp⁡(s(−1))=2−1=0.5\exp(s(-1)) = 2^{-1} = 0.5.
  3. The shift is t(−1)=−3t(-1) = -3.
  4. The second output is y2=6×0.5+(−3)=0y_2 = 6 \times 0.5 + (-3) = 0.
  5. Output: y=(−1,0)y = (-1, 0).
  6. Inverse: x2=(0−(−3))×exp⁡(−s(−1))=3×2=6x_2 = (0 - (-3)) \times \exp(-s(-1)) = 3 \times 2 = 6. The input is recovered.
  7. Determinant: exp⁡(s(−1))=0.5\exp(s(-1)) = 0.5, so the stretch is one half. A region of inputs shrinks to half its area on the way through.

Interpretation: a negative value of x1x_1 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.

Example
Scoring a point through a layer

Let the base density be the standard two-dimensional Gaussian, px(x)=12πe−∥x∥2/2p_x(x) = \frac{1}{2\pi} e^{-\|x\|^2/2}. Use the same layer as in the walkthrough. The point y=(1,11)y = (1, 11) comes from the base point x=(1,4)x = (1, 4).

  1. The base log-density at x=(1,4)x = (1, 4) is log⁡px=−log⁡(2π)−12(12+42)=−1.8379−8.5=−10.3379\log p_x = -\log(2\pi) - \tfrac12(1^2 + 4^2) = -1.8379 - 8.5 = -10.3379.
  2. The layer's scale at the kept coordinate is s(1)=ln⁡2≈0.6931s(1) = \ln 2 \approx 0.6931.
  3. The log-determinant term is −s(1)=−0.6931-s(1) = -0.6931.
  4. The density at yy is log⁡py(y)=−10.3379−0.6931=−11.0310\log p_y(y) = -10.3379 - 0.6931 = -11.0310.
  5. Cross-check: the density at yy is the base density at xx divided by the stretch factor 2. Dividing by 2 subtracts ln⁡2\ln 2 from the log, the same step as line 3.

Interpretation: the point (1,11)(1, 11) 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 ss or tt.

Example
A flow over image pixels (realistic)

Consider a 28×2828 \times 28 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 0.050.05 across the 392 updated pixels. The numbers here are invented for illustration.

  1. Each updated pixel is multiplied by its own scale, so the log-determinant of this layer is the sum of 392 scale values.
  2. The sum is 392×0.05=19.6392 \times 0.05 = 19.6.
  3. So the layer changes the log-density of the image by −19.6-19.6, which is a factor of about e−19.6e^{-19.6} in density.
  4. A second layer uses the complementary mask, which updates the other 392 pixels. If its scales also average 0.050.05, it contributes another 19.619.6 to the log-determinant.
  5. The two layers together contribute a total log-determinant term of 19.6+19.6=39.219.6 + 19.6 = 39.2.

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 0.050.05. What is the total log-determinant term for the three layers? Answer: 3×19.6=58.83 \times 19.6 = 58.8.

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 ss and tt.

It is tempting to think that the inverse requires inverting ss or tt. In fact the inverse uses the values s(y1:d)s(y_{1:d}) and t(y1:d)t(y_{1:d}), 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 exp⁡(s)\exp(s) can make training unstable. Practical models bound ss, 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 exp⁡(s)\exp(s), 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 ss and tt, never their inverses.
  • The determinant is exp⁡\exp 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 ss and tt?Show answer

The inverse formula xd+1:D=(yd+1:D−t(y1:d))⊙exp⁡(−s(y1:d))x_{d+1:D} = (y_{d+1:D} - t(y_{1:d})) \odot \exp(-s(y_{1:d})) only evaluates ss and tt 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 s(x1)=x1ln⁡2s(x_1) = x_1 \ln 2 and t(x1)=3x1t(x_1) = 3x_1. For the input x=(2,1)x = (2, 1), compute the output yy.Show answer

The kept part is y1=2y_1 = 2. The scale is exp⁡(s(2))=22=4\exp(s(2)) = 2^2 = 4, and the shift is t(2)=6t(2) = 6. So y2=1×4+6=10y_2 = 1 \times 4 + 6 = 10. The output is y=(2,10)y = (2, 10).

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 0.10.1 across 100 updated coordinates, and the second has average log-scale 0.050.05 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 100×0.1=10100 \times 0.1 = 10, and the second contributes 100×0.05=5100 \times 0.05 = 5. The total is 1515. The log-density of the output is the base log-density minus 1515, so the density is smaller than the base density by a factor of e15e^{15}.

Sources