SGD: General Analysis and Improved Rates
Robert Mansel Gower, Nicolas Loizou, Xun Qian, Alibek Sailanbayev, Egor Shulgin, Peter Richtarik
Introduction
Stochastic gradient descent (SGD) Robbins & Monro (1951); Nemirovski & Yudin (1978; 1983); Shalev-Shwartz et al. (2007); Nemirovski et al. (2009); Hardt et al. (2016), has become the workhorse for training supervised machine learning problems which have the generic form (1).
Linear convergence of SGD. Moulines & Bach (2011) provided a non-asymptotic analyses of SGD showing linear convergence for strongly convex up to a certain noise level. Needell et al. (2016) improved upon these results by removing the quadratic dependency on the condition number in the iteration complexity results, and considered importance sampling. The analysis of Needell et al. (2016) was later extended to a mini-batch variant where the mini-batches are formed by partitioning the data Needell & Ward (2017). These works are the main starting point for ours.
Contributions: We further tighten and generalize these results to virtually all forms of sampling. We introduce an expected smoothness assumption (Assumption 2.1), first introduced in Gower et al. (2018) in the context of a certain class of variance-reduced methods. This assumption is a joint property of and the sampling scheme utilized by an SGD method, and allows us prove a generic complexity result (Theorem 3.1) that holds for arbitrary sampling schemes . Our work is the first time SGD is analysed under this assumption. We obtain linear convergence rates without strong convexity; in particular, assuming strong quasi-convexity (this class includes some non-convex functions as well). Furthermore, we do not require the functions to be convex.
Contributions: Our analysis does not directly assume a growth condition. Instead, we make use of the remarkably weak expected smoothness assumption.
Optimal mini-batch size. Recently it was experimentally shown by Goyal et al. (2017) that using larger mini-batches sizes is key to efficient training of large scale non-convex problems, leading to the training of ImageNet in under 1 hour. The authors conjectured that the stepsize should grow linearly with the mini-batch size.
Contributions: We prove (see Section 4) that this is the case, upto a certain optimal mini-batch size, and provide exact formulas for the dependency of the stepsizes on the mini-batch sizes.
Learning schedules. Chee & Toulis (2018) develop techniques for detecting the convergence of SGD within a region around the solution.
Contributions: We provide a closed-form formula for when should SGD switch from a constant stepsize to a decreasing stepsize (see Theorem 3.2). Further, we clearly show how the optimal stepsize (learning rate) increases and the iteration complexity decreases as the mini-batch size increases for both independent sampling and sampling with replacement. We also recover the well known convergence rate of gradient descent (GD) when the mini-batch size is ; this is the first time a generic SGD analysis recovers the correct rate of GD.
Over-parameterized models. There has been some recent work in analysing SGD in the setting where the underlying model being trained has more parameters than there is data available. In this zero–noise setting, Ma et al. (2018) showed that SGD converges linearly.
Contributions: In the case of over-parametrized models, we extend the findings of Ma et al. (2018)Recently, the results of Ma et al. (2018) were extended to the accelerated case by Vaswani et al. (2018); however, we do not study accelerated methods in this work. to independent sampling and sampling with replacement by showing that the optimal mini-batch size is . Moreover, we provide results in the more general setting where the model is not necessarily over-parametrized.
Practical performance. We corroborate our theoretical results with extensive experimental testing.
2 Stochastic reformulation
In this work we provide a single theorem through which we can analyse all importance sampling and mini-batch variants of SGD. To do this, we need to introduce a sampling vector which we will use to re-write our problem (1).
With each distribution we now introduce a stochastic reformulation of (1) as follows
By the definition of the sampling vector, and are unbiased estimators of and respectively, and hence probem (4) is indeed equivalent (i.e., a reformulation) of the original problem (1). In the case of the gradient, for instance, we get
Similar but different stochastic reformulations were recently proposed by Richtárik & Takáč (2017) and further used in (Loizou & Richtárik, 2017; 2019) for the more special problem of solving linear systems, and by Gower et al. (2018) in the context of variance-reduced methods. Reformulation (4) can be solved using SGD in a natural way:
where is sampled i.i.d. at each iteration and is a stepsize. However, for different distributions , (6) has a different interpretation as an SGD method for solving the original problem (1). In our main result we will analyse (6) for any satisfying (3). By substituting specific choices of , we obtain specific variants of SGD for solving (1).
Expected Smoothness and Gradient Noise
In our analysis of SGD (6) applied to the stochastic reformulation (4) we rely on a generic and remarkably weak assumption of expected smoothness, which we now define and relate to existing growth conditions.
Expected smoothness Gower et al. (2018) is an assumption that combines both the properties of the distribution and the smoothness properties of function .
We say that is –smooth in expectation with respect to distribution if there exists such that
There are scenarios where the above inequality is tight. Indeed, in the setting of stochastic reformulations of linear systems considered in Richtárik & Takáč (2017), one has , and , which means that (7) holds as an identity with .
In Section 3.3 we show how convexity and –smoothness of implies expected smoothness. However, the opposite implication does not hold. Indeed, the expected smoothness assumption can hold even when the ’s and are not convex, as we show in the next example.
where the last inequality follows from Proposition A.1. So, for .
2 Gradient noise
Our second key assumption is finiteness of gradient noise, defined next:
The gradient noise , defined by
3 Key lemma and connection to the weak growth condition
When the gradient noise is zero (), inequality (9) is known as the weak growth condition Vaswani et al. (2018). We have the following corollary:
If and if , then satisfies the weak growth condition
This corollary should be contrasted with Proposition 2 in Vaswani et al. (2018) and Lemma 1 in Nguyen et al. (2018), where it is shown, by assuming the functions to be smooth and convex, that the weak growth condition holds with . However, as we will show in Lemma E.1, , and hence our bound is often tighter.
Convergence Analysis
We now present our main theorem, and include its proof to highlight how we make use of expected smoothness and gradient noise.
Assume is -quasi-strongly convex and that . Choose for all . Then iterates of SGD given by (6) satisfy:
Hence, given any , choosing stepsize
Let . From (6), we have
Taking expectation conditioned on we obtain:
Taking expectations again and using Lemma 2.4:
where we used in the last inequality that since Recursively applying the above and summing up the resulting geometric series gives
To obtain an iteration complexity result from the above, we use standard techniques as shown in Section A.1. ∎
Note that we do not assume nor to be convex. Theorem 3.1 states that SGD converges linearly up to the additive constant which depends on the gradient noise and on the stepsize . We obtain a more accurate solution with a smaller stepsize, but then the convergence rate slows down. Since we control , we also control and (we compute these parameters for several distributions in Section 3.3).
Furthermore, we can control this additive constant by carefully choosing the stepsize, as shown in the next result.
Assume is -quasi-strongly convex and that . Let and
If , then SGD iterates given by (6) satisfy:
2 Choosing 𝒟𝒟{\cal D}
For (6) to be efficient, the sampling vector should be sparse. For this reason we will construct so that only a (small and random) subset of its entries are non-zero.
The first analysis of a randomized optimization method with an arbitrary (proper) sampling was performed by Richtárik & Takáč (2016) in the context of randomized coordinate descent for strongly convex functions. This arbitrary sampling paradigm was later adopted in many other settings, including accelerated coordinate descent for strongly convex functions Hanzely & Richtárik (2018), coordinate and accelerated descent for convex functions Qu & Richtárik (2016), primal-dual methods Qu et al. (2015); Chambolle et al. (2018), variance-reduced methods with convex Csiba & Richtárik (2015) and nonconvex Horváth & Richtárik (2018) objectives. Arbitrary sampling arises as a special case of our more general analysis by specializing the sampling vector to one dependent on a sampling . We now define practical sampling vector as follows:
Let be a proper sampling, and let Then the random vector given by
We can further specialize and define the following commonly used samplings. Each sampling gives rise to a particular sampling vector (i.e., distribution ), which in turn gives rise to a particular stochastic reformulation (4) and SGD variant (6).
Independent sampling. The sampling includes every , independently, with probability . This type of sampling was considered in different contexts in Horváth & Richtárik (2018); Hanzely & Richtárik (2018).
By assuming that the functions are convex and smooth we can calculate closed form expressions for the expected smoothness and gradient noise . In particular we make the following smoothness assumption:
To better relate the above assumption to the standard smoothness assumptions we make the following remark.
As a consequence of Assumption 3.4 we also have that each is –smooth and is –smooth. Let
Using Assumption 3.4 and a sampling we establish the following bounds on .
and . If , then
By applying the above result to specific samplings, we obtain the following practical bounds on :
(i) For single element sampling , we have
(ii) For partition sampling with partition , we have
For -nice sampling and independent sampling, we get the following very informative bounds on .
(iii) For independent sampling , we have
Gazagnadou et al. (2019) were the first to suggest using (24) as an approximation for . Through extensive experiments, they showed that the bound (24) is very tight. Here we give the first proof that (24) is indeed a valid upper bound.
For given by (17), formulas for the gradient noise are provided in the next result:
Specializing the above theorem to specific samplings gives the following formulas for :
(i) For single element sampling , we have
(iii) For -nice sampling , we have
(iv) For partition sampling with partition , we have
Generally, we do not know the values of . But if we have prior knowledge that belongs to some set , we can obtain upper bounds for for these samplings from Proposition 3.10 in a straightforward way.
Optimal Mini-Batch Size
Here we develop the iteration complexity for different samplings by plugging in the bounds on and given in Section 3.3 into Theorem 3.1. To keep the notation brief, in this section we drop the logarithmic term from the iteration complexity results. Furthermore, for brevity and to better compare our results to others in the literature, we will use and (see Remark 3.5). Finally let for brevity.
Gradient descent. As a first sanity check, we consider the case where with probability one. That is, each iteration (6) uses the full batch gradient. Thus and it is not hard to see that for in (24) or for all in (23) we have Consequently, the resulting iteration complexity (12) is now . This is exactly the rate of gradient descent, which is precisely what we would expect since the resulting method is gradient descent. Though an obvious sanity check, we believe this is the first convergence theorem of SGD that includes gradient descent as a special case. Clearly, this is a necessary pre-requisite if we are to hope to understand the complexity of mini-batching.
To better appreciate how our iteration complexity evolves with increased mini-batch sizes, we now consider independent sampling with and -nice sampling.
Independent sampling. Inserting the bound on (23) and (27) into (12) gives the following iteration complexity
This is a completely new mini-batch complexity result, which opens up the possibility of optimizing the mini-batch size and probabilities of sampling. For instance, if we fix uniform probabilities with then (30) becomes , where
This complexity result corresponds to using the stepsize
if , otherwise only the left-hand-side term in the minimization remains. The stepsize (32) is increasing since both and decrease as increases.
With such a simple expression for the iteration complexity we can choose a mini-batch size that optimizes the total complexity. By defining the total complexity as the number of iterations times the number of gradient evaluations () per iteration gives
Minimizing in is easy because is a max of a linearly increasing term and a linearly decreasing term in . Furthermore . Consequently, if , then , otherwise
Since is proportional to the noise and and is proportional to the smoothness constants the condition holds when there is comparatively a lot of noise or the precision is high. As we will see in Section 4.2 this logic extends to the case where the noise is zero, where the optimal mini-batch size is
–nice sampling. Inserting the bound on (24) and (28) into (12) gives the iteration complexity , where
Again, this is an increasing function in
We are now again able to calculate the mini-batch size that optimizes the total complexity given by Once again is a max of a linearly increasing term and a linearly decreasing term in . Furthermore . Consequently, if then , otherwise
2 Zero gradient noise
Consider the case where the gradient noise is zero (). According to Theorem 3.1, the resulting complexity of SGD with constant stepsize is given by the very simple expression
where we have dropped the logarithmic term . In this setting, due to Corollary 2.5, we know that satisfies the weak growth condition. Thus our results are directly comparable to those developed in Ma et al. (2018) and in Vaswani et al. (2018).
In particular, Theorem 1 in Ma et al. (2018) states that when running SGD with mini-batches based on sampling with replacement, the resulting iteration complexity is
again dropping the logarithmic term. Now gaining insight into the complexity (39) is a matter of studying the expected smoothness parameter for different sampling strategies.
Independent sampling. Setting (thus ) and using uniform probabilities with in (30) gives
–nice sampling. If we use a uniform sampling and then the resulting iteration complexity is given by
Iteration complexities (40), (41) and (42) tell essentially the same story. Namely, the complexity improves as increases to , but this improvement is not enough when considering the total complexity (multiplying by ). Indeed, for total complexity, these results all say that is optimal.
Importance Sampling
For single element sampling, plugging (21) and (26) into (12) gives the following iteration complexity
where and . In order to optimize this iteration complexity over , we need to solve a dimensional linearly constrained nonsmooth convex minimization problem, which could be harder than the original problem (1). So instead, we will focus on minimizing and over seperately. We will then use these two resulting (sub)optimal probabilities to construct a sampling.
In particular, for single element sampling we can recover the partially biased sampling developed in Needell et al. (2016). First, from (21) it is easy to see that the probabilities that minimize are for all . Using these suboptimal probabilities we can construct a partially biased sampling by letting Plugging this sampling in (21) gives , and from (26), we have . This sampling is the same as the partially biased sampling in Needell et al. (2016). From (30) in Theorem 3.1, we get that the total complexity is now given by
For uniform sampling, and . Hence, compared to uniform sampling, the iteration complexity of partially biased sampling is at most two times larger, but could be smaller in the extreme case where
2 Minibatches
Importance sampling for minibatches was first considered in (Csiba & Richtárik, 2018); but not in the context of SGD. Here we propose the first importance sampling for minibatch SGD. In Section J.2 in the appendix we introduce the use of partially biased sampling together with independent sampling with and show that we can achieve a total complexity of (by Proposition J.3)
which not only eliminates the dependence on , but also improves as the mini-batch size increases.
Experiments
In this section, we empirically validate our theoretical results. We perform three experiments in each of which we highlight a different aspect of our contributions.
In the first two experiments we focus on ridge regression and regularized logistic regression problems (problems with strongly convex objective and components ) and we evaluate the performance of SGD on both synthetic and real data. In the second experiment (Section 6.2) we compare the convergence of SGD for several choices of the distribution (different sampling strategies) as described in Section 3.2. In the last experiment (Section 6.3) we focus on the problem of principal component analysis (PCA) which by construction can be seen as a problem with a strongly convex objective but with non-convex functions Allen-Zhu & Yuan (2016); Garber & Hazan (2015); Shalev-Shwartz (2016).
In all experiments, to evaluate SGD we use the relative error measure . For all implementations, the starting point is sampled from the standard Gaussian. We run each method until or until a pre-specified maximum number of epochs is achieved. For the horizontal axis we always use the number of epochs.
For more experiments we refer the interested reader to Section K of the Appendix.
Regularized Regression Problems: In the case of the ridge regression problem we solve:
while for the -regularized logistic regression problem we solve:
We now compare the performance of SGD in the constant and decreasing stepsize regimes considered in Theorems 3.1 (see (11)) and 3.2 (see (14)), respectively. Here we use a uniform single element sampling. As expected from theory, we see in Figure 1 that the decreasing stepsize regime is vastly superior at reaching a higher precision than the constant step-size variant. In our plots, the vertical red line denotes the value of predicted from Theorem 3.2 and highlights the point where SGD needs to change its update rule from constant to decreasing step-size.
2 Minibatches
In Figures 2 and 5 we compare the single element sampling (uniform and importance), independent sampling (uniform, uniform with optimal batch size and importance) and nice sampling (with some and with optimal ). The probabilities of importance samplings in the single element sampling and independent sampling are calculated by formulas (67) and (77) in the Appendix. Formulas for optimal minibatch size in independent sampling and -nice samplings are given in (34) and (38), respectively. Observe that minibatching with optimal gives the best convergence. In addition, note that for constant step size, the importance sampling variants depend on the accuracy . From Figure 2 we can see that before the error reaches the required accuracy, the importance sampling variants are comparable or better than their coresponding uniform sampling variants.
3 Sum-of-non-convex functions
where are diagonal matrices satisfying . In particular, to guarantee that , we randomly select half of the matrices and assign their -th diagonal value equal to ; for the other half we assign to be . We repeat that for all diagonal values. Note that under this construction, each is a non-convex function. Once again, in the first plot we observe that while both are equally fast in the beginning, the decreasing stepsize variant is better at reaching higher accuracy than the fixed stepsize variant. In the second plot we see, as expected, that all four minibatch versions of SGD outperform single element SGD. However, while the -nice and -independent samplings with lead to a slight improvement only, the theoretically optimal choice leads to a vast improvement.
Acknowledgements
RMG acknowledges the support by a public grant as part of the Investissement d’avenir project, reference ANR-11-LABX-0056-LMH, LabEx LMH, in a joint call with Gaspard Monge Program for optimization, operations research and their interactions with data sciences.
References
Appendix A Elementary Results
In this section we collect some elementary results; some of them we use repeatedly.
Lipschitz continuity of the gradient implies that
Now plugging into the above inequality, we get . It remains to note that . ∎
In this section we summarize some elementary results which we use often in our proofs. We do not claim novelty; we but we include them for completeness and clarity.
Taking logarithms and rearranging (47) gives
Now using that for gives (46). ∎
To analyse the iteration complexity, let and choosing the stepsize so that gives (11). Next we choose so that
Taking logarithms and re-arranging the above gives
Now using that for gives
Appendix B Proof of Lemma 2.4
The first inequality follows from the estimate , and the second inequality follows from (7).
Appendix C Proof of Theorem 3.2
Let and let be an integer that satisfies In particular this holds for
Note that is decreasing in and consequently for all This in turn guarantees that (13) holds for all with in place of , that is
Multiplying both sides by we obtain
where the second inequality holds because . Rearranging and summing from we obtain:
For we have that (13) holds, which combined with (53), gives
Choosing that minimizes the second line of the above gives , which when inserted into (54) becomes
where we have used that for all
Appendix D Proof of Theorem 3.6
Since . and since is -smooth, the function
We also define the following smoothness related quantities
Let and notice that , which gives (19). We prove (20) in the following slightly more comprehensive Lemma E.1. ∎
Appendix E Bounds on the Expected Smoothness Constant ℒℒ{\cal L}
Below we establish some lower and upper bounds on the expected smoothness constant . These bounds were referred to in the main paper in Section 2.3. We also make use of notation introduced in Section 3.3.
Assume that there exists such that with probability 1. Let
Define and note that is –smooth. Furthermore
We will now establish the inequalities in (61) starting from left to the right.
(Part III ). Finally, since
Consequently taking the maximum over in the above gives ∎
Appendix F Proof of Proposition 3.7
First note that by combining (19) and (D) we have that
(i) By straight forward calculation from (63) and using that each set is a singleton.
(ii) For every partition sampling we have that if , hence
Appendix G Proof of Proposition 3.8
First, since is -smooth with and convex, it follows from equation (2.1.7) in Theorem 2.1.5 in Nesterov (2013) that
Now consider the case where for Recalling that we have from the above that
Substituting and comparing the above to the definition of expected smoothness (7) we have that
(i) For independent sampling, we have that for , consequently Thus (66) gives (23).
(ii) For -nice sampling, we have that for and , hence and (66) gives (24). ∎
Appendix H Proof of Theorem 3.9
Appendix I Proof of Proposition 3.10
(ii) For independent sampling , for , hence,
(iii) For -nice sampling , if , it is obvious. If , then for , and for all . Hence,
(iv) For partition sampling, if , and otherwise. Hence,
Appendix J Importance sampling
From (21) it is easy to see that the probabilities that minimize are for all , and consequently . On the other hand the probabilities that minimize (26) are given by for all , with .
From and , we construct interpolated probabilities as follows:
where . Then and from (21) we have
Similarly, from (26) we have that . Now by letting , from (30) in Theorem 3.1, we get an upper bound of the right hand side of (12):
By minimizing this bound in we can get
where the right hand side comes by setting . Notice that the minimum of the iteration complexity in (12) is not less than . Hence, the iteration complexity of this importance sampling(left hand side of (70)) is at most two times larger than the minimum of the iteration complexity in (12) over .
J.2 Independent sampling
For the independent sampling , in this section we will use the following upper bound on given by
which follows immediatly from (23) by using that
Minimizing the upper bound of in (71) boils down to minimizing , which is not easy generally. Instead, as a proxy we obtain the probabilities by solving
Let for all , and . If , it is easy to see solves (72). Otherwise, in order to solve (72), we can choose for , and for such that . By letting , we have that (72) becomes
For , from (27), we need to solve
Let for all , and let . If , it is easy to see that solve (74). Otherwise, it is a little complicated to find the optimal solution. For simplicity, if , we choose for , and for such that . By letting , from (27), we have
Importance sampling.
Since by (73) we have that and are obtained by using the upper bounds in (71) and (27), and the upper bounds are nonincreasing as increases, we get the following property.
If for all , then , and if , then .
From Proposition J.1, we can get the following result.
For , let satisfy
If where satisfies (75), then we have
First , we claim that can be constructed to satisfy (75). Since and , we know
From (75), we have . Then by Proposition J.1, we have
We also have , hence, by Proposition J.1, we get
From (12) in Theorem 3.1, by letting in Proposition J.2, we get an upper bound of the right hand side of (12):
where . So suboptimal probabilities
where is given in Equation (76).
Partially biased sampling.
In practice, we do not know generally. But we can use and the uniform probability to construct a new probability just as that in Proposition J.2. More specific, we have the following result.
The proof for is the same as Proposition J.2. For , from (27), since , we have
This sampling is very nice in the sense that it can maintain at least close to , and meanwhile, can acheive nearly linear speedup in by increasing . We can compare the upper bounds of and for this sampling, -nice sampling, and -uniform independent sampling when in the following table.
From Table 1, compared to -nice sampling and -uniform independent sampling, the iteration complexity of this -partially biased independent sampling is at most two times larger, but could be about smaller in some extremely case where and dominates in (12).
Appendix K Additional Experiments
Here we evaluate the choice of the switching moment from a constant to a decreasing step size according to (14) from Theorem 3.2. We are using synthetic data that was generated in the same way as it had been in the Section 6 for the ridge regression problem . In particular we evaluate 4 different cases: (i) the theoretical moment of regime switch at moment as predicted from the Theorem, (ii) early switch at , (iii) late switch at and (iv) the optimal for switch, where the optimal is obtained using one-dimensional numerical minimization of (54) as a function of .
According to Figure 4, when is close to , the moment of regime switch does not play a significant role in minimizing the number of iteration except for a very early switch, which actually also leads to almost the same situation in the long run. The case when is far from shows that preliminary one-dimensional optimization makes sense and allows to reduce the error at least during the early iterations.
K.2 More on minibatches
Figure 5 reports on the same experiment as that described in Section 6.2 (Figure 2) in the main body of the paper, but on ridge regression instead of logistic regression, and using different data sets. Our findings are similar, and corroborate the conclusions made in Section 6.2.
K.3 Stepsize as a function of the minibatch size
In our last experiment we calculate the stepsize as a function of the minibatch size for -nice sampling using equation (37). Figure 6 depicts three plots, for three synthetic data sets of sizes . We consider regularized ridge regression problems with . Note that the stepsize is an increasing function of .