An approximate sampler for energy-based models with divergence diagnostics

Bryan Eikema, Germán Kruszewski, Christopher R Dance, Hady Elsahar, Marc DymetmanTMLR 2022

Abstract

Energy-based models (EBMs) allow flexible specifications of probability distributions. However, sampling from EBMs is non-trivial, usually requiring approximate techniques such as Markov chain Monte Carlo (MCMC). A major downside of MCMC sampling is that it is often impossible to compute the divergence of the sampling distribution from the target distribution: therefore, the quality of the samples cannot be guaranteed. Here, we introduce quasi-rejection sampling (QRS), a simple extension of rejection sampling that performs approximate sampling, but, crucially, does provide divergence diagnostics (in terms of f-divergences, such as KL divergence and total variation distance). We apply QRS to sampling from discrete EBMs over text for controlled generation. We show that we can sample from such EBMs with arbitrary precision in exchange for sampling efficiency and quantify the trade-off between the two by means of the aforementioned diagnostics.

Introduction

Energy-Based Models (EBMs) are powerful tools in machine learning, defining probability distributions through unnormalized energy functions. This flexibility allows for complex constraints and distributions, particularly in discrete spaces like text. However, sampling from these models is notoriously difficult. Standard Markov Chain Monte Carlo (MCMC) methods, while widely used, suffer from a lack of convergence diagnostics in discrete spaces. Researchers often rely on proxy metrics like diversity or perplexity, which can fail to detect when a sampler is stuck or misrepresenting the target distribution.

To address this, we introduce Quasi-Rejection Sampling (QRS). QRS is a relaxation of standard rejection sampling that does not require a strict global upper bound on the target-to-proposal ratio. Instead, it introduces a tuning parameter $\beta$ that allows users to explicitly trade off sampling speed for distributional quality, supported by rigorous divergence diagnostics.

Methodology: Quasi-Rejection Sampling

QRS operates by using a "global proposal" distribution $q$, which is easy to sample from, and a tuning parameter $\beta$. For a target distribution $p(x) \propto \exp(-E(x))$, a sample $x$ from $q$ is accepted with probability proportional to the ratio of the target EBM score to $\beta q(x)$, capped at 1.

Conceptual overview of QRS showing the trade-off between quality (divergence) and speed (acceptance rate).

As shown in Figure 1, the parameter $\beta$ acts as a "quality knob." Increasing $\beta$ raises the threshold for acceptance, which theoretically guarantees a non-increasing divergence from the target distribution. Unlike MCMC, QRS is a "scorer"—it associates an explicit probability with every sample. This property allows for the use of Importance Sampling (IS) to estimate $f$-divergences, such as the Total Variation Distance (TVD) or KL divergence, between the QRS distribution and the true target.

Experimental Evaluation

We evaluated QRS on several controlled text generation tasks using GPT-2, including pointwise constraints (e.g., generating text with specific keywords) and distributional constraints (e.g., debiasing gender splits in biography generation).

Empirical results demonstrating the trade-off between TVD, the tuning parameter $\beta$, and the acceptance rate.

The results in Figure 5 illustrate the core advantage of QRS: the ability to precisely tune the sampler. In the biography debiasing task, QRS achieved near-perfect alignment with the target distribution's moments at an acceptance rate of $10^{-3}$. For pointwise constraints, QRS provided a 10-fold to 100-fold increase in acceptance rate compared to naive filtering while maintaining a low, quantifiable TVD of approximately 0.1.

Discussion and Insights

A key insight from our work is that standard proxy metrics can be misleading. MCMC methods often achieve high scores on diversity or perplexity while maintaining a large, hidden divergence from the target. QRS exposes this gap by providing explicit TVD estimates.

Furthermore, QRS benefits from the increasing capability of Large Language Models (LLMs) to serve as high-quality global proposals. As LLMs become better at following complex prompts, the overlap between the proposal $q$ and the target $p$ increases, making QRS more efficient and practical for a wider range of tasks.

Limitations and Future Work

While QRS offers significant advantages, it remains dependent on the quality of the proposal distribution. If the proposal $q$ has poor overlap with the target $p$, the acceptance rate may become prohibitively low. Additionally, estimating divergences still requires an estimate of the partition function $Z$, which can be challenging in very high-dimensional spaces. Future research could explore hybrid approaches that combine the global nature of QRS with local sequential sampling methods to better scale to longer sequences.

Summary updated

BibTeX
@article{eikema2022approximate,
  author = {Bryan Eikema and Germán Kruszewski and Christopher R Dance and Hady Elsahar and Marc Dymetman},
  title = {{An approximate sampler for energy-based models with divergence diagnostics}},
  journal = {Accepted by TMLR},
  year = {2022},
  url = {https://openreview.net/forum?id=VW4IrC0n0M},
}
Figure