Masked Language Modeling: Fill-in-the-Blank Pretraining
MLM hides random tokens and trains the model to reconstruct them from context. It powers BERT-style encoders for classification and search. The catch is that it never learns left-to-right generation, so it needs extra machinery for open-ended text.
WHY IT EXISTS: Before transformers, language models processed text in a single direction, either left-to-right or right-to-left. Each token could only attend to previous tokens, so the representation of a word like bank in river bank was weaker because the model never saw both sides at once. Masked language modeling was invented to give the model full bidirectional context while still providing a difficult training signal instead of letting it copy the input.
THE MENTAL MODEL: Imagine a fill-in-the-blank puzzle. You cover random words in a sentence and ask the model to guess each hidden word from surrounding text. Because blanks can appear anywhere, the model must look both left and right at every position, building a two-way map of meaning rather than predicting the next word in a one-way stream.
HOW IT WORKS: During pretraining, roughly fifteen percent of tokens are selected for masking. Most are replaced with a special MASK token, some are swapped with random words, and a few are left unchanged. The model outputs a probability distribution over the vocabulary for each masked position and is trained to minimize cross-entropy loss against the original tokens. Since the model sees the full sentence except the masked slots, every layer can attend to all other positions in both directions.
WHEN TO USE IT: Use MLM when you need rich contextual embeddings rather than generated text. It excels at sentence classification, named-entity recognition, extractive question answering, semantic similarity, and dense retrieval. Any task where understanding the full context of every word matters more than producing the next token is a natural fit.
WHEN NOT TO USE IT: Avoid pure MLM for open-ended text generation or long-form completion. Because the model learns to reconstruct scattered holes rather than generate sequences causally, it lacks the left-to-right structure needed to write coherent paragraphs. It also treats masked positions as conditionally independent given the context, which can produce inconsistent predictions when multiple blanks interact.
ONE CANONICAL EXAMPLE: BERT is the archetypal masked language model. Given the sentence The cat sat on the MASK and looked outside, BERT must predict mat by attending to sat, on, and looked from both directions. After pretraining on billions of words with this objective, BERT is fine-tuned for tasks like sentiment analysis or question answering, where its bidirectional context gives it a clear edge over unidirectional models.
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.