Artificial Intelligence 🤖
Practical Aspects of Deep Learning
Gradient Checking

Gradient Checking

This is a method to check the accuracy of the gradients calculated during the backpropagation in a neural network. It uses numerical approximation to estimate the gradient of the cost function and compares it with the gradient computed by backpropagation. For example take f(a)=a3f (a) = a^3 and f′(a)=3f'(a) = 3. We can use the following formula to approximate the derivative:

f′(a)≈f(a+ϵ)−f(a−ϵ)2ϵf'(a) \approx \frac{f(a + \epsilon) - f(a - \epsilon)}{2\epsilon}

This gives us an approximate derivative, which should be close to our backpropagation result if we've done everything right. It's helpful for finding the errors in your backpropagation implementation. I'ts slower than gradient descent, so It's only really used for debugging. Implementation of this is very simple:

  1. First take all the parameters w[1],b[1],...,w[L],b[L]\mathbf{w}^{[1]}, \mathbf{b}^{[1]}, ..., \mathbf{w}^{[L]}, \mathbf{b}^{[L]} and reshape them into one vector θ\theta.
  2. The cost function will be J(θ)J(\theta)
  3. Then do the same for the derivatives dw[1],db[1],...,dw[L],db[L]d\mathbf{w}^{[1]}, d\mathbf{b}^{[1]}, ..., d\mathbf{w}^{[L]}, d\mathbf{b}^{[L]} into one big vector (dθd\theta)
  4. Now, for each element of θ\theta, tweak it a little by adding and subtracting a tiny value ϵ\epsilon, and calculate the cost function for each tweaked θ\theta.
  5. Use the tweaked cost functions to estimate the gradient like this:
eps = 1e-7  # a tiny number
for i in range(len(theta)):
    d_theta_approx[i] = (J(theta1, ..., theta[i] + eps) - J(theta1, ..., theta[i] - eps)) / (2 * eps)
  1. Compare the approximated gradient vector dθ_approxd\theta\_{approx} with the computed gradient vector dθd\theta from backpropagation using the below formula, where ∣∣x∣∣2|| x ||_2 denotes the Euclidean norm of vector xx.:
∣∣dθapprox−dθ∣∣2∣∣dθapprox∣∣2+∣∣dθ∣∣2\frac{||d\theta_{approx} - d\theta||_2}{||d\theta_{approx}||_2 + ||d\theta||_2}
  • <10−7< 10^{-7} : is excellent, meaning your backpropagation is likely correct.
  • 10−510^{-5}: is possibly okay, but be cautious and inspect further.
  • ≥10−3 \geq 10^{-3}: bad, indicates a likely error in backpropagation.

Important Notes on Gradient Checking

  • Gradient checking is a slow process, so don't use it during training—only for debugging.
  • When you find a discrepancy, investigate the components of your gradient vectors to locate potential errors.
  • Remember to include regularization terms in your cost function when performing gradient checking.
λ2m∑l=1L∥w[l]∥F2\frac{\lambda}{2m} \sum_{l=1}^{L} \lVert w^{[l]} \rVert_F^2
  • Gradient checking won't work with dropout since it introduces randomness in your cost function. You should turn off dropout (set keep_prob i.e. pp to 1.0) to check gradients.
  • Try gradient checking after some training, not just at the start. Some bugs only surface when the parameters are far from their initial values.

By using gradient checking, you ensure the reliability of the training process of your neural network, allowing you to trust that it's learning correctly.