BERT & Masked LM
Bidirectional Context
Looking left AND right
"on" has 3 tokens of left context (pink) and 3 tokens of right context (teal). BERT attends to BOTH simultaneously — this is why it outperforms left-to-right models on understanding tasks.
Step 1 of 6: Bidirectional Context
Looking left AND right
"on" has 3 tokens of left context (pink) and 3 tokens of right context (teal). BERT attends to BOTH simultaneously — this is why it outperforms left-to-right models on understanding tasks.
Step 2 of 6: Masking Strategy
Choosing which tokens to hide
15% masking: roughly 1 in 7 tokens are selected. BERT's original paper uses 15% — enough signal to learn, but not so much that the input is unrecognizable.
Step 3 of 6: Predicting [MASK]
Filling in the blank
[MASK] strategy (80% of cases): BERT sees "[MASK]" and correctly predicts "cat" with 91.2% confidence. This is the primary training signal — predict the hidden word from context.
Step 4 of 6: Pre-training Loss
Cross-entropy on masked tokens
Epoch 0: before training, the model predicts randomly across all three strategies. Loss is high (~4.5). The [MASK], random-word, and keep-original boxes all train BERT to predict the true token. Press ▶ Play or drag the slider.
Step 5 of 6: Fine-tuning
From pre-trained to task-specific
Before fine-tuning: the [CLS] embedding has general language knowledge but no task-specific signal. Accuracy starts around 50% (random). The pre-trained BERT weights already encode syntax and semantics.
Step 6 of 6: Contextual Embeddings
Same word, different meanings
"bank" has 2 distinct meanings: financial institution vs. riverbank. Word2Vec maps all uses to one point (amber circle). BERT creates different embeddings for each context — the "financial institution" uses cluster together (teal) and the "riverbank" uses cluster separately (pink). This is the power of contextual embeddings!