VAE with the ELBO training loss and prior-sample .generate().
Wraps VAEModel with the standard reconstruction +
-weighted KL training loss and a prior-sampler convenience
method. forward(x) returns a VAEOutput carrying the
reconstruction, latent (concatenated across levels in HVAE mode),
posterior parameters, and the three loss components
(recon_loss, kl_loss, loss = recon + beta * kl). In
hierarchical mode kl_loss is the sum of per-level KL terms.
generate(n_samples) samples from the prior —
for vanilla, one
per level for HVAE — and
returns the decoder output. BCE reconstructions are squashed through
a sigmoid before being returned.
Parameters
configVAEConfigconfig.kl_weight is the
coefficient (Higgins 2017); config.recon_loss toggles between
Gaussian ("mse") and Bernoulli ("bce") likelihoods.Attributes
vaeVAEModelencode and decode.Notes
Reference: Kingma and Welling, "Auto-Encoding Variational Bayes", ICLR, 2014 (arXiv:1312.6114); -VAE in Higgins et al., ICLR, 2017; hierarchical extension in Sønderby et al., NeurIPS, 2016 (arXiv:1602.02282).
Training objective — evidence lower bound:
In hierarchical mode the KL term is the sum .
Examples
>>> import lucid
>>> from lucid.models.generative.vae import (
... VAEConfig, VAEForImageGeneration,
... )
>>> cfg = VAEConfig(sample_size=32, in_channels=3, out_channels=3,
... latent_dim=16, down_block_channels=(16, 32, 64))
>>> model = VAEForImageGeneration(cfg).eval()
>>> x = lucid.randn((1, 3, 32, 32))
>>> out = model(x)
>>> out.sample.shape, out.loss.shape # reconstruction + scalar loss
((1, 3, 32, 32), ())