Data ScienceDeep learning and computer vision

Backpropagation with a small numerical gradient check

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 2 min read
Technically reviewed by Ishaan Sharma
In this article (5 sections)

Backpropagation applies the chain rule from a loss through each operation to the model parameters. A compact numerical gradient check can catch a wrong transpose, missing activation derivative or incorrect normalization before the same bug is hidden inside a large training run.

For parameter w, central finite differences approximate its derivative as:

[L(w + epsilon) - L(w - epsilon)] / (2 * epsilon).

This approximation is slow, so use it on a tiny deterministic network and a few parameters. Compare it with the analytic backpropagation gradient at float64 precision.

Executed two-layer check

The local lab uses two inputs, two tanh hidden units, one sigmoid output and binary cross-entropy. It checks all four first-layer weights with epsilon = 1e-6.

python
from deep_learning_cases import gradient_check_case

result = gradient_check_case()
assert result["epsilon"] == 1e-6
assert result["relative_error"] < 1e-7
print(f'{result["relative_error"]:.3e}')

The relative error is approximately 2.586e-10. One analytic derivative is -0.1144728155; its numerical counterpart is -0.1144728156 after rounding.

The relative error divides the norm of the difference by the combined gradient norms. An absolute threshold alone behaves poorly when gradients are naturally very small or large.

Make the check stable

Disable dropout and other randomness. Use a tiny batch and deterministic forward pass. Avoid points where ReLU is exactly nondifferentiable. Check parameters from several layers, including biases. Run in double precision because float32 rounding can dominate small perturbations.

Epsilon also matters. Too large measures curvature rather than a local derivative; too small suffers cancellation. Sweep several powers of ten if the check fails. Inspect individual parameters instead of accepting one aggregate silently.

Know what passed

This check supports the first-layer derivative in one small implementation. It does not validate data loading, loss reduction across distributed workers, optimizer updates or an entire neural framework. Add shape assertions, a one-batch overfit test, loss-decrease check and integration test for the complete training path.

A gradient can be mathematically correct while the model solves the wrong target or leaks validation data. Numerical agreement is necessary evidence for custom calculus, not evidence of business validity.

The Data Science course uses small numerical checks to connect matrix calculus with observable implementation behavior.

Exercise

Check the hidden bias and output weights. Then intentionally remove the tanh derivative and confirm the failure. Plot relative error across epsilon from 1e-2 to 1e-10.

Continue learning

This article is part of the Deep learning and computer vision sequence. Use the neighbouring tasks when you need the prerequisite or the next application.

Reference: scikit-learn’s neural-network mathematical formulation.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.

This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.

Explore Data Science
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.