07 / 15 · 60–90 min
Follow the gradient.
How can derivatives calibrate a sensor?
Check my intuition
01 · Predict
Before changing the settings.
The loss is L(w) = ½w², so its derivative is w. Starting at w = 2, what value follows one gradient step with α = 0.1 and no momentum?
A hint
w_new = w − α × gradient
02 · Experiment and compare
Put the model to the test.
Compare both curves at β = 0 and then 0.8. Increase the step until oscillation or divergence appears. Repeat with centred data: the problem conditioning changes.
Target: gain 1.4; bias −0.8. Estimate: (1.309 ; -0.603).
- Plain gradient loss
- 0.03
- Momentum loss
- 1.63E-3
- Calibrated gain
- 1.31
Read values as a table
| Iteration | Gain | Bias | Loss |
|---|---|---|---|
| 0.00 | 0.00 | 0.00 | 2.36 |
| 1.00 | 0.23 | 0.10 | 1.30 |
| 2.00 | 0.57 | 0.25 | 0.31 |
| 3.00 | 0.93 | 0.40 | 0.07 |
| 4.00 | 1.19 | 0.51 | 0.41 |
| 5.00 | 1.32 | 0.56 | 0.71 |
| 6.00 | 1.30 | 0.53 | 0.64 |
| 7.00 | 1.18 | 0.45 | 0.33 |
| 8.00 | 1.00 | 0.35 | 0.09 |
| 9.00 | 0.83 | 0.25 | 0.06 |
| 10.00 | 0.72 | 0.18 | 0.16 |
| 11.00 | 0.67 | 0.14 | 0.23 |
| 12.00 | 0.71 | 0.13 | 0.19 |
| 13.00 | 0.79 | 0.15 | 0.11 |
| 14.00 | 0.90 | 0.17 | 0.05 |
| 15.00 | 0.99 | 0.20 | 0.05 |
| 16.00 | 1.06 | 0.21 | 0.07 |
| 17.00 | 1.09 | 0.20 | 0.09 |
| 18.00 | 1.08 | 0.17 | 0.07 |
| 19.00 | 1.04 | 0.14 | 0.05 |
| 20.00 | 1.00 | 0.10 | 0.03 |
| 21.00 | 0.96 | 0.06 | 0.04 |
| 22.00 | 0.94 | 0.03 | 0.04 |
| 23.00 | 0.94 | 0.01 | 0.04 |
| 24.00 | 0.96 | 2.26E-3 | 0.04 |
| 25.00 | 0.99 | -2.08E-3 | 0.03 |
| 26.00 | 1.03 | -4.79E-3 | 0.03 |
| 27.00 | 1.06 | -9.43E-3 | 0.03 |
| 28.00 | 1.07 | -0.02 | 0.03 |
| 29.00 | 1.08 | -0.03 | 0.03 |
| 30.00 | 1.08 | -0.05 | 0.03 |
| 31.00 | 1.08 | -0.07 | 0.02 |
| 32.00 | 1.07 | -0.09 | 0.02 |
| 33.00 | 1.06 | -0.11 | 0.02 |
| 34.00 | 1.06 | -0.12 | 0.02 |
| 35.00 | 1.07 | -0.14 | 0.02 |
| 36.00 | 1.08 | -0.15 | 0.02 |
| 37.00 | 1.09 | -0.16 | 0.02 |
| 38.00 | 1.11 | -0.16 | 0.02 |
| 39.00 | 1.12 | -0.17 | 0.02 |
| 40.00 | 1.12 | -0.18 | 0.02 |
| 41.00 | 1.13 | -0.20 | 0.02 |
| 42.00 | 1.13 | -0.21 | 0.01 |
| 43.00 | 1.13 | -0.22 | 0.01 |
| 44.00 | 1.13 | -0.23 | 0.01 |
| 45.00 | 1.14 | -0.25 | 0.01 |
| 46.00 | 1.14 | -0.26 | 0.01 |
| 47.00 | 1.15 | -0.27 | 0.01 |
| 48.00 | 1.15 | -0.28 | 0.01 |
| 49.00 | 1.16 | -0.28 | 0.01 |
| 50.00 | 1.17 | -0.29 | 0.01 |
| 51.00 | 1.17 | -0.30 | 0.01 |
| 52.00 | 1.18 | -0.31 | 0.01 |
| 53.00 | 1.18 | -0.32 | 9.64E-3 |
| 54.00 | 1.18 | -0.33 | 9.28E-3 |
| 55.00 | 1.19 | -0.34 | 8.93E-3 |
| 56.00 | 1.19 | -0.35 | 8.60E-3 |
| 57.00 | 1.19 | -0.36 | 8.28E-3 |
| 58.00 | 1.20 | -0.37 | 7.97E-3 |
| 59.00 | 1.20 | -0.37 | 7.68E-3 |
| 60.00 | 1.20 | -0.38 | 7.39E-3 |
| 61.00 | 1.21 | -0.39 | 7.11E-3 |
| 62.00 | 1.21 | -0.40 | 6.85E-3 |
| 63.00 | 1.22 | -0.40 | 6.60E-3 |
| 64.00 | 1.22 | -0.41 | 6.35E-3 |
| 65.00 | 1.22 | -0.42 | 6.12E-3 |
| 66.00 | 1.23 | -0.43 | 5.89E-3 |
| 67.00 | 1.23 | -0.43 | 5.67E-3 |
| 68.00 | 1.23 | -0.44 | 5.46E-3 |
| 69.00 | 1.24 | -0.45 | 5.26E-3 |
| 70.00 | 1.24 | -0.45 | 5.06E-3 |
| 71.00 | 1.24 | -0.46 | 4.87E-3 |
| 72.00 | 1.24 | -0.47 | 4.69E-3 |
| 73.00 | 1.25 | -0.47 | 4.52E-3 |
| 74.00 | 1.25 | -0.48 | 4.35E-3 |
| 75.00 | 1.25 | -0.48 | 4.19E-3 |
| 76.00 | 1.26 | -0.49 | 4.03E-3 |
| 77.00 | 1.26 | -0.50 | 3.88E-3 |
| 78.00 | 1.26 | -0.50 | 3.74E-3 |
| 79.00 | 1.26 | -0.51 | 3.60E-3 |
| 80.00 | 1.27 | -0.51 | 3.47E-3 |
| 81.00 | 1.27 | -0.52 | 3.34E-3 |
| 82.00 | 1.27 | -0.52 | 3.21E-3 |
| 83.00 | 1.27 | -0.53 | 3.10E-3 |
| 84.00 | 1.28 | -0.53 | 2.98E-3 |
| 85.00 | 1.28 | -0.54 | 2.87E-3 |
| 86.00 | 1.28 | -0.54 | 2.76E-3 |
| 87.00 | 1.28 | -0.55 | 2.66E-3 |
| 88.00 | 1.29 | -0.55 | 2.56E-3 |
| 89.00 | 1.29 | -0.56 | 2.47E-3 |
| 90.00 | 1.29 | -0.56 | 2.38E-3 |
| 91.00 | 1.29 | -0.57 | 2.29E-3 |
| 92.00 | 1.29 | -0.57 | 2.20E-3 |
| 93.00 | 1.30 | -0.58 | 2.12E-3 |
| 94.00 | 1.30 | -0.58 | 2.04E-3 |
| 95.00 | 1.30 | -0.58 | 1.97E-3 |
| 96.00 | 1.30 | -0.59 | 1.89E-3 |
| 97.00 | 1.30 | -0.59 | 1.82E-3 |
| 98.00 | 1.30 | -0.60 | 1.76E-3 |
| 99.00 | 1.31 | -0.60 | 1.69E-3 |
| 100.00 | 1.31 | -0.60 | 1.63E-3 |
Understand the calculations
03 · Connect the diagram to calculations
The numbers behind motion.
We calibrate ẑ = ax + b on 21 noiseless measurements generated by a = 1.4 and b = −0.8. The chain rule connects loss to each parameter: error → prediction → gain or bias. Momentum accumulates gradients. Both optimizers start at (0, 0) and see the same data. The plot shows log₁₀ loss with a display floor of 10⁻⁹.
L = mean((a x + b − z)²) / 2
∂L/∂a = mean(error × x) ; ∂L/∂b = mean(error)
vₖ₊₁ = βvₖ + ∇L(wₖ)
wₖ₊₁ = wₖ − αvₖ₊₁A few lines to reconstruct
gradient = [mean(error * x), mean(error)]
velocity = beta * velocity + gradient
weights = weights - rate * velocityThe notebook provides one function per laboratory, hand-calculated examples and tests. Exercises isolate components; these pages run the complete simulations.
Keep an observation
04 · Record
What changed?
Compare two settings. Record the effect, your explanation and a case where the model might fail. Saving keeps the current settings and seed, not a certification of competence.
atomsh / lab