# Gradient checking and advanced optimization

### From Ufldl

(Created page with "Backpropagation is a notoriously difficult algorithm to debug and get right, especially since many subtly buggy implementations of it---for example, one that has an off-by-one er...") |
m (Fixed quotation marks) |
||

Line 34: | Line 34: | ||

In practice, we set {\rm EPSILON} to a small constant, say around <math>\textstyle 10^{-4}</math>. | In practice, we set {\rm EPSILON} to a small constant, say around <math>\textstyle 10^{-4}</math>. | ||

(There's a large range of values of {\rm EPSILON} that should work well, but | (There's a large range of values of {\rm EPSILON} that should work well, but | ||

- | we don't set {\rm EPSILON} to be | + | we don't set {\rm EPSILON} to be "extremely" small, say <math>\textstyle 10^{-20}</math>, |

as that would lead to numerical roundoff errors.) | as that would lead to numerical roundoff errors.) | ||

Line 51: | Line 51: | ||

Now, consider the case where <math>\textstyle \theta \in \Re^n</math> is a vector rather than a single real | Now, consider the case where <math>\textstyle \theta \in \Re^n</math> is a vector rather than a single real | ||

number (so that we have <math>\textstyle n</math> parameters that we want to learn), and <math>\textstyle J: \Re^n \mapsto \Re</math>. In | number (so that we have <math>\textstyle n</math> parameters that we want to learn), and <math>\textstyle J: \Re^n \mapsto \Re</math>. In | ||

- | our neural network example we used | + | our neural network example we used "<math>\textstyle J(W,b)</math>," but one can imagine "unrolling" |

the parameters <math>\textstyle W,b</math> into a long vector <math>\textstyle \theta</math>. We now generalize our derivative | the parameters <math>\textstyle W,b</math> into a long vector <math>\textstyle \theta</math>. We now generalize our derivative | ||

checking procedure to the case where <math>\textstyle \theta</math> may be a vector. | checking procedure to the case where <math>\textstyle \theta</math> may be a vector. | ||

Line 65: | Line 65: | ||

\end{align}</math> | \end{align}</math> | ||

is the <math>\textstyle i</math>-th basis vector (a | is the <math>\textstyle i</math>-th basis vector (a | ||

- | vector of the same dimension as <math>\textstyle \theta</math>, with a | + | vector of the same dimension as <math>\textstyle \theta</math>, with a "1" in the <math>\textstyle i</math>-th position |

- | and | + | and "0"s everywhere else). So, |

<math>\textstyle \theta^{(i+)}</math> is the same as <math>\textstyle \theta</math>, except its <math>\textstyle i</math>-th element has been incremented | <math>\textstyle \theta^{(i+)}</math> is the same as <math>\textstyle \theta</math>, except its <math>\textstyle i</math>-th element has been incremented | ||

by {\rm EPSILON}. Similarly, let <math>\textstyle \theta^{(i-)} = \theta - {\rm EPSILON} \times \vec{e}_i</math> be the | by {\rm EPSILON}. Similarly, let <math>\textstyle \theta^{(i-)} = \theta - {\rm EPSILON} \times \vec{e}_i</math> be the |

## Revision as of 01:20, 22 April 2011

Backpropagation is a notoriously difficult algorithm to debug and get right, especially since many subtly buggy implementations of it---for example, one that has an off-by-one error in the indices and that thus only trains some of the layers of weights, or an implementation that omits the bias term---will manage to learn something that can look surprisingly reasonable (while performing less well than a correct implementation). Thus, even with a buggy implementation, it may not at all be apparent that anything is amiss. In this section, we describe a method for numerically checking the derivatives computed by your code to make sure that your implementation is correct. Carrying out the derivative checking procedure described here will significantly increase your confidence in the correctness of your code.

Suppose we want to minimize as a function of . For this example, suppose , so that . In this 1-dimensional case, one iteration of gradient descent is given by

Suppose also that we have implemented some function that purportedly computes , so that we implement gradient descent using the update . How can we check if our implementation of is correct?

Recall the mathematical definition of the derivative as

Thus, at any specific value of , we can numerically approximate the derivative as follows:

In practice, we set {\rm EPSILON} to a small constant, say around . (There's a large range of values of {\rm EPSILON} that should work well, but we don't set {\rm EPSILON} to be "extremely" small, say , as that would lead to numerical roundoff errors.)

Thus, given a function that is supposedly computing , we can now numerically verify its correctness by checking that

The degree to which these two values should approximate each other will depend on the details of . But assuming , you'll usually find that the left- and right-hand sides of the above will agree to at least 4 significant digits (and often many more).

Now, consider the case where is a vector rather than a single real number (so that we have parameters that we want to learn), and . In our neural network example we used "," but one can imagine "unrolling" the parameters into a long vector . We now generalize our derivative checking procedure to the case where may be a vector.

Suppose we have a function that purportedly computes ; we'd like to check if is outputting correct derivative values. Let , where

is the -th basis vector (a vector of the same dimension as , with a "1" in the -th position and "0"s everywhere else). So, is the same as , except its -th element has been incremented by {\rm EPSILON}. Similarly, let be the corresponding vector with the -th element decreased by {\rm EPSILON}. We can now numerically verify 's correctness by checking, for each , that:

When implementing backpropagation to train a neural network, in a correct implementation
we will have that

This result shows that the final block of psuedo-code in Section~\ref{sec-backprop} is indeed implementing gradient descent. To make sure your implementation of gradient descent is correct, it is usually very helpful to use the method described above to numerically compute the derivatives of , and thereby verify that your computations of and are indeed giving the derivatives you want.

Finally, so far our discussion has centered on using gradient descent to minimize . If you have
implemented a function that computes and , it turns out there are more
sophisticated algorithms than gradient descent for trying to minimize . For example, one can envision
an algorithm that uses gradient descent, but automatically tunes the learning rate so as to try to
use a step-size that causes to approach a local optimum as quickly as possible.
There are other algorithms that are even more
sophisticated than this; for example, there are algorithms that try to find an approximation to the
Hessian matrix, so that it can take more rapid steps towards a local optimum (similar to Newton's method). A full discussion of these
algorithms is beyond the scope of these notes, but one example is
the **L-BFGS** algorithm. (Another example is **conjugate gradient**.) You will use one of
these algorithms in the programming exercise.
The main thing you need to provide to these advanced optimization algorithms is that for any , you have to be able
to compute and . These optimization algorithms will then do their own
internal tuning of the learning rate/step-size (and compute its own approximation to the Hessian, etc.)
to automatically search for a value of that minimizes . Algorithms
such as L-BFGS and conjugate gradient can often be much faster than gradient descent.