BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
- Publication
- NAACL-HLT 2019Conference
- First submitted to arXiv
- arXiv:1810.04805
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{transformersbert2018,
title = {{BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding}},
author = {Jacob Devlin and Ming-Wei Chang and Kenton Lee and Kristina Toutanova},
year = {2018},
eprint = {1810.04805},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1810.04805}
}
After the introduction of the Transformer architecture, many researchers began exploring ways to better utilize it. GPT-1, discussed previously, opted for a decoder-only architecture. In contrast, BERT chose an encoder-only approach.
Defining the Problem
There are generally two strategies for applying pre-trained language models to downstream tasks:
-
Feature-based methods:
- Examples like ELMo (Peters et al., 2018a) use task-specific architectures that include pre-trained representations as additional features.
-
Fine-tuning methods:
- Examples like OpenAI GPT introduce minimal task-specific parameters and train on the downstream tasks by simply fine-tuning all pre-trained parameters.
GPT uses a left-to-right language model; ELMo concatenates separately trained directional representations. They are not the same single language model, and neither jointly conditions every layer on both sides as BERT does.
Unidirectionality
In OpenAI GPT, a left-to-right architecture is used, where each token can only attend to previous tokens in the Transformer’s self-attention layers. This limitation is not ideal for sentence-level tasks and can be particularly detrimental when applying fine-tuning methods to token-level tasks, such as question answering, where incorporating context from both directions can significantly enhance performance.
Solving the Problem
Model Architecture
BERT itself is a bidirectional Transformer encoder. Base has 12 layers and Large has 24; BERT is not limited to the 12-layer configuration.
-
BERT-Base:
- 12 encoder layers, each following the basic Transformer structure.
- Each encoder layer has 768 hidden dimensions and 12 attention heads.
- Total parameter count is 110M.
-
BERT-Large:
- 24 encoder layers, each following the basic Transformer structure.
- Each encoder layer has 1024 hidden dimensions and 16 attention heads.
- Total parameter count is 340M.
For input data, BERT uses several strategies:
-
Flexible definition of input sequences: In BERT, a "sentence" can refer to any range of continuous text, and "sequence" refers to the input token sequence BERT processes, which may include one or two sentences.
-
WordPiece embeddings: Using a 30,000-token WordPiece vocabulary (Wu et al., 2016) to handle unknown or rare words effectively.
-
Special tokens:
- [CLS] token: The first token of every sequence, used as the representation for classification tasks.
- [SEP] token: Used to separate pairs of sentences, helping BERT distinguish and process information from different sentences.
- [MASK] token: Used in pre-training to randomly mask some tokens for the model to predict.
-
Sentence separation features: Besides the [SEP] token, BERT adds a learned segment embedding to each token to indicate whether it belongs to sentence A or B. The position embedding shown in the figure is added as well.
Pre-training Strategy
The left side is pre-training and the right is fine-tuning. Both stages use the encoder; the output layer and parameter updates depend on the task. BERT's pre-training strategy influenced many subsequent models.
Pre-training uses BooksCorpus and English Wikipedia. The model learns the following two objectives together.
-
Masked Language Model (MLM):
- BERT uses the masked language model (also known as the cloze task) to train deep bidirectional representations. In this task, a certain percentage of input tokens are randomly masked, and the goal is to predict these masked tokens.
- Random masking strategy: About 15% of WordPiece tokens in each input sequence are randomly selected for masking. In 80% of cases, selected tokens are replaced with the [MASK] token, in 10% of cases with random tokens, and in the remaining 10% of cases, they are kept unchanged.
- Predicting masked tokens: For each masked token, BERT uses the corresponding final hidden vector through an output softmax layer to predict the original token, similar to traditional language models.
- Mitigating pre-training and fine-tuning mismatch: Since [MASK] tokens are used during pre-training but not during fine-tuning, this creates a theoretical mismatch. BERT mitigates this by not always replacing selected tokens with actual [MASK] tokens but instead using a random strategy.
-
Next Sentence Prediction (NSP):
- To help the model better understand and capture sentence relationships, BERT introduces the next sentence prediction pre-training task.
- Half the examples use the actual next segment B after A (IsNext); the other half use a randomly sampled B (NotNext). The model predicts this label from [CLS]. NSP supervises adjacency under that sampling scheme, not logical entailment or general semantic relatedness.
- The paper's ablation finds a benefit for some downstream evaluations under this setup.
Fine-tuning Strategy
Look again at the right half of Figure 1: each downstream model starts from the same pre-trained weights and then updates those weights along with its task-specific output layer. That output is not always a classification layer: classification reads [CLS], while question answering uses token representations.
-
Question Answering (SQuAD):
- In question answering tasks, BERT processes the question and the paragraph containing the answer as a single sequence, using different embeddings to identify the question and paragraph parts. During fine-tuning, BERT introduces specific start and end vectors to predict the answer's position within the paragraph.
-
Natural Language Inference (NLI):
- NLI packs both sentences into
[CLS] A [SEP] B [SEP]and classifies from the final[CLS]representation, rather than independently encoding and concatenating two sentence vectors.
- NLI packs both sentences into
The paper reports that fine-tuning its downstream experiments from the same pre-trained checkpoint took at most about one hour on a single Cloud TPU, or a few hours on a GPU. That is fine-tuning time under the paper's conditions; it does not include pre-training.
Discussion
Read the last column first: 82.1 for BERT-Large and 75.1 for GPT are the table averages excluding WNLI. For MNLI-m, the scores are 86.7% and 82.1%, respectively. The text separately reports official GLUE leaderboard scores of 80.5 and 72.8 at the time; those are not MNLI scores either. Large beats Base on the tasks in the table, including those with fewer labels. Fine-tuning Large on small datasets was sometimes unstable, however, and the authors selected the best development result from multiple random restarts.
Moreover, BERT set new state-of-the-art results on 11 different NLP tasks, demonstrating its versatility and effectiveness across various tasks.
Ablation Study
The authors compare pre-training objectives and directional constraints under the same architecture. Which scores change?
-
Without NSP:
- This remains a bidirectional MLM, but removes NSP. Against full BERT-Base, QNLI drops 88.4→84.9, MNLI-m 84.4→83.9, and SQuAD 1.1 F1 88.5→87.9. These are results under the paper's training and data setup, not a requirement that every later model use NSP.
-
LTR & Without NSP:
- A model trained with a left-to-right (LTR) language model without MLM or NSP, retaining its left-only attention restriction during fine-tuning. Against bidirectional MLM without NSP, all listed task scores drop; MRPC 86.5→77.5 and SQuAD F1 87.9→77.8 show the largest gaps.
- On SQuAD, the LTR model’s token prediction performance was significantly worse due to the lack of right-side context support.
-
Adding BiLSTM:
- A randomly initialized BiLSTM is added to the LTR system during fine-tuning. SQuAD F1 rises 77.8→84.9, still below bidirectional MLM without NSP at 87.9; all four listed GLUE development scores fall.
-
Comparison with ELMo’s Strategy:
- The authors discuss separately training and concatenating LTR/RTL models, as in ELMo. This is an alternative they analyze, not an additional measured ablation in the table. It needs two directional models and does not jointly mix both contexts at every layer.
Conclusion
BERT drew attention through strong results across many benchmarks. Reading those results at the time felt like watching a clean sweep. It helped shift NLP research toward Transformers, without making LSTMs disappear from every research area or application.
The paper makes a more specific point: with its data and tasks, bidirectional MLM followed by fine-tuning lets the same pre-trained weights serve different language-understanding tasks. The benefits of NSP and model size still need to be read with their evaluation conditions.