BERT
This model was released on 2018-10-11 and added to Hugging Face Transformers on 2020-11-16.
BERT is a bidirectional transformer pretrained on unlabeled text to predict masked tokens in a sentence and to predict whether one sentence follows another. The main idea is that by randomly masking some tokens, the model can train on text to the left and right, giving it a more thorough understanding. BERT is also very versatile because its learned language representations can be adapted for other NLP tasks by fine-tuning an additional layer or head.
You can find all the original BERT checkpoints under the BERT collection.
The example below demonstrates how to predict the [MASK] token with Pipeline, AutoModel, and from the command line.
import torchfrom transformers import pipeline
pipeline = pipeline( task="fill-mask", model="google-bert/bert-base-uncased", dtype=torch.float16, device=0)pipeline("Plants create [MASK] through a process known as photosynthesis.")import torchfrom transformers import AutoModelForMaskedLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained( "google-bert/bert-base-uncased",)model = AutoModelForMaskedLM.from_pretrained( "google-bert/bert-base-uncased", dtype=torch.float16, device_map="auto", attn_implementation="sdpa")inputs = tokenizer("Plants create [MASK] through a process known as photosynthesis.", return_tensors="pt").to(model.device)
with torch.no_grad(): outputs = model(**inputs) predictions = outputs.logits
masked_index = torch.where(inputs['input_ids'] == tokenizer.mask_token_id)[1]predicted_token_id = predictions[0, masked_index].argmax(dim=-1)predicted_token = tokenizer.decode(predicted_token_id)
print(f"The predicted token is: {predicted_token}")echo -e "Plants create [MASK] through a process known as photosynthesis." | transformers run --task fill-mask --model google-bert/bert-base-uncased --device 0- Inputs should be padded on the right because BERT uses absolute position embeddings.
BertConfig
Section titled “BertConfig”[[autodoc]] BertConfig - all
BertTokenizer
Section titled “BertTokenizer”[[autodoc]] BertTokenizer - get_special_tokens_mask - save_vocabulary
BertTokenizerLegacy
Section titled “BertTokenizerLegacy”[[autodoc]] BertTokenizerLegacy
BertTokenizerFast
Section titled “BertTokenizerFast”[[autodoc]] BertTokenizerFast
BertModel
Section titled “BertModel”[[autodoc]] BertModel - forward
BertForPreTraining
Section titled “BertForPreTraining”[[autodoc]] BertForPreTraining - forward
BertLMHeadModel
Section titled “BertLMHeadModel”[[autodoc]] BertLMHeadModel - forward
BertForMaskedLM
Section titled “BertForMaskedLM”[[autodoc]] BertForMaskedLM - forward
BertForNextSentencePrediction
Section titled “BertForNextSentencePrediction”[[autodoc]] BertForNextSentencePrediction - forward
BertForSequenceClassification
Section titled “BertForSequenceClassification”[[autodoc]] BertForSequenceClassification - forward
BertForMultipleChoice
Section titled “BertForMultipleChoice”[[autodoc]] BertForMultipleChoice - forward
BertForTokenClassification
Section titled “BertForTokenClassification”[[autodoc]] BertForTokenClassification - forward
BertForQuestionAnswering
Section titled “BertForQuestionAnswering”[[autodoc]] BertForQuestionAnswering - forward
Bert specific outputs
Section titled “Bert specific outputs”[[autodoc]] models.bert.modeling_bert.BertForPreTrainingOutput