4.2.7 · D1Tokenization & Language Modeling

Foundations — Masked language modeling (BERT)

2,227 words10 min readBack to topic

This page assumes you know nothing. Before you can read the parent note, every squiggle it uses must first be a picture in your head. We build them one at a time, each from the one before.


0. What is a "token"?

Before any math, the atom of everything here is the token.

Picture a sentence as a row of boxes, one word per box:

Look at the figure. "The cat sat" becomes three boxes: [the] [cat] [sat]. Each box is a token. The model never sees letters — it sees this row of boxes.

Why the topic needs it: MLM works by hiding a box and asking the model to refill it. If we couldn't cut text into boxes, there would be nothing to hide.


1. Subscripts and the sequence

Now that we have boxes, we need to name each box so we can point at one.

  • = the token in box 1
  • = the token in box 2
  • = the token in box number (where is a stand-in for any number)
  • = how many boxes there are in total (the length of the sentence)

Why the topic needs it: The MLM loss sums over specific boxes (the hidden ones). We can only say "box 3 and box 5" if every box has a number.


2. Sets and the masked positions

We hide some boxes, not all. We need a way to say "the collection of box-numbers we hid."

In the figure, out of 7 boxes we hide boxes 3 and 5, so . The hidden boxes turn coral; the visible ones stay lavender.

Why the topic needs it: BERT hides ~15% of tokens. is exactly which ones. Every prediction and every piece of loss happens only at positions inside .


3. Probability — the model's confidence

The model doesn't know the hidden word; it guesses with a confidence. That confidence is a probability.

Why the topic needs it: MLM is literally "predict the word given the clues." That single expression is the whole task, written in symbols.


4. Vectors and — turning a word-in-context into numbers

A computer can't multiply words. Every box must become a list of numbers.

The figure shows the pipeline: word → its own number-vector → plus context from neighbours → the final vector . Two identical words in different sentences get different — that's the whole point of "contextual."

Why the topic needs it: Loss and softmax operate on numbers, not letters. is the bridge from language to arithmetic.


5. The dot product — measuring "match"

How do we score how well a target matches the context vector ? We use the dot product.

Why the topic needs it: is the raw score (a "logit") for word at box . Higher score → the model likes word more here.


6. exp and the softmax — turning scores into probabilities

Scores can be any number (even negative). Probabilities must be between 0 and 1 and add up to 1. We need a converter.

Putting it together, the softmax for word at box :

Read it as a fraction: top = "how much score this one word got (made positive)"; bottom = "the total score of all words." Dividing forces the numbers to add to 1 across the vocabulary — a valid probability.

The figure shows raw scores (some negative) becoming positive bars via , then squeezed down so they sum to exactly 1.

Why the topic needs it: This is how the raw dot-product scores become the confidence that the loss reads.


7. log and the minus sign — building the loss

The last piece: turning "confidence in the right word" into a number to shrink.

Now the parent's loss reads plainly: "For every hidden box , take the model's confidence in the true word, log it, flip the sign, and add them up. Shrinking this number = making the model more confident in the right words."


Prerequisite map

Token: text cut into boxes

Sequence x with subscripts x_i

Set M of masked positions

Vector h_i: box as numbers

Dot product: match score

exp: scores to positive

Softmax: scores to probabilities

Probability P given context

MLM loss

log and minus sign

Masked Language Modeling BERT

Read top to bottom: boxes give us a numbered sequence; hiding some gives ; each box becomes a vector ; dot products score candidate words; exp + softmax turn scores into probabilities; log + minus assemble the loss — and that is MLM.


Equipment checklist

Self-test: cover the right side and answer each before revealing.

What is a token?
One small unit of text (a word or word-chunk) the model treats as a single box.
What does the subscript in mean?
"The token in box number 3" — a house number for one item in the sentence.
Difference between bold and plain ?
is the whole sentence (the team); is one token (one player).
What is the set ?
The collection of box-numbers we chose to hide (mask).
What does mean?
Box-number is one of the masked positions.
What does the bar in mean?
"The probability of given that we already know ."
What is ?
The corrupted sentence actually fed to the model — the original with masked boxes covered up.
What is a vector?
An ordered list of numbers; picture a stacked column or an arrow.
What is ?
The number-vector the Transformer produces for box after reading the whole sentence (context-aware).
What does a dot product give you and why use it here?
One number measuring how aligned two vectors are — used to score how well a candidate word matches the context.
What two jobs does do in softmax?
Makes every score positive, and exaggerates gaps so the top choice stands out.
Why divide by in softmax?
To force all the word-probabilities to add up to 1, making them a valid probability distribution.
Why take of the probability?
For numerical stability (products become sums) and to punish confident-wrong guesses harshly.
Why the minus sign in ?
Optimizers minimise; maximising equals minimising .
In one sentence, what does the MLM loss measure?
How un-confident the model is about the true words at the hidden positions — we shrink it to teach the model.