The continuous Bernoulli: fixing a pervasive error in variational autoencoders

Gabriel Loaiza-Ganem, John P. Cunningham

The Problem: A Mathematical "Bug" in VAEs

If you have ever trained a Variational Autoencoder (VAE) on a dataset like MNIST, you likely used a sigmoid output layer paired with a binary cross-entropy loss function. Mathematically, this setup implicitly assumes a discrete Bernoulli likelihood. However, there is a fundamental mismatch here: the Bernoulli distribution is discrete, supported only on the set $\{0, 1\}$, while normalized grayscale pixel values are continuous, spanning the interval $[0, 1]$.

While this practice is widespread—appearing in seminal VAE papers, popular deep learning tutorials, and countless follow-up works—it is technically a mathematical error. By applying the discrete Bernoulli formula to continuous values, researchers effectively ignore a crucial normalizing constant. Consequently, the resulting objective function is no longer a valid lower bound on a coherent probabilistic model.

This raises a simple but critical question: Does this mathematical "nitpicking" actually impact model performance? The answer is a resounding yes.

The Solution: The Continuous Bernoulli Distribution

To resolve this discrepancy, the authors introduce and fully characterize the Continuous Bernoulli (CB) distribution, a principled, single-parameter distribution designed specifically for $[0, 1]$-valued data.

  • The Density: The probability density function is defined as $p(x|\lambda) \propto \lambda^x (1-\lambda)^{1-x}$ for $x \in [0, 1]$.
  • The Normalizing Constant: Unlike the standard discrete Bernoulli, the CB distribution requires a normalizing constant, $C(\lambda)$, to ensure the area under the probability curve integrates to 1.
  • The "Fix": By explicitly including $\log C(\lambda)$ in the VAE's Evidence Lower Bound (ELBO), the model is transformed back into a mathematically sound probabilistic framework for continuous data.

Because the CB belongs to the exponential family and possesses a closed-form cumulative distribution function (CDF) and inverse CDF, it allows for easy sampling and the use of the reparameterization trick. This makes it a seamless drop-in replacement for the standard Bernoulli in existing VAE architectures.

Properties of the Continuous Bernoulli distribution: log normalizing constant, density, and mean.

Key Insights from the Math

Analyzing the properties of the Continuous Bernoulli reveals several fascinating insights into why standard VAEs often struggle:

  1. The Normalizing Constant is a Regularizer: The log-normalizing constant $\log C(\lambda)$ is a U-shaped, convex function. When added to the VAE objective, it acts as a regularizer that pushes the decoder to produce more "extremal" values (closer to 0 or 1). Standard VAEs miss this mathematical pressure, which explains why they notoriously produce blurry, washed-out images.
  2. The "Binary Limit" Paradox: One might assume that if data is almost perfectly binary (like thresholded MNIST), the continuous error doesn't matter. The authors prove the exact opposite: as the parameter $\lambda$ approaches 0 or 1, the normalizing constant $C(\lambda)$ actually goes to infinity. The mathematical error is most severe when the data is nearly binary.
  3. Mean $\neq$ Parameter: In a standard discrete Bernoulli, the parameter $\lambda$ is exactly the mean of the distribution. In a Continuous Bernoulli, the mean $\mu(\lambda)$ is a non-linear function of $\lambda$. Training a VAE correctly requires accounting for this shift to properly interpret the model's outputs.

Key Results: Sharper Reconstructions

The authors evaluated the CB VAE against standard baselines on MNIST, CIFAR-10 (without standard preprocessing to maintain the $[0, 1]$ pixel range), and a synthetic "Warped MNIST" dataset designed to test varying levels of image blurriness.

  • Quantitative Gains: On MNIST, the Continuous Bernoulli VAE achieved a massive ELBO improvement of approximately 300 nats over the standard (incorrect) Bernoulli VAE. It also matched or outperformed Beta-likelihood and Gaussian-likelihood VAEs across multiple metrics.
  • Qualitative Sharpness: Visually, the CB VAE produces significantly sharper and more realistic image samples compared to both the standard Bernoulli ($\mathcal{B}$ VAE) and Gaussian ($\mathcal{N}$ VAE) models, which tend to suffer from blur.
  • Better Representations: Using metrics like the Inception Score and k-Nearest Neighbor classification on the latent space, the authors demonstrated that the CB VAE learns more meaningful and useful underlying data representations.
Visual comparison of VAE reconstructions on MNIST.

Limitations and Implementation Details

While highly effective, the Continuous Bernoulli does come with a minor implementation hurdle: numerical stability. The normalizing constant $C(\lambda)$ becomes numerically unstable when $\lambda$ is very close to $0.5$. The authors easily resolved this in their implementation by using a Taylor expansion near the midpoint, adding only a small layer of complexity to the code.

Ultimately, the Continuous Bernoulli offers a mathematically rigorous, easy-to-implement fix for a pervasive error in deep generative modeling, yielding sharper images and stronger latent representations for any data bounded between 0 and 1.

Summary updated

BibTeX
@inproceedings{loaizaganem2019continuous,
  author = {Gabriel Loaiza-Ganem and John P. Cunningham},
  title = {{The continuous Bernoulli: fixing a pervasive error in variational autoencoders}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2019},
}
Figure