What is BERT and masked language modeling?
BERT (Bidirectional Encoder Representations from Transformers) is a Transformer encoder pre-trained to fill in hidden words using context from both sides. This training task is called masked language modeling: about 15% of the tokens in each sentence are hidden, and the model learns to predict them. After pre-training, BERT produces a contextual vector for every token and can be fine-tuned for a specific task with one small extra layer.
Google released BERT in 2018, and it set new records on 11 language-understanding benchmarks at once. It made the pre-train then fine-tune recipe standard in NLP. BERT-style encoders still run search ranking, text classification, named entity recognition, extractive question answering, and the embedding models behind semantic search and retrieval-augmented generation.
Key ideas
- BERT is a Transformer encoder that reads the whole sentence in both directions at once.
- Masked language modeling hides about 15% of tokens and trains the model to predict them.
- Selected tokens become [MASK] 80% of the time, a random word 10%, and stay unchanged 10%.
- The loss is cross-entropy on the selected positions only.
- Fine-tuning adds a small task head and updates all weights for a few epochs.
- Each occurrence of a word gets its own vector, so different senses separate.
Common questions
What is the difference between BERT and GPT?
Both are built from Transformer blocks. BERT is an encoder with attention in both directions, trained to fill in masked words, which makes it strong at understanding tasks. GPT is a decoder with causal attention, trained to predict the next word, which lets it generate text.
Can BERT generate text?
Not well. It predicts masked tokens in parallel from a fixed-length input and has no built-in left-to-right process for writing new text. Generation needs a decoder, as in GPT, or an encoder-decoder model such as T5 or BART.
What are the [CLS] and [SEP] tokens?
[CLS] is added at the start of every input, and its final vector acts as a summary of the whole sequence for classification. [SEP] marks the boundary between two segments, such as a question and a passage, and BERT also adds a segment embedding so each token knows which part it belongs to.
Is BERT still used?
Yes. Large generative models handle many tasks through prompting, but BERT-style encoders are far cheaper to run and remain the usual choice for high-volume classification, ranking, and embeddings. Newer encoders such as RoBERTa, DeBERTa, and ModernBERT follow the same masked-language-model design.
Related explainers
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- How Self-Attention Works — Query, key, value — the core of self-attention
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
- Transfer Learning — Standing on the shoulders of pretrained giants
- Embeddings — Turning words and categories into vectors
References
- Devlin et al. (2019), "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding", NAACL
- Liu et al. (2019), "RoBERTa: A Robustly Optimized BERT Pretraining Approach"
- Clark et al. (2020), "ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators", ICLR
- Wettig et al. (2022), "Should You Mask 15% in Masked Language Modeling?"
- Reimers & Gurevych (2019), "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks"
Last updated