Masked Language Modeling (MLM)
Cover a word in a sentence and ask a person to guess it. They will use the grammar, the topic and the words on both sides of the gap. Train a model to play that game across billions of sentences and it learns to represent words in context.
The objective
Masked language modeling hides a fraction of the input tokens — 15% in the original BERT recipe — and asks the model to recover what was there, taking the loss only at those positions. Because a transformer encoder reads the whole sequence at once, each prediction draws on tokens to the left and the right, which is what makes the resulting representations bidirectional. The selected tokens are not all replaced by the same marker:
- 80% become the [MASK] token.
- 10% are swapped for a random token, forcing the model to judge whether a word belongs.
- 10% are left untouched, so the model cannot assume a position matters only when it is masked.
That split exists because [MASK] never appears in real text, and a model trained to expect it would meet a mismatch the moment it was fine-tuned.
Why it matters
Labelled text is scarce; raw text is effectively free. MLM manufactures its own supervision — the answer is the word that was removed — so a model absorbs grammar, collocation and a great deal of world knowledge before it ever sees a task. BERT established the objective, RoBERTa showed the original recipe was under-trained and that resampling the mask each epoch helps, and ELECTRA replaced it with a cheaper discriminative variant. The cost is that only masked positions produce signal, which makes pre-training expensive per token seen.
Masked language modeling is a self-supervised pre-training objective: a fraction of the input tokens are hidden and the model must recover them from the surrounding text, with loss taken only at those positions. Seeing context on both sides of each gap yields bidirectional representations, unlike left-to-right prediction. It is how BERT and RoBERTa learn grammar and usage from unlabelled corpora before any task-specific fine-tuning.
Remember those school exercises with words blanked out of a sentence — "she poured the ___ into her cup" — where you work out the missing word from everything around it? That is exactly the exercise a computer is set during masked language modeling.
Take an enormous pile of ordinary text, hide a word here and there, and make the software guess each one. Nobody has to mark the answers, because the hidden word is the answer. Do this billions of times and the program picks up how words behave together — enough that it can later be taught a real job, like sorting reviews or answering questions.