A flow-based generative model is a generative model used in machine learning that explicitly models a probability distribution by leveraging normalizing flow, which is a statistical method using the change-of-variable law of probabilities to transform a simple distribution into a complex one. The direct modeling of likelihood provides many advantages. For example, the negative log-likelihood can be directly computed and minimized as the loss function. Additionally, novel samples can be generated by sampling from the initial distribution, and applying the flow transformation. In contrast, many alternative generative modeling methods, such as variational autoencoders (VAEs), generative adversarial networks (GANs), or diffusion models, do not explicitly represent the likelihood function.
Method
Let z0 be a (possibly multivariate) random variable with distribution p0(z0). For i=1,...,K, let zi=fi(zi−1) be a sequence of random variables transformed from z0. The functions f1,...,fK should be invertible, i.e. the inverse function fi−1 exists. The final output zK models the target distribution. The log likelihood of zK is (see derivation):
logpK(zK)=logp0(z0)−∑i=1Klog|detdfi(zi−1)dzi−1| Learning probability distributions by differentiating such log Jacobians originated in the Infomax (maximum likelihood) approach to ICA, which forms a single-layer (K=1) flow-based model. Relatedly, the single layer precursor of conditional generative flows appeared in. To efficiently compute the log likelihood, the functions f1,...,fK should be easily invertible, and the determinants of their Jacobians should be simple to compute. In practice, the functions f1,...,fK are modeled using deep neural networks, and are trained to minimize the negative log-likelihood of data samples from the target distribution. These architectures are usually designed such that only the forward pass of the neural network is required in both the inverse and the Jacobian determinant calculations. Examples of such architectures include NICE, RealNVP, and Glow.
Derivation of log likelihood Consider z1 and z0. Note that z0=f1−1(z1). By the change of variable formula, the distribution of z1 is:
p1(z1)=p0(z0)|detdf1−1(z1)dz1| Where detdf1−1(z1)dz1 is the determinant of the Jacobian matrix of f1−1. By the inverse function theorem:
p1(z1)=p0(z0)|det(df1(z0)dz0)−1| By the identity det(A−1)=det(A)−1 (where A is an invertible matrix), we have:
p1(z1)=p0(z0)|detdf1(z0)dz0|−1 The log likelihood is thus:
logp1(z1)=logp0(z0)−log|detdf1(z0)dz0| In general, the above applies to any zi and zi−1. Since logpi(zi) is equal to logpi−1(zi−1) subtracted by a non-recursive term, we can infer by induction that:
logpK(zK)=logp0(z0)−∑i=1Klog|detdfi(zi−1)dzi−1|
Training method As is generally done when training a deep learning model, the goal with normalizing flows is to minimize the Kullback–Leibler divergence between the model's likelihood and the target distribution to be estimated. Denoting pθ the model's likelihood and p∗ the target distribution to learn, the (forward) KL-divergence is:
DKL[p∗(x)‖pθ(x)]=−𝔼p∗(x)[logpθ(x)]+𝔼p∗(x)[logp∗(x)] The second term on the right-hand side of the equation corresponds to the entropy of the target distribution and is independent of the parameter θ we want the model to learn, which only leaves the expectation of the negative log-likelihood to minimize under the target distribution. This intractable term can be approximated with a Monte-Carlo method by importance sampling. Indeed, if we have a dataset {xi}i=1N of samples each independently drawn from the target distribution p∗(x), then this term can be estimated as:
−𝔼̂p∗(x)[logpθ(x)]=−1N∑i=0Nlogpθ(xi) Therefore, the learning objective
argminθ DKL[p∗(x)‖pθ(x)] is replaced by
argmaxθ ∑i=0Nlogpθ(xi) In other words, minimizing the Kullback–Leibler divergence between the model's likelihood and the target distribution is equivalent to maximizing the model likelihood under observed samples of the target distribution. A pseudocode for training normalizing flows is as follows:
INPUT. dataset x1:n, normalizing flow model fθ(⋅),p0. SOLVE. maxθ∑jlogpθ(xj) by gradient descent RETURN. θ̂
Variants
Planar Flow The earliest example. Fix some activation function h, and let θ=(u,w,b) with the appropriate dimensions, thenx=fθ(z)=z+uh(⟨w,z⟩+b) The inverse fθ−1 has no closed-form solution in general. The Jacobian is |det(I+h′(⟨w,z⟩+b)uwT)|=|1+h′(⟨w,z⟩+b)⟨u,w⟩|. For it to be invertible everywhere, it must be nonzero everywhere. For example, h=tanh and ⟨u,w⟩>−1 satisfies the requirement.
Nonlinear Independent Components Estimation (NICE) Let x,z∈ℝ2n be even-dimensional, and split them in the middle. Then the normalizing flow functions arex=[x1x2]=fθ(z)=[z1z2]+[0mθ(z1)] where mθ is any neural network with weights θ. fθ−1 is just z1=x1,z2=x2−mθ(x1), and the Jacobian is just 1, that is, the flow is volume-preserving. When n=1, this is seen as a curvy shearing along the x2 direction.
Real Non-Volume Preserving (Real NVP) The Real Non-Volume Preserving model generalizes NICE model by:x=[x1x2]=fθ(z)=[z1esθ(z1)⊙z2]+[0mθ(z1)] Its inverse is z1=x1,z2=e−sθ(x1)⊙(x2−mθ(x1)), and its Jacobian is ∏i=1nesθ(z1,). The NICE model is recovered by setting sθ=0. Since the Real NVP map keeps the first and second halves of the vector x separate, it's usually required to add a permutation (x1,x2)↦(x2,x1) after every Real NVP layer.
Generative Flow (Glow) In generative flow model, each layer has 3 parts:
channel-wise affine transformycij=sc(xcij+bc) with Jacobian ∏cscHW. invertible 1x1 convolutionzcij=∑c′Kcc′ycij with Jacobian det(K)HW. Here K is any invertible matrix. Real NVP, with Jacobian as described in Real NVP. The idea of using the invertible 1x1 convolution is to permute all layers in general, instead of merely permuting the first and second half, as in Real NVP.
… excerpt ends here. Continue reading the full article.

![Flow-based generative model: For the 1-simplex (blue) embedded in ℝ2, when we pull back Lebesgue measure from tangent space (parallel to the simplex), via the embedding p1↦(p1,1−p1), with Jacobian 𝐄=[1−1]′, a scaling factor of 𝐄′𝐄=2 results.](https://thumb.wikimedia.org/wikipedia/commons/thumb/9/9a/Simplex_measure_pullback.svg/500px-Simplex_measure_pullback.svg.png?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)