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

In real-world applications of natural language generation, there are often constraints on the target sentences in addition to fluency and naturalness requirements. Existing language generation techniques are usually based on recurrent neural networks (RNNs). However, it is non-trivial to impose constraints on RNNs while maintaining generation quality, since RNNs generate sentences sequentially (or with beam search) from the first word to the last. In this paper, we propose CGMH, a novel approach using Metropolis-Hastings sampling for constrained sentence generation. CGMH allows complicated constraints such as the occurrence of multiple keywords in the target sentences, which cannot be handled in traditional RNN-based approaches. Moreover, CGMH works in the inference stage, and does not require parallel corpora for training. We evaluate our method on a variety of tasks, including keywords-to-sentence generation, unsupervised sentence paraphrasing, and unsupervised sentence error correction. CGMH achieves high performance compared with previous supervised methods for sentence generation.

Introduction

Most modern natural language generation relies on Recurrent Neural Networks (RNNs) or similar architectures that generate text sequentially, one word at a time. While effective for fluency, this "left-to-right" nature makes it difficult to impose specific constraints. For instance, if a sentence must include the words "BMW" and "sports," a standard RNN must navigate a coherent path that happens to include those terms. Existing workarounds, such as Grid Beam Search (GBS), are often computationally expensive or limited to simple constraints.

Furthermore, many constrained tasks like paraphrasing or error correction typically require massive parallel corpora for supervised training. CGMH (Constrained Generation by Metropolis-Hastings) addresses these challenges by shifting the paradigm from sequential decoding to a sampling-based approach that works entirely in the inference stage without requiring parallel training data.

Methodology

CGMH treats sentence generation as a state-space search where each "state" is a complete sentence. Instead of building a sentence from scratch, the model starts with an initial state (e.g., a list of keywords) and iteratively proposes small changes to move toward a target distribution.

The Sampling Process

The core of CGMH is the Metropolis-Hastings (MH) algorithm, a Markov Chain Monte Carlo (MCMC) method. At each step, the model proposes a modification to the current sentence, which is either accepted or rejected based on an acceptance rate. This rate considers both the fluency of the new sentence (provided by a pre-trained Language Model) and how well it satisfies constraints.

The CGMH sampling process showing iterative local edits—insertion, replacement, and deletion—to satisfy keyword constraints while maintaining fluency.

Local Operations

Unlike Gibbs sampling, which typically keeps sentence length fixed, CGMH utilizes three local operations to allow for arbitrary sentence lengths:

  1. Replacement: Swapping a word for a more likely alternative using the language model.
  2. Insertion: Adding a new word at a random position.
  3. Deletion: Removing an existing word.

By treating generation as a series of edits, the model can "self-correct." If an insertion creates a grammatical error, a subsequent replacement or deletion can resolve it, optimizing the sentence globally rather than sequentially.

Results and Discussion

CGMH was evaluated across several tasks, including keywords-to-sentence generation, unsupervised paraphrasing (using Quora Question Pairs), and grammatical error correction (using the JFLEG benchmark).

Performance and Fluency

In keywords-to-sentence tasks, CGMH significantly outperformed Grid Beam Search (GBS) and Backward-Forward baselines. As shown in the quantitative evaluation, CGMH achieved lower Negative Log-Likelihood (NLL) scores, indicating higher fluency, and received superior human evaluation scores across varying numbers of keyword constraints.

Quantitative comparison of CGMH against baselines (GBS, sep-B/F, asyn-B/F) showing superior fluency (NLL) and human evaluation scores.

Unsupervised Capabilities

Because the constraints are defined mathematically (e.g., cosine similarity for paraphrasing) and fluency is derived from a general-purpose language model, CGMH performs remarkably well without "gold standard" training pairs. In error correction, it achieved a GLEU score of 45.5 on the JFLEG dataset, comparable to some supervised systems that utilize hundreds of thousands of parallel sentence pairs.

Conclusion

CGMH offers a flexible, unsupervised framework for constrained sentence generation. By moving away from sequential generation, it avoids the "drift" and error accumulation common in RNNs, allowing for global optimization of sentence structure. While MCMC sampling is inherently slower than a single forward pass of a transformer, the ability to satisfy complex hard and soft constraints without parallel data makes it a powerful tool for specialized generation tasks. Future work may involve integrating more modern transformer-based language models to further enhance output fluency.

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