Predicting the next word only ever looks left. But "the ___ sat" has a word missing from the middle — and the word after the blank, "sat", carries real information a left-to-right model can never use to fill it in.
The same masked word, the same attention mechanism from Part IV — the only thing that changes is which tokens are allowed to be seen. Toggle between a model that only looks left and one that looks both ways, and watch the reconstruction error change.
Masked language modeling hides a token and asks a model to reconstruct it from context. A causal (left-to-right) model restricts attention to only the tokens before the mask:
- — the token at position in the sequence.
- — the position of the masked (currently predicted) token.
- — the causal context: only tokens strictly before the mask.
- — the bidirectional context: every token except the mask itself, on both sides.
- BERT commits to the bidirectional form
BERT — short for Bidirectional Encoder Representations from Transformers — uses the second form. Since there's no autoregressive generation step to protect — the whole point is reconstructing a token that's already there — nothing stops the model from using tokens on both sides.
- The only difference from GPT is who can see whom
That's the entire architectural difference from a GPT (short for Generative Pre-trained Transformer)-style model: which half of the sentence is allowed to inform each prediction.
"the [MASK] sat" — reconstructing the hidden middle word using attention over the visible tokens. The causal version can only ever see "the"; the bidirectional version sees "the" and "sat" and blends both into its guess.
Tokens "the" , "cat" (masked), "sat" , with a generic mask query :
- Causal: only the left context exists
With no visible token but "the", attention collapses to weight on it: the prediction is exactly "the"'s vector, . Squared error against the true "cat" vector : .
- Bidirectional: both sides, scored equally
"the" and "sat" score identically against the mask query — — so softmax splits attention exactly . The prediction becomes their average, .
- Using both directions nearly halves the error
Squared error for the bidirectional prediction: — worse than a perfect reconstruction, but a real improvement over the causal model's , purely from being allowed to see "sat" as well as "the".
Find the attention mode that achieves the lower reconstruction error on the masked word.
BERT's core idea isn't a new attention mechanism — it's the same self-attention from Part IV, just unrestricted in which tokens can talk to which. Removing the left-to-right constraint costs the ability to generate text one token at a time (there's no "next word" to predict when every position already sees everything), but wins a strictly richer context for tasks like this one, where filling in a blank is the whole point. The next chapter takes the same self-attention mechanism somewhere it was never designed for: an image, with no sequence to speak of at all.