Weighted Sampling for Masked Language Modeling

Linhan Zhang, Qian Chen, Wen Wang, Chong Deng, Xin Cao, Kongzhang Hao, Yuxin Jiang, Wei Wang

Introduction

Masked Language Modeling (MLM) has served as the cornerstone of modern Natural Language Processing since the inception of BERT. By masking a subset of tokens—traditionally 15%—and requiring the model to predict them based on context, MLM forces the model to learn deep bidirectional representations. However, standard MLM implementations rely on uniform random masking. This approach ignores the inherent distribution of natural language, where a small number of high-frequency functional words (e.g., "the", "is", "of") dominate the corpus, while the majority of semantically rich "content" words appear rarely.

This paper introduces Weighted Sampling, a strategy designed to move beyond uniform masking. By prioritizing rare tokens or those that the model finds difficult to predict, the authors develop WSBERT, a model that achieves superior sentence representations and improved performance across a variety of downstream tasks.

The Problem: Frequency Bias and Anisotropy

Language follows a long-tailed distribution. Because standard MLM masks tokens at random, the model spends a disproportionate amount of its training capacity re-learning common words it has already mastered. This leads to two critical deficiencies in the resulting embedding space:

  1. Poor Rare Token Representation: Rare tokens are seldom masked, meaning the model receives insufficient signal to learn their nuanced meanings or contextual relationships.
  2. Anisotropy: The embedding space becomes "skinny" or cone-shaped. Word vectors occupy a narrow region rather than being distributed uniformly. This "frequency bias" makes it difficult for the model to distinguish between the semantic meanings of different sentences, as the representations are pulled toward the high-frequency tokens.

Methodology: WSBERT

The authors propose two distinct strategies for Weighted Sampling to replace the traditional random masking process:

1. Frequency Weighted Sampling (Static)

This approach utilizes a static calculation based on the pre-training corpus. The authors compute token frequencies and apply a power-law transformation to assign higher masking weights to rare tokens. Under this regime, a word's rarity directly correlates with its probability of being selected for masking, forcing the model to focus on the informative "tail" of the distribution.

2. Dynamic Weighted Sampling (Active)

This more sophisticated approach functions similarly to active learning. The model maintains a "weight dictionary" in memory that tracks its performance. During training, if the model exhibits a high cross-entropy loss when predicting a specific token—indicating that it is struggling to understand that word's context—the token's weight in the dictionary is increased. In subsequent iterations, the model is more likely to mask the words it previously failed to predict, effectively creating a curriculum that focuses on the model's current weaknesses.

Experimental Setup

The authors evaluated WSBERT using the following framework:

  • Pre-training: BERT was continually pre-trained on the WikiText dataset (approximately 100 million tokens) using the proposed weighted sampling strategies.
  • Sentence Similarity (STS): To measure the quality of sentence embeddings, the model was tested on the Semantic Textual Similarity (STS) benchmarks (2012–2016), STS-B, and SICK-Relatedness.
  • General Transfer Learning (GLUE): To ensure the model retained its general-purpose utility, it was evaluated on the General Language Understanding Evaluation (GLUE) benchmark, including tasks such as MNLI (inference), QQP (question parity), and SST-2 (sentiment analysis).

Key Results and Insights

Significant Gains in Semantic Similarity

WSBERT (Dynamic) achieved an average STS score of 63.24, representing a +6.54 point improvement over the base BERT model (56.70). Notably, it also outperformed a version of BERT that underwent continued pre-training with standard random masking (BERT-CP), proving that the improvement stems from the sampling strategy rather than just additional training time.

Improved Downstream Performance

Unlike some contrastive learning methods (such as SimCSE), which often improve sentence embeddings at the cost of downstream task performance, WSBERT improved the average GLUE score by 0.52 points over BERT. This suggests that focusing on rare and difficult tokens enhances the model's overall linguistic understanding.

Mitigation of Frequency Bias

Analysis of the embedding space revealed that WSBERT successfully reduced anisotropy. In the WSBERT space, rare tokens have more common tokens among their nearest neighbors compared to standard BERT. This indicates that the model has successfully "filled out" the representation space, moving rare tokens closer to the center of the distribution and making the embeddings more robust.

Limitations and Future Work

The primary drawback of Weighted Sampling is computational overhead. Calculating and updating weights for every token in the vocabulary adds significant time to the training cycle. In the authors' experiments, training with weighted sampling took approximately 20 hours, compared to 11 hours for standard MLM.

Future research will focus on optimizing this process, potentially through parallelizing dictionary updates or using in-memory vectors to mitigate the speed penalty. Additionally, the team plans to investigate whether a specific "frequency bias penalty" could be integrated directly into the loss function to achieve similar results with less overhead.

Conclusion

WSBERT demonstrates that not all tokens are created equal in the eyes of a language model. By shifting the focus from redundant, high-frequency words to informative, rare, and difficult tokens, Weighted Sampling provides a more efficient path toward high-quality, isotropic language representations. This research highlights the importance of sampling strategies in pre-training and offers a clear path forward for improving the semantic depth of transformer-based models.

Summary updated

BibTeX
@inproceedings{zhang2023weighted,
  author = {Linhan Zhang and Qian Chen and Wen Wang and Chong Deng and Xin Cao and Kongzhang Hao and Yuxin Jiang and Wei Wang},
  title = {{Weighted Sampling for Masked Language Modeling}},
  booktitle = {IEEE International Conference on Acoustics, Speech and Signal Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=t8ghMfQ2kD},
}
Figure