4.2.8 · D4Tokenization & Language Modeling

Exercises — Next sentence prediction

2,173 words10 min readBack to topic

This page is a self-test ladder for Next sentence prediction. Work each problem before opening its solution. The levels climb from "can you recognise the pieces" (L1) up to "can you invent a variant" (L5). Every symbol used here is built inside the parent note; if a term feels unfamiliar, re-read the parent first.


Level 1 — Recognition

Exercise 1.1 — Label the input format

NSP feeds the model one concatenated string. Fill in the two special tokens and the segment IDs for this pair:

  • A: the sky is blue
  • B: it never rains here

Write the full token sequence in the form [?] ... [?] ... [?] and the segment-ID row.

Recall Solution 1.1

The format is ==[CLS] A [SEP] B [SEP]==. Segment ID covers [CLS] through the first [SEP]; segment ID covers B's tokens through the final [SEP].

Tokens:   [CLS] the sky is blue [SEP] it never rains here [SEP]
Segment:    0    0   0   0   0    0   1   1    1    1    1

Segment A here has tokens ([CLS] + 4 words + first [SEP]), segment B has tokens (4 words + final [SEP]). Total tokens.

Exercise 1.2 — Which label?

For each pair, state IsNext (1) or NotNext (0), given they were built by BERT's standard 50/50 procedure:

  • (a) A and B are consecutive sentences from the same document.
  • (b) B is drawn uniformly at random from the whole corpus.
Recall Solution 1.2
  • (a) ==IsNext, label ==. This is a positive example.
  • (b) ==NotNext, label ==. This is a negative example (random sampling).

Recall the split is 50% positive, 50% negative.

Exercise 1.3 — What does [CLS] do?

In one sentence: why does NSP read out its prediction from the [CLS] token and not, say, the last token of B?

Recall Solution 1.3

The ==[CLS] token's final hidden state is trained to aggregate the whole pair== — it attends to every token in both A and B through self-attention, so its vector is the natural "summary" to feed a classifier. Any token could in principle be used, but [CLS] is the designated slot BERT reserves and trains for this job.


Level 2 — Application

Exercise 2.1 — Count positive/negative pairs

You want a balanced NSP dataset of training pairs. How many positive and how many negative pairs, following BERT's default ratio?

Recall Solution 2.1

Default ratio is .

Exercise 2.2 — Softmax from logits

The NSP head outputs logits for : Compute .

Here turns two raw scores into probabilities that sum to ; we use it (not just "pick the bigger logit") because training needs a probability to plug into cross-entropy.

Recall Solution 2.2

Numerically , , so And . The model leans IsNext.

Exercise 2.3 — Cross-entropy loss for one example

Using the probabilities from 2.2, the true label is IsNext (). Compute the NSP loss for this single example, where

Recall Solution 2.3

With only the first term survives: (Natural log.) Because the model was already fairly confident and correct, the loss is small.


Level 3 — Analysis

Exercise 3.1 — Reproduce the parent's forward pass

Given and compute both logits, then . Confirm the model's predicted class.

Look at figure s01: each logit is a dot product of the CLS vector with one weight row, then a bias shift.

Figure — Next sentence prediction
Recall Solution 3.1

Row 0 (NotNext): Row 1 (IsNext): Softmax: Since , the ==predicted class is NotNext== at . Matches the parent note.

Exercise 3.2 — Why NSP accuracy hits 97%+ so fast

NSP training accuracy climbs past within a fraction of pre-training. Explain, using the construction of negatives, why this does not prove the model learned deep coherence.

Recall Solution 3.2

Negatives are drawn uniformly from the entire corpus, so B usually comes from a different document on a different topic. The model can separate IsNext from NotNext by a cheap heuristic — detecting topic/word overlap — without reasoning about narrative flow. High accuracy therefore reflects the task being easy, not the model being deep. This is exactly why SOP (same-document, only order swapped) was proposed: it removes the topic shortcut.

Exercise 3.3 — Interpreting the ablation

BERT with NSP scores MNLI; without NSP, . RoBERTa dropped NSP and lost nothing. Compute the reported NSP gain, and reconcile the two findings in one line each.

Recall Solution 3.3

Gain points.

  • BERT view: with BERT's short-pair data setup, NSP added a small but real signal ( pts).
  • RoBERTa view: by packing full sentences up to tokens, MLM already saw cross-sentence context, so NSP became redundant — hence no drop when removed. Both are consistent: NSP helped BERT's specific setup, not language modeling universally.

Level 4 — Synthesis

Exercise 4.1 — Design a harder negative sampler

Rewrite the negative-example rule so the model cannot solve NSP by topic detection. State your rule and name the published objective it corresponds to.

Recall Solution 4.1

Rule: take two genuinely consecutive sentences A, B from the same document and, for negatives, swap their order (present B then A) with label "swapped". Both segments share topic, entities, and style, so the topic-overlap shortcut is gone — the model must judge directional coherence. This is Sentence Order Prediction (SOP), from ALBERT. See 4.2.09-Sentence-order-prediction.

Exercise 4.2 — Combined pre-training objective

BERT optimises MLM and NSP together. Write the total loss and explain, in terms of what each signal teaches, why summing them (rather than training separately) is chosen.

Recall Solution 4.2

  • (see 4.2.06-Masked-language-modeling) teaches token-level semantics — filling masked words.
  • teaches sentence-level discourse — pair coherence. Summing lets one forward pass produce both gradients, so the shared Transformer body learns complementary signals simultaneously; the same [CLS]-and-tokens representation is pushed to be good at both scales at once, which is cheaper and more stable than alternating separate runs.

Exercise 4.3 — Map NSP to a downstream task

Explain how the exact NSP head transfers to a real task with almost no change, and name one such task from the connections.

Recall Solution 4.3

NSP is already a [CLS]-vector → 2-class linear head over a sentence pair. A pair-classification downstream task reuses this structure verbatim: feed [CLS] premise [SEP] hypothesis [SEP], read [CLS], apply a (re-initialised) linear head. This is the essence of 5.1.04-Transfer-learning-NLP. Concrete task: 6.3.02-Natural-language-inference (premise–hypothesis pairs), or 6.2.01-Question-answering-systems (passage–question pairs).


Level 5 — Mastery

Exercise 5.1 — Expected loss of a random classifier

A freshly initialised NSP head outputs for every example. On a balanced dataset, what is the expected per-example cross-entropy loss? Give the exact form and the number.

Recall Solution 5.1

Whatever the true label, the loss is . So the expectation is This is the classic "" starting loss for a balanced binary task — a sanity-check baseline: if your NSP loss starts far from , something is mis-initialised.

Exercise 5.2 — Confidence needed to beat baseline by a target margin

You want each correct, confident example to have loss . What minimum achieves this?

Recall Solution 5.2

Require . So the model must assign probability to the correct class.

Exercise 5.3 — Full end-to-end synthesis

A negative pair is built. After the Transformer, . Using the same as Exercise 3.1: (a) compute logits, (b) , (c) the loss given true label NotNext (), (d) state whether the prediction is correct.

Recall Solution 5.3

(a) Logits: (b) Softmax for NotNext: (c) True label NotNext (): loss (d) Since , predicted class is ==NotNext, which is correct==, with confidence.


Recall Quick self-quiz (reveal answers)

Format [CLS] A [SEP] B [SEP] — final [SEP] segment ID? ::: 1 (belongs to B) BERT positive/negative NSP split? ::: 50% / 50% NSP gain on MNLI in BERT ablation? ::: about 1.2 points (84.6 vs 83.4) Baseline cross-entropy for balanced binary? ::: ln 2 ≈ 0.693 Objective that replaced NSP in ALBERT? ::: Sentence Order Prediction (SOP) Why RoBERTa lost nothing dropping NSP? ::: long packed sequences let MLM capture cross-sentence context

Related: 4.2.06-Masked-language-modeling · 4.2.09-Sentence-order-prediction · 4.3.02-BERT-architecture · 4.3.08-RoBERTa · 4.2.03-Segment-embeddings · 5.1.04-Transfer-learning-NLP · 6.2.01-Question-answering-systems · 6.3.02-Natural-language-inference