Skip to main content

Exercises — Gradients and backpropagation

Self-check questions, exercises, and worked answers

← Back to the lesson: Gradients and backpropagation

Check your understanding

For additional practice outside the lesson, see the exercise space — mathematical foundations.

  1. Why does the cross-entropy derivative with respect to logits simplify to \(p_i-\mathbb{1}[i=y]\)?
Show answer

For a one-hot target, differentiating log-softmax combines the normalization term \(p_i\) with the target-class term \(-\mathbb{1}[i=y]\). Their difference is the gradient with respect to each logit.

  1. What can disagreement between an analytical gradient and a finite difference indicate?
Show answer

It may indicate a derivation or implementation bug, but also a poorly chosen \(\epsilon\), rounding error, nondeterministic behavior, or mismatched conventions. Isolate the case and check tolerances.

  1. Why might a gradient-descent update fail to reduce the loss if the step is too large?
Show answer

The gradient describes a local change. A step that is too large can leave the region where that approximation is useful, overshoot a minimum, or increase the objective; the learning rate must be controlled.

How do updates compare at equal loss?

Keep the prefix and the observed token \(A\) fixed. In this toy model, the logits are \(z=u+b\), where \(u\) is fixed and only the output bias \(b\) is trainable. Two settings of \(b\) give the following distributions over \((A,B,C)\).

\[ p=(1/2,\;1/4,\;1/4),\qquad q=(1/2,\;3/8,\;1/8). \]

  1. Calculate the log-likelihood of \(A\) and the loss for this target in both cases. This is the only target included.
  2. With the one-hot target \(y=(1,0,0)\), calculate the gradient of the loss with respect to the bias. The addition \(z=u+b\) passes each logit derivative to the corresponding bias.
  3. Calculate the change in bias for a gradient-descent step with learning rate \(\eta=1/5\). Does the score of \(A\) determine how the step treats \(B\) and \(C\)?
  4. Make another prediction on the same prefix under two conditions. First stop after computing the gradient, before the optimizer step. Then apply the step and recompute \(p(A)\) and \(q(A)\). For the second calculation, you can use \(\ln p\) and \(\ln q\) as the initial logits up to a shared additive constant.
Show the worked answer
  1. In both cases, the log-likelihood is \(\ln(1/2)=-\ln 2\) and the loss is \(\ln 2\).
  2. The gradient is the predicted distribution minus \(y\). This gives \((-1/2,\,1/4,\,1/4)\) for \(p\) and \((-1/2,\,3/8,\,1/8)\) for \(q\).
  3. The change is \(-\eta\nabla_b L\). It is \((1/10,\,-1/20,\,-1/20)\) for \(p\) and \((1/10,\,-3/40,\,-1/40)\) for \(q\). The bias of \(A\) increases by the same amount. For \(p\), the biases of \(B\) and \(C\) decrease equally. For \(q\), the bias of \(B\) decreases three times as much as that of \(C\). The target probability determines the scalar loss. Each bias update depends on the probability of its corresponding token.
  4. Computing the gradient alone leaves the biases fixed, so both probabilities of \(A\) stay at \(1/2\). After the step, normalize again. For an initial distribution \(r\), the new probability of \(A\) is \(r_A e^{\Delta b_A}/\sum_j r_j e^{\Delta b_j}\). It is about \(0.537\) starting from \(p\) and \(0.540\) starting from \(q\). The numerator is identical in both cases. In the denominator, each term combines a token’s initial probability and its bias change.

Return to the review plan · Score observed tokens · Learn and update parameters

Return to: Overview · Loss

Language: Français · English