TL;DR
Masked language modeling takes a text, hides a random selection of its tokens, and trains a model to recover the hidden ones from the visible words. Because the model sees the words on both sides of each gap, it learns representations of text that capture its meaning in context. Those representations are then reused for tasks such as classification, search and tagging.
Where you'll see it: the pretraining objective of BERT and its many descendants, and the encoder models behind sentiment analysis, search ranking and named-entity recognition.
Intuition
Fill in the blank
A cloze test is a fill-in-the-blank exercise. "The ___ sat on the mat" has several possible answers, and a reader narrows them down with the words on both sides. The word "sat" tells you the subject is probably an animal or a person, and "on the mat" says where it is.
A left-to-right Language Model can only use the words before the gap. Given "The", it has almost nothing to go on. Masked language modeling gives the model the whole sentence with some words replaced by a placeholder, so each prediction can use the context from both directions. That is the reason to use it: for understanding text, a word's meaning depends on what comes after it as well as before it.
Why this is useful beyond the blank
Training on the blanks is a means to an end. To fill them in well, the model has to build internal representations of each word that encode grammar, topic and meaning. Those representations are the valuable output. After pretraining, you can attach a small output layer to them and train it on a labelled task with relatively little data. A review that says "a dull, forgettable film" and one that says "a gripping, unforgettable film" get different representations, and a classifier built on top can tell them apart.
The mechanism looks like this:
flowchart LR A[Original text] --> B[Hide about 15 percent of tokens] B --> C[Encoder reads the whole corrupted text] C --> D[Predict the hidden tokens] D --> E[Loss only on the hidden positions]
The math
Setup
Let be a sequence of tokens from the vocabulary . Choose a random set of positions to hide. In the BERT setup each position is chosen with probability about 0.15. Build a corrupted sequence by replacing each token at a position in with a special placeholder token, written [MASK]. The other tokens are left as they are.
The objective
The model reads the corrupted sequence and, at each hidden position, outputs a distribution over the vocabulary. The training objective is the average negative log-probability of the original tokens at the hidden positions:
This is a cross-entropy loss against the true tokens, with the sum running only over . Visible tokens contribute nothing to the loss. They are still read by the model, because they are the context for the predictions.
What the model computes
A transformer encoder with bidirectional attention turns into one hidden vector per position. At a masked position , a linear layer maps to one logit per vocabulary token, and a Softmax turns those logits into . The architecture is the difference from a left-to-right model. Every position attends to every other position, so depends on words to its left and right.
A subtle approximation
Compare this with the chain rule used for a Language Model. There, the probability of the whole sequence is exactly a product of next-token conditionals. Masked language modeling does not model the joint distribution of the hidden tokens. Given one corrupted input, it predicts each hidden token separately:
This treats the hidden tokens as conditionally independent given the visible context. That is an approximation. "New York" with both words hidden is one example where the two blanks depend on each other, and the objective ignores the dependence. The practical consequence is that a masked model does not give you a clean probability for a whole sentence. The closest quantity is the pseudo-log-likelihood, which hides one token at a time, scores it, and sums the results. That requires one forward pass per token, which is expensive.
How much is hidden
If and each position is chosen with probability 0.15, the expected number of hidden tokens is . Each sentence therefore gives about 3 loss terms, which is one reason masked models are less efficient per pass than next-token models, where every position contributes a term.
Worked examples
The sentence is "the cat sat on the mat", with . Hide position 2 ("cat") and position 6 ("mat"). The model sees "the [MASK] sat on the [MASK]".
Suppose the model gives these probabilities for the true tokens:
- at position 2,
- at position 6,
- Loss at position 2: nats.
- Loss at position 6: nats.
- Average over the two hidden positions: nats.
- The four visible tokens ("the", "sat", "on", "the") add nothing to the loss.
Interpretation: the model is on average about as unsure as a choice among equally likely tokens at these two positions. The loss is computed only where a token was hidden, and the other positions only supply context.
Take the sentence "The bank raised interest rates", and hide "bank". The left-to-right model sees only "The", so it must guess whether a river bank or a financial bank is meant. Its probability for "bank" is about the same whichever words follow, because it never sees them.
Illustrative numbers, not measured ones: a left-to-right model gives . A masked model sees "The [MASK] raised interest rates" and gives .
- Left-to-right loss: nats.
- Masked loss: nats.
Interpretation: the words "raised interest rates" to the right of the gap settle the meaning, and the loss drops by a factor of about 24. This is the kind of information a bidirectional model can use and a left-to-right one cannot.
Tradeoffs and failure modes
Masked models are strong at understanding text and weak at writing it. Because they predict blanks in the middle of a sentence, they are not designed to produce a continuation, and sampling from them left to right gives poor results. For generation, a causal model is the right tool.
A second issue is the gap between pretraining and use. The placeholder [MASK] appears during pretraining but never in real inputs. BERT handles this by choosing a hidden position and then replacing it with [MASK] 80 percent of the time, with a random token 10 percent of the time, and leaving the original token 10 percent of the time. The model cannot tell which case it is in, so it has to keep a good representation of every token.
| Situation | What goes wrong | Better choice |
|---|---|---|
| You need to generate text | Masked models do not produce a clean left-to-right continuation | A causal Language Model |
| You need a sentence probability | The independence approximation breaks the joint distribution | A causal model, or pseudo-likelihood if you accept the cost |
| You need a vector or label for each text | This is the main use case | An encoder pretrained with MLM, then fine-tuned |
| Little labelled data for the task | A from-scratch model overfits | Fine-tune a pretrained encoder |
| Each pass should give maximal learning signal | Only about 15 percent of positions give a loss term | Denser objectives that score every token, such as replaced-token detection |
| Input is very long | Full attention cost grows quickly with length | Long-context attention variants, or chunking |
History
The idea comes from the cloze procedure, introduced by Wilson Taylor in 1953 as a way to measure how readable a text is. Readers filled in words deleted from a passage, and their accuracy was taken as a measure of comprehension. It was a test for people, not a training objective for machines.
Machine learning models later used the same idea for pretraining. Predicting a word from its surrounding context already appeared in word-embedding methods in the 2010s, but the breakthrough for deep transformers came in 2018, when Jacob Devlin and colleagues at Google published BERT. BERT was a deep bidirectional transformer pretrained with masked language modeling and a second objective. Fine-tuned on many benchmark tasks, it beat the best previous systems by large margins, and it became the default starting point for language understanding. Later work such as T5 used a related span-corruption objective inside a Text-to-Text Framework.
It caught on because a single pretraining run on unlabelled text could be reused for many tasks. The labelled data then only had to teach the final step.
Check yourself
QuestionWhat fraction of tokens does a BERT-style setup choose to hide in each sequence, and what happens to the visible tokens in the loss?Show answer
About 15 percent of positions are chosen. The loss is computed only at the chosen positions. Visible tokens contribute nothing to the loss directly, but the model reads them, and they supply the context for each prediction.
QuestionA 20-token sentence has 2 hidden tokens. The model gives probabilities 0.4 and 0.1 to the true tokens. What is the average loss in nats, and what is the corresponding perplexity over these two hidden positions?Show answer
The losses are and . The average is nats. The perplexity is , so the model is as uncertain as a uniform choice among five tokens at these positions.
QuestionWhy does a masked model not give a clean probability for a whole sentence, and why does that matter if you want to generate text?Show answer
The objective predicts each hidden token separately given the visible context, which treats the hidden tokens as conditionally independent. That breaks the exact chain rule, so the product of the predictions is not the true joint probability of the sentence. For generation, you need to produce tokens one after another from a well-defined distribution. A masked model does not supply that, so a causal language model is the better choice for writing text.
QuestionA team wants a classifier that labels product reviews as positive or negative, with only a few thousand labelled reviews. Should they start from a masked encoder or a left-to-right generative model, and why?Show answer
A masked encoder is a sensible starting point. Its pretrained representations use both sides of each word, which suits a classification decision that depends on the whole review. A small output layer fine-tuned on a few thousand labelled reviews can reuse those representations. A left-to-right model can also be used, usually by reading the representation at the last token, but the masked encoder is the more direct fit for this kind of task.