Lensa ML
Lensa ML

BERT & Masked LM

Step 1 of 6

Bidirectional Context

Looking left AND right

Focus token3
Bidirectional: attend to BOTH left and right context0.34The0.27cat0.42saton0.26the0.28warm0.20mat← Left-to-right (GPT)"The cat sat __"Can only see: The cat sat↔ Bidirectional (BERT)"The cat sat [MASK] the warm mat"Sees: The cat sat the warm matBERT sees the full sentence (minus the masked token) from both sides.
FOCUS
"on"
LEFT CTX
3
RIGHT CTX
3

"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

Focus token3
Bidirectional: attend to BOTH left and right context0.34The0.27cat0.42saton0.26the0.28warm0.20mat← Left-to-right (GPT)"The cat sat __"Can only see: The cat sat↔ Bidirectional (BERT)"The cat sat [MASK] the warm mat"Sees: The cat sat the warm matBERT sees the full sentence (minus the masked token) from both sides.
FOCUS
"on"
LEFT CTX
3
RIGHT CTX
3

"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

Mask probability (%)15
Original:Thecatsatonthewarmmat↓ mask 15%Input to BERT:Thecatsatonthe[MASK]matBERT's masking strategy (of selected tokens):80% → [MASK] tokenForces model to predict from context10% → Random wordPrevents model from ignoring [MASK]s10% → Keep originalTeaches model to verify correct words1 of 7 tokens selected (14%) — BERT only trains on predicting these
MASK %
15%
MASKED
1
RANDOM
0
KEPT
0

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 position1
Strategy: [MASK] token (80%)0
80% of selected tokens → replaced with [MASK]BERT sees:The[MASK]satonthewarmmatTarget: always predict original "cat"Top-5 predictions (true: "cat"):cat91.2%✓ truedog4.1%the1.1%hat0.7%cold0.7%+ 5 more words sharing 2.3% probability
STRATEGY
[MASK] token
TRUE TOKEN
"cat"
TOP PRED
cat
TRUE RANK
#1

[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

Epoch0
LossEpochPredictions across all 3 masking strategies:80% [MASK][MASK] → ?cat: 14%dog: 86%10% Random"ran" → ?sat: 8%ran: 92%10% Keep"warm" → ?warm: 13%cold: 87%
EPOCH
0
LOSS
5.42
AVG ACCURACY
12%

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

Fine-tuning epoch0
Pre-trained BERT + classification head[CLS]greatmovie[SEP]BERT (frozen or fine-tuned)h_[CLS]Linear + SoftmaxP(positive) = 53%Confusion Matrix:Pred +Pred −True +2624True −2426Fine-tuning: add a small classification head on top of [CLS] embeddingTrain for just 2-4 epochs on labeled data — pre-trained knowledge transfers
FT EPOCH
0
ACCURACY
51.2%
TRUE POS
26
FALSE NEG
24

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

Polysemous word0
"bank" in different contexts → different embeddingsWord2Vec (static)financial institutionriverbankdepositedriverapprovedsatExample contexts:"I deposited money at the bank" → financial institution"The river bank was muddy" → riverbank
WORD
"bank"
MEANINGS
2
CONTEXTS
4
TYPE
Contextual

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