Loading…
Guiding Text Generation with Constrained Beam Search in 🤗 Transformers
Chan Woo Kim
Summary
Constrained beam search introduces direct control over generated text in Hugging Face Transformers. Standard beam search operates token-by-token without knowing the optimal step to force specific words or phrases, making it difficult to enforce mandatory vocabulary or choose between alternative expressions. To resolve this, the generate interface accepts constraints through arguments such as force_words_ids and a list of Constraint subclasses like PhrasalConstraint. This mechanism enables disjunctive constraints, where generation must include at least one phrase from a provided set, alongside strictly required sequences. Consequently, practitioners can inject prior knowledge or formatting requirements directly at generation time rather than filtering candidate outputs afterward.
Context
Traditional beam search operates token-by-token without an easy mechanism to force specific words or phrases into the output at generation time, complicating tasks like enforcing formality or dictionary lookups in neural machine translation.
Approach / What changed
Hugging Face Transformers added constrained beam search support to model.generate() via the force_words_ids parameter and a modular constraints argument that accepts custom Constraint subclasses like PhrasalConstraint.
Takeaways
- Constrained beam search allows injecting prior requirements directly during generation instead of generating and filtering multiple outputs.
- Disjunctive constraints allow users to supply a list of alternative words or forms, requiring the model to include at least one in the final text.
- Custom constraints can be constructed by subclassing the Constraint abstract interface class and passing instances to model.generate().
Related reading
huggingface.co ·
Introducing Hugging Face for Education 🤗
Hugging Face established an initiative to teach machine learning to 5 million people by the end of 2023. As machine learning expands across software development and impacts non-technical users, adapting workforce skills and assisting educators with ethical considerations become critical challenges. The educational framework provides browser-based model testing widgets, documentation on model biases, and free courses covering Natural Language Processing, Deep Reinforcement Learning, and interactive demo creation. Instructors receive free infrastructure to create Hub classrooms, alongside a multilingual toolkit spanning topics like Transformers and Gradio. Through ongoing global tours and free compute events, the organization supports collaborative machine learning education across diverse experience levels.
Violettehuggingface.co ·
Leveraging Pre-trained Language Model Checkpoints for Encoder-Decoder Models
Pre-training sequence-to-sequence transformer models incurs massive computational costs, limiting development primarily to large institutions. To mitigate these expenses, encoder-decoder architectures can be warm-started using existing pre-trained checkpoints from encoder-only or decoder-only models like BERT and GPT-2. This walkthrough details the methodology and implementation of warm-starting sequence-to-sequence architectures using Hugging Face Transformers. By utilizing the EncoderDecoderModel framework alongside Seq2SeqTrainer, practitioners can construct and fine-tune models such as BERT2BERT on datasets like CNN/DailyMail. The resulting fully trained BERT2BERT model achieves a ROUGE-2 score of 18.22 on the full evaluation set, matching competitive sequence generation baselines at a fraction of standard pre-training costs.