4.3.3 · D4Pretraining & Fine-Tuning LLMs

Exercises — T5 and text-to-text framework

2,214 words10 min readBack to topic

Before we start, one reminder of the single object everything is scored against.


L1 — Recognition

Exercise 1.1 (L1)

Below are four things. For each, say whether T5 treats it as input string, output string, or neither / a separate head. (a) The label "positive"; (b) a softmax regression neuron; (c) "summarize: <article>"; (d) the similarity number 3.8.

Recall Solution 1.1

(a) output string — labels are printed as text, not chosen by a label head. (b) neither — T5 has no regression head; this is exactly what it removes. (c) input string — the prefix "summarize:" plus the article is the encoder input. (d) output string — emitted as the characters "3.8", rounded to the nearest .

Exercise 1.2 (L1)

Match each item to T5's answer: encoder-only, decoder-only, or encoder–decoder — and name one model on each of the other two.

Recall Solution 1.2

T5 is encoder–decoder. The two it is not: encoder-only = BERT and masked language modeling; decoder-only = GPT and decoder-only models. The generic backbone is the Transformer encoder-decoder architecture.


L2 — Application

Exercise 2.1 (L2)

Apply span corruption to the sentence the cat sat on the mat (6 tokens). Corrupt the span cat (position 2) and the span on the (positions 4–5), using sentinels <X>, <Y>, <Z>. Write the corrupted input and the target.

Recall Solution 2.1

Replace each contiguous dropped span with one sentinel, then list sentinel-then-original-text in the target, closing with the next unused sentinel.

  • Input: the <X> sat <Y> mat
  • Target: <X> cat <Y> on the <Z>

<X> stands for the single word cat; <Y> stands for the whole two-word span on the; the trailing <Z> marks the end of the target (nothing follows it).

Exercise 2.2 (L2)

A target string is tokenized as ["Das", "ist", "gut", "</s>"] (). The model's probability for the correct token at each step is Compute the total loss (natural log) and the per-token average.

Recall Solution 2.2

Plug straight into the formula — sum of : Numerically: , , , . Per-token average . Notice the </s> token contributed : the model was perfectly sure the sentence ended, so it added no loss.

Exercise 2.3 (L2)

For the STS-B task the true similarity is . T5 rounds targets to the nearest . What string is used as the training target?

Recall Solution 2.3

Round to the nearest multiple of . The multiples near it are and ; is from and from , so it snaps to . Target string "3.8". (Rounding to keeps the set of possible targets finite — , i.e. 26 possible strings — so it stays a clean language-modeling problem.)


L3 — Analysis

Exercise 3.1 (L3)

BERT and T5 both hide about of tokens. Explain, in terms of what the output object is, why they still need different loss shapes. Then state which of the two could, in principle, output a variable-length answer per masked region.

Recall Solution 3.1
  • BERT (BERT and masked language modeling): the output at a masked position is a single softmax over the vocabulary — one classification per masked slot, independent of the others. Its output length is fixed = number of masks.
  • T5: the output is a generated sequence <X> span <Y> span …. The decoder emits tokens autoregressively, so a span can decode to any number of tokens. Therefore T5 can output variable-length answers per region; BERT cannot (one slot ⇒ one token). This is exactly why T5's loss sums over a target sequence while BERT's sums over fixed positions.

Exercise 3.2 (L3)

Two candidate decodings for a classification task with target "positive" are produced by greedy decoding:

  • Run A emits "positive".
  • Run B emits "positve" (a typo — off-vocabulary label).

Under T5's inference rule, what is B's score on this example, and why is that the correct behavior?

Recall Solution 3.2

B counts as wrong (accuracy on this example). At inference T5 just reads the produced string and checks whether it equals a valid label; "positve" is not a label, so it is scored incorrect — same as an outright wrong answer. This is the honest behavior: the model was asked to produce the label string and did not, so treating a malformed string as "close enough" would inflate accuracy. In practice such off-vocabulary outputs are rare after fine-tuning because the label strings dominate the target distribution.


L4 — Synthesis

Exercise 4.1 (L4)

Design a T5 example for a new task: detect whether an email is spam. You must (i) invent a task prefix, (ii) give the input string, (iii) give the two possible target strings, and (iv) explain why no code changes are needed versus a translation example.

Recall Solution 4.1
  • (i) Prefix: "spam classify: ".
  • (ii) Input: "spam classify: You WON a free prize, click here!!!".
  • (iii) Targets: "spam" or "ham" (two label strings).
  • (iv) The model still receives a string and emits a string; the same encoder reads it, the same decoder generates, the same cross-entropy over target tokens trains it. Only the data changes (different prefix + label vocabulary), never the architecture or loss. This is the Multi-task learning with prefixes payoff — see also Transfer learning and fine-tuning.

Exercise 4.2 (L4)

You want one T5 checkpoint to do both translation and spam detection at once. Write a two-line mixed mini-batch (one example each) and explain how the model knows which task to run.

Recall Solution 4.2

Mini-batch:

  • "translate English to German: Good morning.""Guten Morgen."
  • "spam classify: Claim your reward now!""spam"

The model knows the task purely from the prefix at the start of the input — there is no task-ID input, no per-task head. Because both examples are string → string scored by the same loss, they can share a gradient step. This is exactly Prompting and instruction tuning in seed form: the instruction lives inside the text.


L5 — Mastery

Exercise 5.1 (L5)

Claim: "Cross-entropy loss and maximizing give the same optimum." Prove it for the single-token case, and verify numerically that if , then .

Recall Solution 5.1

Because is a strictly decreasing function of its argument on , whatever value of the parameters maximizes simultaneously minimizes . No maximum of can move without moving in the opposite direction, so the two problems share the same optimizer. The log also turns the product into a sum , which is what makes the multi-token loss additive and numerically stable. Numerically ✓.

Exercise 5.2 (L5)

Two models are graded on Exercise 2.2's target sequence. Model P assigns correct-token probabilities (total loss from 2.2). Model Q assigns . Which has lower loss, and by how much? Then convert both to perplexity and interpret.

Recall Solution 5.2

Model P: (from 2.2), per-token , so . Model Q: , per-token , . Q's loss is lower by . Perplexity reads as "effective number of equally-likely choices per token": Q is near (almost certain each step), P is (roughly like flip-a-slightly-loaded-coin uncertainty). Lower perplexity = crisper predictions.

Exercise 5.3 (L5)

Defend or refute: "Because T5 emits 3.8 as characters, a prediction of 4.0 and a prediction of 4.8 are equally wrong under cross-entropy when the target is 3.8." Reason about it at the token level.

Recall Solution 5.3

Refute (partially). Cross-entropy scores each token against the target token. Target "3.8" tokenizes to (roughly) 3, ., 8. A prediction of 4.0 gets the first digit and the last digit wrong; 4.8 gets the first digit wrong but the last digit 8 right. So token-wise they are not equally wrong — 4.8 can incur less cross-entropy because it matches one more target token. However, cross-entropy has no notion of numeric distance: it does not know that is closer to than is. So the metric rewards character overlap, not numeric proximity — a genuine limitation of text-as-number regression, and the reason rounding to a small fixed grid ( steps) matters.


Active Recall

Connections