Foundations — BERT and encoder models
This page builds every symbol, letter, and piece of notation the parent note BERT and encoder models throws at you — starting from a smart 12-year-old who has seen none of it. Read top to bottom; each block earns the next.
0. What is a "token"? (before any math)
Look at Figure 1. The sentence gets sliced into little boxes (top row). Each box is a token, and directly beneath it sits its numbered slot (bottom row). This matters because everything below counts tokens, never words.

1. The vocabulary
Before any subscripts or vectors, we need the pool that all tokens come from.
Why the topic needs it: the embedding table, the softmax, and the MLM head all have a size tied to . So must come first — every later shape refers back to it.
2. The subscript:
Look again at Figure 1: the bottom row of teal boxes is exactly this — the numbered slots . So just means "look inside slot number ". The letter is a placeholder for "some slot we're pointing at right now".
- ::: total number of tokens (the length of the sequence).
- ::: a stand-in for "whichever position we're currently talking about".
Why the topic needs it: BERT processes a sequence, so we must be able to say "the 3rd token" precisely. Subscripts are how.
3. Vectors, boldface, and
The little symbol means belongs to / "is a member of". Figure 2 shows a 3-number vector as an arrow in space so you can see what a list of numbers looks like.

- ::: how long each meaning-vector is (BERT-base uses ).
- ::: a list of 768 decimal numbers.
- bold vs plain ::: bold = a list (vector); plain italic = one number.
Why the topic needs it: the parent says and . You cannot read those until "list of numbers" (and what bold signals) is solid.
4. How a token becomes a numeric vector: the embedding matrix
Before self-attention can mix anything, each raw token has to be turned into numbers. A token like play is really just an ID number — its row-number in the vocabulary (defined in §1). We look that row up in a big table.
So the pipeline is: raw token → look up in (+ position + segment) → a numeric input vector → then the Transformer.
5. From input vector to output
The key word is contextual: the same token gets a different depending on its neighbours. "bank" near "river" and "bank" near "money" produce different . This is the whole point of self-attention and the Transformer.
Why the topic needs it: every BERT task reads , not the raw token. MLM predicts hidden words from , sentiment reads , tagging reads each . Without the context-aware , "bank" would have one frozen meaning — and the entire bidirectional idea in the parent note would collapse.
6. Matrix–vector product — and which shape of
This is the task head: a small layer bolted on top of . The shape of depends on the job:
| Head | Question asked | Rows of | Shape |
|---|---|---|---|
| Sentiment (2 classes) | pos or neg? | 2 | |
| NSP (IsNext yes/no) | is B the real next sentence? | 2 | (or 1 row + sigmoid) |
| MLM (predict token) | which word of the vocab? | $ | V |
So is a two-class head (2 rows, 768 columns). For MLM the very same idea is used but the head must output one score per vocabulary word, so it has rows — one per possible answer. Figure 3 draws the two-class collapse from 768 down to 2; picture the MLM head as the same funnel but widening to outputs.

- (the "bias") ::: a small fixed vector added on after, to shift the scores.
- ::: the raw logits (unnormalised scores) for token ; it has one entry per output the head produces.
Why the topic needs it: turning a rich 768-number meaning into task scores is exactly what every head does — 2 scores for sentiment/NSP, scores for MLM.
7. Logits, , and softmax
To turn scores into probabilities we need two things: (a) make everything positive, (b) make them add to 1.
Reading the pieces:
- ::: the vocabulary (from §1) — the full list of tokens the model can output.
- ::: "add this up, letting run over every token in the vocabulary". The big (Greek capital sigma) just means sum.
- ::: the resulting probability of word ; the collection of all is the model's probability distribution.
Figure 4 shows three logits becoming three bars that add to 1 — the exponential stretches the gaps, then division normalises.

8. Logarithm and the minus sign:
Here is the softmax probability from §7 — specifically , the probability the model gave to the true answer . Loss punishes the model when this number is small.
- (perfect) (no penalty).
- .
- (confidently wrong) (huge penalty).
This is the heart of cross-entropy loss, the engine behind both MLM and NSP.
9. The [MASK] token, the masked set , and the MLM loss formula
The [MASK] token
The masked set
The MLM loss
Now every symbol is earned, so we can write the actual loss the parent note uses. We grade only the masked slots and average their penalties:
10. Remaining symbols: , , , — and the NSP head
11. Special tokens [CLS] and [SEP]
These aren't real words — they're signposts BERT invents so it can do sentence-level jobs. The subscript in is not a number; it just names which slot (the [CLS] slot).
Prerequisite map
Read it top to bottom: raw tokens are looked up in to become number-lists, self-attention mixes them into contextual , a matrix turns into scores, softmax makes probabilities , grades them, we average over the masked set , and that grade drives BERT's training.
Equipment checklist
Cover the right side. If you can answer each, you are ready for the parent note.
- What is the vocabulary , and what does mean? ::: the full fixed list of tokens BERT knows; is how many there are (~30k).
- What does the subscript in mean? ::: the position/slot of a token in the sequence.
- What does boldface (e.g. ) signal versus a plain italic letter? ::: bold = a vector (a list of numbers); plain italic = a single number.
- What is ? ::: the set of all lists of 768 real numbers; each meaning-vector lives here.
- What is the embedding matrix and its shape? ::: a table with one row per vocabulary token and columns, ; its row for a token is that token's embedding.
- What three embeddings are summed to form the input vector? ::: token, position, and segment embeddings.
- Difference between the input vector and ? ::: the input is context-free; is context-aware after self-attention.
- What does produce and why? ::: logits (raw scores) by mixing the 768 numbers into per-output scores.
- Which shape does take for sentiment/NSP vs MLM? ::: for a two-class head; for MLM (one score per vocabulary word).
- Why does softmax use ? ::: is always positive and amplifies larger scores, giving a clean probability distribution that sums to 1.
- What is in the softmax denominator? ::: the sum of the exponentials of every token's score (the normaliser).
- What is in ? ::: the softmax probability the model assigned to the true answer .
- Why the minus in ? ::: so the loss is positive and smaller means better; perfect gives loss 0.
- What is the
[MASK]token and why isn't it used 100% of the time? ::: a placeholder inserted where a word is hidden; using it always would cause a train/test mismatch, so it's 80% MASK / 10% random / 10% unchanged. - Write the MLM loss formula in words. ::: average over the masked set of of the true-token probability: .
- What is the set and what does mean? ::: is the set of masked slot-numbers; is how many were masked (the count of ).
- When do we use vs softmax, and what shape is the NSP head? ::: (sigmoid) for a single yes/no probability like NSP (a row ); softmax for choosing among many classes like MLM.
- What is used for? ::: as the whole-sentence summary vector for classification (sentiment, NSP).
Next: return to BERT and encoder models and every symbol will now read as plain English. See also Fine-Tuning vs Pretraining, RoBERTa, GPT and decoder models, and T5 and encoder-decoder models.