Language AI GUIDE
Masked Language Modeling
Masked language modeling (MLM) trains a bidirectional encoder to predict selected input tokens from surrounding context.
Listen to this guideAI-narrated · ~1 min
On this page1 min read
Overview
BERT uses a token-corruption scheme; later methods alter the masking or prediction objective.
Deep Dive
BERT’s pretraining combines masked language modeling with a sentence-pair objective in its original formulation. For MLM, selected tokens are hidden or altered and the model predicts the original tokens using both left and right context. RoBERTa removes the next-sentence prediction objective in its experiments and uses dynamic masking. ELECTRA instead trains a discriminator to classify original versus generator-replaced tokens. These approaches differ in objective, data handling and model design.
Real-World Implementation
BERT masks selected token positions and computes prediction loss for those positions.
RoBERTa uses dynamic masking so selected positions can vary across passes.
ELECTRA trains a discriminator to detect tokens replaced by a generator.
Compare MLM with autoregressive next-token training, which uses a causal context.
The Future of Masked Language Modeling
Choose a pretraining objective based on the model’s intended use and available compute/data. Report tokenizer, masking policy and evaluation tasks. MLM’s bidirectional context supports representation learning, while autoregressive training naturally matches left-to-right generation; neither objective is universally best.
Putting it into practice
Why it matters
- Speed and scale. Language workflows can move faster without sacrificing consistency.
- Access and reach. It expands access across languages and communication styles.
- Clearer decisions. Teams can spend more time on judgment while automation handles repetition.
Watch out for
- Hallucinated facts can quietly enter reports, support flows, or research outputs.
- Prompt sensitivity can create inconsistent results across similar requests.
- Sensitive text data may be exposed if access controls are weak.
Next steps
- Define output format, tone, and quality standards before rollout.
- Ground responses with trusted sources whenever accuracy matters.
- Keep a human review checkpoint for high-stakes outputs.
- Track failure patterns and retrain prompts or workflows regularly.
Keep Exploring
Free newsletter
Get the daily AI briefing
Three verified AI stories every weekday morning, written in plain English. Free forever, no ads.
One email each weekday. Unsubscribe in one click. We never sell or share your address.
Test yourself
Take the Masked Language Modeling quiz
Instant feedback on every answer, and a shareable certificate with a verifiable ID once you pass a course.
Support free AI education. AI Understanding is a 501(c)(3) nonprofit — no ads, no paywall, ever. Make a donation
Frequently asked questions
What is Masked Language Modeling?
Masked language modeling (MLM) trains a bidirectional encoder to predict selected input tokens from surrounding context. BERT uses a token-corruption scheme; later methods alter the masking or prediction objective.
What is the core training task in masked language modeling?
MLM hides a fraction of tokens and trains the model to recover them using both left and right context.
Roughly what percentage of tokens does BERT typically mask during pretraining?
BERT masks approximately 15% of input tokens, a balance between giving enough signal and leaving enough context.
Why does MLM give a model bidirectional understanding?
The Transformer encoder uses non-causal self-attention, so every token can see all others on both sides.
In BERT's masking scheme, what happens to about 10% of selected positions instead of becoming [MASK]?
To improve robustness, 10% of masked positions are swapped for a random token and 10% are left unchanged.
On which positions is the MLM loss computed?
Cross-entropy loss is applied only to the masked positions, comparing predictions to the original token IDs.
Keep learning
Related guides
More guides picked for this topic