Hessians - A tool for debugging neural network optimization

Optimizing deep neural networks has long followed a general tried-and-true template. Generally, we randomly initialize our weights, which can be thought of as randomly picking a place on the “hill” which is the optimization landscape. There are some tricks we can do to achieve better initialization schemes, such as the He or Xavier initialization.

Then, we follow the gradient and update our parameters until we’ve met some stopping criterion. This is known as gradient descent. More commonly, stochastic gradient descent is used to add noise (via approximating the gradient instead of computing the exact gradient) to the optimization process and speed up training time. Additionally, vanilla SGD is less common now, with practitioners instead opting for methods that add in momentum or adaptive learning rate techniques.

It’s natural to question theoretical - and practical - aspects of this common technique. What guarantees does it provide, and what are some conditions in which it could fail? Here, I’ll attempt to dissect some of these questions so we can better understand what to watch out for when optimizing deep networks.

Precursor: Convexity and the Hessian

Say we want to optimize where are our parameters that we want to learn. The question of convexity arises. Is convex, meaning that any local minimum we achieve is the optimal solution to our problem? Or is nonconvex, in which case local minima can be larger than global minima? It turns out that the Hessian, or the matrix of ’s second derivatives, can tell us a lot about this.

The Hessian is real and symmetric, since in general we assume that the second derivatives exist and for the functions that we are considering (Schwarz’s theorem provides the conditions that need to be true for this to hold). For example, the Hessian for a function could be expressed as

Real symmetric matrices have nice properties:

  • All eigenvalues are real and distinct eigenvalues correspond to distinct eigenvectors
  • The eigenvectors of distinct values are orthogonal, and therefore form a basis for .

Next, a positive definite matrix is a symmetric matrix that has all positive eigenvalues. One way to determine if a function is convex is to check if its Hessian is positive definite.

To show this, it is enough to show that for any real vector . To see why all positive eigenvalues imply this, first let’s consider the case where is an eigenvector of . Since we have

since .

To prove this for an arbitrary vector , we first note that we can diagonalize as follows:

Where is a matrix whose columns are (distinct) eigenvectors of and is a diagonal matrix with the corresponding eigenvalues on its diagonal. We know that this diagonalization is possible since is real and symmetric.

As mentioned, the eigenvectors are orthogonal. Since is a matrix whose columns are the eigenvectors, is an orthogonal matrix, so we have , giving us:

Let’s define , so we now have . Taking

and

We now have

.

Which is true since all the eigenvalues are positive.

As an example of using this analysis to prove the convexity of a machine learning problem, we can take the loss function of the (l2-regularized) SVM:

The derivatives for each are

The second derivatives can be characterized as and . The latter derivatives will appear as the diagonal entries of the Hessian.

For the first case, since our expression is constant with respect to the derivative is simply . For the second case, the derivative is simply . Therefore, our Hessian is the following diagonal matrix:

Since this is a diagonal matrix, the eigenvalues are simply the entries on the diagonal. Since the regularization constant , the Hessian is positive definite, therefore the above formulation of the hinge loss is a convex problem. We could have alternatively shown that as well, since simply scales by a positive scalar.

Ok…But what about Neural Networks?

The optimization problem for neural networks are generally highly nonconvex, making optimizing deep neural networks much tougher. Fortunately, there are still some applications of the Hessian that we could use.

Local Minima

First, it may be worth it to know if we are at a local minimum at any point during our optimization process. In some cases, if this occurs early in our training process or at a high value for our objective function, then we could consider restarting our training/initialization process or randomly update our objective to “kick” our parameters out of the bad local minima.

Even though the Hessian may not be positive definite at any given point, we can check if we’re at a local minimum by examining the Hessian. This is basically the second derivative test in single-variable calculus. Consider the Taylor Series expansion of our objective around :

If we’re at a critical point, it is a potential local minimum and we have . Considering an SGD update , we have

If the Hessian is positive definite at , then it tells us that any direction in which we choose to travel will result an increased value of the objective function, so we are at a local minimum. We can similarly use the idea of negative definiteness to see if we are at a local maximum.

Are local minima really that big of a problem?

Arguably, the goal of fitting deep neural networks may not be to reach the global minimum, as this could result in overfitting, but rather find an acceptable local minima that obains good performance on the test set. In this paper, the authors state that “while local minima are numerous, they are relatively easy to find, and they are all more or less equivalent in terms of performance on the test set”. In the Deep Learning Book, Goodfellow et al. state the early convergence to a high value for the objective is generally not due to local minima, and this can be verified by plotting the norm of the gradient throughout the training process. The gradient at these supposed local minima exhibit healthy norms, indicating that the point is not actually a local minima.

A Poorly Conditioned Hessian

There are some cases where the regular optimization process - a small step in the direction of the gradient - could actually fail, and increase the value of our objective function - couteractive to our goal. This happens due to the curvature of the local region of our objective function, and can be investigated with the Hessian as well.

How much will a gradient descent update change the value of our objective? First, let’s again look at the Taylor series expansion for an update where :

We can see that the decrease in the objective is related to 2nd order information, specifically the term . If this term is negative, then the decrease in our objective is greater than our (scaled) squared gradient norm, if it is positive, then the decrease is less, if it is zero, the decrease is completely given by first-order information.

“Poor Conditioning” refers to the Hessian at the point being such that it results in an increased value of our objective function. This can happen if the term that indicates our decrease in our objective is actually greater than :

which corresponds to

This can happen if the Hessian is very large, in which case we’d want to use a smaller learning rate than to offset some of the influence of the curvature. An intuitive justification for this is that due to the large magnitude of curvature, we’re more “unsure” of our gradient updates, so we want to take a smaller step, and therefore have a smaller learning rate.

Saddle Points

Getting stuck at a saddle point is a very real issue for optimizing deep neural networks, and arguably a more important issue than getting stuck at a local minima (see http://www.offconvex.org/2016/03/22/saddlepoints/). One explanation of this is that it’s much more likely to arrive at a saddle point than a local minimum or maximum: the probability of a given point in an dimensional optimization space being a local minimum or maximum is just .

A saddle point is defined as a point with gradient, but the Hessian is neither positive definite or negative definite. It is possible that learning would stop if we are relying only on first-order information (i.e. since , the weights will not be updated) but in practice, using techniques such as momentum reduce the chance of this.

Around a saddle point, the expansion of our objective is similar to the expansion at a local minimum:

Here, is not positive definite, so we may be able to pick certain directions that increase or decrease the value of our objective. Concretely, if we pick such that , then is a direction that would decrease the value of our objective, and we can update our parameters with . Ideally we’d like to find a such that is significantly less than , so that we have a steeper direction of descent to travel in.

How do we know what functions have saddle points that are “well-behaved” like this? Ge et al. in their paper introduced the notion of “strict saddle” functions. One of the properties of these “strict saddle” functions is that at all saddle points , the Hessian has a (significant) negative eigenvalue, which means that we can pick the eigenvector corresponding to this eigenvalue as our direction to travel in if we are at a saddle point.

However, it is often infeasible to compute the Hessian while training deep learning models, since it is computationally expensive. How can we escape saddle points using only first-order information?

Get et al. in their paper also describe a variant of stochastic gradient descent which they call “noisy stochastic gradient descent”. The only variant is that some random noise is added to the gradient:

where is random noise sampled from the unit sphere. This ensures that there is noise in every direction, allowing this variant of SGD to explore the region around the saddle point. The authors show that this noise can help escape from saddle points for strict-saddle functions.

The authors mention in the corresponding blog post that it is useful to think of a saddle point as unstable, so slight perturbations can be effective in order to kick your weights out of the saddle point. To me this indicates that any perturbation in addition to the regular SGD update could be beneficial to continue optimization at a saddle point, so techniques such as momentum would be helpful as well.

Computing the Hessian

​ Most of these methods discussed involve the need to compute the Hessian, which is often unrealistic in practice due to the aformented reasons. However, we can use finite difference methods in order to approximate the Hessian, which may be enough to do our job. To understand this, first we can consider the limit definition of the derivative in the single-variable case:

This means an approximation for can be given by

where our approximation improves with a smaller but we run into numerical stability issues if we take to be too small. It turns out that a slightly better approximation would be to use the centered version of the above (see the explanation on CS 231n for details):

For vector-valued functions, we can generalize this idea to several dimensions:

where is a vector that has as its th entry, and is zero everywhere else. We’re basically taking the unit vector pointing in that direction, scaling it so that it is small, and adding that to our setting of parameters given by the vector .

It turns out that this identity can be further generalized, to approximate the gradient-vector product (i.e. the product of the gradient times a given vector). We simply remove the constraint that is the unit vector pointing in a particular direction, and instead let it be any vector . This lets us approximate the gradient vector product:

It’s quite simple to extend this to computing the Hessian-vector product: given the gradient, the gradient of the gradient is the Hessian. This means that we can replace the gradient on the left hand side of the above equation with the Hessian, and the function on the right hand side with the gradient:

which gives us the Hessian-vector product.

Takeaways

The Hessian can give us useful second order information when optimizing machine learning algorithms, though it is computationally tough to compute in practice. By analyzing the Hessian, we may be able to get information regarding the convex nature of our problem, and it can also help us determine local minima or “debug” gradient descent when it actually fails to reduce our cost function or gets stuck at a saddle point.

Learn More

If you’re interested in learning more about theoretical aspects about machine learning, I find that an ML textbook is really useful. My favorite one so far has been Pattern Recognition and Machine Learning, which is available on Amazon.

Sources
  1. Escaping from Saddle Points
  2. Paper: Escaping from Saddle Points - Online SGD for Tensor Decomposition
  3. Deep Learning Book Ch. 8
  4. Explanation of approximating gradient-vector products
Written on March 3, 2019