Foundations · A Visual Primer

Demystifying BERT Under the Hood

How a language model works out what a word means from the words around it.

The idea

“You shall know a word by the company it keeps.”

John Rupert Firth · Linguist · 1957
Same word, different meaning
We went to the bank money
Down by the river bank land

The context decides the meaning.

BBidirectional
EEncoder
RRepresentations
TTransformers

BERT reads a sentence in both directions at once, so every word is understood through its full context.

Search update · Oct 2019
Before BERT

Read as US citizens travelling to Brazil, the opposite of what was asked.

After BERT

Understood as visa rules for Brazilians visiting the United States.

0%of English-language searches improved. The word that changed everything was “to”.
Foundations · Transformer Architecture

The Revolution Behind BERT & Modern LLMs

Self-attention lets a model read every word at once. BERT and GPT are both built on it.

Seminar Breakthrough

“Attention Is All You Need” (2017)

Replaced older recurrent networks with self-attention mechanisms to process input sequences in parallel, laying the foundation for modern AI.

Parallel Self-Processing (4 Tokens)
Token 1
Token 2
Token 3
Token 4
Transformers come in three flavours: encoder-only, decoder-only, or a combination of both.
BERT

Encoder Transformer

Processes the entire input sequence simultaneously to deeply understand and contextualize text.

GPT / ChatGPT

Decoder Transformer

Optimized for autoregressive text generation, predicting subsequent tokens sequentially.

Masked Language Modeling · Origins

Where the [MASK] Came From

BERT’s training task is a reading test from 1953.

Excerpt from the paper The Cloze Procedure: A Conspectus, describing Wilson Taylor’s 1953 technique
Bickley, Ellington & Bickley, The Cloze Procedure: A Conspectus
  1. 1953
    The cloze test

    Wilson Taylor removes words from a passage and asks readers to fill in the gaps.

  2. 2018
    BERT’s training

    Google hides words behind [MASK] and trains the model to recover them.

Fill the gap

The cat sat on the ? and fell asleep.

You used the words on both sides of the gap. BERT does the same.

Masked Language Modeling

How BERT is trained

BERT learns language by filling in hidden words, using clues from both sides.

before after He went to the to transfer money [MASK] bank BERT one blank · clues on both sides · one confident guess
1 Hide a word 2 Read both sides 3 Predict the [MASK]
  • Hide 15% of words behind a [MASK]
  • BERT guesses each from words on both sides
  • Learns grammar, facts & context on its own

Transformer Architecture is used for deep bidirectional understanding

Inside BERT · 8 Steps

From Sentence to Prediction

Follow one sentence through each layer until BERT picks the missing word.

01 · Tokenize · like Lego bricks

Words snap into reusable pieces

30,522pieces are enough
to build every word
30,522 is the total size of its vocabulary dictionary.
hug ##ging his favorite

A few hundred Lego shapes build anything — you never need a custom brick. (Here hug·##ging splits into two pieces, while his and favorite are common enough to be single pieces — the ## means “glue me to the piece before.”)

02 · Embeddings · like a colour code

Each piece becomes 768 numbers

Why not just a couple of numbers?
Flat — 2 numbers
Action level → Release year → The Dark Knight The Avengers ≈ same spot

Two very different movies land almost on top of each other — the computer can’t tell them apart.

→
Add one axis — 3 numbers
Action level → Release year → Grittiness ↘ The Avengers The Dark Knight

One extra dimension sends The Dark Knight into the dark, gritty corner while The Avengers stays bright and comic-book.

The BERT reality — 768 dimensions at once
the dark knight →
dim 214Philosophical villain
dim 402Orchestral score
dim 588CGI explosions
orange
→
colour code #412
→
R 240 · G 90 · B 20sits near other oranges

Humans can’t picture a 768-D graph, but the computer reads it like a colour code — “#412” tells you nothing, while R240·G90·B20 says orange. BERT builds one of these 768-number fingerprints for every single token, so similar words land near each other.

03 · Multi-head Attention · a panel of experts

Every word asks “who matters to me?”

Attentionevery word looks at every other
He went to the [MASK] to transfer money
Every word weighs every other — the blank leans hardest on “transfer” & “money”.
Multi-head Attentiona panel of experts
Head 1subject
Head 2meaning
Head 3verb
Other headsHead 12

Like twelve people proofreading one page — the editor watches grammar, the lawyer watches risk, the designer watches rhythm. Each head notices a different link, then they pool their views.

04 · Query, Key, Value · like a search engine

How a word decides who matters

Every word searches every other word, then takes in information from the best matches.

Q
QueryWhat I’m looking forlike what you type into search
K
KeyWhat I can be matched onlike a page’s title
V
ValueWhat I hand overlike the page’s content
1Match · Query vs every Key
“I’m a place. Which words tell me what kind of place?”[MASK]’s Query
2Blend · mix the Values by score
[MASK] : a place→a place for money
→ next layers predict bank

Every word gets its own Q, K and V by multiplying its 768-number embedding by three learned matrices. BERT runs this for all words at once, in 12 heads per layer. Numbers are illustrative.

softmax( QKT / √d ) · V
1QKT match scores softmax scores → % 2· V blend √d keeps numbers stable
05 · Add & Layer Norm · the safety net

Keep the signal strong & balanced

Adda shortcut for the original
the original, kept layer output — weaker signal layer + add strong again
Deep layers can wash the original out — like a photocopy of a photocopy. So the signal takes a shortcut and is added back, so nothing important is lost.
Layer Normthe sound leveller
→
wild, unevencalm, balanced

First add the original back so nothing is lost — then normalise, like a sound engineer riding the faders: nothing too loud, nothing gone. Every layer stays stable.

06 · Feed-Forward Network · a private workshop

Each word thinks it over

768

contextual
vector

Zoom in→
3072

room to
think

Filter→
3072

weak signals
dropped

Zoom out→
768

sharper
word

Having heard the room, each word steps aside on its own — expanding to think it over, then compressing back down, refining what it just learned.

07 · Language Model Head · to get the output

From vector to word-scores

BERT hands over the blank’s 768-number vector. The LM (Language Model) Head compares it against every word BERT knows.

[MASK] bank ✓ cat ✗ same direction → high score
Why multiply? The blank has a meaning-vector and so does every word. Their dot product measures how much two meanings point the same way: big for bank, tiny for cat.
[MASK]1 × 768 · every word768 × 30,522 = scores1 × 30,522 logits

One dot product per word → a raw logit (score) for all 30,522 words.

08 · Score & Predict · the final vote

The best-fit word wins

He went to the [MASK] to transfer money
raw score  — softmax (eˣ) →  probability
bank8.9eˣ→71%
atm7.1eˣ→12%
office6.2eˣ→5%

Softmax turns the logits into probabilities that add to 100%. Winner bank, 71%.