4.2.7 · D3Tokenization & Language Modeling

Worked examples — Masked language modeling (BERT)

1,806 words8 min readBack to topic

This page drills Masked language modeling (BERT) through concrete numbers. We attack every case the masking objective and its softmax loss can throw at you: normal masking, the two "no-[MASK]" corruption cases, degenerate zero-mask sequences, the limiting best/worst-case loss, a word problem, and an exam twist that mixes MLM with NSP.

Before we start, one symbol we will lean on constantly, built from zero:

Figure — Masked language modeling (BERT)

The scenario matrix

Every worked example below is tagged with the cell it fills. Together they cover the whole grid.

Cell What makes it tricky Covered by
A. Standard 80% [MASK] ordinary masked-token prediction Ex 1
B. 10% random-token swap input is wrong word, target is original Ex 2
C. 10% keep-unchanged input already correct, still must predict Ex 3
D. Zero / degenerate mask : what is the loss? Ex 4
E. Limiting values perfect () vs uniform-guess () Ex 5
F. Multi-token sum several masked slots added together Ex 6
G. Real-world word problem count masked tokens in a real corpus Ex 7
H. Exam twist combine MLM loss with NSP loss Ex 8

Ex 1 — Cell A · The ordinary 80% [MASK]

Forecast: cat has the top score, so guess around and a modest loss below .

  1. Exponentiate every score. Why? Softmax needs positive weights.
  2. Add them to get the normaliser. Why? The denominator makes the list sum to .
  3. Divide cat's weight by . Why? That is the definition of .
  4. Take negative log. Why? That is the per-slot MLM loss.

Verify: All four probabilities are and sum to ✓. Loss is well under (the clueless value), so the model is genuinely confident — consistent with cat having the highest score.


Ex 2 — Cell B · The 10% random-token swap

Forecast: the input literally shows mat, so the model is tempted by mat (highest score) but must still say cat — expect a larger loss than Ex 1.

  1. Note the target is the clean token. Why? Cell B's whole point is denoising: input is noisy, label is original cat. This is the train–test-robustness trick.
  2. Exponentiate: .
  3. Normaliser: . Why? Same softmax rule.
  4. Probability of the true word cat: .
  5. Loss: .

Verify: Probabilities sum to ✓. Loss from Ex 1 — correct, because the corrupted input actively misleads the model, so the same objective is harder here. This is exactly why the 10% swap forces robustness.


Ex 3 — Cell C · The 10% keep-unchanged

Forecast: input already shows the right word and score for cat is high — expect a small loss.

  1. Understand the branch. Why? Cell C stops the model from assuming "a position is masked ⇒ it's wrong". The word is unchanged but still a labelled prediction target.
  2. Exponentiate: ; three copies of .
  3. Normaliser: .
  4. Probability: .
  5. Loss: .

Verify: Sum ✓. Loss — the smallest of the three corruption cells, matching intuition: when the correct token is already visible and highly scored, the model is barely surprised.


Ex 4 — Cell D · Degenerate: zero masked tokens

Forecast: no masked slots means nothing to predict — guess loss .

  1. Write the loss as a sum over . Why? The objective only ranges over masked positions.
  2. Empty sum rule. Why? A sum over the empty set is by definition.
  3. Gradient consequence. Why? , so this sequence contributes no weight update.

Verify: With , there is indeed no term. In practice BERT guarantees at least one masked token per sequence precisely to avoid wasting a training example on a zero-loss forward pass. ✓.


Ex 5 — Cell E · Limiting values (best and worst case)

Forecast: best case loss ; worst case is a positive number around ish (since ).

  1. Best case. Why? means zero surprise. .
  2. Worst case — uniform. Why? If every word is equally likely, , and
  3. Interpret the range. Why? Any real MLM loss lives in . Early training sits near ; a converged BERT drops toward .

Verify: ✓ and ✓. This is the theoretical ceiling — if you ever log an MLM loss above , you have a bug (e.g. labels misaligned).


Ex 6 — Cell F · Summing several masked slots

Forecast: two positive contributions added; each is under , so total under .

  1. Per-slot losses. Why? Each masked slot contributes independently (the conditional-independence assumption in the parent derivation).
  2. Sum them. Why? literally adds over .

Verify: Equivalent to ✓ — confirming the "product-inside becomes sum-outside" log identity from the parent's step 2.


Ex 7 — Cell G · Real-world word problem

Forecast: of 2B is M targets; of those are [MASK] M.

  1. Total masked targets. Why? The protocol selects of all tokens as prediction targets.
  2. Of those, actual [MASK] symbols. Why? Only the branch writes the literal [MASK] token.
  3. Sanity on the other branches. Why? Completeness — the remaining M split as random (M) + unchanged (M).

Verify: = total targets ✓, and is exactly of 2B ✓. Units: all counts are dimensionless token counts, consistent throughout.


Ex 8 — Cell H · Exam twist: total BERT loss (MLM + NSP)

Forecast: NSP loss is small (label almost certain), so total just above .

  1. NSP loss. Why? NSP is a binary classification: .
  2. Add the two objectives. Why? Parent note: .

Verify: NSP contributes only of the total — MLM dominates, which is why later models (RoBERTa) dropped NSP with little loss of quality. ✓.


Recall checks

Recall Why does Cell B give a bigger loss than Cell A?

Because the corrupted input shows a wrong word, actively misleading the model, yet the target is still the original — a harder prediction. ::: The 10% random-swap forces robustness.

Recall What is the maximum possible per-slot MLM loss?

(uniform guessing). ::: For a 30k vocab that is .

Recall Loss of a sequence with an empty mask set?

Exactly — an empty sum. ::: BERT forces mask to avoid wasting the example.