Modeladvanced
BERT
Also called bidirectional encoder representations from transformers.
A transformer encoder pretrained by masked language modeling and next sentence prediction, then fine-tuned with a small head per task.
BERT is a stack of transformer encoder blocks in which every token attends to every other token, pretrained with two objectives and then fine-tuned with a small output layer for each task. Three names here are easy to mix up. Masked language modeling is an objective: hide some tokens and predict them from both sides. BERT is a particular network, released by Google in 2018, that was pretrained with that objective plus next sentence prediction. Replaced token detection is a different objective for the same kind of encoder: a discriminator learns which tokens a small generator replaced. So BERT is the model, and the other two are ways to train an encoder like it.
The machine
Sizes. is the number of tokens, at most 512. is the vocabulary, a WordPiece vocabulary of 30,000 entries. is the width, the number of heads, , and the number of blocks. The two released sizes are BERT-BASE with , , (110M parameters) and BERT-LARGE with , , (340M parameters).
Input. The input is one or two segments packed as [CLS] A [SEP] B [SEP]. Each token gets the sum of three embeddings, each of size :
- a token embedding from a table;
- a segment embedding from a table, which says whether the token is in A or in B;
- a learned position embedding from a table.
The result is an matrix .
One block. As in the original transformer, the layer normalization comes after each residual sum:
Multi-head attention forms , and of shape for each head, and the score matrix has no causal mask: token sees tokens . Only padding is masked. The feed-forward layer maps with the GELU activation function.
Output. After blocks there is one vector for each token, and the vector at [CLS].
Pretraining objective 1: the masked LM. Choose 15% of the token positions at random. For a chosen position, put [MASK] there 80% of the time, a random token 10% of the time, and leave the token as it is 10% of the time. So each position is [MASK] with probability 0.12, random with 0.015 and unchanged but scored with 0.015. An output layer of shape and a softmax predict the original token at the chosen positions only, with the cross-entropy loss. In a sequence of 512 tokens, about 77 positions are scored.
Pretraining objective 2: next sentence prediction (NSP). Half the time B is the sentence that follows A in the corpus, and half the time it is a random sentence. A layer of shape on predicts which case it is.
Fine-tuning. All weights are trained again on the task, with a new head:
- for a classification with labels, a layer on ;
- for tagging each token (for example named entity recognition), a layer on each ;
- for an answer span in a passage, a start vector and an end vector in , with and the same for the end.
Counting parameters. The blocks hold weights plus biases and gains per block. With , 512 positions, 2 segments, the embedding normalization and the layer on , BERT-BASE has parameters, which the paper rounds to 110M.
What it counts
One step is one floating-point multiply-add. One forward pass costs : the term is the full attention matrix, which is why the input is limited to 512 tokens. Pretraining used batches of 256 sequences of 512 tokens for 1,000,000 steps, about 40 passes over 3.3 billion words: BooksCorpus (800M words) and English Wikipedia (2,500M words) (Appendix A.2).
Only about 15% of the positions give a loss in each sequence, against all positions in causal language modeling. Devlin et al. (Appendix C.1) report that the masked LM converges slightly more slowly than a left-to-right model, but that it is ahead in accuracy almost from the start. Replaced token detection was designed to give a loss at every position.
Fine-tuned BERT is measured by task scores: GLUE, SQuAD F1, accuracy on SWAG. Unlike a causal model, it gives no perplexity of a sentence.
Relation to other models
- GPT. A generative pretrained transformer uses the same kind of block with a causal mask and trains on the next token. It can generate text from left to right. BERT sees both sides of each token, which helps understanding tasks, but it has no direct way to generate.
- ELMo. ELMo trains a left-to-right and a right-to-left LSTM model separately and joins their states. Each direction never sees the other side. BERT's attention mixes both sides in every layer.
- Contextual embeddings. The vectors are contextual word embeddings: the same word gets a different vector in each sentence. They can be used as features without fine-tuning.
- Models built on BERT. Sentence-BERT trains BERT in a siamese setup so that or the mean of the can be compared with a cosine. ColBERT keeps all for retrieval. BERTScore compares the of two texts. SimCSE trains sentence vectors from BERT by contrast. Smaller copies are made by knowledge distillation.
- Other encoders. RoBERTa keeps the architecture and changes the training: no NSP, more data, longer training. ELECTRA trains the same architecture with replaced token detection.
Known results in the model
- Measurement (eleven tasks). At publication BERT-LARGE gave the best reported results on eleven tasks, including a GLUE score of 80.5 (7.7 points above the earlier best), MultiNLI accuracy of 86.7%, SQuAD v1.1 test F1 of 93.2 and SQuAD v2.0 test F1 of 83.1 (abstract, Section 4).
- Measurement (both sides matter). In Table 5, on BERT-BASE, a left-to-right model with no NSP scored 77.8 F1 on the SQuAD development set against 88.5 for BERT, and 77.5 against 86.7 on MRPC. Adding a BiLSTM on top raised SQuAD to 84.9 but did not close the gap.
- Measurement (NSP). In the same table, removing NSP lowered QNLI from 88.4 to 84.9 and SQuAD from 88.5 to 87.9. Liu et al. (RoBERTa, Section 4.2) later found that training without NSP, on full sentences from contiguous text, matched or slightly improved the scores. The two results used different input formats, so they do not contradict each other.
- Measurement (the 80/10/10 rule). In Table 8, fine-tuned MNLI accuracy hardly changed between the 80/10/10 rule (84.2) and always using
[MASK](84.3). For NER with BERT as a fixed feature extractor, always using[MASK]was worse, 94.0 against 94.9. The token[MASK]never appears in fine-tuning data, and the random and unchanged cases reduce that mismatch. This is the authors' reason; the gain is small. - Measurement (size). In Table 6, larger models gave higher scores on all four GLUE tasks studied, even on MRPC with 3,600 training examples.
- Fact (no sentence probability). The masked LM gives the distribution of a hidden token given the others. In general these conditionals do not combine into a joint distribution of the sentence by the chain rule, so BERT is not a language model in the sense of assigning a probability to every sequence.
- Heuristic (fine-tuning is unstable on small data). Devlin et al. (Section 4.1) note that BERT-LARGE fine-tuning was sometimes unstable on small data sets, and they ran several random restarts and kept the best on the development set.
References
- Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova, BERT: Pre-training of deep bidirectional transformers for language understanding, NAACL 2019, Section 3 (the input with segment and position embeddings, the sizes BASE and LARGE), Section 3.1 (the masked LM with the 80/10/10 rule and next sentence prediction), Section 3.2 (fine-tuning), Section 4 (GLUE, SQuAD and SWAG), Section 5.1 and Table 5 (the ablation of the pretraining tasks), Appendix A.2 (the pretraining procedure), Appendix C.2 and Table 8 (the masking ablation)
- Ashish Vaswani, Noam Shazeer, Niki Parmar and others, Attention is all you need, NeurIPS 2017, Section 3 (the encoder stack)
- Yinhan Liu, Myle Ott, Naman Goyal and others, RoBERTa: A robustly optimized BERT pretraining approach, 2019, Section 4.2 (training without next sentence prediction)
- Kevin Clark, Minh-Thang Luong, Quoc V. Le and Christopher D. Manning, ELECTRA: Pre-training text encoders as discriminators rather than generators, ICLR 2020, Section 3.2 and Table 5 (the comparison with BERT at the same size)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag RFZZFD Edit this page History Report an error Markdown