Masked Language Modeling
Masked Language Modeling (MLM) is a self-supervised pre-training technique where a model learns to predict randomly hidden ("masked") words in a sentence using only the surrounding context. It matters enormously in modern AI because it allows transformers like BERT to learn rich, bidirectional language understanding from raw text — no human labels required. The counterintuitive fact: a model trained to do nothing but fill in blanks turns out to be one of the most powerful general-purpose language understanding systems ever built.
The "aha moment" is this: by forcing a model to reconstruct missing words, you inadvertently teach it everything about language — grammar, facts, relationships, even common sense — because the only way to reliably fill in a blank is to genuinely understand the surrounding context. It's a beautiful trick: the training task is trivially defined (mask some tokens, predict them back), but the knowledge required to solve it is vast. Unlike reading a sentence left-to-right, MLM lets the model look in both directions at once, making its understanding richer than traditional language models.
How It Works
MLM training proceeds in three clean steps: mask, encode, predict.
Step 1 — Masking. Given a sequence of tokens, randomly select 15% of them. Of those selected tokens:
- 80% are replaced with a special `[MASK]` token
- 10% are replaced with a random other token
- 10% are left unchanged
This mixed strategy (not just always masking) prevents the model from learning to only pay attention to `[MASK]` symbols. It must treat every token as potentially important.
Step 2 — Encoding. The corrupted sequence is passed through a transformer encoder, which produces a contextual embedding for every token position — masked or not — simultaneously.
Step 3 — Predicting. For each masked position, the model uses the embedding at that position to predict what the original token was, via a linear layer followed by a softmax over the entire vocabulary. The loss function is:
Explanation: The model is penalized for assigning low probability to the true original token at each masked position — the better its guesses, the smaller the loss. Where:
- is the set of masked token positions
- is the true original token at position
- is the corrupted input sequence (with masks)
- is the model's predicted probability for the correct token
The masking probability is typically fixed:
Explanation: Only 15% of tokens are selected for prediction — enough signal to learn from, but enough context left visible that the task is solvable. Here is a minimal Python sketch of the masking procedure: ```python import random MASK_TOKEN = "[MASK]" VOCAB = ["the", "cat", "sat", "on", "mat", "dog", "ran", "a"] def apply_mlm_mask(tokens, mask_prob=0.15): labels = [] # what we want the model to predict masked = list(tokens) for i, token in enumerate(tokens): if random.random() < mask_prob: labels.append(token) # save the true label roll = random.random() if roll < 0.80: masked[i] = MASK_TOKEN # 80%: replace with [MASK] elif roll < 0.90: masked[i] = random.choice(VOCAB) # 10%: random token
# else: 10% leave unchanged, label still recorded else: labels.append(None) # not masked, no prediction needed return masked, labels tokens = ["the", "cat", "sat", "on", "the", "mat"] masked_tokens, labels = apply_mlm_mask(tokens) print("Original:", tokens) print("Masked: ", masked_tokens) print("Labels: ", labels) ``` Example output: ``` Original: ['the', 'cat', 'sat', 'on', 'the', 'mat'] Masked: ['the', '[MASK]', 'sat', 'on', '[MASK]', 'mat'] Labels: [None, 'cat', None, None, 'the', None] ``` The model only computes loss on positions where `labels[i] is not None`.
Jacob Devlin is a research scientist at Google who led the team that introduced BERT (Bidirectional Encoder Representations from Transformers) in 2018, the paper that made MLM the dominant pre-training paradigm. His key insight was combining the transformer encoder architecture with MLM and a second task (Next Sentence Prediction) to produce embeddings that shattered benchmarks across 11 NLP tasks simultaneously. A surprising fact: BERT's name was partly chosen as a nod to the Sesame Street character, starting a tradition of NLP models named after Muppets (ELMO, ERNIE, Big Bird).
Intuition
Imagine you're doing a crossword puzzle. To fill in a missing word, you don't just read the clue — you look at intersecting words above, below, left, and right, all at once. That's exactly what MLM does. It removes words from a sentence and asks the model: "given everything around this blank, what was here?" Compare this to a different game where you read a story one word at a time, always guessing the next word — that's causal language modeling. The crossword version (MLM) forces much richer reasoning because you must reconcile clues from both sides of the gap. After billions of such puzzles on web text, the model has effectively memorized the rules of the whole crossword grid — which is language itself. Fine-tuning later is just telling the model which specific squares of that giant grid your application cares about.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
BERT and Its Variants · Causal Language Modeling · Self-Attention Mechanism · Transfer Learning in NLP · Tokenization