CS 230 — Mathematics, Statistics and Data for AI · Weeks 3–5
Derivatives, gradients and optimization steps
Full teaching sequence and cumulative subject work
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.
A derivative, a finite step and a loss comparison
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.
Trace the complete reasoning
Initially prediction is 6, residual −6, loss 36, and gradient 2×(−6)×3=−36.
At rate .05, w becomes 3.8. Prediction is 11.4, residual −.6 and loss .36.
At rate .2, w becomes 9.2. Prediction is 27.6, residual 15.6 and loss 243.36, worse than 36.
The derivative supplied local direction, not a blanket guarantee for either finite step. Check the loss and document the update convention.
Independent transfer problem
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.
Compare the transfer solution
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.
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.
Study the mechanism
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 example
x, y, w = 2.0, 6.0, 1.0
rate = 0.1
before = (w * x - y) ** 2
gradient = 2 * x * (w * x - y)
w = w - rate * gradient
after = (w * x - y) ** 2
assert round(before, 6) == 16.0
assert round(w, 6) == 2.6
assert round(after, 6) == 0.64The 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.
Check your understanding
What does a falling training loss prove?
- Improvement on that optimization objective, not unseen-data performance
- Absence of data leakage
- Correctness of every model assumption
Answer and reasoning for every option
Correct: Improvement on that optimization objective, not unseen-data performance Optimization and generalization are different questions.
Reconsider: Absence of data leakage Leakage can produce misleadingly good training and validation results.
Reconsider: Correctness of every model assumption A wrong objective can also be minimized.
Transfer laboratory: A finite-difference gradient check
For L(w)=(2w−6)² at w=1, calculate the derivative and a symmetric finite-difference approximation with h=0.001.
Work through the full solution
The derivative is 4(2w−6), giving −16. The symmetric expression [L(1.001)−L(0.999)]/0.002 also gives approximately −16; for this quadratic the symmetric difference is exact in exact arithmetic. Floating-point rounding can perturb the computed result, especially for excessively small h. Compare against a tolerance rather than exact equality.
Evidence of understanding
Derive −16, use the correct denominator 2h and explain numerical tolerance.
Complete on-site teaching readings
These are full attributed source sections, not previews. Read them alongside the original Academy explanation and code.
- 2.4. Calculus Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 2.5. Automatic Differentiation Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 22.3. Single Variable Calculus Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 22.4. Multivariable Calculus Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 22.5. Integral Calculus Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
Open the complete practice sequence and cumulative assessment →