The VAE assumes that data is generated in the following two stages (Section 2.1 of the paper).
- Draw a latent variable z from the prior pθ(z). The VAE uses the standard normal distribution N(0,I).
- Draw data x from the distribution pθ(x∣z) represented by the decoder.
The variational lower bound
Since logpθ(x) cannot be computed directly, it is split as follows using the encoder qϕ(z∣x) (Eq. 1).
logpθ(x)=DKL(qϕ(z∣x)∥pθ(z∣x))+L(θ,ϕ;x)
Since the KL divergence is non-negative, the second term L is a lower bound on logpθ(x). It is called the variational lower bound and can be written as follows (Eq. 3).
L(θ,ϕ;x)=−DKL(qϕ(z∣x)∥pθ(z))+Eqϕ(z∣x)[logpθ(x∣z)]
- The first term (the KL term) pulls the distribution produced by the encoder toward the prior N(0,I). It corresponds to the regularizer of an autoencoder.
- The second term (the reconstruction term) grows the better the original x can be recovered from z. It corresponds to the autoencoder's reconstruction error with the sign flipped.
Maximizing L with respect to both θ (the decoder) and ϕ (the encoder) raises the likelihood of the data while bringing the encoder closer to the true posterior.
Reparameterization
The reconstruction term is computed by drawing z from qϕ, so as it stands the gradient with respect to ϕ cannot be obtained through the "draw a random number" operation. Naive estimators exist, but according to the paper their variance is very high and they are impractical. So the randomness is moved outside (Section 2.4).
z=μ+σ⊙ϵ,ϵ∼N(0,I)
⊙ denotes the element-wise product. Since the random number ϵ does not depend on the parameters, z becomes a differentiable function of μ,σ, and backpropagation carries gradients all the way to the encoder. Picture moving "the person who rolls the dice" from inside the network to outside, so that the network only receives the result and computes with it.
The loss of the Gaussian VAE
The VAE in the paper makes the encoder output a diagonal normal distribution qϕ(z∣x)=N(z;μ,σ2I). Since the prior is also normal, the KL term can be computed in closed form (Appendix B). The lower bound for a single data point x is the following (Eq. 10).
L≃21j=1∑J(1+logσj2−μj2−σj2)+L1l=1∑Llogpθ(x∣z(l))
J is the dimension of the latent variable, and L is the number of samples of ϵ drawn per data point.
The training procedure is as follows (Algorithm 1).
- Take a mini-batch of M data points from the training data.
- Compute μ and logσ2 for each data point with the encoder.
- Draw ϵ∼N(0,I) and form z=μ+σ⊙ϵ.
- Use the decoder to produce the parameters of pθ(x∣z) from z, and compute L in the equation above.
- Update θ and ϕ together with the gradient of L (the paper gives SGD and AdaGrad as examples).
The paper's experiments use M=100 and L=1. It reports that one sample per data point is enough as long as the mini-batch is large enough.
Inside the encoder and decoder
The encoder and decoder in the paper are fully connected networks (MLPs) with a single hidden layer, and the hidden layer's activation function is tanh (Appendix C).
| Part | Output | Equation |
|---|
| Encoder | μ and logσ2 of a normal distribution | h=tanh(W3x+b3), μ=W4h+b4, logσ2=W5h+b5 |
| Decoder (binary data) | Bernoulli probabilities y | y=sigmoid(W2tanh(W1z+b1)+b2) |
| Decoder (real-valued data) | Mean and logσ2 of a normal distribution | Same form as the encoder, with z and x swapped |
For binary data, logpθ(x∣z)=∑i[xilogyi+(1−xi)log(1−yi)], so the reconstruction term is the binary cross-entropy with the sign flipped.
Having the encoder output logσ2 instead of σ guarantees that σ=exp(21logσ2) is positive even when the output is negative. Appendix C of the paper uses this form as well.