4.2.8 · D3Tokenization & Language Modeling

Worked examples — Next sentence prediction

3,547 words16 min readBack to topic

This page hammers the Next Sentence Prediction objective from every angle. To keep it self-contained, we restate every symbol and assumption from zero below, then squeeze the machinery through every case class it can hit.

Two tools we lean on constantly, defined from zero:

Recall Why the [CLS] token carries the answer

Question: which single vector do we feed to the NSP classifier, and why that one? ::: The final hidden state of the [CLS] token — it sits at the front and, through attention, has "looked at" every token in both A and B, so it aggregates the whole pair into one vector. Why this step? Feeding one pair-summary vector to a tiny linear head is what lets a single 2-way classifier decide IsNext/NotNext without re-reading every token itself.


The scenario matrix

Every situation NSP can produce falls into one of these cells. The examples below are labelled with the cell they cover.

Cell What varies Concrete case Example
C1 Positive pair, clear coherence Real next sentence, anaphora resolves Ex 1
C2 Negative pair, easy (topic shift) Random cross-document B Ex 2
C3 Sign of logits: model correct & confident High for true label Ex 3
C4 Sign of logits: model wrong High for wrong label → big loss Ex 4
C5 Degenerate: tie () Equal logits, Ex 5
C6 Limiting behaviour of loss and Ex 6
C7 Real-world word problem Build a batch, count labels Ex 7
C8 Exam twist: hard negative (same doc) Why NSP fails, SOP fix Ex 8
C9 Combined objective total Ex 9

Figures s01–s03 give the geometric backbone: the logit-to-probability curve, the loss curve, and the decision picture that places all four logit-sign examples on that loss curve.

Figure — Next sentence prediction

The curve above is the whole story of softmax for two classes: the x-axis is the signed vote gap (how much more the model votes IsNext than NotNext), the y-axis is . When , (cell C5). Positive pushes toward , negative toward . Keep this picture in mind — every example is just a dot on this S-curve.


Worked examples

Figure — Next sentence prediction

The loss curve (s02) shows why "correct but unconfident" (Ex 3) sits at moderate loss : it is on the gentle right side of the curve where is above but not near .

Figure — Next sentence prediction

Figure s03 is the decision picture: it drops the four "logit-sign" examples (Ex 3 correct-but-unsure, Ex 4 wrong, Ex 5 tie, Ex 6 both limits) as dots onto the very same loss curve. Reading it left to right you literally see the ranking we proved: confident-correct sits near , the tie sits at , correct-but-unsure just below it, wrong just above it, and confident-wrong shoots toward the top. That one image is the summary of the whole page.

Recall Quick self-test

Which row/index of the NSP head corresponds to IsNext? ::: Row 1 / index 1 (row 0 / index 0 is NotNext). A model outputs . What is the NSP loss no matter the true label? ::: . If but the true label is IsNext, what happens to the loss? ::: It diverges to (confidently wrong = most expensive). Why does BERT add MLM and NSP losses instead of choosing one? ::: A sum's gradient is the sum of gradients, so each head trains from its own signal while sharing one Transformer body (multi-task learning). What does SOP stand for and how does it differ from NSP? ::: Sentence Order Prediction — it swaps two same-document consecutive sentences and asks if the order is correct, removing NSP's easy topic-shift cue.