NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

CGMH: Constrained Sentence Generation by Metropolis-Hastings Sampling

Ning Miao, Hao Zhou, Lili Mou, Rui Yan, Lei Li

Abstract

CGMH (Constrained Generation by Metropolis-Hastings) is a novel unsupervised framework designed to generate sentences that satisfy specific constraints, such as mandatory keywords or semantic themes. Unlike traditional sequential models, CGMH treats sentence generation as a sampling problem in discrete word space. By utilizing Metropolis-Hastings sampling with local operations—insertion, deletion, and replacement—the model achieves state-of-the-art results in keywords-to-sentence generation, unsupervised paraphrasing, and error correction without requiring parallel training data.

Introduction

Traditional Recurrent Neural Networks (RNNs) generate text sequentially from left to right. While effective for general language modeling, this nature makes it difficult to enforce specific constraints. For instance, incorporating multiple non-sequential keywords like "BMW" and "sports car" into a fluent advertisement often results in awkward phrasing or missed constraints when using standard autoregressive models.

Existing solutions, such as Grid Beam Search (GBS), are computationally expensive and struggle with the vast search space of natural language. Furthermore, most constrained generation tasks typically rely on massive datasets of parallel sentences (e.g., for paraphrasing or grammatical error correction), which are difficult to curate. CGMH addresses these challenges by providing a flexible, unsupervised framework that edits sentences globally to satisfy constraints while maintaining fluency.

Methodology

CGMH moves away from sequential generation in favor of Metropolis-Hastings (MH) sampling, a classic Markov Chain Monte Carlo (MCMC) algorithm. The process involves four key components:

  1. Direct Sampling in Sentence Space: Rather than sampling from a continuous latent space (like VAEs), CGMH operates directly on the space of discrete word sequences.
  2. Local Operations: The model begins with an initial state (e.g., a set of keywords) and proposes "jumps" to new sentence states using three basic operations: Replacement, Insertion, and Deletion.
  3. The Acceptance Rule: Proposed changes are accepted or rejected based on an acceptance rate derived from a stationary distribution. This distribution balances Fluency (measured by a pre-trained language model) and Constraint Satisfaction (measured by a matching function).
  4. The Pre-selector: To manage the massive vocabulary of human language, the authors use forward and backward language models to filter out nonsensical word proposals before the expensive MH calculation, ensuring the process remains computationally efficient.
Figure 1: The step-by-step MH sampling process showing how keywords are expanded into a fluent sentence through iterative insertion, replacement, and deletion operations.

Experimental Setup

The researchers evaluated CGMH across three distinct tasks using several benchmarks:

  • One-Billion-Word Corpus: Used to train the base language models for fluency.
  • Quora Question Pairs: Utilized for unsupervised paraphrase generation.
  • JFLEG (JHU Fluency-Extended GEC): A benchmark for sentence error correction.
  • WMT18 English Monolingual Corpus: Used to train an n-gram model for objective fluency evaluation via Negative Log-Likelihood (NLL).

Key Results and Insights

CGMH demonstrates that treating a sentence as a collection of words that can be edited globally allows the model to "self-correct." If an insertion creates a grammatical error, a subsequent deletion or replacement can rectify it. This global perspective, combined with the randomness of sampling, naturally produces more diverse outputs than greedy search algorithms.

Performance Comparison

In keywords-to-sentence tasks, CGMH consistently outperformed Grid Beam Search (GBS) and "Backward-Forward" models. As shown in the table below, CGMH achieved lower (better) NLL scores and higher human evaluation ratings across varying numbers of keyword constraints.

Figure 6: Quantitative comparison of CGMH against baselines, showing superior performance in both fluency (NLL) and human evaluation scores across different keyword counts.
  • Unsupervised Paraphrasing: CGMH achieved a BLEU score of 20.89 without any parallel training data, rivaling supervised Seq2Seq models trained on 50,000 sentence pairs.
  • Error Correction: On the JFLEG benchmark, CGMH’s unsupervised approach achieved a GLEU score of 45.5, outperforming the supervised AMU system trained on 2.3 million parallel pairs.

Limitations and Future Work

Despite its success, CGMH faces challenges regarding sampling speed; it typically requires roughly 150 steps to reach a fluent sentence from a raw keyword list. Additionally, in paraphrasing tasks, the model can sometimes remain too literally similar to the source sentence. Future research may focus on better heuristics to encourage more significant structural changes and optimizing the stationary distribution for more complex linguistic constraints.

Summary updated

BibTeX
@inproceedings{miao2019cgmh,
  author = {Ning Miao and Hao Zhou and Lili Mou and Rui Yan and Lei Li},
  title = {{CGMH: Constrained Sentence Generation by Metropolis-Hastings Sampling}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2019},
  doi = {10.1609/aaai.v33i01.33016834},
}
Figure