An Alternative View: When Does SGD Escape Local Minima?
Robert Kleinberg, Yuanzhi Li, Yang Yuan
Introduction
Nowadays, stochastic gradient descent (SGD), as well as its variants (Adam , Momentum , Adagrad , etc.) have become the de facto algorithms for training neural networks. SGD runs iterative updates for the weights : , where is the step sizeIn this paper, we use step size and learning rate interchangeably.. is the stochastic gradient that satisfies , and is usually computed using a mini-batch of the dataset.
In the regime of convex optimization, SGD is proved to be a nice tradeoff between accuracy and efficiency: it requires more iterations to converge, but fewer gradient evaluations per iteration. Therefore, for the standard empirical risk minimizing problems with points and smoothness , to get to -close to , GD needs gradient evaluations , but SGD with reduced variance only needs gradient evaluations . In these scenarios, noise is a by-product of cheap gradient computation, and does not help training.
By contrast, for non-convex optimization problems like training neural networks, noise seems crucial. It is observed that with the help of noisy gradients, SGD does not only converge faster, but also converge to a better solution compared with GD . To formally understand this phenomenon, people have analyzed the role of noise in various settings. For example, it is proved that noise helps to escape saddle points , gives better generalization , and also guarantees polynomial hitting time of good local minima under some assumptions .
However, it is still unclear why SGD could converge to better local minima than GD. Empirically, in additional to the gradient noise, the step size is observed to be a key factor in optimization. More specifically, small step size helps refine the network and converge to a local minimum, while large step size helps escape the current local minimum and go towards a better one . Thus, standard training schedule for modern networks uses large step size first, and shrinks it later . While using large step sizes to escape local minima matches with intuition, the existing analysis on SGD for non-convex objectives always considers the small-step-size settings .
To formalize this intuition, instead of analyzing the sequence , let us look at the sequence , where is defined to be , as in the preceding paragraph. The SGD algorithm never computes these vectors , but we are only using them as an analysis tool. From the equation we obtain the following update rule relating to .
This alternative view helps to explain why SGD converges to a good local minimum, even when has many other sharp local minima. Intuitively, sharp local minima are eliminated by the convolution operator that transforms to , since convolution has the effect of smoothing out short-range fluctuations. This reasoning ensures that SGD converges to a good local minimum under much weaker conditions, because instead of imposing convexity or one-point convexity requirements on itself, we only require those properties to hold for the smoothed functions obtained from by convolution. We can formalize the foregoing argument using the following assumption.
For point , since the direction points to , by having positive inner product with , we know the direction in (1) approximately points to in expectation (See more discussion on one point convexity in Appendix). Therefore, will converge to with decent probability:
Notice that our main theorem not only says SGD will get close to , but also says with constant probability, SGD will stay close to for the future steps. As we will see in Section 5, we observe that Assumption 2 holds along the SGD trajectory for the modern neural networks when the noise comes from real data mini-batches. Moreover, the SGD trajectory matches with our theory prediction in practice.
Our main theorem can also help explain why SGD could escape “sharp” local minima and converge to “flat” local minima in practice . Indeed, the sharp local minima have small loss value and small diameter, so after convolved with the noise kernel, they easily disappear, which means Assumption 2 holds. However, flat local minima have large diameter, so they still exists after convolution. In that case, our main theorem says, it is more likely that SGD will converge to flat local minima, instead of sharp local minima.
Previously, people already realized that the noise in the gradient could help SGD to escape saddle points or achieve better generalization . With the help of noise, SGD can also be viewed as doing approximate Bayesian inference or variational inference . Besides, it is proved that SGD with extra noise could “hit” a local minimum with small loss value in polynomial time under some assumptions . However, the extra noise is too big to guarantee convergence, and that model cannot deal with escaping sharp local minima.
Escaping sharp local minima for neural network is important, because it is conjectured (although controversial ) that flat local minima may lead to better generalization . It is also observed that the correct learning rate schedule (small or large) is crucial for escaping bad local minima . Furthermore, solutions that are farther away from the initialization may lead to wider local minima and better generalization . Under a Bayesian perspective, it is shown that the noise in stochastic gradient could drive SGD away from sharp minima, which decides the optimal batch size . There are also explanations for why small batch methods prefers flat minima while large batch methods are not, by investigating the canonical quadratic sums problem .
To visualize the loss surface of neural network, a common practice is projecting it onto a one dimensional line , which was observed to be convex. For the simple two layer neural network, a local one point strongly convexity property provably holds under Gaussian input assumption .
Motivating Example
Main Theorem
Assume that we are running SGD on the sequence . Recall the update rule (1) for . Our main theorem says that is converging to and will stay around afterwards.
This corollary can be easily generalized to shrink the learning rate multiple times.
Our main theorem is based on the important assumption that the step size is bounded. If the step size is too big, even if the whole function is one point convex (a stronger assumption than Assumption 2), and we run full gradient descent, we may not keep getting closer to , as we show below.
For function , if , and we are at the point . If we run full gradient descent with step size , we have .
The proof is straightforward and we defer it to Appendix C. ∎
This theorem can be best illustrated with Figure 4. If is too big, although the gradient (the arrow) is pointing to the approximately correct direction, will be farther away from (going outside of the -centered ball).
Although this theorem analyzes the simple full gradient case, SGD is similar. In the high dimensional case, it is natural to assume that most of the noise will be orthogonal to the direction of , therefore with additional noise inside the stochastic gradient, a large step size will drive away from more easily.
Therefore, our paper provides a theoretical explanation for why picking step size is so important (too big or too small will not work). We hope it could lead to more practical guidelines in the future.
Proof for Theorem 4
In the proof, we will use the following lemma.
Step 1. Since Assumption 2 holds, we show that SGD always makes progress towards in expectation, plus some noise.
Step 2. Since SGD makes progress in every step, after many steps, SGD gets very close to in expectation. By Markov inequality, this event holds with large probability.
Notice that since , we have . Recall , we get:
Let , we get:
That means, is a supermartingale. We have
Since , we get:
By Markov inequality, we know with probability at least ,
For notational simplicity, for the analysis below we relabel the point as . Therefore, at time we already have .
Step 3. Conditioned on the event that we are close to , below we show that if for , is close to , then is also close to with high probability.
Let . Let event , where is a parameter satisfies . If with probability , holds for every , we are done.
Apply Azuma inequality (Theorem 4), for any , we know
Therefore, with probability ,
Step 4. The inequality above says, if holds, i.e., for all , then with probability , is bounded. If we can show from the upper bound of that is also true, we automatically get holds. In other words, that means if holds, then holds with probability . Therefore, by applying this claim times, we get holds with probability . Combining with inequality (3), we know with probability at least , the theorem statement holds. Thus, it remains to show that .
The second last inequality holds because we know , since , and .
It remains to prove the following lemma, which we defer to Appendix B.
Therefore, . Combining the 4 steps together, we have proved the theorem.
Empirical Observations
In this section, we explore the loss surfaces of modern neural networks, and show that they enjoy many nice one point convex properties. Therefore, our main theorem could be used for explaining why SGD works so well in practice.
It is well known that the loss surface of neural network is highly non-convex, with numerous local minima. However, we observe that the loss surface is consisted of many one point convex basin region, while each time SGD traverses one of such regions.
See Figure 5(a) for details. We ran experiments on Resnet ( layers, M parameters), Densenet ( layers, M parameters) on cifar10 and cifar100, each for 5 trials with epochs and different initializations. For the start of every epoch in each trial, we compute the inner product between the negative gradient and the direction . In Figure 5(a), we plot the minimum value for every epoch among trials for each setting. Notice that except for the starting period of densenet on Cifar-10, all the other networks in all trials have positive inner products, which shows that the trajectory of SGD (except the starting period) is one point convex with respect to the final solutionSimilar observations were implicitly observed previously .. In these experiments, we have used the standard step size schedule ( initially, after epoch , and after epoch ). However, we got the same observation when using smoothly decreasing step sizes (shrink by per epoch).
2 The neighborhood of the trajectory is one point convex
Having a one point convex trajectory for trials does not suffice to show SGD always has a simple and easy trajectory, due to the randomness of the stochastic gradient. Indeed, by a slight random perturbation, SGD might be in a completely different trajectory that is far from being one point convex to the final solution. However, in this subsection, we show that it is not the case, as the SGD trajectory is one point convex after convolving with uniform ball with radius . That means, the whole neighborhood of the SGD trajectory is one point convex with respect to the final solution.
In this experiment, we tried Resnet ( layers, M parameters), Densenet ( layers, M parameters) on cifar10 and cifar100We also tried VGG with M parameters, but does not have similar observations. This might be why Resnet and Densenet are slightly easier to optimize.. For every epoch in each setting, we take one point and look at its neighborhood with radius (upper bound of the length of one SGD step, as we will show below). We take random points inside each neighborhood to verify Assumption 2We also tried to sample points that are one SGD step away to represent the neighborhood, and got similar observations.. More specifically, for every random point in the neighborhood of , we computer . Figure 5(b) shows the mean value (solid line), as well as upper and lower bound of the inner product (shaded area). As we can see, the inner products for all epochs in every setting have small variances, and are always positive. Although we could not verify Assumption 2 by computing the exact expectation due to limited computational resources, from Figure 5(b) and Hoeffding bound (Lemma 6), we conclude that Assumption 2 should hold with high probability.
Figure 5(c) shows the norm of the stochastic gradients, including both the mean value (solid lines), as well as upper and lower bounds (shaped area). For all settings, the stochastic gradients are always less than before epoch with learning rate , and less than afterwards with learning rate . Therefore, multiplying step size with gradient norm, we know SGD step length is always bounded by .
Notice that the gradient norm gets bigger when we get closer to the final solution (after epoch ). This further explains why shrinking step size is important.
3 Loss surface is locally a “slope”
Even with the observation that the whole neighborhood along the SGD trajectory is one point convex with respect to the final solution, there exists a chicken-and-egg concern, as the final target is generated using the SGD trajectory.
In this subsection, we show that the one point convexity is a pretty “global” property. We were running Resnet and Densenet on Cifar10, but with smaller networks (each with about parameters). For each network, if we fix the first epochs, and generate SGD trajectories with different random seeds for epochs and learning rate, we get different final solutions (they are pretty far away from each other, with minimum pairwise distance ). For each network, if we look at the inner product between the negative gradient of any epoch of any trajectories, and the vector pointing to any final solutions, we find that the inner products are almost always positive. (only of the inner products are not positive for Densenet, and only out of inner products are not positive for Resnet).
This indicates that the loss surface is “skewed” to the similar direction, and our observation that the whole SGD trajectory is one point convex w.r. to the last point is not a coincidence. Based on our Theorem 4, such loss surface is very friendly to SGD optimization, even with a few exceptional points that are not one point convex with respect to the final solution.
Notice that in general, it is not possible that all the negative gradients of all points are one point convex with respect to multiple target points. For example, if we take interpolation between any two target points, we could easily find points that have negative gradients only pointing to one target point. However, based on our simulation, empirically SGD almost never traverse those regions.
4 Spectrum of the local minima
From the previous subsections, we know that the loss surface of neural network has great one point convex properties. It seems that by our Theorem 4, SGD will almost always converge to a few target points (or regions). However, empirically SGD converges to very different target points. In this subsection, we argue that this is because of the learning rate is too big for SGD to converge (Theorem 3). On the other hand, whenever we shrink the learning rate to , Theorem 4 immediately applies and SGD converges to a local minimum.
In this experiment, we were running smaller version of Resnet and Densenet (each with about parameters) on Cifar10 and Cifar100. For each setting, we first train the network with step size for epochs, then we pick different epochs as the new starting points for finding nearby local minima using smaller learning rates with additional epochs.
See Figure 6(a) and Figure 6(b). Starting from different epochs, we got local minima with decreasing validation loss and training loss.
To show that these local minima are not from the same region, we also plot the distance of the local minima to the (unique) initialized point. As we can see, as we pick later epochs as the starting points, we get local minima that are farther away from the initialization with better quality (also observed in ).
Furthermore, we observe that for every local minimum, the whole trajectory is always one point convex to that local minimum. Therefore, the time for shrinking learning rate decides the quality of the final local minimum. That is, using large step size initially avoids being trapped into a bad local minimum, and whenever we are distant enough from the initialization, we can shrink the step size and converge to a good local minimum (due to one point convexity by Theorem 4).
Conclusion
In this paper, we take an alternative view of SGD that it is working on the convolved version of the loss function. Under this view, we could show that when the convolved function is one point convex with respect to the final solution , SGD could escape all the other local minima and stay around with constant probability.
To show our assumption is reasonable, we look at the loss surface of modern neural networks, and find that SGD trajectory has nice local one point convex properties, therefore the loss surface is very friend to SGD optimization. It remains an interesting open question to prove local one point convex property for deep neural networks.
Acknowledgement
The authors want to thank Zhishen Huang for pointing out a mistake in an early version of this paper, and want to thank Gao Huang, Kilian Weinberger, Jorge Nocedal, Ruoyu Sun, Dylan Foster and Aleksander Madry for helpful discussions. This project is supported by a Microsoft Azure research award and Amazon AWS research award.
References
Appendix A Discussions on one point convexity
If is -one point strongly convex around in a convex domain , then is the only local minimum point in (i.e., global minimum).
To see this, for any fixed , look at the function for , then . The definition of -one point strongly convex implies that the right side is negative for . Therefore, for . This implies that for every point on the line segment joining to , we have , so is the only local minimum point.
Appendix B Proof for Lemma 5
On the left hand side there are three summands. Below we show that each of them is bounded by We made no effort to optimize the constants here..
Since , we know . Next, we have
Adding the three summands together, we get the claim. ∎
Appendix C Proof for Theorem 3
Recall that we have . Since we have , then
Where the last inequality holds since we know . ∎