Skip to main content
GW
HomeWorkAI appsPapersAboutNotesStackLabPilotPlan↗ (opens in a new tab)INVST↗ (opens in a new tab)
Govind Waghmare

Finance systems and AI. Building in public from NYC.

Resume (PDF)Book 20 minEmail
HomeNotesWorkAboutStackPapersLabFor founders
XLinkedInGitHubllms.txt
© 2026 · New York City

Keyboard shortcuts

?
Open this list
g then h
Go to Home
g then w
Go to Work
g then n
Go to Notes
g then a
Go to About
c
Ask the AI guide
Esc
Close this list
Original paperSection A, The abstract
Open in new tab (opens in a new tab)
All papers
FoundationsClassic

BERT: teach a model to read both ways, then adapt it

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

Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova · arXiv preprint (Google AI Language) · Posted 11 Oct 2018 · 16 min read · arXiv 1810.04805

Share this paper

LinkedIn (opens in a new tab)X (opens in a new tab)WhatsApp (opens in a new tab)EmailDownload PDFExplainer (PDF), 750 KB

  • Earlier models read text in one direction only.
  • That limits what they learn about a word from its context.
  • It hurts tasks like question answering, where both sides matter.
80.5%

GLUE score for BERT, a 7.7% point gain over the old best.

  • The authors pre-trained BERT on unlabeled books and Wikipedia text.
  • They fine-tuned it on 11 tasks.
  • They removed parts of BERT to see what matters.
  • They tested two model sizes.

  • BERT set a new best on all 11 tasks.
  • Both-way reading mattered most.
  • The larger model beat the smaller model on every task.
  • Even tasks with little data gained.

BERT beats OpenAI GPT on the GLUE averageFrom Table 1
0255075100GLUE test average scoreOpenAI GPT75.1 averageBERT BASE79.6 averageBERT LARGE82.1 average

Hover, tap or focus a bar to read its exact value.

ModelAverage
OpenAI GPT75.1
BERT BASE79.6
BERT LARGE82.1

  • These are 2018 scores on public benchmarks.
  • BERT-LARGE fine-tuning was unstable on small data. The authors ran random restarts.
  • Pre-training is costly: 4 days on up to 64 TPU chips.
  • The tasks test understanding, not open-ended writing.

Jump to

  1. A The abstract
  2. 1 Introduction
  3. 2 Related work
  4. 3 BERT: the model
  5. 3.1 Pre-training BERT
  6. 3.2 Fine-tuning BERT
  7. 4.1 Results: GLUE
  8. 4.2 Results: SQuAD v1.1
  9. 4.3 Results: SQuAD v2.0
  10. 4.4 Results: SWAG
  11. 5.1 Ablation: the pre-training tasks
  12. 5.2 Ablation: model size
  13. 5.3 Ablation: BERT as fixed features
  14. 6 Conclusion
  15. A Appendix A: more details (summary)
  16. B Appendix B: the GLUE tasks
  17. C Appendix C: more ablations
  18. W Words to know

Every detail from the paper

Cards follow the paper's own order. Dotted words link to Words to know. Numbers match the paper.

The idea

  • BERT means Bidirectional Encoder Representations from Transformers.
  • Earlier models read in one direction.
  • The authors call a both-way model bidirectional.
  • BERT pre-trains on unlabeled text.
  • It uses left and right context in all layers.

The use

  • Add one output layer and fine-tune.
  • This gives top models for many tasks, such as question answering and language inference.
  • It needs no big task-specific changes to the architecture.

The results

  • BERT is simple and works well.
  • It sets new bests on eleven language tasks.
  • GLUE score: 80.5% (a 7.7% point gain).
  • MultiNLI accuracy: 86.7% (a 4.6% gain).
  • SQuAD v1.1 Test F1: 93.2 (a 1.5 point gain).
  • SQuAD v2.0 Test F1: 83.1 (a 5.1 point gain).

Pre-training helps

  • Pre-training a language model helps many language tasks.
  • Sentence-level tasks include language inference and paraphrasing.
  • A token is a small piece of text.
  • Token-level tasks include named entity recognition and question answering.

Two ways to use it

  • Feature-based: use the pre-trained output as extra features in a task model. ELMo does this.
  • Fine-tuning: add few new parameters, then train all on the task.
  • OpenAI GPT does this.
  • Both use one-direction language models in pre-training.

The limit

  • Standard language models read in one direction.
  • This limits which architectures you can use in pre-training.
  • OpenAI GPT is left-to-right. Each token sees only earlier tokens.
  • That is weak for sentence tasks.
  • It can hurt token-level tasks like question answering, where both sides matter.

The fix

  • BERT uses a masked language model (MLM).
  • It hides some tokens at random and predicts the original token from context.
  • It is inspired by the Cloze task.
  • So the model can fuse left and right context.
  • BERT also uses a next sentence prediction task for text pairs.

Contributions

  • One: they show that both-way pre-training matters.
  • Radford et al. use one-direction models.
  • Peters et al. join two shallow ones.
  • Two: pre-trained models cut the need for task-specific architectures.
  • BERT is the first fine-tuning model to be best on many sentence and token tasks.
  • Three: it sets a new best on eleven tasks.
  • Code and pre-trained models are at github.com/google-research/bert.

2.1 Unsupervised feature-based approaches
  • Learning general word representations is decades old.
  • Pre-trained word embeddings beat embeddings learned from scratch.
  • Methods used left-to-right language modeling, or told correct words from wrong ones.
  • Later work moved to sentence and paragraph embeddings.
  • Training used next-sentence ranking, next-sentence generation or denoising.

ELMo

  • ELMo takes features from a left-to-right and a right-to-left language model.
  • A token's representation joins the two.
  • It improved question answering, sentiment analysis and named entity recognition.
  • Melamud et al. predicted a word from both sides with LSTMs.
  • Like ELMo, that model is feature-based and not deeply bidirectional.
  • Fedus et al. show the cloze task makes text generation models more robust.
2.2 Unsupervised fine-tuning approaches
  • Early work pre-trained only word embeddings.
  • Later work pre-trained sentence or document encoders, then fine-tuned them.
  • The gain: few parameters are learned from scratch.
  • OpenAI GPT used this to reach earlier bests on GLUE sentence tasks.
  • It used left-to-right language modeling and auto-encoder objectives.
2.3 Transfer learning from supervised data
  • Transfer also works from big supervised data sets.
  • Examples: natural language inference and machine translation.
  • Computer vision also gains from big pre-trained models.
  • A common recipe is fine-tuning models pre-trained on ImageNet.

Two steps: pre-train, then fine-tuneFrom Figure 1
  1. Pre-train on unlabeled text
  2. Start from pre-trained weights
  3. Add one output layer
  4. Fine-tune all weights

Each task gets its own fine-tuned model, though all start from the same pre-trained weights.

The two steps

  • Pre-training: train on unlabeled data with different pre-training tasks.
  • Fine-tuning: start from the pre-trained weights.
  • Then fine-tune all weights on labeled data.
  • The question answering example in Figure 1 runs through this section.

One design for all tasks

  • BERT has a unified architecture across tasks.
  • The pre-trained and final architectures differ very little.
Model architecture
  • BERT is a multi-layer bidirectional Transformer encoder.
  • It follows the original Transformer closely, from the tensor2tensor library.
  • The paper skips a full description and points to Vaswani et al. (2017).
  • L is the number of layers. H is the hidden size.
  • A is the number of self-attention heads.
ModelLayers (L)Hidden size (H) and heads (A)Parameters
BERT BASE12H=768, A=12110M
BERT LARGE24H=1024, A=16340M

The two model sizes

The feed-forward size is always 4H: 3072 for H=768 and 4096 for H=1024.

Compared to GPT

  • BERT BASE has the same size as OpenAI GPT, for a fair test.
  • The key difference: BERT uses bidirectional self-attention.
  • GPT can only look at context to its left.
  • A bidirectional Transformer is often called a Transformer encoder.
  • The left-context-only kind is called a Transformer decoder, as it can generate text.
Input and output representations

What goes in

  • The input can be one sentence or a pair, packed in one token sequence.
  • A 'sentence' can be any span of text.
  • BERT uses WordPiece embeddings with a 30,000 token vocabulary.
  • The first token is always a special [CLS] token.
  • Its final hidden state stands for the whole sequence in classification tasks.

Telling sentences apart

  • Sentence pairs share one sequence.
  • First, a special [SEP] token separates them.
  • Second, a learned embedding marks each token as sentence A or B.

Notation

  • E is the input embedding.
  • C is the final hidden vector of [CLS].
  • Ti is the final hidden vector of input token i.

Sum of three

  • A token's input is the sum of three embeddings.
  • They are token, segment and position embeddings.
  • Figure 2 shows this.

The approach

  • BERT does not use left-to-right or right-to-left language models.
  • It pre-trains on two unsupervised tasks.
Task 1: masked LM

Why mask

  • A deep bidirectional model should beat left-only and shallow joined models.
  • But standard language models train in one direction only.
  • Two-way conditioning would let each word indirectly see itself.
  • So the authors mask some input tokens at random and predict them.

The recipe

  • They mask 15% of all WordPiece tokens in each sequence.
  • The final hidden vectors of masked tokens go to a softmax over the vocabulary.
  • Only masked words are predicted. The whole input is not rebuilt, unlike denoising auto-encoders.

The mismatch problem

  • The [MASK] token never appears in fine-tuning.
  • So pre-training and fine-tuning do not match.
  • To lower this, chosen tokens are not always replaced by [MASK].
How one chosen token is changedFrom Section 3.1
  1. Pick 15% of positions at random
  2. 80%: replace with [MASK]
  3. 10%: replace with a random token
  4. 10%: keep the token unchanged
  5. Predict the original token

The final vector Ti predicts the original token, with cross entropy loss.

Effect of the recipe

  • The model cannot know which words it must predict.
  • It must keep a good representation of every token.
  • Appendix C.2 compares variations.
Task 2: next sentence prediction

Why

  • Question answering and inference need the link between two sentences.
  • Language modeling does not capture this directly.
  • So the authors pre-train a binary next sentence prediction (NSP) task.
  • It is easy to make from any single-language text.

The recipe

  • For each pair, 50% of the time B really follows A (IsNext).
  • 50% of the time B is a random sentence (NotNext).
  • The vector C is used for NSP.
  • The final model reaches 97%-98% accuracy on NSP.
  • C is not a good sentence representation without fine-tuning.

Compared to earlier work

  • Earlier work transfers only sentence embeddings to the task.
  • BERT transfers all parameters to start the task model.
Pre-training data
  • BooksCorpus: 800M words.
  • English Wikipedia: 2,500M words, text passages only.
  • Lists, tables and headers are dropped.
  • A document-level corpus is critical, to get long contiguous sequences.
  • A shuffled sentence-level corpus such as the Billion Word Benchmark is not enough.

How

  • Self-attention lets BERT handle many tasks, with single text or text pairs.
  • You swap the inputs and outputs.
  • Common practice encodes the two texts alone, then applies cross attention.
  • BERT encodes the joined pair with self-attention.
  • That includes two-way cross attention between the sentences.

Plugging in a task

  • Sentence A and B are like paraphrase pairs, hypothesis-premise pairs or question-passage pairs.
  • For tagging or classification, the second part is empty.
  • Token vectors go to an output layer for tagging or question answering.
  • The [CLS] vector goes to an output layer for classification.
  • All parameters are fine-tuned end to end.

Cost

  • Fine-tuning is cheap next to pre-training.
  • All paper results run in at most 1 hour on one Cloud TPU.
  • On a GPU, they take a few hours.
  • The SQuAD model trains in about 30 minutes on one Cloud TPU.
  • That gives a Dev F1 of 91.0%.
  • Task details are in Section 4 and Appendix A.5.

The test

  • GLUE is a set of varied language understanding tasks.
  • Details of each set are in Appendix B.1.
  • The input is formed as in Section 3.
  • The vector C for [CLS] is the summary of the input.
  • The only new parameters are classification weights W, size K by H.
  • K is the number of labels.
  • The loss is standard classification loss.

Settings

  • Batch size 32, for 3 epochs on all GLUE tasks.
  • They picked the best learning rate on the Dev set.
  • The choices were 5e-5, 4e-5, 3e-5 and 2e-5.
  • BERT LARGE was sometimes unstable on small data sets.
  • So they ran several random restarts and kept the best Dev model.
  • Restarts use the same pre-trained weights.
  • Data shuffling and the classifier start differ.
BERT LARGE has the top GLUE averageFrom Table 1
0255075100GLUE test average scoreOpenAI GPT75.1 averageBERT BASE79.6 averageBERT LARGE82.1 average

Hover, tap or focus a bar to read its exact value.

ModelAverage
OpenAI GPT75.1
BERT BASE79.6
BERT LARGE82.1

What they found

  • BERT BASE and BERT LARGE beat all systems on all tasks by a wide margin.
  • Average gains over the prior best: 4.5% and 7.0%.
  • BERT BASE and OpenAI GPT differ almost only in attention masking.
  • On MNLI, the largest task, BERT gains 4.6% accuracy.
  • On the official GLUE leaderboard, BERT LARGE scores 80.5.
  • OpenAI GPT scores 72.8 as of the writing date.
  • BERT LARGE beats BERT BASE on all tasks, most on tasks with little data.
SystemMNLI-(m/mm), 392kQQP, 363kQNLI, 108k
Pre-OpenAI SOTA80.6/80.166.182.3
BiLSTM+ELMo+Attn76.4/76.164.879.8
OpenAI GPT82.1/81.470.387.4
BERT BASE84.6/83.471.290.5
BERT LARGE86.7/85.972.192.7

Table 1 (part 1): MNLI, QQP, QNLI

The number under each task is the count of training examples. QQP uses F1. The others use accuracy.

SystemSST-2, 67kCoLA, 8.5kSTS-B, 5.7k
Pre-OpenAI SOTA93.235.081.0
BiLSTM+ELMo+Attn90.436.073.3
OpenAI GPT91.345.480.0
BERT BASE93.552.185.8
BERT LARGE94.960.586.5

Table 1 (part 2): SST-2, CoLA, STS-B

STS-B uses Spearman correlation.

SystemMRPC, 3.5kRTE, 2.5kAverage
Pre-OpenAI SOTA86.061.774.0
BiLSTM+ELMo+Attn84.956.871.0
OpenAI GPT82.356.075.1
BERT BASE88.966.479.6
BERT LARGE89.370.182.1

Table 1 (part 3): MRPC, RTE, average

MRPC uses F1. The average leaves out the problematic WNLI set. BERT and GPT are single-model, single-task.

The task

  • SQuAD v1.1 has 100k crowd-sourced question and answer pairs.
  • Given a question and a Wikipedia passage, predict the answer span.
  • Scores use exact match (EM) and F1.

The setup

  • The question gets the A embedding. The passage gets the B embedding.
  • New parameters: a start vector S and an end vector E.
  • The start chance of word i uses the dot product of Ti and S.
  • Then a softmax runs over all words in the paragraph.
  • End works the same way.
  • A span from i to j scores S·Ti + E·Tj.
  • The best span with j at least i is the answer.
  • The goal sums the log-likelihoods of the right start and end.
  • Settings: 3 epochs, learning rate 5e-5, batch size 32.

Extra data

  • Top leaderboard systems may use any public data.
  • So the authors first fine-tune on TriviaQA, then on SQuAD.
  • The TriviaQA data: the first 400 tokens of documents that hold an answer.

What they found

  • Best system: +1.5 F1 as an ensemble over the top leaderboard system.
  • It is +1.3 F1 as a single system.
  • One BERT model beats the top ensemble in F1.
  • Without TriviaQA, they lose only 0.1-0.4 F1.
  • They still beat all earlier systems by a wide margin.
  • The BERT ensemble is 7x systems with different checkpoints and seeds.
SystemTest EMTest F1Dev EM / Dev F1
Human82.391.2-
#1 Ensemble, nlnet86.091.7-
#2 Ensemble, QANet84.590.5-
BiDAF+ELMo (Single)-85.8- / 85.6
R.M. Reader (Ensemble)82.388.581.2 / 87.9
BERT BASE (Single)--80.8 / 88.5
BERT LARGE (Single)--84.1 / 90.9
BERT LARGE (Ensemble)--85.8 / 91.8
BERT LARGE (Sgl.+TriviaQA)85.191.884.2 / 91.1
BERT LARGE (Ens.+TriviaQA)87.493.286.2 / 92.2

Table 2: SQuAD 1.1 (Test)

Top leaderboard systems as of Dec 10th, 2018. EM is exact match.

The task

  • SQuAD 2.0 allows that no short answer exists in the passage.
  • This is more realistic.

The method

  • Questions with no answer get a span that starts and ends at [CLS].
  • The start and end space now includes the [CLS] position.
  • The no-answer score is snull = S·C + E·C.
  • It is compared to the best real span score.
  • The model gives a real answer when the span score is above snull + τ.
  • τ is picked on the Dev set to maximize F1.
  • No TriviaQA data was used.
  • Settings: 2 epochs, learning rate 5e-5, batch size 48.

What they found

  • BERT gets a +5.1 F1 gain over the previous best system.
  • Entries that use BERT are left out.
SystemDev EM / F1Test EM / F1
Human86.3 / 89.086.9 / 89.5
#1 Single, MIR-MRC (F-Net)-74.8 / 78.0
#2 Single, nlnet-74.2 / 77.1
unet (Ensemble)-71.4 / 74.9
SLQA+ (Single)-71.4 / 74.4
BERT LARGE (Single)78.7 / 81.980.0 / 83.1

Table 3: SQuAD 2.0

The task

  • SWAG has 113k sentence-pair examples of grounded common-sense inference.
  • Given a sentence, pick the most likely continuation of four.

The method

  • They build four input sequences. Each joins the sentence and one continuation.
  • The only new parameter is a vector.
  • Its dot product with C gives a score per choice.
  • A softmax normalizes the scores.
  • Settings: 3 epochs, learning rate 2e-5, batch size 16.

What they found

  • BERT LARGE beats the ESIM+ELMo baseline by +27.1%.
  • It beats OpenAI GPT by 8.3%.
SystemDevTest
ESIM+GloVe51.952.7
ESIM+ELMo59.159.2
OpenAI GPT-78.0
BERT BASE81.6-
BERT LARGE86.686.3
Human (expert)-85.0
Human (5 annotations)-88.0

Table 4: SWAG accuracy

Human numbers use 100 samples, as reported in the SWAG paper.

The test

  • Section 5 runs ablation tests to see which parts of BERT matter.
  • All versions use the same data, fine-tuning and settings as BERT BASE.

The versions

  • No NSP: a both-way model with masked LM but no next sentence task.
  • LTR and No NSP: a left-only model with a standard left-to-right language model.
  • The left-only limit was also used in fine-tuning, because removing it hurt.
  • This is like OpenAI GPT, but with BERT's data, input form and fine-tuning.
  • + BiLSTM: a random BiLSTM added on top of the left-only model.
A left-to-right model loses a lot on SQuADFrom Table 5
0255075100SQuAD F1 on the dev setBERT BASE88.5 F1No NSP87.9 F1LTR & No NSP77.8 F1+ BiLSTM84.9 F1

Hover, tap or focus a bar to read its exact value.

Pre-training setupSQuAD F1
BERT BASE88.5
No NSP87.9
LTR & No NSP77.8
+ BiLSTM84.9
SetupMNLI-mQNLIMRPC
BERT BASE84.488.486.7
No NSP83.984.986.5
LTR & No NSP82.184.377.5
+ BiLSTM82.184.175.7

Table 5: ablation over the pre-training tasks (dev)

Accuracy for each. SST-2 and SQuAD are in the next table.

SetupSST-2SQuAD F1
BERT BASE92.788.5
No NSP92.687.9
LTR & No NSP92.177.8
+ BiLSTM91.684.9

Table 5 (continued)

What they found

  • Removing NSP hurts a lot on QNLI, MNLI and SQuAD 1.1.
  • The left-to-right model is worse than the masked model on all tasks.
  • It drops a lot on MRPC and SQuAD.
  • On SQuAD this is clear, as token states have no right-side context.
  • A BiLSTM helps SQuAD a lot, but results stay far worse than BERT.
  • The BiLSTM hurts the GLUE tasks.

Why not join two models

  • ELMo trains separate left and right models and joins them.
  • One: this costs twice as much as one bidirectional model.
  • Two: it makes less sense for QA, as the right-to-left model cannot see the question.
  • Three: it is less powerful than a deep bidirectional model.
  • A deep model uses both sides at every layer.

The test

  • They trained BERT models with different layers, hidden units and heads.
  • All else stayed the same.
  • Table 6 shows the average Dev accuracy over 5 random restarts.
  • It also lists masked-LM perplexity.
Bigger models score higher on MNLIFrom Table 6
0255075100MNLI-m dev accuracy (%)L=3, H=768, A=1277.9%L=6, H=768, A=380.6%L=6, H=768, A=1281.9%L=12, H=768, A=1284.4%L=12, H=1024, A=1685.7%L=24, H=1024, A=1686.6%

Hover, tap or focus a bar to read its exact value.

Size (layers, hidden, heads)MNLI-m (%)
L=3, H=768, A=1277.9
L=6, H=768, A=380.6
L=6, H=768, A=1281.9
L=12, H=768, A=1284.4
L=12, H=1024, A=1685.7
L=24, H=1024, A=1686.6
Layers, hidden, headsLM pplMNLI-mMRPC
3, 768, 125.8477.979.8
6, 768, 35.2480.682.2
6, 768, 124.6881.984.8
12, 768, 123.9984.486.7
12, 1024, 163.5485.786.9
24, 1024, 163.2386.687.8

Table 6: ablation over BERT model size (dev)

SST-2 accuracy for the same rows: 88.4, 90.7, 91.3, 92.9, 93.3, 93.7. LM ppl is masked-LM perplexity on held-out training data.

What they found

  • Larger models give a strict gain on all four data sets.
  • This holds even for MRPC, which has only 3,600 labeled examples.
  • This is surprising, as the models are already large next to earlier work.

Context

  • The largest Transformer in Vaswani et al. is L=6, H=1024, A=16, with 100M parameters.
  • The largest in the literature is L=64, H=512, A=2, with 235M parameters.
  • BERT BASE has 110M parameters. BERT LARGE has 340M.

The authors' claim

  • Bigger models help big tasks like translation. This was known.
  • The authors believe this is the first clear proof for this claim.
  • Extreme size also helps very small tasks.
  • The condition: enough pre-training.
  • Peters et al. saw mixed results going from two to four layers.
  • Melamud et al. saw gains from 200 to 600 hidden size, none at 1,000.
  • Both used the feature-based approach.
  • The authors guess fine-tuning with few new parameters can use bigger models.
  • This holds even with small task data.

Why test it

  • Not every task fits a Transformer encoder, so some need a task model on top.
  • You can also compute features once, then run many cheap experiments.

The setup

  • Task: CoNLL-2003 named entity recognition.
  • The BiLSTM below is a network that reads both ways.
  • They use a case-preserving WordPiece model and the full document context.
  • They tag tokens with no CRF layer.
  • The first sub-token's representation feeds the tagger.
  • For the feature test they take activations from one or more layers, with no fine-tuning.
  • These feed a random two-layer BiLSTM with 768 dimensions.
SystemDev F1Test F1
ELMo95.792.2
CVT-92.6
CSE-93.1
Fine-tuning: BERT LARGE96.692.8
Fine-tuning: BERT BASE96.492.4
Features: Embeddings91.0-
Features: Second-to-Last Hidden95.6-
Features: Last Hidden94.9-
Features: Weighted Sum Last Four Hidden95.9-
Features: Concat Last Four Hidden96.1-
Features: Weighted Sum All 12 Layers95.5-

Table 7: CoNLL-2003 named entity recognition

Feature rows use BERT BASE. Scores are averaged over 5 random restarts.

What they found

  • BERT LARGE does as well as the best methods.
  • The best feature method joins the top four hidden layers.
  • It is only 0.3 F1 behind fine-tuning the whole model.
  • So BERT works for both fine-tuning and feature use.

The message

  • Rich unsupervised pre-training is now part of many language systems.
  • It lets even tasks with little data gain from deep architectures.
  • The authors' main step is to extend this to deep bidirectional architectures.
  • So the same pre-trained model can tackle a broad set of language tasks.

A.1 Examples of the pre-training tasks

Masking example

  • Take 'my dog is hairy' and pick the 4th token.
  • 80% of the time: 'my dog is [MASK]'.
  • 10% of the time: 'my dog is apple'.
  • 10% of the time: 'my dog is hairy'. This biases toward the real word.

Why it works

  • The encoder does not know which words it must predict or which are random.
  • So it keeps a good representation of every token.
  • Random swaps hit only 1.5% of tokens (10% of 15%).
  • This does not seem to harm understanding.

Cost of masking

  • Masked LM predicts only 15% of tokens per batch.
  • So it may need more pre-training steps.
  • Section C.1 shows it converges only a little slower.

Next sentence example

  • Pair: 'the man went to [MASK] store' and 'he bought a gallon [MASK] milk'.
  • Label: IsNext.
  • Pair: 'the man [MASK] to the store' and 'penguin [MASK] are flight ##less birds'.
  • Label: NotNext.
  • Figure 3 compares BERT, OpenAI GPT and ELMo designs.
A.2 Pre-training procedure
  • Two spans of text are sampled. Together they are at most 512 tokens.
  • Masking happens after WordPiece, at a uniform 15% rate.
  • Batch size: 256 sequences, which is 128,000 tokens.
  • Steps: 1,000,000, about 40 epochs over the 3.3 billion word corpus.
  • Adam with learning rate 1e-4, β1 = 0.9, β2 = 0.999.
  • L2 weight decay 0.01. Warmup over the first 10,000 steps, then linear decay.
  • Dropout 0.1 on all layers.
  • They use a gelu activation, not relu, like OpenAI GPT.
  • The loss is the sum of mean masked LM and mean NSP likelihood.

Hardware and speed

  • BERT BASE: 4 Cloud TPUs in Pod setup (16 TPU chips).
  • BERT LARGE: 16 Cloud TPUs (64 TPU chips).
  • Each pre-training took 4 days.
  • Long sequences cost much more, because attention grows with the square of length.
  • So 90% of steps use length 128.
  • The last 10% use length 512, to learn position embeddings.
A.3 Fine-tuning procedure
  • Most settings match pre-training, except batch size, learning rate and epochs.
  • Dropout stays 0.1.
  • Values that worked well: batch size 16 or 32.
  • Learning rate (Adam): 5e-5, 3e-5 or 2e-5.
  • Epochs: 2, 3 or 4.
  • Large data sets (100k+ examples) are far less sensitive to settings.
  • Fine-tuning is fast, so a full search is practical.
A.4 BERT, ELMo and OpenAI GPT
  • ELMo is feature-based. BERT and GPT fine-tune.
  • BERT was made to be close to GPT, so they compare well.
  • The authors argue both-way reading and the two tasks cause most gains.
  • Other differences exist, listed next.

Other differences

  • GPT trains on BooksCorpus (800M words).
  • BERT uses BooksCorpus and Wikipedia (2,500M words).
  • GPT adds [SEP] and [CLS] only at fine-tuning. BERT learns them in pre-training.
  • GPT: 1M steps, batch of 32,000 words. BERT: 1M steps, batch of 128,000 words.
  • GPT uses one learning rate of 5e-5 for all tasks.
  • BERT picks a task-specific rate on the Dev set.
  • Section 5.1 shows the main gain comes from the two tasks.
  • It also comes from both-way reading.
A.5 Fine-tuning on different tasks
  • Figure 4 shows each task setup.
  • Each task model is BERT plus one output layer.
  • Sequence-level tasks: (a) and (b).
  • Token-level tasks: (c) and (d).

Source

  • GLUE results come from the GLUE leaderboard and an OpenAI blog post.

The data sets

  • MNLI: does sentence two follow, contradict or stay neutral to sentence one.
  • QQP: are two Quora questions the same in meaning.
  • QNLI: does a passage sentence contain the answer to a question.
  • SST-2: movie review sentiment, a yes or no label.
  • CoLA: is an English sentence linguistically acceptable.
  • STS-B: a score from 1 to 5 for how similar two sentences are.
  • MRPC: are two news sentences equal in meaning.
  • RTE: a yes or no entailment task, with much less data than MNLI.
  • WNLI: a small inference set with problems in how it was built.

The WNLI choice

  • Every system sent to GLUE did worse than the 65.1 majority-class baseline on WNLI.
  • So the authors leave it out of the average, to be fair to OpenAI GPT.
  • For the GLUE submission they always predicted the majority class.
  • They report single-task fine-tuning only.
  • Multitask training might help more. It helped RTE a lot, with MNLI.

C.1 Number of training steps

Does BERT need so much pre-training?

  • Yes.
  • BERT BASE gains almost 1.0% more MNLI accuracy at 1M steps than at 500k steps.
  • That is 128,000 words per batch for 1,000,000 steps.

Does masked LM converge slower?

  • Slightly slower than the left-to-right model.
  • But its accuracy is better almost at once.
  • Figure 5 plots MNLI Dev accuracy against pre-training steps.
C.2 Different masking procedures

The test

  • BERT masks with 80% [MASK], 10% same, 10% random.
  • They tried other mixes, on MNLI and on NER with fine-tuning and with features.
  • For features they join the last 4 layers.
MASK / SAME / RNDMNLINER fine-tuneNER features
80% / 10% / 10%84.295.494.9
100% / 0% / 0%84.394.994.0
80% / 0% / 20%84.195.294.6
80% / 20% / 0%84.495.294.7
0% / 20% / 80%83.794.894.6
0% / 0% / 100%83.694.994.6

Table 8: masking rates

Dev set results.

What they found

  • Fine-tuning is surprisingly robust to the masking mix.
  • Using only [MASK] was a problem for the feature-based NER test.
  • Using only random replacement did much worse than the BERT mix.

Words to know

  • Ablation: A test that removes a part to see what it does.
  • Bidirectional: Using the words on both the left and the right.
  • BiLSTM: A network that reads a sequence in both directions, step by step.
  • Cloze task: A fill-in-the-blank task.
  • Ensemble: Several models whose answers are combined.
  • Epoch: One full pass over the training data.
  • Exact match (EM): The share of answers that match the right answer exactly.
  • F1: A score that balances finding right answers and avoiding wrong ones.
  • Fine-tuning: Training a pre-trained model a little more on a specific task.
  • GLUE: A set of language tests that are scored together.
  • Learning rate: How big each training update step is.
  • Masked language model: A task that hides words and asks the model to guess them.
  • Next sentence prediction: A task that asks if one sentence follows another.
  • Perplexity: A score for how surprised a model is. Lower is better.
  • Pre-training: Training a model on lots of general text before any task.
  • Token: A small piece of text, such as a word or word part.
  • WordPiece: A way to split words into small, common pieces.

Share this paper

LinkedIn (opens in a new tab)X (opens in a new tab)WhatsApp (opens in a new tab)EmailDownload PDFExplainer (PDF), 750 KB

Ask AI about this paper

Stuck on a word or an idea? Ask in plain words. The answer comes from the write-up on this page.

Read the original paper on arXiv: BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding (opens in a new tab)

Jump to

One paper a week, in plain English

Every Monday: the new AI papers worth your time. Short lines, the key number, and what to do with it.

One email a week. Unsubscribe with one click. No spam.