MASKER: Masked Keyword Regularization for Reliable Text Classification

Seung Jun Moon, Sangwoo Mo, Kimin Lee, Jaeho Lee, Jinwoo Shin

Abstract

Pre-trained language models have achieved state-of-the-art accuracies on various text classification tasks, e.g., sentiment analysis, natural language inference, and semantic textual similarity. However, the reliability of the fine-tuned text classifiers is an often underlooked performance criterion. For instance, one may desire a model that can detect out-of-distribution (OOD) samples (drawn far from training distribution) or be robust against domain shifts. We claim that one central obstacle to the reliability is the over-reliance of the model on a limited number of keywords, instead of looking at the whole context. In particular, we find that (a) OOD samples often contain in-distribution keywords, while (b) cross-domain samples may not always contain keywords; over-relying on the keywords can be problematic for both cases. In light of this observation, we propose a simple yet effective fine-tuning method, coined masked keyword regularization (MASKER), that facilitates context-based prediction. MASKER regularizes the model to reconstruct the keywords from the rest of the words and make low-confidence predictions without enough context. When applied to various pre-trained language models (e.g., BERT, RoBERTa, and ALBERT), we demonstrate that MASKER improves OOD detection and cross-domain generalization without degrading classification accuracy.

Introduction

While pre-trained language models (PLMs) like BERT and RoBERTa have set new benchmarks for accuracy, their reliability in real-world scenarios remains a challenge. A primary cause of this fragility is the "shortcut" bias: models often learn to rely on a small set of highly predictive keywords rather than understanding the broader context. This over-reliance leads to two critical failures: the inability to detect out-of-distribution (OOD) samples and poor generalization across different domains.

Conceptual example of how keyword over-reliance leads to failures in OOD detection and cross-domain generalization.

As illustrated in Figure 1, a model trained to associate "Apple" with the technology company may incorrectly classify a sentence about fruit as tech-related (OOD failure). Conversely, it might fail to recognize a sentence about "Tim Cook" as being related to the company if the specific keyword "Apple" is missing (cross-domain failure).

The Keyword Shortcut Problem

The authors empirically demonstrate that this reliability gap stems from the distribution of keywords. In-distribution samples are characterized by a high density of class-specific keywords. However, OOD samples often contain these same keywords in different contexts, leading to overconfident misclassifications. In cross-domain scenarios, the relevant keywords may shift or disappear entirely, leaving the model unable to leverage its learned shortcuts.

Confusion matrices showing the distribution of keywords across in-distribution, out-of-distribution, and cross-domain datasets.

Figure 2 provides a quantitative view of this phenomenon. In the OOD case (b), keywords from the training set appear frequently in unrelated classes, while in the cross-domain case (c), the overlap of predictive keywords between datasets (like IMDB and SST-2) is significantly lower than within a single domain.

The MASKER Framework

To mitigate these issues, the researchers propose Masked Keyword Regularization (MASKER). This method shifts the model's focus from individual keywords to the surrounding context through two primary mechanisms applied during fine-tuning:

  1. Keyword Selection: The model first identifies potential "shortcut" keywords. While frequency-based methods (TF-IDF) are possible, the authors found that an attention-based approach—identifying words the model naturally relies on most—is more effective.
  2. Masked Keyword Reconstruction (MKR): The model is trained to predict masked keywords using only the surrounding context. This forces the model to learn the semantic relationships between context and category.
  3. Masked Entropy Regularization (MER): When presented with a sentence where the context is masked and only keywords remain, the model is penalized for making high-confidence predictions. This teaches the model that keywords alone are insufficient for reliable classification.

Experimental Results

MASKER was evaluated across various benchmarks, including 20 Newsgroups, Amazon Reviews, and sentiment analysis tasks (IMDB, SST-2). The results demonstrate significant improvements in reliability:

  • OOD Detection: On the 20 Newsgroups to SST-2 task, MASKER improved BERT's OOD detection (AUROC) from 87.0% to 98.6%.
  • Cross-Domain Generalization: When training on "Fine Food" and testing on "IMDB," MASKER reduced the generalization gap from 19.2% to 10.9%.
  • Performance Stability: Crucially, these reliability gains were achieved without sacrificing standard in-distribution accuracy, a common trade-off in other regularization techniques.

Conclusion

MASKER offers a simple yet powerful way to de-bias PLMs by encouraging context-based reasoning. By treating keywords as something to be reconstructed rather than just exploited, MASKER builds more robust classifiers capable of handling the complexities of distribution shifts and domain changes. Future work may extend this context-vs-shortcut paradigm to other modalities, such as computer vision.

Summary updated

Abstract

Pre-trained language models have achieved state-of-the-art accuracies on various text classification tasks, e.g., sentiment analysis, natural language inference, and semantic textual similarity. However, the reliability of the fine-tuned text classifiers is an often underlooked performance criterion. For instance, one may desire a model that can detect out-of-distribution (OOD) samples (drawn far from training distribution) or be robust against domain shifts. We claim that one central obstacle to the reliability is the over-reliance of the model on a limited number of keywords, instead of looking at the whole context. In particular, we find that (a) OOD samples often contain in-distribution keywords, while (b) cross-domain samples may not always contain keywords; over-relying on the keywords can be problematic for both cases. In light of this observation, we propose a simple yet effective fine-tuning method, coined masked keyword regularization (MASKER), that facilitates context-based prediction. MASKER regularizes the model to reconstruct the keywords from the rest of the words and make low-confidence predictions without enough context. When applied to various pre-trained language models (e.g., BERT, RoBERTa, and ALBERT), we demonstrate that MASKER improves OOD detection and cross-domain generalization without degrading classification accuracy. Code is available at https://github.com/alinlab/MASKER.
BibTeX
@inproceedings{moon2021masker,
  author = {Seung Jun Moon and Sangwoo Mo and Kimin Lee and Jaeho Lee and Jinwoo Shin},
  title = {{MASKER: Masked Keyword Regularization for Reliable Text Classification}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2021},
  url = {https://openreview.net/forum?id=HafZDBASGM9},
}
Figure