The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← CS 230 — Mathematics, Statistics and Data for AI

Weeks 3–5

Derivatives, gradients and optimization steps

Learning objectives

  • Explain and trace the mechanisms in derivatives, gradients and optimization steps.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

A derivative measures local change. In optimization, the gradient collects partial derivatives with respect to model parameters. Subtracting a small positive multiple of the gradient moves locally downhill for a differentiable loss, but a step that is too large can increase loss or diverge. The learning rate has an operational meaning: it determines the size of the parameter update.

For squared loss on one example, L(w) = (wx − y)², the derivative is 2x(wx − y). The chain rule accounts for both the residual and how the prediction changes with w. Omitting x produces the wrong update except in special cases. A numerical finite-difference check compares the derivative with the change in loss under a tiny perturbation; very tiny steps can amplify rounding error.

Automatic differentiation applies differentiation rules to the actual computation graph. It does not determine whether the chosen loss corresponds to the business objective or whether data leaked from the future. Keep training state separate from evaluation. Record initial values and update order, because an accidental in-place modification can change a later calculation. Convergence of training loss is not evidence of good generalization.

Worked reasoning The initial prediction is 2 and residual −4, giving derivative −16. Subtracting 0.1 times −16 raises w to 2.6. The new prediction 5.2 has residual −0.8 and loss 0.64. A rate of 1 instead jumps to w = 17 and loss 784, illustrating that direction alone does not ensure a safe finite step.

Extended teaching and laboratory

For squared loss L(w)=(wx−y)², the chain rule gives dL/dw=2(wx−y)x. The sign points toward local increase; subtracting a positive multiple of it is a descent attempt. A finite learning rate can overshoot, so verify the resulting loss instead of assuming any step against the gradient must improve it.

Derivatives depend on the exact loss definition. Including a factor one-half removes the leading two. State the convention before comparing implementations; otherwise two correct derivations can appear inconsistent.

Worked problem Let x=3, y=12, w=2 and L=(wx−y)². Take a gradient step with learning rate .05, then compare a step with rate .2.

Step-by-step reasoning 1. Initially prediction is 6, residual −6, loss 36, and gradient 2×(−6)×3=−36.

  1. At rate .05, w becomes 3.8. Prediction is 11.4, residual −.6 and loss .36.
  1. At rate .2, w becomes 9.2. Prediction is 27.6, residual 15.6 and loss 243.36, worse than 36.
  1. The derivative supplied local direction, not a blanket guarantee for either finite step. Check the loss and document the update convention.

Apply the idea

Let x=3, y=12, w=2 and L=(wx−y)². Take a gradient step with learning rate .05, then compare a step with rate .2.

For L=.5(wx−y)² at the same starting point, calculate the gradient and a .05 update. Explain whether the changed result proves a software bug.

Read the worked reasoning

The gradient is (6−12)×3=−18. The update gives w=2.9, prediction 8.7 and loss .5×3.3²=5.445 under the new definition. The smaller update follows the changed objective scaling; it is not inherently a bug. Compare like loss conventions and adjust the learning rate deliberately.

Check understanding

What does a falling training loss prove?

  1. Improvement on that optimization objective, not unseen-data performance
    Feedback

    Optimization and generalization are different questions.

  2. Absence of data leakage
    Feedback

    Leakage can produce misleadingly good training and validation results.

  3. Correctness of every model assumption
    Feedback

    A wrong objective can also be minimized.

Further reading

Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study Dive into Deep Learning: Calculus and Automatic Differentiation; work chain-rule and gradient exercises before reading optimization chapters.

My learning →