How does Masked Language Modeling (MLM) loss work?
Masked Language Modeling (MLM) is a self-supervised denoising objective that enables encoder Transformers such as BERT to learn bidirectional representations. During training, roughly 15% of input tokens are selected—typically corrupted via an 80/10/10 rule (80% [MASK], 10% random token, 10% unchanged)—and the model minimizes cross-entropy reconstruction loss solely over those selected positions using both left and right context. Because standard MLM predicts masked tokens independently given the corrupted input, Span Masking and Permutation Language Modeling extend it to model multi-token dependencies.
Masked Language Modeling (MLM) is a “denoising” pretext task that enables a Transformer to learn bidirectional representations. Unlike Causal Language Modeling (predicting the next token), MLM hides a percentage of the input tokens and tasks the model with reconstructing them based on the surrounding context from both the left and right. This bidirectional nature is what allows models like BERT to capture deep nuances in syntax and semantics that unidirectional models might miss.
- Corruption Strategy: Typically, 15% of the input tokens are selected for the MLM task. To prevent the model from becoming overly dependent on the specific [MASK] token, a “80/10/10” rule is applied to these selected tokens: 80% are replaced with [MASK], 10% with a random token, and 10% remain unchanged.
- Objective Function: The model produces a probability distribution over the entire vocabulary for each masked position. The loss is calculated only on the selected tokens, ignoring the correctly predicted non-masked tokens in the sequence.
- Independent Prediction: MLM traditionally assumes that masked tokens are independent of one another given the unmasked context. This allows for parallel computation of the loss across all masked positions in a single forward pass.
🧪 Core Insights & Mathematical Foundations
Follow-up 1: Explain the purpose of the 80/10/10 corruption rule and its impact on the “Pre-training/Fine-tuning gap.”
The 80/10/10 rule is designed to mitigate the discrepancy between pre-training and fine-tuning. During pre-training, the model sees the [MASK] token frequently, but during downstream fine-tuning (e.g., sentiment analysis), the [MASK] token never appears. If 100% of selected tokens were replaced with [MASK], the model might learn to only generate high-quality representations when it senses the presence of a [MASK] token.
By keeping 10% of the tokens unchanged, the model is forced to maintain an accurate representation of the actual observed token, even when that token is technically “under observation” for the loss. By replacing 10% with random tokens, the model learns that the observed token might be incorrect, forcing it to rely more heavily on the bidirectional context rather than just the token at that specific position. This “jitter” ensures the latent representations remain robust and generalizable to sequences without masks.
Follow-up 2: How do Span Masking and Permutation Language Modeling address the limitations of standard MLM?
Standard MLM has two primary weaknesses: the assumption that masked tokens are independent (the “independence assumption”) and the inability to model relationships between contiguous entities (like “New York”).
- Span Masking (SpanBERT/T5): Instead of masking individual random tokens, Span Masking hides contiguous spans of text (e.g., 3-4 words at once). This forces the model to predict entire phrases from the boundary context, which is significantly harder and results in better performance on span-selection tasks like Question Answering (SQuAD).
- Permutation Language Modeling (XLNet): This approach avoids the [MASK] token entirely. It predicts the target token given a random permutation of the preceding tokens in a factorization order. By using Two-Stream Self-Attention (content and query streams) with a permutation mask, it captures dependencies between all tokens (solving the independence assumption) while remaining bidirectional in its context gathering, effectively combining the strengths of BERT and GPT.