Research

BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding

BERT made deep bidirectional Transformer representations practical by pre-training on unlabeled text and fine-tuning the same model for many NLP tasks.

Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina ToutanovaOct 11, 2018Proceedings of NAACL-HLT 2019 (Long and Short Papers)14 min readGoogle AI Language
FOUNDATIONALNatural Language ProcessingAdvanced

Key takeaway

BERT is an encoder-only Transformer pretrained using masked language modeling and next sentence prediction. By allowing each token to use both left and right context, it learns contextual representations that can be adapted to classification, question answering and token-level tasks with a small task-specific output layer.

Updated on Nov 18, 2026

01

Abstract

The shift from task-specific NLP to reusable language representations

Before BERT, strong NLP systems often relied on task-specific architectures or language models that read text in only one direction. BERT introduced a deeply bidirectional Transformer encoder pretrained on unlabeled text. Each token representation can incorporate information from both its left and right context.

The same pretrained parameters can then initialize models for many downstream tasks. Fine-tuning adds a small task-specific output layer and updates the full network, rather than building a new architecture from scratch for every task.

02

Background

Papers that lead into and follow BERT

Attention Is All You Need (2017): Introduced the Transformer architecture that BERT uses as its encoder backbone.

Improving Language Understanding by Generative Pre-Training (2018): Demonstrated large-scale generative pretraining with a left-to-right Transformer, a key contrast to BERT's bidirectional objective.

RoBERTa (2019): Revisited BERT's training recipe, including the value of removing next sentence prediction and training longer with more data.

ALBERT (2019), ELECTRA (2020) and DeBERTa (2020): Explored parameter sharing, replaced-token detection and disentangled attention to improve efficiency or representation quality.

Figure 3

Why bidirectionality changed pre-training

The comparison isolates the central architectural difference. BERT's Transformer layers let each token attend to context on both sides at every layer, so a representation can use information from the entire sentence. OpenAI GPT in this comparison is left-to-right: each position can use earlier tokens but not future tokens. ELMo combines separate left-to-right and right-to-left LSTMs, then exposes their features to a downstream model. BERT and GPT are fine-tuned by updating the model for the target task; ELMo is primarily a feature-based approach. The comparison explains why BERT can form deeply contextual representations before seeing task labels.

Figure 3 from Devlin et al. (2019), supplied by the user. Paper: NAACL 2019.

03

Methodology

Learn context from both directions

A conventional left-to-right language model predicts the next token using only earlier tokens. BERT instead hides selected tokens and predicts their original identities using the surrounding context. This masked language modeling objective lets the encoder use both preceding and following words without directly seeing the answer token.

For example, in “The animal sat on the [MASK]”, the model can use context from both sides to predict “mat”. The resulting representation is contextual: the same word can have different vectors in different sentences.

Two-stage learning: pretraining, then fine-tuning

Pretraining: Train on large unlabeled text using masked language modeling and next sentence prediction. This produces general-purpose contextual parameters.

Fine-tuning: Initialize a task model from the pretrained checkpoint, attach a small output layer suited to the task, and update all parameters on labeled examples. For classification, the final [CLS] representation feeds a classifier. For token-level tasks, token representations feed task-specific heads. For extractive question answering, the model predicts start and end positions in the passage.

The key idea is parameter reuse: each task starts from the same learned language representation rather than learning language features from scratch.

Figure 1

One encoder, two phases: pre-training and fine-tuning

This overview explains BERT's transfer-learning recipe. During pre-training, the Transformer encoder learns from unlabeled sentence pairs using masked language modeling (MLM), which hides some tokens and asks the model to recover them, and next sentence prediction (NSP), which asks whether one sentence follows another in the original text. During fine-tuning, the same encoder is initialized with those learned parameters and paired with a small task-specific output layer. The task heads differ for MNLI, NER, and SQuAD, but the encoder parameters are fine-tuned end to end rather than kept fixed. The [CLS] token provides a sequence-level representation, while [SEP] separates sentence segments.

Figure 1 from Devlin et al. (2019), supplied by the user. Paper: NAACL 2019.

04

Architecture

A stack of bidirectional Transformer encoder blocks

BERT uses the Transformer encoder architecture from Vaswani et al. It contains multi-head self-attention and feed-forward layers, with residual connections and normalization.

The paper evaluates two configurations:

  • BERT_BASE: 12 encoder layers, hidden size 768, 12 attention heads, approximately 110 million parameters.
  • BERT_LARGE: 24 encoder layers, hidden size 1024, 16 attention heads, approximately 340 million parameters.

Unlike a decoder-only causal language model, BERT's self-attention is not restricted to attending only to earlier tokens. Every token can attend to the other tokens in the input sequence.

Figure 2

How BERT builds each input vector

Every input position is represented by adding three learned embeddings: a token embedding identifies the wordpiece, a segment embedding marks whether it belongs to sentence A or sentence B, and a position embedding records its location. In the example, [CLS] starts the sequence, [SEP] separates the two sentences, and the word 'playing' is split into 'play' and the ##ing wordpiece. This additive representation lets one encoder use both token identity and sentence-pair structure while self-attention mixes information across the full sequence.

Figure 2 from Devlin et al. (2019), supplied by the user. Paper: NAACL 2019.

05

Training

Masked language modeling and the 80/10/10 replacement rule

The authors select 15% of WordPiece token positions as prediction targets. Of those selected positions, 80% are replaced by [MASK], 10% by a random token, and 10% are left unchanged. The model predicts the original token at each selected position.

The random and unchanged cases reduce the mismatch between pretraining, where [MASK] appears, and fine-tuning, where it does not. This is not conventional dropout-style regularization; it is part of the paper's masked-token corruption strategy.

Training data, sequence lengths and model scale

The pretraining corpus combines BooksCorpus (about 800 million words) and English Wikipedia (about 2.5 billion words). The authors use document-level text so that contiguous sequences remain available for the next sentence task.

BERT is pretrained with batches of sequences and a two-phase sequence-length schedule: most steps use sequences of length 128, followed by a smaller number of steps at length 512. BERT_BASE and BERT_LARGE differ in depth, hidden size and number of attention heads, trading compute for representational capacity.

A practical BERT fine-tuning workflow

  1. Tokenize text with the matching WordPiece tokenizer and vocabulary.
  2. Build input IDs, attention masks and token-type IDs for sentence pairs.
  3. Add [CLS] at the start and [SEP] between segments and at the end.
  4. Load a pretrained BERT encoder checkpoint.
  5. For classification, use the final [CLS] representation with a linear classifier; for token labeling, use per-token representations; for extractive QA, predict start and end positions.
  6. Fine-tune the full model on labeled task data and validate hyperparameters on a held-out split.

For a modern implementation, use a maintained Transformers library checkpoint and keep the tokenizer, model configuration and checkpoint version aligned.

06

Experiments

Figure 4

The same encoder adapts to many task shapes

BERT keeps the input format stable and changes only the prediction head. Sentence-pair classification uses the [CLS] representation to predict one label for two segments; single-sentence classification uses [CLS] for tasks such as sentiment or linguistic acceptability. In question answering, the model predicts the start and end token positions of the answer span inside the question-plus-passage sequence. For token tagging, each token representation receives its own label, as in named-entity recognition. This is the practical meaning of fine-tuning: the encoder's contextual features are reused, while a small output layer matches the supervision available for each task.

Figure 4 from Devlin et al. (2019), supplied by the user. Paper: NAACL 2019.

07

Results

Eleven NLP tasks and major benchmark gains

These results demonstrated that one pretrained encoder could transfer effectively across sentence-level and token-level tasks, including natural language inference, sentiment classification, named entity recognition and question answering.

How the paper evaluated transfer

The authors evaluate BERT on GLUE, MultiNLI, SQuAD v1.1 and SQuAD v2.0, among other tasks. The evaluation covers sentence classification and inference, semantic similarity, and extractive question answering.

A useful reproduction should report the benchmark-specific metric (accuracy, F1, exact match or correlation), identify whether a result is single-model or ensemble, and separate development-set tuning from test-set reporting. The paper also compares BERT_BASE and BERT_LARGE to study the effect of scale.

Figure 5

BERT's GLUE benchmark results

This table reports BERT's transfer performance across the GLUE suite. BERT_BASE improves over the earlier baselines shown, while BERT_LARGE reaches the strongest average in the table at 82.1. The columns use task-appropriate metrics: accuracy for most tasks, F1 for QQP and MRPC, and Spearman correlation for STS-B. The MNLI column contains matched and mismatched accuracy, so it is shown as two values. The result is not one universal score from one dataset; it is evidence that the same pre-trained encoder transfers across sentence-pair classification, linguistic acceptability, semantic similarity, and textual entailment tasks.

Table 1 from Devlin et al. (2019), supplied by the user. Paper: NAACL 2019.

08

Limitations

Important caveats and later corrections

BERT is an encoder, not a natural left-to-right text generator. Its masked-token objective is not the same as the autoregressive objective used by GPT-style models.

The original paper's next sentence prediction objective was later questioned: follow-up work such as RoBERTa showed that removing NSP while changing the training recipe could improve performance. The 80/10/10 masking procedure also creates a pretraining/fine-tuning mismatch that the authors attempt to reduce rather than eliminate.

BERT's bidirectional attention requires the full input sequence, so it is not directly suited to streaming next-token generation. The original English-only training corpus also limits language coverage.

09

Conclusion

What BERT changed

  • Deep bidirectional pretraining using masked language modeling.
  • A unified Transformer encoder that supports sentence-level and token-level tasks.
  • A simple transfer workflow: initialize from pretrained weights, add a small task head, then fine-tune end-to-end.
  • Strong results across a broad suite of NLP benchmarks without extensive task-specific architecture engineering.

BERT helped make pretrained language encoders a default starting point for applied NLP and inspired many domain-specific and multilingual descendants.

Key takeaways

  • BERT is an encoder-only Transformer with bidirectional self-attention.
  • Masked language modeling allows pretraining with left and right context.
  • Pretraining learns general representations; fine-tuning adapts them to a task.
  • [CLS] is commonly used for sequence-level classification; token outputs support tagging and question answering.
  • BERT_BASE has 110M parameters; BERT_LARGE has 340M.
  • BERT is foundational for language understanding, but its objective is not designed for open-ended autoregressive generation.

Frequently asked questions

What does BERT stand for? Bidirectional Encoder Representations from Transformers.

Why is BERT bidirectional? Its self-attention lets each token use both left and right context, rather than imposing a causal left-to-right mask.

What is masked language modeling? Randomly hide selected input tokens and train the model to recover their original identities from context.

What is next sentence prediction? A binary pretraining task that predicts whether sentence B follows sentence A in the corpus.

Is BERT a decoder or encoder? It is an encoder-only Transformer.

Can BERT generate paragraphs like GPT? Not natively. Its training objective and bidirectional attention are designed for understanding representations, not standard autoregressive generation.

Browse all research