Certainly! A Conditional Variational Autoencoder (CVAE) extends the standard Variational Autoencoder (VAE) by incorporating conditional information (e.g., class labels or other auxiliary data) into both the encoder and decoder. Below, I'll break down the key mathematical components of a CVAE.
1. Standard VAE Recap
A standard VAE consists of:
- An encoder ( q_\phi(z|x) ) that maps input data ( x ) to a latent distribution ( z ).
- A decoder ( p_\theta(x|z) ) that reconstructs ( x ) from ( z ).
- A prior ( p(z) ) (usually a standard Gaussian ( \mathcal{N}(0, I) )).
The objective is to maximize the Evidence Lower Bound (ELBO):
[
\mathcal{L}(\theta, \phi; x) = \mathbb{E}{q\phi(z|x)} \left[ \log p_\theta(x|z) \right] - \beta \cdot D_{KL} \left( q_\phi(z|x) | p(z) \right),
]
where:
- The first term is the reconstruction loss (log-likelihood).
- The second term is the KL divergence between the approximate posterior ( q_\phi(z|x) ) and the prior ( p(z) ).
2. Conditional VAE (CVAE)
In a CVAE, we condition both the encoder and decoder on additional information ( y ) (e.g., a class label). The key changes are:
Encoder: ( q_\phi(z|x, y) )
The encoder now takes both ( x ) and ( y ) as input and outputs a conditional latent distribution:
[
z \sim q_\phi(z|x, y).
]
This is typically parameterized as a Gaussian:
[
q_\phi(z|x, y) = \mathcal{N}(z; \mu_\phi(x, y), \sigma_\phi(x, y)).
]
Decoder: ( p_\theta(x|z, y) )
The decoder reconstructs ( x ) given both ( z ) and ( y ):
[
x \sim p_\theta(x|z, y).
]
This is often parameterized as a Gaussian or Bernoulli distribution, depending on the data type.
Prior: ( p(z|y) )
The prior is now conditional on ( y ). A common choice is:
[
p(z|y) = \mathcal{N}(z; \mu_p(y), \sigma_p(y)),
]
where ( \mu_p(y) ) and ( \sigma_p(y) ) are learned functions of ( y ).
3. CVAE Objective (Conditional ELBO)
The CVAE optimizes a conditional version of the ELBO:
[
\mathcal{L}(\theta, \phi; x, y) = \mathbb{E}{q\phi(z|x, y)} \left[ \log p_\theta(x|z, y) \right] - \beta \cdot D_{KL} \left( q_\phi(z|x, y) | p(z|y) \right).
]
Here:
- The reconstruction term ( \mathbb{E}{q\phi(z|x, y)} \left[ \log p_\theta(x|z, y) \right] ) measures how well the decoder reconstructs ( x ) given ( z ) and ( y ).
- The KL term ( D_{KL} \left( q_\phi(z|x, y) | p(z|y) \right) ) regularizes the latent space to match the conditional prior ( p(z|y) ).
4. Training the CVAE
During training:
- For a given ( (x, y) ), the encoder computes ( q_\phi(z|x, y) ).
- A latent sample ( z ) is drawn using the reparameterization trick:
[
z = \mu_\phi(x, y) + \sigma_\phi(x, y) \odot \epsilon, \quad \epsilon \sim \mathcal{N}(0, I).
]
- The decoder computes ( p_\theta(x|z, y) ).
- The loss ( \mathcal{L}(\theta, \phi; x, y) ) is computed and backpropagated.
5. Key Differences from VAE
- The latent space is conditioned on ( y ), allowing for controlled generation (e.g., generating images of a specific class).
- The prior is also conditioned on ( y ), making the latent space more structured.
- The KL divergence is computed between ( q_\phi(z|x, y) ) and ( p(z|y) ), not ( p(z) ).
6. Applications
CVAEs are useful for:
- Conditional generation (e.g., generating images of a specific class).
- Semi-supervised learning (using labeled and unlabeled data).
- Controlled data synthesis (e.g., generating data with specific attributes).
Summary
A CVAE extends the VAE by conditioning the encoder, decoder, and prior on auxiliary information ( y ). The key mathematical components are:
- Conditional encoder ( q_\phi(z|x, y) ).
- Conditional decoder ( p_\theta(x|z, y) ).
- Conditional prior ( p(z|y) ).
- Conditional ELBO that balances reconstruction and KL divergence.
This allows for more structured and controllable latent representations.