Papers · arXiv 1810.04805
BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
Summary in three lines
- 🎯 Standard language model pre-training is constrained by unidirectional (left-to-right) context, which is suboptimal for sentence-level tasks and harmful for token-level tasks like question answering.
- 🔧 Introduces BERT, which pre-trains a multi-layer bidirectional Transformer encoder using a Masked Language Model (Cloze) objective and Next Sentence Prediction.
- 📈 Achieved state-of-the-art results on 11 NLP tasks, including an 80.5 GLUE score (+7.7 points absolute) and 86.7% accuracy on MultiNLI (+4.6% absolute).
Abstract
We introduce a new language representation model called BERT, which stands for Bidirectional Encoder Representations from Transformers. Unlike recent language representation models, BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly conditioning on both left and right context in all layers. As a result, the pre-trained BERT model can be fine-tuned with just one additional output layer to create state-of-the-art models for a wide range of tasks, such as question answering and language inference, without substantial task-specific architecture modifications. BERT is conceptually simple and empirically powerful. It obtains new state-of-the-art results on eleven natural language processing tasks, including pushing the GLUE score to 80.5% (7.7% point absolute improvement), MultiNLI accuracy to 86.7% (4.6% absolute improvement), SQuAD v1.1 question answering Test F1 to 93.2 (1.5 point absolute improvement) and SQuAD v2.0 Test F1 to 83.1 (5.1 point absolute improvement).
Reading guide
Problem and motivation
Prior language representation models relied either on feature-based approaches (such as ELMo) or fine-tuning approaches (such as OpenAI GPT). While effective, both paradigms were restricted by unidirectional language modeling objectives: standard auto-regressive models process text strictly left-to-right (or right-to-left). In unidirectional models, tokens can only attend to previous tokens in self-attention layers to prevent words from trivially "seeing themselves" across multi-layer contexts. ELMo attempted bidirectionality by shallowly concatenating separately trained left-to-right and right-to-left LSTMs, but this fails to capture deep, joint conditioning on bidirectional context at every layer. This unidirectionality limits sentence-level representations and is especially detrimental for token-level downstream tasks such as question answering, where understanding both left and right context simultaneously is essential.
Key ideas
- Deep Bidirectional Transformer Architecture: BERT uses a multi-layer bidirectional Transformer encoder, allowing self-attention layers to condition jointly on both left and right context across all layers.
- Masked Language Model (MLM): Inspired by the Cloze task, BERT randomly masks 15% of the input tokens and trains the model to predict the masked tokens using their context, resolving the "seeing itself" triviality issue.
- Pre-train/Fine-tune Mismatch Mitigation: Because
[MASK]tokens do not appear during fine-tuning, the selected 15% tokens are replaced with[MASK]80% of the time, a random token 10% of the time, and kept unchanged 10% of the time. - Next Sentence Prediction (NSP): To capture sentence-pair relationships critical for Natural Language Inference (NLI) and Question Answering (QA), BERT pre-trains on a binarized task predicting whether sentence naturally follows sentence () or is a random sentence from the corpus ().
- Unified Architecture for Downstream Tasks: The exact same pre-trained architecture is reused for downstream tasks with minimal added parameters (e.g., a simple linear classification head or start/end span pointers).
Method
Model Configuration
BERT is evaluated primarily on two sizes:
- : layers, hidden size, attention heads, total parameters = .
- : layers, hidden size, attention heads, total parameters = . Feed-forward filter sizes are .
Input and Output Representations
Inputs can represent a single text sequence or a pair of sentences packed together. Embeddings are constructed by summing:
- WordPiece token embeddings (30,000 token vocabulary).
- Segment embeddings (learned embedding identifying whether a token is from sentence or sentence ).
- Learned positional embeddings.
Every sequence begins with [CLS]. Its final hidden vector represents the sequence aggregate. Sentence pairs are separated by [SEP]. Final token hidden representations are denoted .
Pre-training Objectives
- Masked LM: Given masked token , the representation is fed into a softmax over the vocabulary with cross-entropy loss:
- Next Sentence Prediction: The aggregate vector is fed into binary classification: The overall pre-training loss is the sum of the mean masked LM likelihood and the mean next sentence prediction likelihood.
Fine-tuning Adaptations
- Classification (GLUE): Adding a projection matrix , loss is computed as .
- Span Extraction (SQuAD v1.1): Learning start vector and end vector . The probability that token is the start of the answer span is: The score for span is , maximized over .
- Unanswerable Questions (SQuAD v2.0): Extended by allowing the
[CLS]token to serve as the null answer span, scoring , and predicting a non-null answer only when .
Results
BERT was evaluated across 11 NLP tasks:
- GLUE Leaderboard: achieved an average GLUE score of 80.5% (a 7.7% absolute improvement over the previous state of the art) and 86.7% accuracy on MNLI (a 4.6% absolute gain).
- SQuAD v1.1: achieved 84.1 EM and 90.9 F1 on Dev as a single model; an ensemble with TriviaQA pre-training reached 87.4 EM and 93.2 F1 on the Test set, outperforming human performance (82.3 EM / 91.2 F1).
- SQuAD v2.0: reached 80.0 EM and 83.1 F1 on the Test set, outperforming the previous top system by +5.1 F1.
- SWAG: scored 86.3% accuracy on Test, outperforming OpenAI GPT (78.0%) and human expert baseline (85.0%).
- Ablation Findings: Removing NSP degraded performance across MNLI (-0.5%), QNLI (-3.5%), and SQuAD F1 (-0.6%). Replacing MLM with a standard Left-to-Right (LTR) LM caused large drops on SQuAD (-10.7 F1) and MRPC (-9.2% accuracy).
Limitations and open questions
- Pre-train / Fine-tune Discrepancy: The artificial
[MASK]token never appears during fine-tuning, requiring heuristic replacement rules (80/10/10) to mitigate mismatch. - Pre-training Efficiency: Because BERT only predicts 15% of tokens per batch rather than 100% of tokens as in standard autoregressive models, it requires more pre-training steps (1,000,000 steps with 128,000 tokens/batch) to converge.
- Quadratic Attention Bottleneck: Standard Transformer attention scales quadratically () with sequence length, making long sequences expensive and requiring a two-phase pre-training strategy (90% steps at sequence length 128, 10% steps at 512).
- Text Generation Inapplicability: As a bidirectional encoder, BERT is directly structured for representation and discriminative downstream tasks, and cannot directly perform autoregressive left-to-right generation without architectural modifications.
Key terms
- Masked Language Model (MLM)
- A pre-training objective inspired by the Cloze task where random input tokens are replaced by a mask and the model is trained to reconstruct the original token from deep bidirectional context.
- Next Sentence Prediction (NSP)
- A binarized pre-training task where the model predicts whether a given second sentence segment logically follows the first segment or was randomly sampled from the corpus.
- [CLS]
- A special classification token placed at the beginning of every sequence whose final hidden state is used as the aggregate sequence representation for classification tasks.
- [SEP]
- A special token used to separate non-consecutive or distinct sentence segments within a single packed input sequence.
- WordPiece
- A subword tokenization algorithm used by BERT with a 30,000 token vocabulary to handle out-of-vocabulary words and split words into subword units.
- Segment Embeddings
- Learned embeddings added to input token embeddings to distinguish whether a token belongs to sentence span A or sentence span B in packed pairs.
- Fine-tuning
- The downstream training strategy where all pre-trained BERT parameters are updated end-to-end alongside minimal task-specific output layers using labeled data.
- Feature-based approach
- An alternative strategy where fixed contextual representations are extracted from BERT layers without updating its parameters, then fed into separate task-specific architectures.
Questions
Why does BERT replace masked tokens with random tokens or keep them unchanged instead of always using [MASK]?
Because the [MASK] token never appears during fine-tuning, replacing every target token with [MASK] would create a discrepancy between pre-training and fine-tuning. BERT mitigates this by replacing chosen tokens with [MASK] 80% of the time, a random token 10% of the time, and the unchanged original token 10% of the time, forcing the model to keep a distributed representation of all tokens.
What is the difference between BERT and OpenAI GPT?
While both are Transformer-based fine-tuning models, OpenAI GPT uses a left-to-right Transformer decoder where attention is constrained so every token can only attend to previous tokens. BERT uses a bidirectional Transformer encoder where attention attends across both left and right context in all layers, enabled by its masked language model pre-training objective.
Can BERT be used as a feature extractor instead of being fine-tuned end-to-end?
Yes. On the CoNLL-2003 Named Entity Recognition task, extracting and concatenating token activations from the top four hidden layers of BERT and feeding them into a 2-layer BiLSTM achieved 96.1 Dev F1, just 0.3 F1 behind fully fine-tuning the entire model (96.4 F1).
Why did BERT use a two-phase pre-training sequence length schedule?
Self-attention computational complexity is quadratic in sequence length. To reduce computation time, BERT was pre-trained with a sequence length of 128 for the first 900,000 steps (90% of training), and then trained with a sequence length of 512 for the remaining 100,000 steps to learn the positional embeddings.
The overview on this page was written by AI from the paper and may contain mistakes. Read the paper for the details.