§ 10. Flow Models¶
Probabilistic Generative Models¶
Definition 10.1 : Probabilistic generative models
A probabilistic generative model learns a distribution \(p_{\theta}\) from \(X_{1}, \ldots, X_{N} \sim p_{\text {true }}\) such that \(p_{\theta} \approx p_{\text {true }}\) and such that we can generate new samples \(X \sim p_{\theta}\).
The ability to generate new synthetic data is interesting, but by itself not very useful. (Generating fake images to use in fake social media accounts is the only direct application that I can think of.)
The structure of the data learned through the unsupervised learning is of higher value. However, we won't talk about the downstream applications in this course.
In this class, we will talk about flow models, VAEs, and GANs.
Fit a probability density function \(p_{\theta}(x)\) with continuous data \(X_{1}, \ldots, X_{N} \sim p_{\text {true }}(x)\).
- We want to fit the data \(X_{1}, \ldots, X_{N}\) (or really the underlying distribution \(p_{\text {true}}\)) well.
- We want to be able to sample from \(p_{\theta}\).
- (We want to get a good latent representation.)
We first develop the mathematical discussion with 1D flows, and then generalize the discussion to high dimensions.
Concept 10.2 : Example Density Model
Parameters: means and variances of components, mixture weights
Problems with GMM:
- Highly non-convex optimization problem. Can easily get stuck in local minima.
- It is does not have the representation power to express high-dimensional data.

GMM doesn't work with high-dimensional data. The sampling process is:
- Pick a cluster center
- Add Gaussian noise
If this is done with natural images, a realistic image can be generated only if it is a cluster center, i.e., the clusters must already be realistic images.

So then how do we fit a general (complex) density model?
1D Flow Models¶
A random variable \(X\) is continuous if there exists a probability density function (PDF) \(p_{X}(x) \geq 0\) such that
In this case, we write \(X \sim p_{X}\).

The cumulative distribution function (CDF) of \(X\) is defined as
- \(F_{X}(t)\) is a nondecreasing function.
- \(F_{X}(t)\) is a continuous function if \(X\) is a continuous random variable.

Naïve approach for fitting a density model. Represent \(p_{\theta}(x)\) with DNN.

There are some challenges:
-
How to ensure proper distribution?
\[ \int_{-\infty}^{+\infty} p_{\theta}(x) d x=1, \quad p_{\theta}(x) \geq 0, \quad x \in \mathbb{R} \] -
How to sample?
Normalization of \(p_{\theta}\)
For discrete random variables, one can use the soft-max function \(\mu: \mathbb{R}^{k} \rightarrow \mathbb{R}^{k}\) defined as
to normalize probabilities.
For continuous random variables, we can ensure \(p_{\theta} \geq 0\) with \(p_{\theta}(x)=e^{f_{\theta}(x)}\), where \(f_{\theta}\) is the output of the neural network. However, ensuring the normalization
is not a simple matter. (Any Bayesian statistician can tell you how difficult this is.)
What happens if we ignore normalization?
Do we really need this normalization thing? Yes, we do.
Without normalization, one can just assign arbitrarily large probabilities everywhere when we perform maximum likelihood estimation:
The solution is to set \(p_{\theta}(x)=M\) with \(M \rightarrow \infty\).
We want model to place large probability on data \(X_{1}, \ldots, X_{N}\) while placing small probability elsewhere. Normalization forces model to place small probability where data doesn't reside.
Key insight of normalizing flow: DNN outputs random variable \(Z\), rather than \(p_{\theta}(X)\). We choose \( Z \) from a known distribution that is easy to sample from, such as \( \mathcal{N}(0,1) \) or \( \operatorname{Uniform}([0,1]) \).

In normalizing flow, find \(\theta\) such that the flow \(f_{\theta}\) normalizes the random variable \(X \sim p_{X}\) into \(Z \sim \mathcal{N}(0,1)\). Generally, we can consider \(Z \sim pZ\). The choice of \(pZ\), however, does not seem to make a significant difference.
Important questions to resolve:
- How to train? (How to evaluate \(p_{\theta}(x)\) ? DNN outputs \(f_{\theta}\), not \(p_{\theta}\).) (Concept 10.7)
- How to sample \(X\) ? (Concept 10.8)
Assume \(f\) is invertible, \(f\) is differentiable, and \(f^{-1}\) is differentiable.
If \(X \sim p_{X}\), then \(Z=f(X)\) has pdf
If \(Z \sim p_{Z}\), then \(X=f^{-1}(Z)\) has pdf
Since \(Z=f(X)\), one might think \(p_{X}(x)=p_{Z}(z)=p_{Z}(f(x))\). \(\leftarrow\) This is wrong.
Invertibility of \(f\) is essential; it is not a minor technical issue.
Definition 10.7 : Traning Flow Models
Train model with MLE
where \(f_{\theta}\) is invertible and differentiable, and \(X=f_{\theta}^{-1}(Z)\) with \(Z \sim p_{Z}\) so
Can optimize with SGD, if we know how to perform backprop on \(\left|\frac{\partial f_{\theta}}{\partial x}\left(X_{i}\right)\right|\). More on this later.
Definition 10.8 : Sampling from Flow Models

- Sample \(Z \sim p_{Z}\)
- Compute \(X=f_{\theta}^{-1}(Z)\)
Concept 10.9 : Requirements of Flow \(f_{\theta}\)
Theoretical requirement:
- \(f_{\theta}(x)\) invertible and differentiable.
Computational requirements:
- \(f_{\theta}(x)\) and \(\nabla_{\theta} f_{\theta}(x)\) efficient to evaluate (for training)
- \(\left|\frac{\partial f_{\theta}}{\partial x}(x)\right|\) and \(\nabla_{\theta}\left|\frac{\partial f_{\theta}}{\partial x}(x)\right|\) efficient to evaluate (for training)
- \(f_{\theta}^{-1}\) efficient to evaluate (for sampling)
Example 10.10 : Example of Trained Flow Models
- Flow to \(Z\) ~ \(\operatorname{Uniform}([0,1])\)

- Flow to \(Z \sim \operatorname{Beta}(5,5)\)

- Flow to \(Z \sim \mathcal{N}(0,1)\)

Concept 10.11 : Universality of Flows
Are flows universal, i.e., can \(f_{\theta}^{-1}(Z) \sim p_{X}\) for any \(X\) provided that \(f_{\theta}\) can represent any invertible function?
Yes, 1D flows are universal due to the inverse CDF sampling technique. (Some basic conditions are being omitted.)
Higher dimensional flows are also universal as shown by Huang et al.\(^{\star}\) or earlier by the general theory of optimal transport. (link)
\(^{\star}\) C.-W. Huang, D. Krueger, A. Lacoste, and A. Courville, Neural Autoregressive Flows, ICML, 2018.
Inverse CDF sampling is a technique for sampling \(X \sim p_{X}\). If \(F_{X}(t)\) is furthermore a strictly increasing function, then \(F_{X}\) is invertible, i.e., \(F_{X}^{-1}\) exists.
Generate a random number \(U \sim \operatorname{Uniform}([0,1])\) and compute \(F_{X}^{-1}(U)\). Then
since
Technique can be generalized to when \(F_{X}\) is not invertible.
Inverse CDF can be seen as flow model from \(X \sim p_X\) to \(U \sim \operatorname{Uniform}([0,1])\). As seen above, the flow is \(F_X\), so when we do not know the true distribution \(p_X\), we can train the flow model to learn \(F_{X}^{-1}(U) \sim p_{X}\).
Concept 10.13 : Universality of 1D Flows
Composition of flows is a flow, and inverse of a flow is a flow.
Universality of 1D flows:
- Use inverse CDF as flow to transform \(X \sim p_{X}\) into \(U \sim \operatorname{Uniform}([0,1])\) and \(Z \sim \mathcal{N}(0,1)\) into \(U \sim \operatorname{Uniform}([0,1])\).
- Compose flow \(X \rightarrow U\) and inverse flow \(U \rightarrow Z\).

High Dimensional Flow Models¶
Let \(f: \mathbb{R}^{n} \rightarrow \mathbb{R}^{n}\), such that
The Jacobian matrix is
The Jacobian determinant is \(\operatorname{det}\left(\frac{\partial f}{\partial x}\right)\). We use the notation
where the second \(|\cdot|\) is the absolute value of the determinant. (This notation is not completely standard.)
Let \(f: \mathbb{R}^{n} \rightarrow \mathbb{R}^{n}\) be an invertible function such that both \(f\) and \(f^{-1}\) are differentiable. Let \(U \subseteq \mathbb{R}^{n}\). Then
for any \(h: \mathbb{R}^{n} \rightarrow \mathbb{R}\). (Change of variable from \(v=f(u)\) to \(u=f^{-1}(v)\).)
(The conditions for this change of variable formula can be further generalized.)
A multivariate random variable \(X \in \mathbb{R}^{n}\) is continuous if there exists a probability density function \(p_{X}(x)\) such that
where the integral is over the volume \(A \subseteq \mathbb{R}^{n}\). In this case, we write \(X \sim p_{X}\).
The joint cumulative distribution function (the copula) does not seem to be useful in the context of high-dimensional flow models.
Let \(f: \mathbb{R}^{n} \rightarrow \mathbb{R}^{n}\) be an invertible function such that both \(f\) and \(f^{-1}\) are differentiable. Let \(X\) be a continuous random variable with probability density function \(p_{X}\) and let \(Y=f(X)\) have density \(p_{Y}\). Then
Proof
Invertibility of \(f\) is essential; it is not a minor technical issue.
Fact: Determinant definitions in undergraduate linear algebra textbooks require exponentially many operations to compute:
Efficient computation of determinant for general matrices and performing backprop through the computation is difficult. Therefore, high-dimensional flow model are designed to compute determinants only on simple matrices.
- Product formula: if \(A\) and \(B\) are square, then
- Block lower triangular formula: if \(A \in \mathbb{R}^{n \times n}\) and \(C \in \mathbb{R}^{m \times m}\), then
- Lower triangular formula: if \(a_{1}, \ldots, a_{n} \in \mathbb{R}\) and \(*\) represents arbitrary values, then
- Upper triangular formula: same as for lower triangular matrices.
Definition 10.19 : Training High Dimensional Flow Models
Train model with MLE
where \(f_{\theta}(z)\) is invertible and differentiable, and \(X=f^{-1}(Z)\) with \(Z \sim p_{Z}\) so
(Exactly the same formula as with 1D flow.)
Can optimize with SGD, if we know how to perform backprop on \(\left|\frac{\partial f_{\theta}}{\partial x}\left(X_{i}\right)\right|\).
Coupling Flows¶
Concept 10.20 : Composing Flows
Flows can be composed to increase expressiveness. (Deep NN more expressive.) Consider composition of \(k\) flows
Determinant computation splits nicely due to chain rule and product formula
Definition 10.21 : Affine Flows
An affine (linear) transformation
is a flow if matrix \(A\) is invertible. Then
and
Sampling: \(X=A Z+b\), where \(Z \sim \mathcal{N}(0, I)\).
Problem with affine flows:
- Computing \(|\operatorname{det}(A)|\) is expensive and performing backprop over it is difficult. We want \(\frac{\partial f_{A, b}}{\partial x}\) to be further structured so that determinant is easy to compute.
- One affine flow is insufficient to generate complex data. However, composing multiple affine flows yields an affine flow and therefore is pointless. We need to introduce nonlinearities.
Definition 10.22 : Coupling Flows
A coupling flow is a general and practical approach for constructing non-linear flows.
Partition input into two disjoint subsets \(x=\left(x^{A}, x^{B}\right)\). Then
where \(\psi_{\theta}\) is a neural network and \(\hat{f}\left(x^{B} \mid \psi_{\theta}\left(x^{A}\right)\right)\) is another flow whose parameters depend on \(x^{A}\).
Concept 10.24 : Jacobian of Coupling Flows
The Jacobian of a coupling flow has a nice block structure
which leads to the simplified determinant formula
Note \(\frac{\partial \hat{f}}{\partial x^{A}}\left(x^{B} \mid \psi_{\theta}\left(x^{A}\right)\right)\), which will be very complicated, does not appear in the determinant.
Definition 10.25 : Coupling transformation \(\hat{f}(x \mid \psi)\)
-
Additive transformations (NICE)
\[ \hat{f}(x \mid \psi)=x+t \]where \(\psi=t\).
-
Affine transformations (Real NVP)
\[ \hat{f}(x \mid \psi)=e^{s} \odot x+t \]where \(\psi=(s, t)\).
Other transformations studied throughout the literature.
Definition 10.26 : NICE (Non-linear Independent Components Estimation)
NICE uses additive coupling layers: Split variables in half: \(x_{1: n / 2}, x_{n / 2: n}\)
Easily invertible:
Jacobian determinant is easy to compute:
(L. Dinh, D. Krueger, and Y. Bengio, NICE: Non-linear independent components estimation, ICLR Workshop, 2015.)
Definition 10.27 : Real NVP (Real-valued Non-Volume Preserving)
Real NVP uses affine coupling layers:
Easily invertible:
Jacobian determinant is easy to compute:
(L. Dinh, J. Sohl-Dickstein, and S. Bengio, Density estimation using Real NVP, ICLR, 2017.)
- Results of Real NVP

Concept 10.28 : How to partition variables?
Note that the additive and affine coupling layers of NICE and Real NVP are nonlinear mappings from \(x_{1: n}\) to \(z_{1: n}\), since \(s_{\theta}\left(x_{1: n / 2}\right)\) and \(t_{\theta}\left(x_{1: n / 2}\right)\) are nonlinear.
Flow models compose multiple nonlinear flows. But if \(x_{1: n / 2}\) is always unchanged, then the full composition will leave it unchanged. Therefore, we change the partitioning for every coupling layer.
Concept 10.29 : Real NVP Variable Partitioning
Two partition strategies:
- Partition with checkerboard pattern.
- Reshape tensor and then partition channelwise.

(L. Dinh, J. Sohl-Dickstein, and S. Bengio, Density estimation using Real NVP, ICLR, 2017.)
Definition 10.30 : Real NVP Architecture

Input \(X\) : \(c \times 32 \times 32\) image with \(c=3\)
Layer 1: Input \(X: c \times 32 \times 32\)
- Checkerboard \(\times 3\), channel reshape into \(4 c \times 16 \times 16\), channel \(\times 3\)
- Output: Split result to get \(X_{1}: 2 c \times 16 \times 16\) and \(Z_{1}: 2 c \times 16 \times 16\) (fine-grained latents)
Layer 2: Input \(X_{1}: 2 c \times 16 \times 16\) from layer 1
- Checkerboard \(\times 3\), channel reshape into \(8 c \times 8 \times 8\), channel \(\times 3\)
- Split result to get \(X_{2}: 4 c \times 8 \times 8\) and \(Z_{2}: 4 c \times 8 \times 8\) (coarser latents)
Layer 3: Input \(X_{2}: 4 c \times 8 \times 8\) from layer 2
- Checkerboard \(\times 3\), channel reshape into \(16 c \times 4 \times 4\), channel \(\times 3\)
- Get \(Z_{3}: 16 c \times 4 \times 4\) (latents for highest-level details)
Output \(Z = (Z_1, Z_2, Z_3) \in \mathbb{R}^{c \dot 32^2}\)
Concept 10.31 : Batch Normalization in Deep Flows
To train deep flows, BN is helpful. However, the large model size forces the use of small batch sizes, and BN is not robust with small batch sizes. RealNVP uses a modified form of BN
(No \(\beta\) and \(\gamma\) parameters.) This layer has the log Jacobian determinant
The mean and variance parameters are updated with
where \(\rho\) is the momentum. During gradient computation, only backprop through the current batch statistics \(\hat{\mu}_{k}\) and \(\hat{\sigma}_{k}^{2}\).
Concept 10.32 : \(s_{\theta}\) and \(t_{\theta}\) networks
The \(s_{\theta}\) and \(t_{\theta}\) do not need to be invertible. The original RealNVP paper does not describe its construction.
We let \(\left(s_{\theta}, t_{\theta}\right)\) be a deep (20-layer) convolutional neural network using residual connections and standard batch normalization.
Researches¶
The authors of the Glow paper also released a blog post. link
(D. P. Kingma and P. Dhariwal, Glow: Generative flow with invertible 1x1 convolutions, NeurIPS, 2018.)
Instead of a discrete composition of flows, what if we have a continuous-time flow?
Inverse:
(R. T. Q. Chen, Y. Rubanova, J. Bettencourt, and D. Duvenaud, Neural ordinary differential equations, NeurIPS, 2018.
W. Grathwohl, R. T. Q. Chen, J. Bettencourt, I. Sutskever, and D. Duvenaud, FFJORD: Free-form continuous dynamics for scalable reversible generative models, ICLR, 2019.)

