Flow architectures
The layers that real image flows are built from: MAF and IAF autoregressive layers with MADE masks, splines, Glow's invertible 1×1 convolution and ActNorm, the squeeze and multi-scale architecture, and conditional flows.
What a flow on images needs
The topic The change of variables left us with one working layer. An affine coupling layer copies half of the coordinates and scales and shifts the other half with networks of the copied half. Its Jacobian is triangular, so is a plain sum of the scales. Stacked with swaps in between, such layers already make a flow.
A real image needs more than that. A colour image has coordinates, and a general Jacobian of that size would cost about operations per layer and per example. So every layer of this topic keeps the two rules of the previous one: an inverse we can compute and a cheap log-determinant. Within those rules we meet four more parts:
- autoregressive layers, a second way to get a triangle, and the masks that build them from one network;
- spline layers, a more flexible element-wise map than "scale and shift";
- Glow's linear layers, a learned convolution that mixes channels and ActNorm that normalizes them;
- the image skeleton: masks on the pixel grid, the squeeze, and multiple scales.
One fact serves all of them. The inverse of a layer has the inverse Jacobian, at , and the determinant of an inverse matrix is the reciprocal. So a determinant that is cheap in one direction is cheap in the other, and the log-determinant only changes sign.
The autoregressive layer
A coupling layer lets half of the coordinates depend on the other half. Push the idea to the end: let each coordinate depend on all the coordinates before it. Take an autoregressive model with Gaussian conditionals, where and (a log standard deviation) are any functions of the earlier coordinates :
This model is already a flow (Papamakarios, Pavlakou and Murray, 2017). Sampling draws and computes , one coordinate after another. Solving for gives the map from data to noise:
Coordinate depends on only, so the Jacobian is lower-triangular. Its diagonal holds the factors , and
The change of variables gives back exactly the likelihood of the autoregressive model, as it should. A small example with : take , , and the point . Then , , and the log-determinant is : the layer squeezes , because its conditional standard deviation is larger than one. In total nats, where is the standard normal density.
MAF and IAF: which direction is fast
The same layer can be run in two ways, and they are fast in opposite directions.
A masked autoregressive flow (MAF; Papamakarios, Pavlakou and Murray, 2017) is the layer of the previous block: and read the data side . To evaluate a density, all of is known, so one network pass gives every and at once. To sample, we need before we can compute , then before , and so on: passes one after another.
An inverse autoregressive flow (IAF; Kingma et al., 2016) lets the shift and scale read the noise side instead:
Now all of is known when we sample, so sampling takes one pass. The density of a given takes passes, because recovering needs first. There is one important exception: for its own samples the IAF already knows , so it can score them in one pass. That is exactly what a VAE posterior needs: draw a code and compute its .
The coupling layer sits in between. It is the autoregressive layer in which the first coordinates are copied and every later , reads only . Both directions then take one pass. The price is that a coupling layer changes only part of the coordinates, so it needs more layers to do the same work.
MADE masks
An autoregressive layer needs outputs, and for every , and output must not see the inputs . Training separate networks would be wasteful. MADE (Germain et al., 2015) uses one ordinary fully connected network and multiplies its weights by fixed binary masks.
Every unit gets a degree . Input has degree , output has degree , and each hidden unit gets some : the number of leading inputs it may depend on. A hidden unit reads units of degree at most its own; an output reads only hidden units of strictly smaller degree. Written as masks, with rows as receivers:
Take and four hidden units of degrees :
Entry of the product counts the paths from input to output . It is strictly lower-triangular, and a nonlinearity cannot create a path that the masks removed. So and depend on only, exactly as required. Output receives nothing at all and is a learned constant, which is right: has no earlier coordinates to condition on.
Where one layer falls short
One affine autoregressive layer has a hard limit. Given , the coordinate is an affine function of one Gaussian , so every conditional is Gaussian, hence unimodal.
Take a toy target shaped like an X: and with a random sign. Given , the data put two narrow bumps at . With unlimited data, maximum likelihood picks the Gaussian with the true conditional mean and variance (the forward-KL fit by moments from Fitting distributions with KL). The random sign makes the mean , and the variance is . At the model puts its peak at , density , where the data have essentially none (). At the data point it gives against the true . Mass-covering, exactly as forward KL promised.
There are two fixes.
- Stack layers and reorder. The next layer models the of the previous one in a different coordinate order. A MAF of five such layers already learns multimodal conditionals.
- Use a more flexible element-wise map. The triangle needs only a strictly increasing, differentiable map with a computable inverse, whose parameters come from a network of the conditioning coordinates. The diagonal entry of the Jacobian becomes , and the log-determinant is . Neural spline flows (Durkan et al., 2019) take to be a monotone rational-quadratic spline: bins on an interval , the identity outside it. Inverting one bin means solving a quadratic equation, so both directions stay cheap.
Glow's linear layers: 1×1 convolution and ActNorm
Between couplings a flow permutes coordinates, and a permutation is a linear map with a fixed matrix. Glow (Kingma and Dhariwal, 2018) learns that matrix. On an image tensor of height , width and channels, a convolution applies the same matrix to the channel vector of every pixel. Each output pixel reads only its own input pixel, so the Jacobian is block-diagonal with copies of :
A general still costs . Glow stores already factored,
where is a fixed permutation, is lower-triangular with ones on the diagonal, is strictly upper-triangular and . The determinant of a product is the product of determinants, , , and a triangular matrix contributes the product of its diagonal. So
which costs . For example, with and we get and . The negative entry is harmless for the density, but a free could pass through zero on its way from to , and there is singular. Glow therefore learns and keeps the signs fixed.
The second linear layer is ActNorm: a per-channel scale and shift, at every pixel, with . It plays the role of batch normalization (see Batch normalization, gradients and convolutions), but its parameters do not depend on the batch. Glow trains on large images with one image per device, and statistics of a batch that small are mostly noise. ActNorm is initialized from the first minibatch so that every channel starts with zero mean and unit variance, and after that and are ordinary trainable parameters.
Flows on images: masks, squeeze and scales
Convolutional networks for and need a split into and that respects the pixel grid. RealNVP (Dinh, Sohl-Dickstein and Bengio, 2017) writes the split as a binary mask. A checkerboard mask copies the pixels whose row and column indices sum to an odd number, so every transformed pixel is conditioned on its four neighbours. A channel-wise mask copies half of the channels. Consecutive layers swap the roles of the two halves.
The squeeze trades space for channels. It cuts every channel into squares and stacks the four values of a square as four channels, turning into . It only reorders coordinates, so its log-determinant is , but after it a convolution sees twice as far.
The multi-scale architecture saves work. After each scale, half of the variables are factored out: they go straight into the latent , and only the other half continues to the next scale. For a image the first scale squeezes to , sends numbers to and continues with ; the next scales send , , and finally the last . They add up to , so no dimension is lost, yet the deep layers work on ever smaller tensors.
Glow keeps this skeleton. One step of flow is ActNorm, then a convolution, then an affine coupling whose last convolution starts at zero, so that every step starts as the identity. One scale squeezes, runs many such steps and factors out half.
Conditional flows and what comes next
A flow can be conditioned on a vector that it does not transform: the networks inside simply read as an extra input, as in and . The Jacobian is taken in the transformed variable with fixed, so it keeps the same triangle and the same log-determinant. A conditional flow is an exact density , and inside a VAE it can replace any of its three Gaussians: an IAF as a richer posterior , a MAF as a learned prior , or a flow of coupling layers as the likelihood , as in VAPNEV (see β-VAE and VQ-VAE).
| Layer | Why is cheap | One pass for | |
|---|---|---|---|
| affine coupling | triangle, copied half | both directions | |
| MAF | triangle, masks | density | |
| IAF | triangle, masks | , noise to data | sampling |
| spline coupling | triangle, monotone pieces | both directions | |
| convolution | both directions | ||
| ActNorm | diagonal | both directions | |
| squeeze, permutation | reordering | both directions |
Every row of this table is a restriction bought for an exact likelihood. And the exact number is not a judge of quality either: flows trained on CIFAR-10 give higher likelihood to never-seen SVHN house numbers (Nalisnick et al., 2019).
What if we drop the restrictions altogether? Any network with one scalar output defines a density , as long as the integral is finite. Nobody can compute that integral for images, so the density is known only up to a constant. Before such a model can be trained, we need to draw samples from a density we cannot normalize. The main line picks this up in Sampling unnormalized densities: MALA and HMC.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.