Skip to main content

Review — GPT loss function

Back to the lesson · Learning path plan

What to remember

  • At each valid training position, compare the prediction with the observed next token. For a one-hot target the penalty is minus the log of its predicted probability.
  • Aggregate these penalties over valid positions in the batch, usually by averaging. Ignored positions contribute to neither the sum nor the denominator.
  • Loss is a scalar objective. Backpropagation computes its gradients, and the optimizer determines the update.
  • On a fixed batch, changing from mean to sum multiplies the gradient by the number of valid positions. Weighting tokens differently can change their relative contributions to the gradient.

Recall from memory

Three positions have penalties of 1, 3 and 5, but the third is ignored. What is the mean loss?

Show the answer

(1 + 3) ÷ 2 = 2. The ignored position does not count in the denominator.

Trace a token visible as input and ignored as a target, then recalculate the mean.

Language · Français · English