AI Dictionary of Terms

BERT (Bidirectional Encoder Representations from Transformers)

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.

The Simple Version

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.

Detailed Explanation

Introduced by Google in 2018, BERT revolutionized NLP by applying the Transformer’s encoder stack to pre-train a deep bidirectional representation.

Key Innovations:

  1. Bidirectional Context: Unlike previous models (like GPT-1) that read left-to-right, BATTENDS to both left and right context simultaneously.
  2. Masked Language Modeling (MLM): During pre-training, 15% of words are masked. The model must predict the original word based on surrounding context.
  3. Next Sentence Prediction (NSP): The model learns to predict if one sentence logically follows another, crucial for QA and NLI tasks.

Architecture:

Key Characteristics

Business Context

BERT and its derivatives (RoBERTa, DistilBERT) are the workhorses of enterprise NLP:

Real-World Analogy

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.

Code Example

# 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"

Common Misconceptions

Sources & Further Reading