20/32
Part 5 of 6 · Prove it with a gradient checkLearn and do

Prove it with a gradient check

Nudge a weight both ways, and the loss gives its gradient

You can't eyeball a gradient, but you can measure one. Try it on v3v^3 at v=2v = 2, whose true slope is 12:

def cube(v: float) -> float:
    """Returns v cubed. Its slope at v is 3 v²."""
    return v ** 3

for eps in [1e-2, 1e-4, 1e-6]:
    central = (cube(2 + eps) - cube(2 - eps)) / (2 * eps)
    one_sided = (cube(2 + eps) - cube(2)) / eps
    print(f"{eps:.0e}   {abs(central - 12):.0e}   {abs(one_sided - 12):.0e}")
Predict

The slope of v³ at 2 is 12. With a nudge of 1e-4, how far off are the central estimate, which nudges both ways, and the one-sided one, which nudges only up?

Pick one to see what it prints.
Pick an answer above to see the rest