Search

Search papers and lessons

NICE: Non-linear Independent Components Estimation

Laurent Dinh, David Krueger, Yoshua Bengio · 2014

NICE (2014) learns an invertible neural transform that turns data into independent components, so its exact likelihood and its exact sampling are both easy to compute.

On this page

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

TL;DR

NICE is a 2014 paper by Laurent Dinh, David Krueger and Yoshua Bengio. It learns a model of complicated high-dimensional data by learning one invertible transform that maps each data point to a code whose parts are statistically independent. Because the transform is built so that its inverse and its volume change are cheap, the model gives an exact likelihood for each example and can generate new examples by running the transform backwards. On four image datasets it gave good generative results.

Builds on: Normalizing Flow, Bijective Function, Feedforward Neural Network · Leads to: Flow Matching for Generative Modeling, Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified...

Why it mattered

NICE was one of the early deep models that was a true flow: an invertible map with an exact change of variables, trained by maximum likelihood, with no sampling inside training. Many later flow models kept the idea of invertible blocks with a tractable Jacobian, and the coupling layer it introduced became a common building block of later flow architectures. The paper also made a practical point that became standard. A deterministic encoder whose inverse is exact avoids the approximate posterior and the reconstruction noise that variational autoencoders need. That point shaped how people compare flows with variational autoencoders and with the autoregressive models that were popular at the time.

The paper is also a clear example of the idea that a good representation is one in which the data is easy to model. The authors use that idea to make the code factorial, which is a strong and checkable structure, rather than only hoping that a learned code is useful.

History

By the early 2010s, generative modelling of high-dimensional data had two main routes. Undirected models such as deep Boltzmann machines were powerful, but their likelihood was intractable and their sampling was slow. Directed models such as variational autoencoders gave fast sampling with a lower bound on the likelihood. Independent component analysis had long asked a related question, whether data is a mixture of independent sources, but its classic form used orthogonal maps, which were costly to fit.

The paper's authors combined the change of variables rule with a deep, invertible map. The key engineering step was the coupling layer, whose Jacobian is triangular by construction. NICE appeared in 2014, in the same period as the variational autoencoder and the generative adversarial network, and it caught on because it gave exact likelihoods with a clear training objective. Later flow work, such as the flow-matching and rectified-flow papers linked below, continued this line of research.

The problem

A generative model should assign a probability to each data point, and it should be possible to draw new points from it. For image-like data the space has hundreds or thousands of dimensions, and the probability of a point is hard to write down. Three families were available in 2014, and each had a cost.

Undirected models such as deep Boltzmann machines need Markov chain sampling, and their likelihood is intractable, so it can only be estimated. Variational autoencoders train with a lower bound on the likelihood and use a noisy stochastic encoder. Autoregressive models give exact likelihoods, but they generate one dimension at a time, which is slow for images.

The question NICE asks is this: can we find a flexible transform whose inverse is cheap, whose volume change is cheap, and whose output has a simple distribution? If yes, the likelihood is exact, and sampling is one pass through the inverse.

How it works

The walkthrough below follows one data point and one training step through the model. Each step adds one idea.

flowchart LR
  X["data x (D numbers)"] --> F["encoder f: a stack of coupling layers and one scaling layer"]
  F --> H["code h = f(x), independent parts"]
  H --> P["score h under a factorial prior p_H"]
  P --> L["log-likelihood = log p_H(h) + log det Jacobian"]
  H -. "sampling: h drawn from p_H, then x = f inverse of h" .-> X
  1. Start with data and a goal. Take a data point x with D numbers, for example the pixels of a small image. We want a density p_X(x). We will not model it directly. We will model a simpler density p_H on a code h and link the two with a map h = f(x) that can be inverted.

  2. Use the change of variables rule. If f is invertible and maps D numbers to D numbers, the density of x is the density of h times how much f stretches space at x. In logarithms, log p_X(x) = log p_H(f(x)) + log |det Jacobian of f at x|. The Jacobian term is the stretch factor. It is what stops the model from cheating by squashing data into a small region.

  3. Choose a factorial prior. Pick p_H so that its D parts are independent. Then the prior term splits into a sum of one-dimensional terms, and the code is encouraged to have independent parts. A standard logistic distribution is a common choice for each part. Check: at h = 0 the logistic density is 1/4, and its log is about -1.386.

  4. Make the Jacobian easy: a coupling layer. Split the input into two blocks, x1 and x2. Keep x1 as it is and change x2 by adding a function of x1: y1 = x1 and y2 = x2 + m(x1). The function m can be any neural network. Check: the Jacobian is a block matrix with an identity block in the top-left and a triangular structure, so its determinant is 1. Check: the inverse is x2 = y2 - m(y1), which costs the same as the forward pass.

  5. Make every dimension change. One coupling layer leaves x1 unchanged, so the next layer swaps the roles of the two blocks. A composition of two layers changes every dimension. The paper observes that at least three coupling layers are needed for all dimensions to influence one another, and it generally uses four.

  6. Fix the volume problem with a scale. Every additive coupling layer has determinant 1, so their product does too. That means the model cannot change the overall volume at all. The fix is a diagonal scaling layer at the top, h_i = S_ii x_i, which adds a term log |S_ii| for each dimension. The scale lets the model give more variation to some dimensions than others.

  7. Train by maximum likelihood. Sum the terms for one data point and average over the data. The training signal is only the exact log-likelihood. No sampling is needed, and no reconstruction term appears.

  8. Sample. Draw h from the factorial prior, then compute x = f inverse of h by running the coupling layers backwards. This is ancestral sampling in the directed graph h to x, and it is one pass with no iteration.

Worked examples

Example 1 (tiny, by hand): inverting one coupling layer. Take D = 2, d = 1, and let m(x1) = 2 x1.

  1. Input x = (1, 3). Here x1 = 1 and x2 = 3.
  2. The layer keeps y1 = x1 = 1.
  3. It computes y2 = x2 + m(x1) = 3 + 2 * 1 = 5. So the output is y = (1, 5).
  4. Invert: x1 = y1 = 1.
  5. Invert: x2 = y2 - m(y1) = 5 - 2 * 1 = 3. We get back (1, 3).

The inverse used only one evaluation of m, the same cost as the forward pass. Interpretation: an additive coupling layer can be undone exactly, whatever m is.

Example 2 (standard): the log-likelihood of one point. Use the same layer, with a factorial logistic prior, log p(h) = -log(1 + e^h) - log(1 + e^{-h}) for each coordinate. The map has determinant 1.

  1. The code is h = (1, 5), from Example 1.
  2. For h_1 = 1: log(1 + e) = 1.31326 and log(1 + e^{-1}) = 0.31326, so log p(1) = -1.62652.
  3. For h_2 = 5: log(1 + e^5) = 5.00672 and log(1 + e^{-5}) = 0.00672, so log p(5) = -5.01343.
  4. Add the two prior terms: log p_H(h) = -1.62652 - 5.01343 = -6.63995.
  5. The log-determinant is log 1 = 0.
  6. So log p_X(x) = -6.63995, about -6.64.

Interpretation: the point (1, 3) lies in a region where this toy model puts low density, because h_2 = 5 is far out in the tail of the logistic prior. A better m would move h_2 toward the centre of the prior and raise the score.

Example 3 (realistic): a scaled model on images. The paper's MNIST model uses four coupling layers, each with a network of five hidden layers of 1000 rectified units, then a diagonal scale. Dequantisation adds uniform noise of size 1/256 to each pixel, and the data is rescaled to [0, 1], so that discrete pixel values become a continuous density. The paper reports a test log-likelihood of 1980.50 on MNIST.

  1. Dequantise: add noise of size 1/256 to each pixel (the MNIST setting), so the data is continuous.
  2. Rescale to [0, 1]^D, the domain of the prior.
  3. Apply four coupling layers, alternating which half of the pixels is changed, with m^(1) to m^(4) each a five-layer network.
  4. Apply the diagonal scale exp(s) on top. Its log-determinant is \sum_i s_i, which is the term the training objective adds.
  5. Score the code under the logistic prior and add the terms.
  6. Average over the training set. The paper trained with Adam, learning rate 10^{-3}, and kept the best validation model after 1500 epochs.

Try it: a coupling layer with m(x1) = -x1 takes the input x = (2, 4) to an output y. Compute y, then apply the inverse to y and check that you get (2, 4) back. Answer: m(x1) = -2, so y1 = 2 and y2 = 4 + (-2) = 2, giving y = (2, 2). The inverse keeps x1 = y1 = 2 and gives x2 = y2 - m(y1) = 2 - (-2) = 4, so (2, 4) comes back. The inverse subtracts the same quantity the forward pass added.

The math

Definition (NICE criterion). Let f be an invertible map from D numbers to D numbers with components f_d, and let p_H be a factorial prior with densities p_{H_d}. The NICE model defines the density of x as

log⁡pX(x)=∑d=1Dlog⁡pHd(fd(x))+log⁡∣det⁡∂f(x)∂x∣.\log p_X(x) = \sum_{d=1}^{D} \log p_{H_d}(f_d(x)) + \log \left| \det \frac{\partial f(x)}{\partial x} \right|.

The first sum is the prior term. The last term is the log of the stretch factor. Training maximises this quantity over the data set.

Step 2 in symbols: the change of variables

If h = f(x), the Jacobian matrix J has entries J_ij = \partial f_i / \partial x_j. The density of x is p_X(x) = p_H(f(x)) |det J|. Taking logs gives the formula above. The determinant of a triangular matrix is the product of its diagonal entries, which is why the architecture is designed around triangular Jacobians.

Step 4 in symbols: the additive coupling layer

Write x = (x1, x2) with x1 of size d and x2 of size D - d. The layer is

y1=x1,y2=x2+m(x1).y_1 = x_1, \qquad y_2 = x_2 + m(x_1).

Its Jacobian is

∂y∂x=[Id0∂y2∂x1ID−d],\frac{\partial y}{\partial x} = \begin{bmatrix} I_d & 0 \\ \frac{\partial y_2}{\partial x_1} & I_{D-d} \end{bmatrix},

a block lower-triangular matrix with ones on the diagonal. Its determinant is 1, so log |det| = 0. The inverse is x_1 = y_1 and x_2 = y_2 - m(y_1).

Step 5 in symbols: composing layers

If f = f_L \circ \cdots \circ f_1, the Jacobian of f is the product of the Jacobians of the layers, and the log-determinant is the sum of the layer log-determinants. For additive coupling layers that sum is zero. For the scale layer it is \sum_i \log |S_ii|. So the whole model's log-determinant is

log⁡∣det⁡∂f∂x∣=∑i=1Dlog⁡∣Sii∣.\log \left| \det \frac{\partial f}{\partial x} \right| = \sum_{i=1}^{D} \log |S_{ii}|.

Step 6 in symbols: the scaled criterion

With the scaling layer on top, the NICE criterion becomes

log⁡pX(x)=∑i=1D[log⁡pHi(fi(x))+log⁡∣Sii∣].\log p_X(x) = \sum_{i=1}^{D} \left[ \log p_{H_i}(f_i(x)) + \log |S_{ii}| \right].

Larger S_ii makes h_i larger, which the prior term penalises, while the determinant term log |S_ii| rewards it, so training balances the two. The paper relates these scale values to the eigenspectrum of PCA: a large S_ii marks a dimension with little variation, which is an unimportant direction.

Step 8 in symbols: sampling

Sampling draws h_d from p_{H_d} independently for each d, and returns x = f^{-1}(h). The inverse of each layer is applied in reverse order.

Results

The paper trains on four datasets: MNIST, the Toronto Face Dataset (TFD), Street View House Numbers (SVHN) and CIFAR-10. The reported test log-likelihoods are:

DatasetTest log-likelihood (paper)
MNIST1980.50
TFD5514.71
SVHN11496.55
CIFAR-105371.78

For TFD and CIFAR-10 the paper compares with the best results it knew of, from deep mixtures of factor analysers: 5250 on TFD and 3622 on CIFAR-10. The paper notes that those numbers are lower bounds, not exact log-likelihoods, so the comparison is not like-for-like. For MNIST the paper says no fair comparison is possible, because the usual evaluation of continuous MNIST models uses Parzen window estimates.

The paper also shows unbiased samples drawn from the prior and passed through the inverse, which are the visual evidence for generation. For inpainting, the model is not trained for the task. The observed pixels are clamped to their values, and the masked pixels are found by projected gradient ascent on the likelihood. The authors describe the output as reasonable in quality but note the occasional presence of spurious modes. With middle masks there is almost no information left about the digit, so the fill-in cannot recover it.

Limits. The results are log-likelihoods on each dataset's test split after its own preprocessing: uniform noise of size 1/256 with data in [0,1][0,1] for MNIST, TFD and SVHN, and noise of size 1/128 with data in [−1,1][-1,1] for CIFAR-10. Different preprocessing gives different log-likelihoods, so the numbers are not directly comparable across datasets. The paper selects the best model by validation log-likelihood after 1500 epochs. The paper's main claim is that the architecture gives tractable likelihood and sampling with competitive results, and that is what the numbers support.

Common mistakes and tradeoffs

It is tempting to think that a good likelihood means the model has learned a good code. In fact the likelihood can rise by squashing data into a small region, which is what the Jacobian term prevents, because it penalises contraction. Only the combination of the prior term and the Jacobian term gives a meaningful score.

It is tempting to think that a coupling layer with a deep network inside is slow to invert. In fact the inverse of an additive coupling layer costs the same as its forward pass, because m is only evaluated, never inverted.

It is tempting to think that one coupling layer is enough. In fact one layer leaves one block unchanged, and at least three are needed for all dimensions to influence one another.

It is tempting to think that the scale layer is optional. In fact without a scale, every additive coupling layer has unit determinant, so the model cannot change volume, and the likelihood cannot reflect how much variation each dimension has.

Tradeoffs. NICE gives an exact likelihood and one-pass sampling, and its training is simple. Its layers must be invertible, which limits the design, and each layer keeps half the input unchanged. Compared with autoregressive models, which generate one dimension at a time, NICE samples in one pass through the inverse. Use a flow like this when exact likelihood and fast sampling matter and the data is continuous. Do not use it when the data has a very different dimension at the code level, or when a discrete model fits better.

Recap

  • A change of variables turns a simple density into a complicated one, and the stretch factor keeps the density honest.
  • An additive coupling layer keeps one block fixed and shifts the other by a neural function, so its inverse and its determinant are both trivial.
  • At least three coupling layers, usually four, are needed so every dimension can influence every other.
  • A diagonal scale restores the volume the coupling layers cannot change, and the factorial prior encourages independent code parts.
  • Training maximises the exact log-likelihood, and sampling runs the inverse once.

Read next: Flow Matching for Generative Modeling and Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified... for later flows built on the same invertible-map idea.

Reading guide

Check yourself

QuestionWhy does an additive coupling layer have a Jacobian determinant of 1?Show answer
The Jacobian is block lower-triangular, with an identity block in the top-left and an identity block on the diagonal of the lower-right. The determinant of a triangular matrix is the product of its diagonal entries, which are all 1, so the determinant is 1.
QuestionA coupling layer has m(x1) = 3 x1. The input is x = (2, 1). Compute the output y and check the inverse.Show answer
Here x1 = 2, so y1 = 2 and m(x1) = 6. Then y2 = x2 + m(x1) = 1 + 6 = 7, so y = (2, 7). The inverse gives x1 = 2 and x2 = y2 - m(y1) = 7 - 6 = 1, which returns (2, 1).
QuestionWhy does the NICE model need a diagonal scaling layer, when the coupling layers already give a flexible map?Show answer
Each additive coupling layer has determinant 1, so their composition has determinant 1 too. The model then cannot change volume. The scaling layer adds a log-determinant term sum of log |S_ii|, so the likelihood can reflect how much variation each dimension has.
QuestionA factorial logistic prior has log density -log(1 + e^h) - log(1 + e^{-h}) per coordinate. A code is h = (0, 0) and the Jacobian determinant is 1. What is the log-likelihood of x?Show answer
At h = 0, each coordinate gives -log 2 - log 2 = -2 log 2 which is about -1.386. Two coordinates give -4 log 2, about -2.773. The log-determinant is 0, so log p_X(x) is about -2.773.
QuestionA data set of images is dequantised by adding uniform noise of size 1/256 to each pixel. Why is this step needed before maximum likelihood?Show answer
Pixels take discrete values, so a continuous density assigns them zero probability mass unless the values are spread out. Adding noise of size 1/256 turns the discrete data into a continuous distribution, so the change of variables formula and the density can be used directly.

Sources

Read the full paper

Laurent Dinh, David Krueger, Yoshua Bengio, 2014. arXiv:1410.8516