Loading lesson…
Courses›The Price of a Nudge

The Price of a Nudge

DetailedOverviewQuizFlashcard

The Price of a Nudge

The Price of a Nudge

Scenario

Amber's test kiln has one knob this afternoon: temperature offset tt, in a unit where t=0t=0 is the scheduled cone and t>0t>0 is hotter. The energy the studio pays — fuel plus the risk of a bloated glaze — is

e(t)=t3−3t+0.5.e(t) = t^3 - 3t + 0.5.

They are sitting at t=0.5t = 0.5. The question is not "what is a derivative?" The question is: if we nudge the knob a tiny bit hotter, how much extra energy do we pay, per unit nudge?

That price is the whole of gradient descent in one dimension. Lesson 2 will call the same price a slope and draw it as an arrow that points uphill. Lesson 3 will walk the other way by a step η\eta times that price. Autodiff exists to compute this price when there are 101110^{11} knobs. If the 1D price is fuzzy, every later picture is theater.

A few answers look almost right and are wrong:

  • −0.875-0.875 — you reported e(0.5)e(0.5). That is the height of the hill, not the slope. You can stand high with a flat slope, or low with a steep one.
  • −2-2 — you formed a coarse chord from 00 to 11: e(1)−e(0)1=(1−3+0.5)−0.51=−2\frac{e(1)-e(0)}{1} = \frac{(1-3+0.5)-0.5}{1} = -2. That is a whole cone away. It is a cousin of the price, not the price at 0.50.5.
  • 0.750.75 — you took the slope as 3t23t^2 and dropped the linear term −3t-3t. At t=0.5t=0.5, 3(0.25)=0.753(0.25) = 0.75. But the linear term is doing four times as much work as the cubic term here.

Keep every term. The exact price of a nudge at 0.50.5 is −2.25-2.25.

Height −0.875 is not the price −2.25. A downhill step lands at 0.6125.

Height is not slope

Let us evaluate the kiln's baseline state:

e(0.5)=(0.5)3−3(0.5)+0.5=0.125−1.5+0.5=−0.875.e(0.5) = (0.5)^3 - 3(0.5) + 0.5 = 0.125 - 1.5 + 0.5 = -0.875.

The kiln is currently below its nominal scheduled energy of 0.50.5 (at t=0t=0). That number is a height. It tells you where you stand on the hill, but it does not tell you which way the ground tilts under your boots. You can stand high on a wide plateau where nudging changes nothing, or you can stand at sea level on the edge of a sheer cliff.

Now define the price as a limit, so that "tiny nudge" is a mathematical contract rather than a loose vibe:

e′(t)=lim⁡h→0e(t+h)−e(t)h.e'(t) = \lim_{h \to 0} \frac{e(t+h) - e(t)}{h}.

The numerator is extra energy. The denominator is the nudge. The ratio is extra energy per unit nudge.

Deriving the slope: expand the cubic

Do not simply recite the power rule as a magic spell. Look at what happens to the cubic term under a nudge hh:

(t+h)3=t3+3t2h+3th2+h3.(t+h)^3 = t^3 + 3t^2 h + 3t h^2 + h^3.

Now assemble the nudged energy e(t+h)e(t+h):

e(t+h)=(t3+3t2h+3th2+h3)−3(t+h)+0.5.e(t+h) = \left(t^3 + 3t^2 h + 3t h^2 + h^3\right) - 3(t+h) + 0.5.

Subtract the baseline energy e(t)=t3−3t+0.5e(t) = t^3 - 3t + 0.5:

e(t+h)−e(t)=3t2h+3th2+h3−3h.e(t+h) - e(t) = 3t^2 h + 3t h^2 + h^3 - 3h.

Divide by the nudge hh:

e(t+h)−e(t)h=3t2−3+3th+h2.\frac{e(t+h) - e(t)}{h} = 3t^2 - 3 + 3t h + h^2.

As the nudge hh shrinks to zero, the terms 3th3th and h2h^2 vanish completely, leaving the exact instantaneous derivative:

e′(t)=3t2−3.e'(t) = 3t^2 - 3.

At Amber's operating point t=0.5t = 0.5:

e′(0.5)=3(0.5)2−3=3(0.25)−3=0.75−3=−2.25.e'(0.5) = 3(0.5)^2 - 3 = 3(0.25) - 3 = 0.75 - 3 = -2.25.

The negative sign has an immediate physical meaning: a hotter nudge lowers energy. Uphill is to the left (colder). The knob at 0.50.5 wants to turn hotter if you want to walk downhill.

At the stationary points where the price is zero:

e′(t)=0  ⟹  3t2−3=0  ⟹  t2=1  ⟹  t=±1.e'(t) = 0 \implies 3t^2 - 3 = 0 \implies t^2 = 1 \implies t = \pm 1.

The second derivative is e′′(t)=6te''(t) = 6t. At t=1t = 1, e′′(1)=6>0e''(1) = 6 > 0, so t=1t = 1 is a local minimum (e(1)=−1.5e(1) = -1.5). At t=−1t = -1, e′′(−1)=−6<0e''(-1) = -6 < 0, so t=−1t = -1 is a local maximum (e(−1)=2.5e(-1) = 2.5).

[!IMPORTANT] Zero price means a stationary point, not automatically a bottom. Later in Lesson 4 and Lesson 13 we will encounter two-well landscapes and saddle points where the slope is zero yet you are standing on a ridge.


A chord is a cousin, not a twin

Suppose you do not have an analytical formula for e(t)e(t). You only have a black-box kiln that you can query at two temperatures. You choose a nudge width h=0.5h = 0.5 and compute the two-sided difference quotient:

e(t+h)−e(t−h)2h.\frac{e(t+h) - e(t-h)}{2h}.

Two-sided differences are vastly superior to one-sided differences. Why? Because the Taylor expansions on both sides cancel the leading error term:

e(t+h)=e(t)+e′(t)h+12e′′(t)h2+16e′′′(t)h3+…e(t + h) = e(t) + e'(t)h + \frac{1}{2}e''(t)h^2 + \frac{1}{6}e'''(t)h^3 + \dots e(t−h)=e(t)−e′(t)h+12e′′(t)h2−16e′′′(t)h3+…e(t - h) = e(t) - e'(t)h + \frac{1}{2}e''(t)h^2 - \frac{1}{6}e'''(t)h^3 + \dots

Subtract the two equations: the baseline e(t)e(t) cancels, and crucially, the quadratic curvature term 12e′′(t)h2\frac{1}{2}e''(t)h^2 cancels as well:

e(t+h)−e(t−h)=2e′(t)h+13e′′′(t)h3.e(t+h) - e(t-h) = 2e'(t)h + \frac{1}{3}e'''(t)h^3.

Divide by 2h2h:

e(t+h)−e(t−h)2h=e′(t)+16e′′′(t)h2.\frac{e(t+h) - e(t-h)}{2h} = e'(t) + \frac{1}{6}e'''(t)h^2.

For Amber's cubic, the third derivative is constant: e′′′(t)=6e'''(t) = 6. Therefore 16(6)h2=h2\frac{1}{6}(6)h^2 = h^2, giving an exact identity:

e(t+h)−e(t−h)2h=e′(t)+h2.\frac{e(t+h) - e(t-h)}{2h} = e'(t) + h^2.

The error is not "approximately" h2h^2; for any cubic with leading coefficient 1, it is identically h2h^2!

In contrast, a one-sided chord retains the 12e′′(t)h\frac{1}{2}e''(t)h term. For Amber's kiln at t=0.5t=0.5, e′′(0.5)=6(0.5)=3e''(0.5) = 6(0.5) = 3, so its error is 1.5h+h21.5h + h^2.

The Home Numbers Table

Here is the exact comparison of prices and chords around t=0.5t = 0.5:

QuantityFormulaNumerical ValueError vs e′(0.5)=−2.25e'(0.5) = -2.25
Hill Heighte(0.5)e(0.5)−0.875-0.875N/A (different physical unit)
Instantaneous Pricee′(0.5)e'(0.5)−2.25-2.250.000.00 (exact)
Two-sided chord (h=0.5h = 0.5)e(1.0)−e(0.0)1.0\frac{e(1.0) - e(0.0)}{1.0}−2.00-2.00+0.25=h2+0.25 = h^2
Two-sided chord (h=0.1h = 0.1)e(0.6)−e(0.4)0.2\frac{e(0.6) - e(0.4)}{0.2}−2.24-2.24+0.01=h2+0.01 = h^2
Two-sided chord (h=0.01h = 0.01)e(0.51)−e(0.49)0.02\frac{e(0.51) - e(0.49)}{0.02}−2.2499-2.2499+0.0001=h2+0.0001 = h^2
One-sided chord (h=0.5h = 0.5)e(1.0)−e(0.5)0.5\frac{e(1.0) - e(0.5)}{0.5}−1.25-1.25+1.00=1.5h+h2+1.00 = 1.5h + h^2
One-sided chord (h=0.1h = 0.1)e(0.6)−e(0.5)0.1\frac{e(0.6) - e(0.5)}{0.1}−2.09-2.09+0.16=1.5h+h2+0.16 = 1.5h + h^2

The Price of a Nudge: tangent versus chord


One timid downhill hop

How do we use this price to train the knob? Gradient descent updates the parameter by subtracting the slope scaled by a small step size η\eta:

t←t−η e′(t).t \leftarrow t - \eta\, e'(t).

Amber, being cautious with the studio kiln, picks η=0.05\eta = 0.05 from t=0.5t = 0.5:

t′=0.5−0.05(−2.25)=0.5+0.1125=0.6125.t' = 0.5 - 0.05(-2.25) = 0.5 + 0.1125 = 0.6125.

Notice how the two minus signs interact:

  1. The formula has a minus sign because we want to go downhill (opposite the gradient).
  2. The slope e′(0.5)e'(0.5) is −2.25-2.25 (negative).
  3. The product −ηe′(t)- \eta e'(t) is positive (+0.1125+0.1125), moving the knob rightward to t′=0.6125t' = 0.6125.

Evaluating the new energy:

e(0.6125)=(0.6125)3−3(0.6125)+0.5≈0.2298−1.8375+0.5=−1.1077.e(0.6125) = (0.6125)^3 - 3(0.6125) + 0.5 \approx 0.2298 - 1.8375 + 0.5 = -1.1077.

The energy fell from −0.875-0.875 to −1.1077-1.1077. The hop succeeded.

The Wrong-Sign Disaster

What happens if you accidentally add the slope (t←t+ηe′(t)t \leftarrow t + \eta e'(t))?

twrong=0.5+0.05(−2.25)=0.5−0.1125=0.3875.t_{\text{wrong}} = 0.5 + 0.05(-2.25) = 0.5 - 0.1125 = 0.3875.

Evaluating energy:

e(0.3875)=(0.3875)3−3(0.3875)+0.5≈−0.6043.e(0.3875) = (0.3875)^3 - 3(0.3875) + 0.5 \approx -0.6043.

Energy rose by +0.2707+0.2707 toward the local maximum at t=−1t = -1. You walked uphill. In a production neural network, dropping that minus sign causes the loss to explode immediately.

[!NOTE] How far to hop is Lesson 3's business. In Lesson 3 we will study the learning rate η\eta in depth, and in Lesson 5 we will meet the Lipschitz constant LL that sets its ceiling. Here, we keep η=0.05\eta = 0.05 fixed solely to verify the direction of descent.


Interactive Workbench: Chord vs Tangent

Explore the relationship between the chord span hh, chord symmetry, and descent hops in the interactive laboratory below:

OPT-P2-NUDGE

Now examine the physical geometry on the 3D concept rig:

OPT-P2-ANATOMY


Try it: Transfer example

Work this problem on scratch paper before revealing the answer:

Consider the quadratic energy:

g(t)=t2−4tatt=1.g(t) = t^2 - 4t \quad \text{at} \quad t = 1.
  1. Find the exact derivative g′(1)g'(1).
  2. Compute the two-sided chord with h=0.5h = 0.5.
  3. Compute the one-sided chord with h=0.5h = 0.5.
  4. Why is the two-sided chord exact here?
Reveal solution
  1. Exact derivative: g′(t)=2t−4  ⟹  g′(1)=2(1)−4=−2.g'(t) = 2t - 4 \implies g'(1) = 2(1) - 4 = -2.
  2. Two-sided chord (h=0.5h = 0.5): Evaluate at 1+0.5=1.51 + 0.5 = 1.5 and 1−0.5=0.51 - 0.5 = 0.5: g(1.5)=(1.5)2−4(1.5)=2.25−6=−3.75.g(1.5) = (1.5)^2 - 4(1.5) = 2.25 - 6 = -3.75. g(0.5)=(0.5)2−4(0.5)=0.25−2=−1.75.g(0.5) = (0.5)^2 - 4(0.5) = 0.25 - 2 = -1.75. g(1.5)−g(0.5)2(0.5)=−3.75−(−1.75)1.0=−2.01.0=−2.\frac{g(1.5) - g(0.5)}{2(0.5)} = \frac{-3.75 - (-1.75)}{1.0} = \frac{-2.0}{1.0} = -2. The two-sided chord gives −2-2 exactly!
  3. One-sided chord (h=0.5h = 0.5): g(1)=12−4(1)=−3.g(1) = 1^2 - 4(1) = -3. g(1.5)−g(1)0.5=−3.75−(−3)0.5=−0.750.5=−1.5.\frac{g(1.5) - g(1)}{0.5} = \frac{-3.75 - (-3)}{0.5} = \frac{-0.75}{0.5} = -1.5.
  4. Why is the two-sided chord exact? Recall our Taylor error term: 16g′′′(t)h2\frac{1}{6}g'''(t)h^2. For any quadratic function, g′′(t)=2g''(t) = 2 and g′′′(t)=0g'''(t) = 0. The third derivative vanishes everywhere! Because there is no cubic or higher-order curvature, the Taylor series terminates, leaving zero chord error.

Runnable code verification

Run this in Python to confirm the calculations and observe machine precision limits:

e  = lambda t: t**3 - 3*t + 0.5
de = lambda t: 3*t**2 - 3

print(e(0.5), de(0.5))                       # -0.875 -2.25

# Two-sided chord error shrinks as h**2
for h in (0.5, 0.1, 0.01):
    two = (e(0.5+h) - e(0.5-h)) / (2*h)
    print(f"h={h:<4} chord={two:.6f} error={round(two - de(0.5), 6)} (= h**2)")

# Downhill hop
print("hop landing:", 0.5 - 0.05 * de(0.5))  # 0.6125

# The floor of finite differences: catastrophic cancellation
h_micro = 1e-13
two_micro = (e(0.5 + h_micro) - e(0.5 - h_micro)) / (2 * h_micro)
print(f"h=1e-13: error = {abs(two_micro - de(0.5)):.6e}")  # ~4.4e-4 (worse than h=0.01!)

The Floor of Numerical Differentiation

Why does h=10−13h = 10^{-13} yield an error around 4×10−44 \times 10^{-4}, which is hundreds of times worse than h=0.01h = 0.01?

At h=10−13h = 10^{-13}, e(0.5+h)e(0.5 + h) and e(0.5−h)e(0.5 - h) agree in their first 13 decimal digits. When you subtract two numbers that agree to 13 digits in standard 64-bit IEEE floating-point arithmetic (which has roughly 15 to 17 digits of mantissa), the leading digits cancel completely. The remaining difference is dominated by round-off noise. Dividing that noise by 2h=2×10−132h = 2 \times 10^{-13} magnifies the error enormously.

Tiny is a scale, not a personality trait. Finite differences check analytic derivatives at scales like h=10−5h = 10^{-5}; pushing hh to machine epsilon causes catastrophic cancellation.


Failure modes this chapter names

  • Reporting height as slope: Confusing where you stand (−0.875-0.875) with the slope of the ground (−2.25-2.25).
  • Dropping a term of e′(t)e'(t): Taking only 3t23t^2 (0.750.75) and dropping −3-3.
  • Treating a coarse chord as the derivative: Assuming h=0.5h=0.5 gives the exact slope (−2.00-2.00 instead of −2.25-2.25).
  • Stepping +ηe′+ \eta e' and calling it descent: Walking uphill toward the local maximum.
  • Calling a one-sided chord two-sided: Forgetting that one-sided chords carry an O(h)O(h) error that is much larger than h2h^2.

Why autodiff is this price, not a second calculus

When the knob is a weight wjw_j in a neural network, ee is the training loss, and ∂e∂wj\frac{\partial e}{\partial w_j} is "how much extra loss we pay if we nudge wjw_j."

Reverse-mode automatic differentiation (backprop) computes every such 1D price in a single backward sweep. It does not invent a brand-new form of calculus. It is the chain rule, scheduled efficiently (which we will formalize in Chapter P5).

The desk problem asks for a two-sided chord with a whole-number hh so you can do it by hand. Expect it to differ from the exact slope by h2h^2. The desk problem also does a hop like this on a different cubic. Work that problem on paper and type the integers into the desk quiz.

Next: P3 The Uphill List — what happens when the hill has more than one knob.