Skip to main content

📝 Gradient Checking

Description​

< What is it? >​

Gradient checking is a debugging technique for verifying a hand-written backpropagation implementation. It compares the gradient from backpropagation with a numerical approximation computed from repeated forward passes.

For parameter vector θ\theta, approximate the iith gradient with the central-difference formula:

gapprox,i=J(θ+ϵei)−J(θ−ϵei)2ϵg_{\text{approx},i} = \frac{J(\theta+\epsilon e_i)-J(\theta-\epsilon e_i)}{2\epsilon}

Here, eie_i changes only parameter ii, ϵ\epsilon is a small number, and JJ is the loss. Compare the approximation with the backpropagation gradient gg using relative difference:

difference=∥g−gapprox∥2∥g∥2+∥gapprox∥2\text{difference} = \frac{\lVert g-g_{\text{approx}}\rVert_2} {\lVert g\rVert_2+\lVert g_{\text{approx}}\rVert_2}

Key points​

< How to use it >​

  1. Compute gradients with backpropagation.
  2. Flatten the model parameters and gradients into vectors.
  3. Perturb one parameter at a time by +ϵ+\epsilon and −ϵ-\epsilon, and run the forward pass for each.
  4. Compute gapproxg_{\text{approx}} and compare it with the backpropagation gradient.

A very small relative difference gives evidence that the implementation is correct. For a small float64 test, approximately 10−710^{-7} or lower is often reassuring, but the appropriate threshold depends on the loss scale, parameter scale, and ϵ\epsilon.

< When to use it >​

  • Use it while implementing or changing backpropagation, custom layers, or custom losses.
  • Use a small model and a small batch: checking every parameter needs two extra forward passes per parameter, so it is too slow for ordinary training.
  • Turn it off after debugging. Training should use backpropagation, not numerical gradients.

< Dropout and non-smooth operations >​

Gradient checking assumes that J(θ+ϵei)J(\theta+\epsilon e_i) and J(θ−ϵei)J(\theta-\epsilon e_i) evaluate the same deterministic function.

  • Disable dropout while checking, or otherwise ensure the same mask is used for both evaluations.
  • Be careful near ReLU's zero boundary and other non-smooth points, where a numerical derivative can disagree even when backpropagation is correct.

Implementation​

< Notebook >​

Reference​