Английская Википедия:Flow-based generative model
A flow-based generative model is a generative model used in machine learning that explicitly models a probability distribution by leveraging normalizing flow,[1][2] 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 autoencoder (VAE) and generative adversarial network do not explicitly represent the likelihood function.
Method
Let <math>z_0</math> be a (possibly multivariate) random variable with distribution <math>p_0(z_0)</math>.
For <math>i = 1, ..., K</math>, let <math>z_i = f_i(z_{i-1})</math> be a sequence of random variables transformed from <math>z_0</math>. The functions <math>f_1, ..., f_K</math> should be invertible, i.e. the inverse function <math>f^{-1}_i</math> exists. The final output <math>z_K</math> models the target distribution.
The log likelihood of <math>z_K</math> is (see derivation):
- <math>\log p_K(z_K) = \log p_0(z_0) - \sum_{i=1}^{K} \log \left|\det \frac{df_i(z_{i-1})}{dz_{i-1}}\right|</math>
To efficiently compute the log likelihood, the functions <math>f_1, ..., f_K</math> should be 1. easy to invert, and 2. easy to compute the determinant of its Jacobian. In practice, the functions <math>f_1, ..., f_K</math> 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,[3] RealNVP,[4] and Glow.[5]
Derivation of log likelihood
Consider <math>z_1</math> and <math>z_0</math>. Note that <math>z_0 = f^{-1}_1(z_1)</math>.
By the change of variable formula, the distribution of <math>z_1</math> is:
- <math>p_1(z_1) = p_0(z_0)\left|\det \frac{df_1^{-1}(z_1)}{dz_1}\right|</math>
Where <math>\det \frac{df_1^{-1}(z_1)}{dz_1}</math> is the determinant of the Jacobian matrix of <math>f^{-1}_1</math>.
By the inverse function theorem:
- <math>p_1(z_1) = p_0(z_0)\left|\det \left(\frac{df_1(z_0)}{dz_0}\right)^{-1}\right|</math>
By the identity <math>\det(A^{-1}) = \det(A)^{-1}</math> (where <math>A</math> is an invertible matrix), we have:
- <math>p_1(z_1) = p_0(z_0)\left|\det \frac{df_1(z_0)}{dz_0}\right|^{-1}</math>
The log likelihood is thus:
- <math>\log p_1(z_1) = \log p_0(z_0) - \log \left|\det \frac{df_1(z_0)}{dz_0}\right|</math>
In general, the above applies to any <math>z_i</math> and <math>z_{i-1}</math>. Since <math>\log p_i(z_i)</math> is equal to <math>\log p_{i-1}(z_{i-1})</math> subtracted by a non-recursive term, we can infer by induction that:
- <math>\log p_K(z_K) = \log p_0(z_0) - \sum_{i=1}^{K} \log \left|\det \frac{df_i(z_{i-1})}{dz_{i-1}}\right|</math>
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 <math>p_\theta</math> the model's likelihood and <math>p^*</math> the target distribution to learn, the (forward) KL-divergence is:
- <math>D_{KL}[p^{*}(x)||p_{\theta}(x)] = -\mathbb{E}_{p^{*}(x)}[\log(p_{\theta}(x))] + \mathbb{E}_{p^{*}(x)}[\log(p^{*}(x))]</math>
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 <math>\theta</math> 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 <math>\{x_{i}\}_{i=1:N}</math> of samples each independently drawn from the target distribution <math>p^*(x)</math>, then this term can be estimated as:
- <math>-\hat{\mathbb{E}}_{p^{*}(x)}[\log(p_{\theta}(x))] = -\frac{1}{N} \sum_{i=0}^{N} \log(p_{\theta}(x_{i})) </math>
Therefore, the learning objective
- <math>\underset{\theta}{\operatorname{arg\,min}}\ D_{KL}[p^{*}(x)||p_{\theta}(x)]</math>
is replaced by
- <math>\underset{\theta}{\operatorname{arg\,max}}\ \sum_{i=0}^{N} \log(p_{\theta}(x_{i}))</math>
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.[6]
A pseudocode for training normalizing flows is as follows:[7]
- INPUT. dataset <math>x_{1:n}</math>, normalizing flow model <math>f_\theta(\cdot), p_0 </math>.
- SOLVE. <math>\max_\theta \sum_j \ln p_\theta(x_j)</math> by gradient descent
- RETURN. <math>\hat\theta</math>
Variants
Planar Flow
The earliest example.[8] Fix some activation function <math>h</math>, and let <math>\theta = (u, w, b)</math> with the appropriate dimensions, then<math display="block">x = f_\theta(z) = z + u h(\langle w, z \rangle + b)</math>The inverse <math>f_\theta^{-1}</math> has no closed-form solution in general.
The Jacobian is <math>|\det (I + h'(\langle w, z \rangle + b) uw^T)| = |1 + h'(\langle w, z \rangle + b) \langle u, w\rangle|</math>.
For it to be invertible everywhere, it must be nonzero everywhere. For example, <math>h = \tanh</math> and <math>\langle u, w \rangle > -1</math> satisfies the requirement.
Nonlinear Independent Components Estimation (NICE)
Let <math>x, z\in \R^{2n}</math> be even-dimensional, and split them in the middle.[3] Then the normalizing flow functions are<math display="block">x = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}= f_\theta(z) = \begin{bmatrix} z_1 \\z_2 \end{bmatrix} + \begin{bmatrix} 0 \\ m_\theta(z_1) \end{bmatrix}</math>where <math>m_\theta</math> is any neural network with weights <math>\theta</math>.
<math>f_\theta^{-1}</math> is just <math>z_1 = x_1, z_2 = x_2 - m_\theta(x_1)</math>, and the Jacobian is just 1, that is, the flow is volume-preserving.
When <math>n=1</math>, this is seen as a curvy shearing along the <math>x_2</math> direction.
Real Non-Volume Preserving (Real NVP)
The Real Non-Volume Preserving model generalizes NICE model by:[4]<math display="block">x = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}= f_\theta(z) = \begin{bmatrix} z_1 \\ e^{s_\theta(z_1)} \odot z_2 \end{bmatrix} + \begin{bmatrix} 0 \\ m_\theta(z_1) \end{bmatrix}</math>
Its inverse is <math>z_1 = x_1, z_2 = e^{-s_\theta (x_1)}\odot (x_2 - m_\theta (x_1))</math>, and its Jacobian is <math>\prod^n_{i=1} e^{s_\theta(z_{1, })}</math>. The NICE model is recovered by setting <math>s_\theta = 0</math>. Since the Real NVP map keeps the first and second halves of the vector <math>x</math> separate, it's usually required to add a permutation <math>(x_1, x_2) \mapsto (x_2, x_1)</math> after every Real NVP layer.
Generative Flow (Glow)
In generative flow model,[5] each layer has 3 parts:
- channel-wise affine transform<math display="block">y_{cij} = s_c(x_{cij} + b_c)</math>with Jacobian <math>\prod_c s_c^{HW}</math>.
- invertible 1x1 convolution<math display="block">z_{cij} = \sum_{c'} K_{cc'} y_{cij}</math>with Jacobian <math>\det(K)^{HW}</math>. Here <math>K</math> 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.
Masked autoregressive flow (MAF)
An autoregressive model of a distribution on <math>\R^n</math> is defined as the following stochastic process:[9]
<math display="block">\begin{align} x_1 \sim& N(\mu_1, \sigma_1^2)\\ x_2 \sim& N(\mu_2(x_1), \sigma_2(x_1)^2)\\ &\cdots \\ x_n \sim& N(\mu_n(x_{1:n-1}), \sigma_n(x_{1:n-1})^2)\\ \end{align}</math>where <math>\mu_i: \R^{i-1} \to \R</math> and <math>\sigma_i: \R^{i-1} \to (0, \infty)</math> are fixed functions that define the autoregressive model.
By the reparametrization trick, the autoregressive model is generalized to a normalizing flow:<math display="block">\begin{align} x_1 =& \mu_1 + \sigma_1 z_1\\ x_2 =& \mu_2(x_1) + \sigma_2(x_1) z_2\\ &\cdots \\ x_n =& \mu_n(x_{1:n-1}) + \sigma_n(x_{1:n-1}) z_n\\ \end{align}</math>The autoregressive model is recovered by setting <math>z \sim N(0, I_{n})</math>.
The forward mapping is slow (because it's sequential), but the backward mapping is fast (because it's parallel).
The Jacobian matrix is lower-diagonal, so the Jacobian is <math>\sigma_1 \sigma_2(x_1)\cdots \sigma_n(x_{1:n-1})</math>.
Reversing the two maps <math>f_\theta</math> and <math>f_\theta^{-1}</math> of MAF results in Inverse Autoregressive Flow (IAF), which has fast forward mapping and slow backward mapping.[10]
Continuous Normalizing Flow (CNF)
Instead of constructing flow by function composition, another approach is to formulate the flow as a continuous-time dynamic.[11][12] Let <math>z_0</math> be the latent variable with distribution <math>p(z_0)</math>. Map this latent variable to data space with the following flow function:
- <math>x = F(z_0) = z_T = z_0 + \int_0^T f(z_t, t) dt</math>
where <math>f</math> is an arbitrary function and can be modeled with e.g. neural networks.
The inverse function is then naturally:[11]
- <math>z_0 = F^{-1}(x) = z_T + \int_T^0 f(z_t, t) dt = z_T - \int_0^T f(z_t,t) dt </math>
And the log-likelihood of <math>x</math> can be found as:[11]
- <math>\log(p(x)) = \log(p(z_0)) - \int_0^T \text{Tr}\left[\frac{\partial f}{\partial z_t} \right] dt</math>
Since the trace depends only on the diagonal of the Jacobian <math>\partial_{z_t} f</math>, this allows "free-form" Jacobian.[13] Here, "free-form" means that there is no restriction on the Jacobian's form. It is contrasted with previous discrete models of normalizing flow, where the Jacobian is carefully designed to be only upper- or lower-diagonal, so that the Jacobian can be evaluated efficiently.
The trace can be estimated by "Hutchinson's trick":[14][15]
Given any matrix <math>W\in \R^{n\times n}</math>, and any random <math>u\in \R^n</math> with <math>E[uu^T] = I</math>, we have <math>E[u^T W u] = tr(W)</math>. (Proof: expand the expectation directly.)
Usually, the random vector is sampled from <math>N(0, I)</math> (normal distribution) or <math>\{\pm n^{-1/2}\}^n</math> (Radamacher distribution).
When <math>f</math> is implemented as a neural network, neural ODE methods[16] would be needed. Indeed, CNF was first proposed in the same paper that proposed neural ODE.
There are two main deficiencies of CNF, one is that a continuous flow must be a homeomorphism, thus preserve orientation and ambient isotopy (for example, it's impossible to flip a left-hand to a right-hand by continuous deforming of space, and it's impossible to turn a sphere inside out, or undo a knot), and the other is that the learned flow <math>f</math> might be ill-behaved, due to degeneracy (that is, there are an infinite number of possible <math>f</math> that all solve the same problem).
By adding extra dimensions, the CNF gains enough freedom to reverse orientation and go beyond ambient isotopy (just like how one can pick up a polygon from a desk and flip it around in 3-space, or unknot a knot in 4-space), yielding the "augmented neural ODE".[17]
Any homeomorphism of <math>\R^n</math> can be approximated by a neural ODE operating on <math>\R^{2n+1}</math>, proved by combining Whitney embedding theorem for manifolds and the universal approximation theorem for neural networks.[18]
To regularize the flow <math>f</math>, one can impose regularization losses. The paper [14] proposed the following regularization loss based on optimal transport theory:<math display="block">\lambda_{K} \int_{0}^{T}\left\|f(z_t, t)\right\|^{2} dt +\lambda_{J} \int_{0}^{T}\left\|\nabla_z f(z_t, t)\right\|_F^{2} dt </math>where <math>\lambda_K, \lambda_J >0 </math> are hyperparameters. The first term punishes the model for oscillating the flow field over time, and the second term punishes it for oscillating the flow field over space. Both terms together guide the model into a flow that is smooth (not "bumpy") over space and time.
Downsides
Despite normalizing flows success in estimating high-dimensional densities, some downsides still exist in their designs. First of all, their latent space where input data is projected onto is not a lower-dimensional space and therefore, flow-based models do not allow for compression of data by default and require a lot of computation. However, it is still possible to perform image compression with them.[19]
Flow-based models are also notorious for failing in estimating the likelihood of out-of-distribution samples (i.e.: samples that were not drawn from the same distribution as the training set).[20] Some hypotheses were formulated to explain this phenomenon, among which the typical set hypothesis,[21] estimation issues when training models,[22] or fundamental issues due to the entropy of the data distributions.[23]
One of the most interesting properties of normalizing flows is the invertibility of their learned bijective map. This property is given by constraints in the design of the models (cf.: RealNVP, Glow) which guarantee theoretical invertibility. The integrity of the inverse is important in order to ensure the applicability of the change-of-variable theorem, the computation of the Jacobian of the map as well as sampling with the model. However, in practice this invertibility is violated and the inverse map explodes because of numerical imprecision.[24]
Applications
Flow-based generative models have been applied on a variety of modeling tasks, including:
- Audio generation[25]
- Image generation[5]
- Molecular graph generation[26]
- Point-cloud modeling[27]
- Video generation[28]
- Lossy image compression[19]
- Anomaly detection[29]
References
External links
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite journal
- ↑ 3,0 3,1 Шаблон:Cite arXiv
- ↑ 4,0 4,1 Шаблон:Cite arXiv
- ↑ 5,0 5,1 5,2 Шаблон:Cite arXiv
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite journal
- ↑ 11,0 11,1 11,2 Шаблон:Cite arXiv
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite arXiv
- ↑ 14,0 14,1 Шаблон:Cite journal
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite arXiv
- ↑ 19,0 19,1 Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite journal
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv
- ↑ Шаблон:Cite arXiv