Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

Part 02 / Topic 7 of 7

Checking maths with code

The habit that separates working ML code from wishful thinking: check analytic maths against a numerical estimate. You saw it in section 4 — a hand-derived gradient tested with (f(w+h) − f(w−h)) / 2h.

The same trick scales: write a gradient checker once, run it against any derivative you implement. Part 9 uses it to validate a from-scratch neural network, and PyTorch's autograd (Part 10) is trusted because it passes exactly these checks.

def numerical_grad(f, w, h=1e-6):
    return (f(w + h) - f(w - h)) / (2 * h)

If the analytic and numerical numbers disagree, the maths (or the code) is wrong — no debate needed.

Done here? The practice set for Part 2 — 4 exercises with solutions — is on the part page.

Search the guide