Worked examples — Supervised fine-tuning (SFT)
This page is a case-by-case drill for the SFT loss. The parent SFT topic note built the loss from scratch; here we make sure you never meet a scenario you haven't already solved by hand. Every symbol used below was earned in the parent — we recall each one the moment it appears.
Recall The two formulas we will use over and over
Per-token loss for a target token : the model outputs a probability (a number between and ) of the correct next token, and the loss is The here is the natural log (base ). "" turns a probability into a penalty: probability → loss (perfect), probability near → loss shoots to (terrible).
Masked average loss over a sequence of tokens with mask (1 = response token we grade, 0 = prompt token we ignore): The denominator is just "how many response tokens we counted" — dividing by it gives a per-token average, so length doesn't inflate the loss.
The scenario matrix
Cross-entropy has fewer "quadrants" than trigonometry, but it has its own edge cases: probabilities near the two boundaries and , the loss-mask bookkeeping, length normalization, and the "is loss going up or down?" reading of training curves. Here is every cell we must cover.
| Cell | Case class | What makes it tricky | Example |
|---|---|---|---|
| A | Typical mid-confidence token | baseline arithmetic | Ex 1 |
| B | Near-perfect prediction () | loss → 0, limiting behaviour | Ex 2 |
| C | Near-zero prediction () | loss → , degenerate | Ex 2 |
| D | Loss mask on prompt vs response | which tokens count | Ex 3 |
| E | Length normalization (short vs long answer) | why we divide by token count | Ex 4 |
| F | Reading a training curve (loss up vs down) | sign/direction interpretation | Ex 5 |
| G | Real-world word problem (dataset choice) | applying imitation logic | Ex 6 |
| H | Exam twist: multi-class softmax → probability | where comes from | Ex 7 |
| I | Exam twist: log base & units (nats vs bits) | which log, converting | Ex 8 |
Every numeric answer below is machine-checked in the Verify block.
Example 1 — Cell A: a typical token
Forecast: Probability is "one-in-four" — not great, not disastrous. Guess: loss somewhere around to .
- Write the formula. . Why this step? Every per-token loss is just the negative log of the probability given to the true token — nothing else enters.
- Plug in. . Why? is exactly the probability of the correct token, which is what eats.
- Compute. , so . Why? is negative because ; the minus sign flips it positive so it's a valid penalty.
Verify: Sanity check the boundaries. gives ; gives . Our sits between them at . ✓ Monotone: lower probability → higher loss.
Example 2 — Cells B & C: the two limits ( and )
Forecast: B should be tiny (nearly perfect). C should be large. Guess: B ≈ , C ≈ .
- Case B: . Why? Near-perfect prediction — we expect the penalty to vanish.
- Compute B: . Why it looks like : write with . The Taylor expansion of around is , so for tiny the leading term dominates — hence . The curve is nearly flat and touches at .
- Case C: . Why? A confidently-wrong token is punished hard; as the loss .
Verify (limiting behaviour): Look at the loss curve below. At the blue curve hits (loss can never go negative — probabilities can't exceed ). As the curve rockets up with no ceiling. Both computed points ( and ) lie on the curve. ✓

Example 3 — Cell D: where the loss mask goes
Forecast: Only the last three tokens should count. Guess: loss around .
- Set the mask. for
<user>,Hi,<asst>(prompt / template); forHello,!,<eos>. Why this step? We only teach the model to produce the answer. The prompt is user-given input — grading the model for "predicting" it wastes capacity. - Keep only masked tokens. Response probabilities: . Why? The in the formula zeroes out every term.
- Per-token losses. , , . Why? Same applied to each surviving token.
- Average over the counted tokens. Why divide by 3, not 6? The denominator is the number of masked-in tokens, not the total sequence length.
Verify: Include <eos> — it carries because "knowing when to stop" is a skill we teach. If we had (wrongly) trained on all 6 tokens, we'd have divided a larger sum by 6 and gotten a different, contaminated number. Our masked answer ignores <user>/Hi/<asst> entirely. ✓
Example 4 — Cell E: length normalization
Forecast: Summed loss should favour the short answer (fewer terms). Averaged loss should call them equal. That's the whole point of dividing.
- Per-token loss is identical. Each token: . Why? Same probability everywhere, so same per-token penalty.
- Summed (un-normalized) loss. A: . B: . Why this matters: if we didn't divide, long answers would look "worse" purely for being long, and the optimizer would bias toward terseness.
- Averaged loss. A: . B: . Why? Dividing by (2 and 8 respectively) cancels the length. Now both equal exactly .
Verify: Averaged losses match to the digit (). ✓ This is precisely why the parent note's formula has out front — loss magnitude must not depend on answer length.
Example 5 — Cell F: reading a training curve
Forecast: Loss falling means the model gets more probability right, so the effective probability should climb toward 1.
- Invert the loss. Since , we recover . Why this tool — the exponential? is the exact inverse of ; it "undoes" the loss transform to recover the probability the loss corresponds to. We use (not ) because SFT losses are in natural log.
- Convert each.
- Why? Each step maps the reported loss to the geometric-mean probability the model assigns to true tokens.
- Read the direction. The effective probability rose . Why this is "learning": gradient descent is doing its job — SFT is imitation, so rising probability on demonstration tokens = the model is copying the demos better.
Verify: Round-trip check: , . ✓ Loss down ⇔ true-token probability up. (Falling loss is good; a rising SFT loss would mean it's un-learning the demos.)
Example 6 — Cell G: real-world word problem
Forecast: SFT is imitation, and the loss rewards matching whatever you show it. So messy demos → messy behaviour. Guess: pick A.
- Restate the loss's job. For a single example, the loss is our masked average — the same denominator (number of response tokens) from the recall box. It is minimized when the model assigns high probability to exactly the response tokens in your demonstration. Why this step? The math has no notion of "truth" — only "match the target." Good target → good habits; bad target → bad habits.
- Trace what B teaches. In (B), many targets contain hallucinated facts, sloppy formatting, rude tone. Minimizing loss on them literally trains the model to reproduce those. Why? Every noisy demonstration is a token sequence the loss pushes the model toward.
- Trace what A teaches. In (A), every target is clean, so the same loss now pushes toward clean behaviour. Fewer examples, but every gradient step points the right way. Why fewer can win: SFT mostly teaches format and behaviour, not facts (those come from pretraining). A few thousand excellent demos are enough to fix behaviour — the LIMA finding.
- Decision: choose A.
Verify (consistency check): This matches the parent's Worked Example 3 (80/20: data quality is the 20% that matters) and its mistake box "SFT gives new knowledge" (false — it teaches behaviour). No contradiction. ✓
Example 7 — Cell H: where actually comes from (softmax)
Forecast: cat has the biggest logit, so it should get the biggest probability — maybe — and a modest loss.
- Why softmax at all? Logits can be any real number (negative, huge), but a loss needs a probability in that sums to across the vocab. The softmax is the standard tool that turns arbitrary scores into a valid probability distribution: it exponentiates (making everything positive) then normalizes (making them sum to 1).
- Exponentiate each logit. , , . Why? Exponentiation guarantees positivity and amplifies gaps between scores.
- Sum and normalize. Sum . So Why? Dividing by the total makes the three probabilities add to exactly 1.
- Loss on the true token. . Why? is exactly the fed into the per-token loss.
Verify: The three probabilities sum to . ✓ And . ✓ Softmax is where every in this whole page is born.

cat, dog, eos; blue bars are the probabilities after exponentiating and normalizing (, which sum to ). The biggest logit (cat) becomes the biggest probability, and the orange note shows its loss . This bar-to-bar transform is the single step that produces the every other example on this page consumes.
Example 8 — Cell I: which log? nats vs bits
Forecast: Bits use , nats use . A bit is a "bigger" unit than a nat, so the nat number should be larger than .
- Why two units exist. Cross-entropy is , but which base? Base- gives bits (info-theory convention); base- gives nats (ML/optimizer convention, because and have clean derivatives). They measure the same thing in different rulers.
- Convert bits → nats. nats nats. So Why multiply by ? Because , so a nats-loss is times the bits-loss.
- Recover the probability. From nats, . Why ? We're now in natural-log land, so the inverse is the natural exponential.
- Cross-check via bits directly: . Same answer.
Verify: and agree. ✓ Always know whether your loss is in nats or bits before comparing to a paper — off-by- is a classic confusion.
Recall Quick self-test
A token gets probability ; its loss? :::
Response has 3 tokens with ; averaged loss? ::: (all equal, so the average is just that)
Loss drops from to — good or bad, and what happened to true-token probability? ::: Good; probability rose from to
Why does <eos> get mask = 1? ::: So the model learns when to stop — a genuine response skill
Confident-wrong () vs unsure-wrong (): which costs more? ::: Confident-wrong, vs nats
What happens when ? ::: The example has no response tokens — drop it before averaging, else divide-by-zero
See also: Cross-entropy loss, Autoregressive language modeling, Chat templates & special tokens, and the preference-tuning follow-ups RLHF / Direct Preference Optimization (DPO).