A transformer-based machine learning technique for natural language processing pre-training, developed by Google, that understands the context of a word by looking at the words that come both before and after it.
Imagine reading a sentence with a word blacked out: “The animal didn’t cross the street because it was too [MASK].”
If you only read left-to-right, you might guess “wide” or “busy.” But if you can look at the whole sentence at once, you realize “it” refers to the “street,” so the street was too “wide.”
BERT reads text in both directions simultaneously. This bidirectional understanding allows it to grasp the full context of a word, making it incredibly powerful for tasks like search, question answering, and text classification.
Introduced by Google in 2018, BERT revolutionized NLP by applying the Transformer’s encoder stack to pre-train a deep bidirectional representation.
Key Innovations:
Architecture:
BERT and its derivatives (RoBERTa, DistilBERT) are the workhorses of enterprise NLP:
A proofreader reading a sentence. A bad proofreader reads word-by-word and misses the meaning. A good proofreader reads the whole sentence, looks back and forth, and instantly knows that “their” should be “there” based on the surrounding words. BERT is the ultimate proofreader.
# Using BERT for masked language modeling and feature extraction
from transformers import BertTokenizer, BertForMaskedLM
# Load pre-trained BERT tokenizer and model
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
# Input with a masked token
text = "The capital of France is [MASK]."
inputs = tokenizer(text, return_tensors="pt")
# Get predictions for the masked token
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
# Find the predicted token
mask_token_index = (inputs.input_ids == tokenizer.mask_token_id)[0].nonzero(as_tuple=True)[0]
predicted_token_id = logits[0, mask_token_index].argmax(axis=-1).item()
predicted_word = tokenizer.decode([predicted_token_id])
print(f"Predicted word: {predicted_word}") # Output: "paris"