BERT (language model) explained

Bidirectional Encoder Representations from Transformers (BERT)
Author:Google AI
Released:October 31, 2018
Latest Release Date:March 11, 2020
Repo:https://github.com/google-research/bert
License:Apache 2.0

Bidirectional Encoder Representations from Transformers (BERT) is a language model introduced in October 2018 by researchers at Google.[1] [2] It learned by self-supervised learning to represent text as a sequence of vectors. It had the transformer encoder architecture. It was notable for its dramatic improvement over previous state of the art models, and as an early example of large language model., BERT was a ubiquitous baseline in Natural Language Processing (NLP) experiments.[3]

BERT is trained by masked token prediction and next sentence prediction. As a result of this training process, BERT learns contextual, latent representations of tokens in their context, similar to ELMo and GPT-2. It found applications for many many natural language processing tasks, such as coreference resolution and polysemy resolution.[4] It is an evolutionary step over ELMo, and spawned the study of "BERTology", which attempts to interpret what is learned by BERT.

BERT was originally implemented in the English language at two model sizes, BERTBASE (110 million parameters) and BERTLARGE (340 million parameters). Both were trained on the Toronto BookCorpus[5] (800M words) and English Wikipedia (2,500M words). The weights were released on GitHub.[6] On March 11, 2020, 24 smaller models were released, the smallest being BERTTINY with just 4 million parameters.

Architecture

BERT is an "encoder-only" transformer architecture. At a high level, BERT consists of 4 modules:

The task head is necessary for pre-training, but it is often unnecessary for so-called "downstream tasks," such as question answering or sentiment classification. Instead, one removes the task head and replaces it with a newly initialized module suited for the task, and finetune the new module. The latent vector representation of the model is directly fed into this new module, allowing for sample-efficient transfer learning.

Embedding

This section describes the embedding used by BERTBASE. The other one, BERTLARGE, is similar, just larger.

The tokenizer of BERT is WordPiece, which is a sub-word strategy like byte pair encoding. Its vocabulary size is 30,000, and any token not appearing in its vocabulary is replaced by [UNK] ("unknown"). The first layer is the embedding layer, which contains three components: token type embeddings, position embeddings, and segment type embeddings.

The three embedding vectors are added together representing the initial token representation as a function of these three pieces of information. After embedding, the vector representation is normalized using a LayerNorm operation, outputting a 768-dimensional vector for each input token. After this, the representation vectors are passed forward through 12 Transformer encoder blocks, and are decoded back to 30,000-dimensional vocabulary space using a basic affine transformation layer.

Architectural family

The encoder stack of BERT has 2 free parameters:

L

, the number of layers, and

H

, the hidden size. There are always

H/64

self-attention heads, and the feed-forward/filter size is always

4H

. By varying these two numbers, one obtains an entire family of BERT models.

For BERT

The notation for encoder stack is written as L/H. For example, BERTBASE is written as 12L/768H, BERTLARGE as 24L/1024H, and BERTTINY as 2L/128H.

Training

Pre-training

BERT was pre-trained simultaneously on two tasks.[7]

Masked Language Modeling

In Masked Language Modeling, 15% of tokens would be randomly selected for masked-prediction task, and the training objective was to predict the masked token given its context. In more detail, the selected token is

The reason not all selected tokens are masked is to avoid the dataset shift problem. The dataset shift problem arises when the distribution of inputs seen during training differs significantly from the distribution encountered during inference. A trained BERT model might be applied to word representation (like Word2Vec), where it would be run over sentences not containing any [MASK] tokens. It is later found that more diverse training objectives are generally better.

As an illustrative example, consider the sentence "my dog is cute". It would first be divided into tokens like "my1 dog2 is3 cute4". Then a random token in the sentence would be picked. Let it be the 4th one "cute4". Next, there would be three possibilities:

After processing the input text, the model's 4th output vector is passed to its decoder layer, which outputs a probability distribution over its 30,000-dimensional vocabulary space.

Next Sentence Prediction

Given two spans of text, the model predicts if these two spans appeared sequentially in the training corpus, outputting either [IsNext] or [NotNext]. The first span starts with a special token [CLS] (for "classify"). The two spans are separated by a special token [SEP] (for "separate"). After processing the two spans, the 1-st output vector (the vector coding for [CLS]) is passed to a separate neural network for the binary classification into [IsNext] and [NotNext].

Fine-tuning

BERT is meant as a general pretrained model for various applications in natural language processing. That is, after pre-training, BERT can be fine-tuned with fewer resources on smaller datasets to optimize its performance on specific tasks such as natural language inference and text classification, and sequence-to-sequence-based language generation tasks such as question answering and conversational response generation.

The original BERT paper published results demonstrating that a small amount of finetuning (for BERTLARGE, 1 hour on 1 Cloud TPU) allowed it to achieved state-of-the-art performance on a number of natural language understanding tasks:

Cost

BERT was trained on the BookCorpus (800M words) and a filtered version of English Wikipedia (2,500M words) without lists, tables, and headers.

Training BERTBASE on 4 Cloud TPU (16 TPU chips total) took 4 days, at an estimated cost of 500 USD. Training BERTLARGE on 16 Cloud TPU (64 TPU chips total) took 4 days.

Interpretation

Language models like ELMo, GPT-2, and BERT, spawned the study of "BERTology", which attempts to interpret what is learned by these models. Their performance on these natural language understanding tasks are not yet well understood.[10] [11] Several research publications in 2018 and 2019 focused on investigating the relationship behind BERT's output as a result of carefully chosen input sequences,[12] [13] analysis of internal vector representations through probing classifiers,[14] [15] and the relationships represented by attention weights.

The high performance of the BERT model could also be attributed to the fact that it is bidirectionally trained. This means that BERT, based on the Transformer model architecture, applies its self-attention mechanism to learn information from a text from the left and right side during training, and consequently gains a deep understanding of the context. For example, the word fine can have two different meanings depending on the context (I feel fine today, She has fine blond hair). BERT considers the words surrounding the target word fine from the left and right side.

However it comes at a cost: due to encoder-only architecture lacking a decoder, BERT can't be prompted and can't generate text, while bidirectional models in general do not work effectively without the right side, thus being difficult to prompt. As an illustrative example, if one wishes to use BERT to continue a sentence fragment "Today, I went to", then naively one would mask out all the tokens as "Today, I went to [MASK] [MASK] [MASK] ... [MASK] ." where the number of [MASK] is the length of the sentence one wishes to extend to. However, this constitutes a dataset shift, as during training, BERT has never seen sentences with that many tokens masked out. Consequently, its performance degrades. More sophisticated techniques allow text generation, but at a high computational cost.[16]

History

BERT was originally published by Google researchers Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. The design has its origins from pre-training contextual representations, including semi-supervised sequence learning,[17] generative pre-training, ELMo,[18] and ULMFit.[19] Unlike previous models, BERT is a deeply bidirectional, unsupervised language representation, pre-trained using only a plain text corpus. Context-free models such as word2vec or GloVe generate a single word embedding representation for each word in the vocabulary, whereas BERT takes into account the context for each occurrence of a given word. For instance, whereas the vector for "running" will have the same word2vec vector representation for both of its occurrences in the sentences "He is running a company" and "He is running a marathon", BERT will provide a contextualized embedding that will be different according to the sentence.

On October 25, 2019, Google announced that they had started applying BERT models for English language search queries within the US.[20] On December 9, 2019, it was reported that BERT had been adopted by Google Search for over 70 languages.[21] [22] In October 2020, almost every single English-based query was processed by a BERT model.[23]

Variants

The BERT models were influential and inspired many variants.

RoBERTa (2019)[24] was an engineering improvement. It preserves BERT's architecture (slightly larger, at 355M parameters), but improves its training, changing key hyperparameters, removing the next-sentence prediction task, and using much larger mini-batch sizes.

DistilBERT (2019) distills BERTBASE to a model with just 60% of its parameters (66M), while preserving 95% of its benchmark scores.[25] Similarly, TinyBERT (2019) is a distilled model with just 28% of its parameters.

ALBERT (2019) used shared-parameter across layers, and experimented with independently varying the hidden size and the word-embedding layer's output size as two hyperparameters. They also replaced the next sentence prediction task with the sentence-order prediction (SOP) task, where the model must distinguish the correct order of two consecutive text segments from their reversed order.

ELECTRA (2020) applied the idea of generative adversarial networks to the MLM task. Instead of masking out tokens, a small language model generates random plausible plausible substitutions, and a larger network identify these replaced tokens. The small model aims to fool the large model.

DeBERTa

DeBERTa (2020) is a significant architectural variant, with disentangled attention. Its key idea is to treat the positional and token encodings separately throughout the attention mechanism. Instead of combining the positional encoding (

xposition

) and token encoding (

xtoken

) into a single input vector (

xinput=xposition+xtoken

), DeBERTa keeps them separate as a tuple: (

(xposition,xtoken)

). Then, at each self-attention layer, DeBERTa computes three distinct attention matrices, rather than the single attention matrix used in BERT:
! Attention Type ! Query Type! Key Type! Example
Content-to-Content TokenToken"European"; "Union", "continent"
Content-to-PositionTokenPosition[adjective]
+1, +2, +3
Position-to-ContentPositionToken-1; "not", "very"
The three attention matrices are added together element-wise, then passed through a softmax layer and multiplied by a projection matrix.

Absolute position encoding is included in the final self-attention layer as additional input.

Further reading

External links

Notes and References

  1. 1810.04805v2 . cs.CL . Jacob . Devlin . Ming-Wei . Chang . BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding . 11 October 2018 . Lee . Kenton . Toutanova . Kristina.
  2. Web site: 2 November 2018 . Open Sourcing BERT: State-of-the-Art Pre-training for Natural Language Processing . 2019-11-27 . Google AI Blog . en.
  3. Rogers. Anna. Kovaleva. Olga. Rumshisky. Anna. 2020. A Primer in BERTology: What We Know About How BERT Works. Transactions of the Association for Computational Linguistics. 8. 842–866. 10.1162/tacl_a_00349. 2002.12327. 211532403.
  4. Web site: Anderson . Dawn . 2019-11-05 . A deep dive into BERT: How BERT launched a rocket into natural language understanding . 2024-08-06 . Search Engine Land . en.
  5. Zhu. Yukun. Kiros. Ryan. Zemel. Rich. Salakhutdinov. Ruslan. Urtasun. Raquel. Torralba. Antonio. Fidler. Sanja. 2015. Aligning Books and Movies: Towards Story-Like Visual Explanations by Watching Movies and Reading Books. 19–27. cs.CV. 1506.06724.
  6. Web site: BERT . 28 March 2023 . GitHub.
  7. Web site: Summary of the models — transformers 3.4.0 documentation . 2023-02-16 . huggingface.co.
  8. 1606.05250 . cs.CL . Pranav . Rajpurkar . Jian . Zhang . SQuAD: 100,000+ Questions for Machine Comprehension of Text . 2016-10-10 . Lopyrev . Konstantin . Liang . Percy.
  9. 1808.05326 . cs.CL . Rowan . Zellers . Yonatan . Bisk . SWAG: A Large-Scale Adversarial Dataset for Grounded Commonsense Inference . 2018-08-15 . Schwartz . Roy . Choi . Yejin.
  10. Book: Kovaleva. Olga. Romanov. Alexey. Rogers. Anna. Rumshisky. Anna. November 2019. Revealing the Dark Secrets of BERT. https://www.aclweb.org/anthology/D19-1445. en-us. 4364–4373. 10.18653/v1/D19-1445. Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP). 201645145.
  11. Clark. Kevin. Khandelwal. Urvashi. Levy. Omer. Manning. Christopher D.. 2019. What Does BERT Look at? An Analysis of BERT's Attention. Proceedings of the 2019 ACL Workshop BlackboxNLP: Analyzing and Interpreting Neural Networks for NLP. 276–286. Stroudsburg, PA, USA. Association for Computational Linguistics. 10.18653/v1/w19-4828. free. 1906.04341.
  12. Khandelwal. Urvashi. He. He. Qi. Peng. Jurafsky. Dan. 2018. Sharp Nearby, Fuzzy Far Away: How Neural Language Models Use Context. Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). 284–294. Stroudsburg, PA, USA. Association for Computational Linguistics. 10.18653/v1/p18-1027. 1805.04623. 21700944.
  13. Book: Gulordava. Kristina. Bojanowski. Piotr. Grave. Edouard. Linzen. Tal. Baroni. Marco. Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long Papers) . Colorless Green Recurrent Networks Dream Hierarchically . 2018. 1195–1205. Stroudsburg, PA, USA. Association for Computational Linguistics. 10.18653/v1/n18-1108. 1803.11138. 4460159.
  14. Giulianelli. Mario. Harding. Jack. Mohnert. Florian. Hupkes. Dieuwke. Zuidema. Willem. 2018. Under the Hood: Using Diagnostic Classifiers to Investigate and Improve how Language Models Track Agreement Information. Proceedings of the 2018 EMNLP Workshop BlackboxNLP: Analyzing and Interpreting Neural Networks for NLP. 240–248. Stroudsburg, PA, USA. Association for Computational Linguistics. 10.18653/v1/w18-5426. 1808.08079. 52090220.
  15. Zhang. Kelly. Bowman. Samuel. 2018. Language Modeling Teaches You More than Translation Does: Lessons Learned Through Auxiliary Syntactic Task Analysis. Proceedings of the 2018 EMNLP Workshop BlackboxNLP: Analyzing and Interpreting Neural Networks for NLP. 359–361. Stroudsburg, PA, USA. Association for Computational Linguistics. 10.18653/v1/w18-5448. free.
  16. 2209.14500 . cs.LG . Ajay . Patel . Bryan . Li . Bidirectional Language Models Are Also Few-shot Learners . Mohammad Sadegh Rasooli . Constant . Noah . Raffel . Colin . Callison-Burch . Chris . 2022.
  17. Dai . Andrew . Le . Quoc . Semi-supervised Sequence Learning . 4 November 2015 . 1511.01432. cs.LG .
  18. Peters . Matthew . Neumann . Mark . Iyyer . Mohit . Gardner . Matt . Clark . Christopher . Lee . Kenton . Luke . Zettlemoyer . Deep contextualized word representations . 15 February 2018 . 1802.05365v2. cs.CL .
  19. Howard . Jeremy . Ruder . Sebastian . Universal Language Model Fine-tuning for Text Classification . 18 January 2018 . 1801.06146v5. cs.CL .
  20. Web site: Nayak . Pandu . Understanding searches better than ever before . Google Blog . 25 October 2019 . 10 December 2019.
  21. Web site: 2019-10-25 . Understanding searches better than ever before . 2024-08-06 . Google . en-us.
  22. Web site: Montti . Roger . Google's BERT Rolls Out Worldwide . Search Engine Journal . 10 December 2019 . 10 December 2019.
  23. Web site: 2020-10-15. Google: BERT now used on almost every English query. 2020-11-24. Search Engine Land.
  24. 1907.11692 . cs.CL . Yinhan . Liu . Myle . Ott . RoBERTa: A Robustly Optimized BERT Pretraining Approach . 2019 . Goyal . Naman . Du . Jingfei . Joshi . Mandar . Chen . Danqi . Levy . Omer . Lewis . Mike . Zettlemoyer . Luke . Stoyanov . Veselin.
  25. Web site: DistilBERT . 2024-08-05 . huggingface.co.