Latent Variable Models

1. Use latent variable model

𝑝 ( đ‘Ĩ ) = âˆĢ 𝑝 ( đ‘Ĩ | 𝑧 ) 𝑝 ( 𝑧 ) 𝑑 𝑧 𝑝 𝜃 ( đ‘Ĩ | 𝑧 ) → 𝑝 ( đ‘Ĩ | 𝑧 )

𝑝(𝑧) is the prior distribution, will be a predefined density function.

𝑝 ( đ‘Ĩ ) = âˆĢ 𝑝 ( đ‘Ĩ | 𝑧 ) 𝑝 ( 𝑧 ) 𝑑 𝑧 𝑞 ( đ‘Ĩ | 𝑧 ) → 𝑝 ( đ‘Ĩ | 𝑧 )

𝑝(𝑧) is the prior distribution, will be a predefined density function.

What we want is to learn 𝑝𝜃(đ‘Ĩ|𝑧) to approximate 𝑝(đ‘Ĩ|𝑧), which is usually measured by the KL divergence. But its hard to deal with that, so we approximate 𝑝(đ‘Ĩ,𝑧) instead since 𝑝(đ‘Ĩ,𝑧)=𝑝(đ‘Ĩ|𝑧)𝑝(𝑧).

KL ( 𝑝 ( đ‘Ĩ , 𝑧 ) ‖ 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) ) = âˆŦ 𝑝 ( đ‘Ĩ , 𝑧 ) log ( 𝑝 ( đ‘Ĩ , 𝑧 ) 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) ) 𝑑 đ‘Ĩ 𝑑 𝑧 = âˆĢ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 ( đ‘Ĩ ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) 𝑑 𝑧 ] 𝑑 đ‘Ĩ = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) ( log 𝑝 ( đ‘Ĩ ) + log 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ log 𝑝 ( đ‘Ĩ ) âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ] + đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ log 𝑝 ( đ‘Ĩ ) ] − đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ]

The first term is a constant, so we only need to maximize the second term:

â„’ī¸€ = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 𝜃 ( đ‘Ĩ | 𝑧 ) 𝑝 ( 𝑧 ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 𝜃 ( đ‘Ĩ | 𝑧 ) 𝑑 𝑧 + âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 ( 𝑧 ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ đ”ŧ 𝑧 âˆŧ 𝑝 ( 𝑧 | đ‘Ĩ ) [ log 𝑝 𝜃 ( đ‘Ĩ | 𝑧 ) ] ] − KL ( 𝑝 ( 𝑧 | đ‘Ĩ ) ‖ 𝑝 ( 𝑧 ) )

This is Evidence Lower Bound (ELBO). But is maximizing the ELBO similar to doing maximum likelihood estimation (MLE)? Yes, since we can show that

đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ ELBO ( đ‘Ĩ ) ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 𝜃 ( đ‘Ĩ , 𝑧 ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ âˆĢ 𝑝 ( 𝑧 | đ‘Ĩ ) log 𝑝 𝜃 ( đ‘Ĩ ) 𝑝 𝜃 ( 𝑧 | đ‘Ĩ ) 𝑝 ( 𝑧 | đ‘Ĩ ) 𝑑 𝑧 ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ log 𝑝 𝜃 ( đ‘Ĩ ) ] − KL ( 𝑝 ( 𝑧 | đ‘Ĩ ) ‖ 𝑝 𝜃 ( 𝑧 | đ‘Ĩ ) ) ]

1.1. VAE

We choose 𝑝(𝑧) to be đ’Šī¸€(0,1), and use networks to approximate 𝑝(𝑧|đ‘Ĩ) and 𝑝(đ‘Ĩ|𝑧).

( 𝜇 , 𝜎 2 ) = EncoderNetwork 𝜙 ( đ‘Ĩ ) , 𝑞 𝜙 ( 𝑧 | đ‘Ĩ ) = đ’Šī¸€ ( 𝑧 | 𝜇 , diag ( 𝜎 2 ) )

𝑝(đ‘Ĩ|𝑧) is one-to-one mapping. We use 𝑝𝜃(đ‘Ĩ|𝑧)=đ›ŋ(đ‘Ĩ−𝑓𝜃(𝑧)) to approximate 𝑝(đ‘Ĩ|𝑧).

𝑓 𝜃 ( 𝑧 ) = DecoderNetwork 𝜃 ( 𝑧 ) , 𝑝 𝜃 ( đ‘Ĩ | 𝑧 ) = đ’Šī¸€ ( đ‘Ĩ | 𝑓 𝜃 ( 𝑧 ) , 𝜎 dec 2 đŧ )

where 𝜎dec is a hyperparameter. Thus the first term of the loss function is

đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ đ”ŧ 𝑧 âˆŧ 𝑞 𝜙 ( 𝑧 | đ‘Ĩ ) [ − log 𝑝 𝜃 ( đ‘Ĩ | 𝑧 ) ] ] = đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ đ”ŧ 𝑧 âˆŧ 𝑞 𝜙 ( 𝑧 | đ‘Ĩ ) [ − log 1 2 𝜋 𝜎 dec exp ( − ( đ‘Ĩ − 𝑓 𝜃 ( 𝑧 ) ) 2 2 𝜎 dec 2 ) ] ] = 1 2 𝜎 dec 2 đ”ŧ đ‘Ĩ âˆŧ 𝑝 ( đ‘Ĩ ) [ đ”ŧ 𝑧 âˆŧ 𝑞 𝜙 ( 𝑧 | đ‘Ĩ ) [ ( đ‘Ĩ − 𝑓 𝜃 ( 𝑧 ) ) 2 ] ] − log 1 2 𝜋 𝜎 dec

Since KL(𝑁0‖𝑁1)=12(tr(∑1−1∑0)+(𝜇1−𝜇0)𝑇∑1−1(𝜇1−𝜇0)+log|∑1||∑0|−𝑘), the second term is

KL ( 𝑞 𝜙 ( 𝑧 | đ‘Ĩ ) ‖ 𝑝 ( 𝑧 ) ) = 1 2 ( − log 𝜎 2 + 𝜇 2 + 𝜎 2 − 1 )

We are trying to find (𝜙,𝜃)=argmax𝜙,𝜃đ”ŧđ‘Ĩâˆŧ𝑝(đ‘Ĩ)[ELBO(đ‘Ĩ)].

1.1.1. Conditioned VAE (CVAE)

We define đŋCVAE=đ”ŧ(đ‘Ĩ,đ‘Ļ)âˆŧ𝑝(đ‘Ĩ,đ‘Ļ)[đ”ŧ𝑧âˆŧ𝑞𝜙(𝑧|đ‘Ĩ,đ‘Ļ)[log𝑝𝜃(đ‘Ļ|đ‘Ĩ,𝑧)]]−KL(𝑞𝜙(𝑧|đ‘Ĩ,đ‘Ļ)‖𝑝𝜃(𝑧|đ‘Ĩ)) and also Gaussian stochastic neural network (GSNN) with loss đŋGSNN=đ”ŧ(đ‘Ĩ,đ‘Ļ)âˆŧ𝑝(đ‘Ĩ,đ‘Ļ)[đ”ŧ𝑧âˆŧ𝑞𝜙(𝑧|đ‘Ĩ)[log𝑝𝜃(đ‘Ļ|đ‘Ĩ,𝑧)]]. The total loss is đŋhybrid=đ›ŧđŋCVAE+(1−đ›ŧ)đŋGSNN.

1.1.2. đ›Ŋ-VAE

â„’ī¸€=đ”ŧđ‘Ĩâˆŧ𝑝(đ‘Ĩ)[đ”ŧ𝑧âˆŧ𝑞𝜙(𝑧|đ‘Ĩ)[log𝑝𝜃(đ‘Ĩ|𝑧)]]−đ›ŊKL(𝑞𝜙(𝑧|đ‘Ĩ)‖𝑝(𝑧)), when đ›Ŋ>1, each dimension of 𝑧âˆŧ𝑞𝜙(𝑧|đ‘Ĩ) are forced to be more independent (disentangled).

1.1.3. VAE with Discrete Latent

1.1.3.1. Gumbel-Softmax

Gumbel Max is a way to sample from a categorical distribution. We assume the probability of each category is 𝑝𝑖, then argmax𝑖(log𝑝𝑖−log(−log𝜀𝑖)),𝜀𝑖âˆŧ𝑈[0,1] is equivalent to sampling from the categorical distribution, which is a reparametrization trick.

But argmax is not differentiable, so we use softmax to approximate it:

softmax ( log 𝑝 𝑖 − log ( − log 𝜀 𝑖 ) 𝜏 ) , 𝜀 𝑖 âˆŧ 𝑈 [ 0 , 1 ]

Where 𝜏 is a temperature parameter. The smaller 𝜏, the more likely the result is to be one-hot.

Using Gumbel-Softmax, we can use 𝑝(𝑧)=uniform(0,𝑘−1) instead of 𝑝(𝑧)=đ’Šī¸€(0,1).

1.1.3.2. Vector-Quantization VAE (VQ-VAE)

Reduce dimensions and use PixelCNN to generate images.

In reality, we encoder đ‘Ĩ into a đ‘šÃ—đ‘š grid of 𝑑-dimensional vectors. But argmin is not differentiable, so we use Straight-Through Estimator to define our own gradient and change loss function:

‖ đ‘Ĩ − decoder ( 𝑧 𝑞 ) ‖ 2 2 → ‖ đ‘Ĩ − decoder ( 𝑧 + sg [ 𝑧 𝑞 − 𝑧 ] ) ‖ 2 2

To make 𝑧𝑞 more similar to 𝑧, we can add ‖𝑧−𝑧𝑞‖22 to the loss function. Decompose ‖𝑧𝑞−𝑧‖22 into ‖sg[𝑧]−𝑧𝑞‖22+‖𝑧−sg[𝑧]‖22. The first term fixes 𝑧 and makes 𝑧𝑞 closer to 𝑧 and the second term makes 𝑧 closer to 𝑧𝑞. Since 𝑧𝑞 is more free to change, so the loss function is:

‖ đ‘Ĩ − decoder ( 𝑧 + sg [ 𝑧 𝑞 − 𝑧 ] ) ‖ 2 2 + đ›Ŋ ‖ sg [ 𝑧 ] − 𝑧 𝑞 ‖ 2 2 + 𝛾 ‖ 𝑧 − sg [ 𝑧 𝑞 ] ‖ 2 2

where 𝛾<đ›Ŋ. After training, we can use 𝑝(𝑧) to train auto-regressive models like PixelCNN for better sampling.

1.1.3.3. VQ-VAE 2

Bi-level VQ-VAE, bottom level conditions on top level.

1.1.3.4. DALL-E

Discrete VAE using ResNet with 8192 codebook size & 1024 image tokens.

1.1.3.5. DALL-E 2/3

Image generation model over image embeddings.

1.1.3.6. Latent Diffusion Models (LDM)

dVAE + Transformer prior over large-scale text-image paired data