Masked Conditional Random Fields for Sequence Labeling

Tianwen Wei, Jianwei Qi, Shenghuan He, Songtao SunNAACL 2021

Abstract

Sequence labeling models often predict illegal tag sequences, such as an "Inside" tag following an "Outside" tag without an intervening "Beginning" tag. Masked Conditional Random Fields (MCRF) address this fundamental flaw by enforcing hard constraints during both training and decoding. By masking illegal transitions in the CRF transition matrix, MCRF achieves state-of-the-art results on multiple benchmarks with negligible computational overhead.

Introduction

Sequence labeling is a core task in Natural Language Processing (NLP), involving the assignment of categorical tags to tokens in a sequence. Common applications include Named Entity Recognition (NER), Part-of-Speech (POS) tagging, and chunking. Most models utilize encoding schemes like BIO (Beginning, Inside, Outside) to delineate entity boundaries.

A significant issue in standard neural-CRF architectures is the generation of "illegal paths"—sequences of tags that violate the logic of the encoding scheme. For instance, an I-ORG tag cannot logically follow an O tag. While standard CRFs learn "soft" constraints to penalize these transitions, they do not strictly forbid them. Research indicates that these illegal paths are not merely a nuisance but a fundamental flaw, contributing significantly to model errors—accounting for up to 40% of total errors in some datasets.

Comparison of CRF and MCRF predictions. The standard CRF predicts an illegal transition (B-MISC to I-ORG), whereas MCRF adheres to the legal path.

Methods: Masked Conditional Random Fields

The proposed Masked Conditional Random Field (MCRF) converts soft constraints into hard ones by modifying the CRF's transition matrix $A$.

The Masking Trick

The core of MCRF is the identification of all illegal transitions based on the chosen encoding scheme (e.g., BIO or BIOES). In the transition matrix, entries corresponding to these illegal transitions are set to a very large negative constant ($c \ll 0$). This effectively "masks" these paths, making their selection by the model nearly impossible.

The MCRF transition matrix. Red cells represent illegal transitions that are masked with a large negative value to enforce hard constraints.

Constrained Training and Decoding

Unlike post-processing methods that only fix errors during inference, MCRF applies these constraints during training. This ensures the model does not allocate probability mass to impossible sequences, leading to a more focused learning process. Mathematically, as the masking constant approaches negative infinity, the loss function converges to one that only considers the space of legal paths. During inference, the Viterbi algorithm naturally adheres to legal paths because the scores for illegal transitions are prohibitively low.

Results and Discussion

MCRF was evaluated across eight diverse datasets, including Chinese and English NER, slot filling, and chunking.

  • Performance Gains: MCRF consistently outperformed standard CRF baselines across all tasks. It achieved new state-of-the-art results on the MSRA and OntoNotes 4.0 Chinese NER datasets.
  • Efficiency: The masking operation is computationally trivial, adding near-zero overhead to training or inference times.
  • Transformer Sensitivity: Interestingly, Transformer-based models like BERT were found to be more susceptible to illegal paths than older LSTM-based models, making the structured guidance of MCRF particularly valuable for modern architectures.
  • Handling Illegal Paths: A key finding was that if a standard CRF must be used, simply discarding illegal predictions often yields better results than attempting to "fix" them with rule-based post-processing.

Conclusion

Masked Conditional Random Fields provide a simple yet highly effective solution to the problem of illegal tag sequences in NLP. By integrating hard constraints directly into the CRF layer, MCRF improves model accuracy and robustness across a wide range of sequence labeling tasks without increasing computational complexity. Future work may involve extending these masking techniques to more complex hierarchical constraints.

Summary updated

BibTeX
@inproceedings{wei2021masked,
  author = {Tianwen Wei and Jianwei Qi and Shenghuan He and Songtao Sun},
  title = {{Masked Conditional Random Fields for Sequence Labeling}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.naacl-main.163},
}
Figure