Exercises — Loss: objective and masking
Self-check questions, exercises, and worked answers
← Back to the lesson: Loss: objective and masking
Check your understanding
For additional practice outside the lesson, see the exercise space — mathematical foundations.
To review the shapes of scores, targets, and batches, see Tensors.
- Why should the denominator count valid targets rather than all tokens when some are ignored?
Show answer
Otherwise ignored positions, such as padding, would artificially change the average. The denominator should match the positions actually included in the objective.
- Why does lower training loss not prove better generalization?
Show answer
Training loss measures fit to the optimized data. It can decrease while the model overfits, exploits leakage, or remains poorly evaluated; use independent validation data and a separate test set for final assessment.
- When is perplexity comparable across runs?
Show answer
At minimum, use compatible logarithm and reduction conventions, tokenizer, ignored-target handling, and evaluation corpora/context settings. Otherwise the values do not measure exactly the same quantity.
Related connection to observed-sequence probability: Likelihood.
A token visible as input and ignored as a target
A causal GPT receives the inputs \((A,B,C)\) with fixed parameters. The reference sequence is \((A,B,C,D)\), so the shifted targets are \((B,C,D)\). We want to train the model on the answer \((C,D)\) using the prefix \((A,B)\). The illustrative probabilities are for the target of each row.
| Position | Input | Target | Probability | Included? |
|---|---|---|---|---|
| 1 | \(A\) | \(B\) | \(1/8\) | No |
| 2 | \(B\) | \(C\) | \(1/2\) | Yes |
| 3 | \(C\) | \(D\) | \(1/4\) | Yes |
- Can the query in row 2 use the key for \(B\) even though \(B\) was ignored as a target in row 1? Can it use the key for \(C\)?
- Compute the mean over the included targets. What would the mean be if you included target \(B\) with the same inputs and the same causal mask?
- Replace \(C\) with \(C'\) in the reference sequence and input while keeping the loss mask fixed. Why does the prediction distribution at row 2 stay the same while its contribution to the loss may change?
Show the worked answer
- At row 2, input positions 1 and 2 are accessible. This makes \(B\) visible, including at its own position. Position 3 is in the query’s future, so the key for \(C\) is blocked. Target \(B\) in row 1 and input \(B\) in row 2 play different roles.
- The two included targets give a sum of \(\ln 2+\ln 4=3\ln 2\) and a mean of \(\tfrac32\ln 2\approx 1.040\) nats per token. Including target \(B\) adds \(\ln 8\) to the sum and raises the denominator to three. The mean becomes \(2\ln 2\approx 1.386\) nats per token. The distributions stay the same because only the selection of loss terms has changed.
- With fixed parameters, row 2 still conditions on the same prefix \((A,B)\). The causal mask blocks the change to the input at future position 3 for this row. Scoring now reads the probability of \(C'\) from the same distribution. That probability may differ from the probability of \(C\).
Return to the review plan · Understand Transformer computation · Learn and update parameters
Compare checkpoints by validation loss
Two checkpoints score the same held-out tokens, with the same tokenizer and mask. The table gives the sum of negative log probabilities (NLL, in nats) over the valid targets in each batch.
| Batch | Valid targets | Total NLL, X | Total NLL, Y |
|---|---|---|---|
| 1 | 2 | 6 | 2 |
| 2 | 8 | 4 | 12 |
- Compute each checkpoint’s mean NLL per valid token across both batches.
- Average the two batch means with equal weight. How does the ranking change, and which calculation answers question 1?
Show worked answer
- Mean per valid token. X scores \((6+4)/10 = 1.0\) and Y scores \((2+12)/10 = 1.4\). X has lower validation loss per token.
- Mean of batch means. X scores \((3+0.5)/2 = 1.75\) and Y scores \((1+1.5)/2 = 1.25\). This calculation ranks Y first because it gives the two-token batch the same weight as the eight-token batch.
Sum the NLLs and valid-target counts separately, then divide. This gives every valid target the same weight, regardless of how the fixed predictions and targets are grouped into batches.