BERT
25 memberslucid.models.text.bertBERT family — Devlin et al., 2018 (encoder-only transformer).
Devlin, Jacob, et al. "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding." Proceedings of NAACL-HLT, 2019, pp. 4171–4186.
BERT — Bidirectional Encoder Representations from Transformers — is a transformer encoder pre-trained on two unsupervised objectives over raw text:
- Masked Language Modelling (MLM). A random 15% of input
tokens are replaced with a
[MASK]placeholder; the model must predict the original token from its bidirectional context. Formally, for masked positions ,
Unlike the left-to-right LMs that preceded it, every token's prediction conditions on both left and right context — hence bidirectional.
- Next-Sentence Prediction (NSP). Two segments and
are concatenated with separator tokens; the model
reads the pooled
[CLS]representation and predicts whether was the actual next sentence in the corpus or a random other sentence. This objective gives the model a coarse notion of inter-sentence coherence used by downstream sentence-pair tasks (QA, NLI).
After pre-training on BookCorpus + English Wikipedia, the same
encoder is fine-tuned on individual downstream tasks by adding a
thin task-specific head on top of either the [CLS] token
representation (classification) or all per-token representations
(tagging, span prediction). This "pre-train once, fine-tune
everywhere" recipe defined the post-2018 NLP landscape and set
new state-of-the-art on the GLUE, SQuAD, and SWAG benchmarks at
publication time.
Classes
BERTConfig1 methodsConfiguration dataclass for every BERT variant.
BERTForCausalLM2 methodsBERT trunk repurposed as a left-to-right (causal) language model.
BERTForMaskedLM2 methodsBERT with a tied masked-language-modeling head.
BERTForNextSentencePrediction2 methodsBERT with the standalone next-sentence-prediction head.
BERTForPreTraining2 methodsBERT with the original joint MLM + NSP pre-training objective.
BERTForPreTrainingOutput1 methodsCombined output for BERTForPreTraining.
BERTForQuestionAnswering2 methodsBERT with a 2-way span head for extractive question answering.
BERTForSequenceClassification2 methodsBERT with a pooled-CLS linear classifier for sequence-level tasks.
BERTForTokenClassification2 methodsBERT with a per-token linear classifier for tagging tasks.
BERTModel4 methodsBare BERT encoder returning hidden states and pooled CLS embedding.
BERTTokenizer1 methodsBERT tokenizer — pure-Python reference.
BERTTokenizerFast1 methodsBERT tokenizer — C++-backed.
Functions
bert_base→ BERTModelConstruct a BERT-Base encoder.
bert_base_cls→ BERTForSequenceClassificationConstruct a BERT-Base model with a sequence-classification head.
bert_base_mlm→ BERTForMaskedLMConstruct a BERT-Base model with a tied masked-LM head.
bert_base_qa→ BERTForQuestionAnsweringConstruct a BERT-Base extractive-QA model (SQuAD v1.1).
bert_base_token_cls→ BERTForTokenClassificationConstruct a BERT-Base per-token classification model (CoNLL-2003 NER).
bert_large→ BERTModelConstruct a BERT-Large encoder.
bert_large_cls→ BERTForSequenceClassificationConstruct a BERT-Large model with a sequence-classification head.
bert_large_mlm→ BERTForMaskedLMConstruct a BERT-Large model with a tied masked-LM head.
bert_large_qa→ BERTForQuestionAnsweringConstruct a BERT-Large extractive-QA model (whole-word-masking, SQuAD v1.1).
bert_medium→ BERTModelConstruct a BERT-Medium encoder.
bert_mini→ BERTModelConstruct a BERT-Mini encoder.
bert_small→ BERTModelConstruct a BERT-Small encoder.
bert_tiny→ BERTModelConstruct a BERT-Tiny encoder.