4.3.2 · D3Pretraining & Fine-Tuning LLMs

Worked examples — BERT and encoder models

2,190 words10 min readBack to topic

This deep dive gives you a full worked-example gym for BERT. We do not re-teach the theory — we exercise it. Every knob that BERT can turn (how confident the model is, how many tokens are masked, the 80/10/10 split, a two-sentence NSP head, a fine-tuning classifier head, and the degenerate/limiting corners) gets a concrete number that you can check by hand.

Before we start, one promise: every symbol used below is re-earned here. If you have not read the parent note, that's fine — I define each ingredient the moment it appears.


The scenario matrix

Think of BERT's math as a machine with a few dials. Each row below is a class of situation the machine can be in. Our examples must hit every cell.

# Case class What makes it special Hit by
C1 Confident correct prediction true token has the biggest logit → small loss Ex 1
C2 Confident wrong prediction true token has a small logit → large loss Ex 2
C3 Uniform / uncertain (degenerate) all logits equal → loss Ex 3
C4 Multiple masked tokens averaged loss is a mean over positions Ex 4
C5 Zero-mask / limiting case nothing masked → loss undefined/zero contribution Ex 4 (part b)
C6 80/10/10 counting (real-world word problem) how many tokens become [MASK] vs random vs kept Ex 5
C7 NSP binary head (both signs of label) IsNext vs NotNext, sigmoid + binary cross-entropy Ex 6
C8 Fine-tuning classifier head [CLS] vector → linear → softmax over classes Ex 7
C9 Exam-style twist "predict left-to-right?" trap + combined total loss Ex 8

Two tools appear repeatedly, so let me name them once.

Figure — BERT and encoder models

(All logs below are natural logs, base , to match the theory in the parent note.)


Ex 1 — C1: Confident and correct


Ex 2 — C2: Confident and WRONG


Ex 3 — C3: Uniform logits (the degenerate "no information" case)


Ex 4 — C4 & C5: Averaging over masked positions, and the zero-mask limit


Ex 5 — C6: The 80/10/10 counting word problem


Ex 6 — C7: The NSP head, both label signs


Ex 7 — C8: Fine-tuning classifier head


Ex 8 — C9: The exam-style twist


Coverage check

Recall Did we hit every matrix cell?

C1 correct-confident ::: Ex 1 (loss 0.408) C2 confident-wrong ::: Ex 2 (loss 3.095) C3 uniform/degenerate ::: Ex 3 (loss = log V = 1.386) C4 averaging masks ::: Ex 4a (1.7515) C5 zero-mask limit ::: Ex 4b (undefined, excluded) C6 80/10/10 counting ::: Ex 5 (24 / 3 / 3) C7 NSP both signs ::: Ex 6 (0.263 vs 1.463) C8 fine-tune head ::: Ex 7 (0.127) C9 exam twist ::: Ex 8 (total 2.0149)

Return to the parent: BERT and encoder models. Related machinery: Cross-Entropy Loss, Transformer Architecture, T5 and encoder-decoder models.