It seemed a bit unfair to devote a blog to machine learning (ML) without talking about its current core algorithm: stochastic gradient descent (SGD). Indeed, SGD has become, year after year, the basic foundation of many algorithms used for large-scale ML problems. However, the history of stochastic approximation is much older than that of ML: its first study by Robbins and Monro [1] dates back to 1951. Their aim was to find the zeros of a function that can only be accessed through noisy measurements; and this set-up was applied (and studied!) later on for many problems [2]. Stochastic gradient descent, in its most general definition, is simply the application of the Robbins and Monro’s procedure to find the zeros of the gradient of a function f.
In this post, I will try to show that the instance of SGD used to solve modern ML problems carries rich particularities. In particular, we will put emphasis on the difference between the under and overparametrised regimes. To provide prototypical representations of these, we will try to cast the typical dynamics of each regime as a known stochastic process.
General set-up
Stochastic approximation. At each time t∈N of the procedure, let us suppose that we only have access to an unbiased estimate of the gradient, ∇ft, of a function f we want to minimise. More formally this means that given the information of the first t−1 steps, denoted by Ft−1, the estimate we get at time t is centred around the true gradient: E[∇ft(θt−1)∣Ft−1]=∇f(θt−1). Then, the SGD iterates with step-sizes (γt)t∈N, and initialised at θt=0=θ0, write
θt=θt−1–γt∇ft(θt−1).
To put the emphasis on the noise induced by the noisy estimates of the true gradient, we prefer sometimes to rephrase the recursion in term of the conditional zero-mean noise sequence εt=∇f–∇ft.
θt=θt−1–γt∇f(θt−1)+γtεt(θt−1).
As we will see later, to analyse this discrete time stochastic dynamics, we crucially need to understand the behaviour of (εt)t∈N.
Supervised learning reformulation. This general recursion can be applied to many settings and fits particularly well to the supervised learning framework. In this case, the function to minimise is the empirical risk
where ρn:=n1∑i=1nδ(xi,yi) is the empirical measure associated to the samples (xi,yi)1⩽i⩽n. We can derive an unbiased estimate ∇θℓ((xit,yit),θ) of its gradient where (it)t is the sequence of uniformly sampled indices over {1,…,n}. The recursion reads:
θt=θt−1–γt∇θℓ((xit,yit),θt−1).
However, one of the real power of SGD is that it can be seen as a direct stochastic gradient method to optimise the true risk [3]. Indeed, recall that the true risk is R(θ)=Eρ[ℓ((X,Y),θ)] and consider an input/output sample pair (xi,yi) drawn from ρ. Now, ℓ((xi,yi),θ) is an unbiased estimate of the true risk R(θ) such that ∇θℓ((xi,yi),θ) is an unbiased estimate of its gradient. Hence, if we denote Ft=σ((xi,yi),i⩽t), then the stochastic gradient descent optimises R as long as new points (x,y) are added in the data set
θt=θt−1–γt∇θℓ((xt,yt),θt−1).
This reveals the real strength of SGD against other types of gradient descent algorithm: beyond its low computational cost, as long as we use unseen data, SGD optimises directlythe true risk although it is an a priori unknown function. As a consequence, the SGD algorithm, when using only fresh samples, cannot overfit the dataset and does not need any regularisation.
The noise of SGD in practical ML setting
The story outlined in the previous paragraph has been very popular to explain the success of SGD. However, nowadays, the number of samples is usually way smaller than the number of iterations performed, i.e. t≫n: several epochs are made over the dataset and the fact that SGD is a stochastic approximation algorithm that minimises directly the true risk doesn’t hold any longer. Hence, from now on, let us set the data to n input/output pairs(xi,yi)1⩽i⩽n.
First, let us briefly present certain crucial properties of SGD in this setting (see also the nice paper [8] for more details). We take a parameterised family of predictors {x↦h(θ,x),for θ∈Θ}, and assume that ℓ is the square-loss. The empirical risk classically writes Rn(θ)=2n1∑i=1n(h(θ,xi)–yi)2 and the SGD recursion can be rewritten as
θt=θt−1–γ∇Rn(θt−1)+γεt(θt−1),
where εt(θ) is the noise term at time t, which writes
where for all i∈{1,…,n}, we define the i-th residual ri(θ)=h(θ,xi)–yi. Note that while the expression written requires that the loss is the square-loss, the conclusions of this section extend to different losses (such as the logistic).
Multiplicative noise and additive noise. Let us call θ∗ any vector belonging to the set of critical points of the loss Rn, that is ∇Rn(θ∗)=0 (note that, in the convex case, it is exactly the set of global minima of the loss). We say that we are in the “overparametrised” regime if there exists at least one interpolator which fits the data set, i.e. if there exists θ∗ such that Rn(θ∗)=0. Otherwise, we say that we are in the “underparametrised” regime. We now decompose the noise as the sum of two different noises, that will contribute differently to the dynamics:
If εt is L-Lipshitz continuous, which can be verified for instance if we use the squared loss, the term εt(θ)–εt(θ∗) converges to 0 when θ approaches θ∗. This motivates the “multiplicative” denomination of this term. On the contrary, remark that the additive noise does not depend on the state of the dynamics, i.e. it is independent of θt. Notably, in the overparametrised regime, taking θ∗ as an interpolator, we see that εt(θ∗)=0: there is no additive noise in this case! This is part of the explanation of the very different optimisation dynamics observed in the underparametrised and overparametrised regimes [14, 15].
As an illustration of these two distinct regimes, consider a least-squares problem:
Rn(θ)=2n1i=1∑n(⟨θ,xi⟩–yi)2.
In the overparametrised setting, which typically requires that d⩾n, where d is the dimension of the inputs, it is possible to interpolate the dataset and the noise vanishes at optimum. On the opposite, in any underparametrised setting, the global minimum of the loss function is strictly positive, and the noise’s variance is always lower-bounded: the presence of additive noise totally changes the nature of the dynamics as shown by the following pictures.
Left: Iterates of SGD for underparametrised (blue) and overparametrised (orange) regimes. The ellipses represent different level sets of Rn. Inputs (xi)i⩽n are the same in the two models: centred Gaussian with anisotropic covariance (n=100,d=2). In the underparametrised setting, the outputs have been generated randomly, and there is no interpolator of the dataset, whereas in the overparametrised problem, we generated the outputs according to Y=Xθ∗, where θ∗ is the unique global minimum of the first setup and hereby of the second as well.
Right: The ℓ2-norm of the SGD noise in log-scale along iterations in the underparametrised (blue) and overparametrised (orange) regimes. In the underparametrised regime, the noise stays constant after ∼75 iterations, whereas the intensity of the noise goes to 0 linearly over the iterates.
Noise geometry. Another important fact to stress concerning the noise εt(θ) is that, at state θ, it belongs to a specific linear space: span{∇θh(θ,x1),…,∇θh(θ,xn)}. To provide an intuition on how restrictive it can be, consider once again our favourite linear model: in this case, ∇θh(θ,xi)=xi and εt(θ) belongs to span{x1,…,xn}, which is a space of dimension at most n. In the overparametrised case, this is a strict subspace of Rd: noise is degenerate in d−n directions! On the contrary, in the underparametrised setting, as d⩽n there is no specificity at this level.
3D picture showing the geometric bias of SGD: in the least-squares setting, the iterates (red lines) stay in the affine space θ0+span(xi). The contours represent different levels set of the loss highlighting the translational invariance along the null space Ker(X).
SGD as a Markov chain
In this section, we develop a different point of view on the general SGD algorithm, that also provides helpful intuition: we try to understand its dynamics through a Markov chain interpretation.
The first key to understand the behaviour of SGD is that, with constant step-sizes γt=γ, the iterates define an homogeneous Markov chain [4]. Let us recall the SGD recursion to keep it next to us.
θt=θt−1–γ∇Rn(θt−1)+γεt(θt−1).
This point of view induces a natural question: does the distribution of the iterates converge to some limit we can characterise? This will again depend on the regime: under or overparametrised. Roughly, for the former, with a constant step size γ, the distribution will converge to a stationary distribution with a strictly positive variance (e.g. a Gaussian centred around θ∗ at scale γ1/2 [5]). Less understood is the fact that, for the latter, it will converge to a Dirac distribution δθ∗, with θ∗ being one specific interpolator, that will depend on the precise structure of the algorithm (initialisation, set-size, architecture of the model… more details on this will be discussed in a future blog post)!
The underparametrised case. Recall that in this case, the additive noise is nondegenerate. Hence, the gradient and noise parts of the dynamics cannot cancel simultaneously and under some mild assumption on Rn, the dynamics equilibrates after a certain time: formally this means that the distribution of the iterates (θt)t⩾0 converges to an invariant distribution πγ. Hence the question: how far is πγ from δθ∗? Giving a general and precise answer to this question is not trivial, and more importantly, it depends heavily on the multiplicative part of the noise. For now, let us stick with some common modelling to study the recursion: we make the assumption that the noise does not depend on the current iterate θ (multiplicative part is 0). In this case, it has been shown that the iterates of SGD have Gaussian fluctuations around θ∗ at scale γ1/2 [5]. From this point of view, the smaller γ, the closer to θ∗ we get and this justifies a known variance reduction technique: decaying step-sizes. Another technique to reduce the variance is to resort to averaging. Once again, to explain this, ergodic theorems for Markov chains [4] give us an important insight: the time-mean θˉt=t+11∑i=0tθi converges to θˉγ=Eπγ[θ] almost surely. Furthermore, some magic happens with the quadratic case, as we can show that θˉγ=θ∗, which implies, as a direct application of the strong law of large numbers, that θˉt converges almost surely to θ∗, and proves that averaging the iterates is relevant [6].
Here is a series of plots corresponding to different step-sizes γ showing the dynamics of plain SGD (blue), averaged SGD (red) and step-sizes decay at 1/t rate (orange) in an underparametrised setting (n=100, d=2). Plain SGD is always faster that the other two methods but the larger the γ the larger the variance at optimum is.
Note also that the same analysis can be done in the convex, yet nonquadratic case, indeed, in [7] the authors showed that the order of magnitude of the distance between θˉγ and θ∗ is γ2.
The overparametrised case. We have seen in the previous section that both the scale of noise and the geometry are affected in this case. The effect of the geometry is a little bit difficult to handle at a general level and we will detail its role in specific examples (see below and in the next blog post to come). Concerning the scale, we have seen that the noise vanishes at a global optimum and that the closer to a global optimum, the smaller the intensity of the noise is. Hence, global optima are fixed points of the dynamics, and under technical assumptions (e.g. local attractiveness), it can be shown the dynamics eventually converge to one of them [8, 9]. In this case, the nature of the dynamics is very different from the previous one: the distribution of the iterates (θt)t⩾0 converges to δθ∗ where θ∗ is a random variable in the set of interpolators! Remarkably, no variance reduction technique are required for convergence towards a global minimum!
The stochastic gradient flow and the least-squares example
Continuous time model of SGD. Continuous time counterparts of (discrete) numerical optimisation methods and Markov chains are well-worn subjects in applied mathematics and have found applications in machine learning in particular. Indeed, in recent years, gradient descent has been actively studied through gradient flows, which have led to convergence results for neural networks discussed in previous blogposts. However, due to its stochasticity, SGD cannot be properly modelled as a deterministic flow. An interesting and natural approach is to consider stochastic differential equations (SDEs)
dθt=b(t,θt)dt+σ(t,θt)dBt,
where (Bt)t⩾0 is a standard Brownian motion [10]. In order to accurately represent the SGD dynamics, the drift term b and the noise σ should meet the following requirements:
(i) The drift term b should match the negative full gradient: b=−∇Rn.
(ii) The noise covariance σσ⊤(t,θ) should match γE[εt(θt)εt(θt)⊤∣θt=θ].
(iii) The noise at state θ should belong to span{∇θh(θ,x1),…,∇θh(θ,xn)}.
f
Points (i) and (ii) imply that the SGD recursion corresponds in fact to the Euler-Maruyama discretisation with step-size γ of the SDE [11]. Hence, the SDE and the discrete models match perfectly for infinitesimal step-sizes (up to first order terms in γ) and the model is said to be consistent. Point (iii) states a geometrical property of the SGD noise and is of crucial importance for a proper modelisation.
The least-squares model. Consider once again our favourite least-squares model
Rn(θ)=2n1i=1∑n(⟨θ,xi⟩–yi)2,
and let X:=[x1,…,xn]⊤∈Rn×d be the data matrix and y=[y1,…,yn]⊤∈Rn the vector of outputs. Let us explicitly write the SDE model in this case: the drift corresponds to the gradient b(θ)=−n1X⊤(Xθ–y) and it is simple linear algebra to calculate the covariance of the noise (see also [19, Remark 4])
To simplify a bit this analysis let us neglect the residual term of order 1/n2 and assume that the residuals ⟨θ,xi⟩–yi, are approximately equal across all the samples of the data set: diag[(⟨θ,xi⟩–yi)2]i=1n≃n1∥Xθ–y∥2In. Then the SDE model writes:
dθt=–n1X⊤(Xθt–y)dt+nγ∥Xθt−y∥X⊤dBt.
As stressed before, notice that there is an important difference between the under and overparametrised regimes: in the former, the noise spans all Rd and as there exists σ>0 such that ∥Xθt−y∥⩾σ, the noise is non degenerate in every direction. Instead, in the overparametrised regime, the noise vanishes at a global optimum θ∗ and is degenerate in the directions of [range(X⊤)]⊥=Ker(X). As also noticed in [19], this will result in important differences in term of the processes’ behaviour! In the rest of the post, let us denote θLS:=X†y, where X† is the pseudo inverse of X. It corresponds to the ordinary least-square estimator in the underparametrised regime and the projection of 0 into Ker(X) in the overparametrised setting.
The underparametrised regime and the multivariate Ornstein–Uhlenbeck process. As written before, let us consider a model where the residuals scale like the best possible fit, i.e. n1∥Xθt−y∥≃σ. The dynamics reads:
dθt=–n1X⊤(Xθt–y)dt+nγσX⊤dBt.
This can be viewed as a multivariate Ornstein–Uhlenbeck process for which we can show the following convergence result: the law πt of θt converges to πγ, a Gaussian distribution of mean θLS and covariance 2γσ2Id:
t→∞limπt=N(θLS,2γσ2Id).
From this, as for the Markov chain interpretation, we can nicely interpret the need to decrease step-sizes to 0 in order to concentrate to θLS. Similarly, note that the ergodic theorem gives you automatically that the time average of θt goes to θLS as
t→∞limt1∫0tθsds=E[πγ]=θLS,almost surely.
Besides its nice interpretation as a Ornstein-Uhlenbeck process, we illustrate below the predictive power of the SDE model to understand the SGD dynamics.
Left: 10 realisations of SGD in blue-like colours (corresponding to 10 different random sampling sequences), next to 10 realisations of the SDE model in red-like colours (corresponding to 10 different realisations of the Brownian motion).
Right: A histogram of snap-shots of the SGD dynamics (marginal along the first coordinate) at increasing time intervals that show the convergence (by ergodicity) to the predicted Gaussian N(θLS,γσ2/2) !
The overparametrised regime and the geometric Brownian motion. We take back our SDE model, initialise it at θ0=0, but now we are extra careful that the noise cancels at any interpolator θ∗. We will show that despite being a random process, the dynamics with constant γ>0 converges almost surely to θLS. To do so let us consider the evolution of the squared difference ηt=∥θt–θLS∥2. Thanks to Itô calculus (chain rule for stochastic processes),
where (Wt)t⩾0 is a one dimensional Brownian motion (see details of this calculation at the end of the post). To simplify the calculation set the step-size such that γtr(n−1X⊤X)=1 (note that we retrieve here the usual step-size for least-squares SGD [16]). Noting μt=ηt2Rn(θt) and νt=ηt4γRn(θt), the dynamics of (ηt)t⩾0 follows
dηt=−μtηtdt+νtηtdWt,
Furthermore, as θt–θLS∈span(X⊤) , we have μ⋅ηt⩽2Rn(θt)⩽L⋅ηt, where μ and L stand respectively for the minimum and maximum eigenvalues of n1XX⊤. Hence, the behaviour of (ηt)t⩾0 is approximately the same as that of the solution to the following SDE:
dSt=−μStdt+νStdWt,
for ν≃4Lγ. That is, the dynamics of (∥θt–θLS∥2)t⩾0 is approximately the one of a Geometric Brownian Motion (GBO), a.k.a. the process underlying the Black-Scholes model! Hence, to understand SGD in the overparametrised regime, one “simply” need to study the properties of the GBO. It is known that in fact there is an explicit solution of this SDE
St=S0exp(−(μ+2ν2)t+νWt).
Hence, for all t>0, St is distributed according to a log-normal random variable with mean E(St)=S0e−μt and variance var(St)=S02e−2μt(eν2t–1). Furthermore, it is a nice application to the law of the iterated logarithm for the Brownian motion to show that
t→∞limSt=0,almost surely!
The almost sure convergence of (θt)t⩾0 to θLS is in stark contrast with the limiting behaviour we have seen in the underparametrised setting. Remarkably, the process does not require any variance reduction to converge to a specific point. Note also that among all the possible interpolators the process could have converged to, the process in fact goes almost surely to θLS. This selection of the minimum norm interpolator [12], known as the implicit bias of the process will be the core of a next blog post (for more complex architectures).
As in the underparametrised case, below are two plots showing the typical behaviour of the SGD dynamics as a GBO.
Left: 10 realisations of SGD in blue-like colours (corresponding to 10 different random sampling sequences), next to 10 realisations of the SDE model in red-like colours (corresponding to 10 different realisations of the Brownian motion).
Right: The exponential convergence of ηt=∥θt–θLS∥2 to 0 for the 10 realisations of SGD and the SDE (in log-scale).
Conclusion
In this blog post, I have tried to show that despite the old legacy inherited from the stochastic approximation literature, the particularities of the noise of the stochastic gradient descent in machine learning contexts necessitate a fresh look. This specificity strikes the most in modern overparametrised models as the noise can be largely degenerate, both (i) in direction (which I called noise geometry), and (ii) in scale, as its variance is dominated by the loss and hence vanishes at a global optimum. In fact I have tried to show that SDE models can be a relevant framework to shed light on typical behaviours of stochastic learning dynamics: for overparametrised architectures, they differentiate from the overdamped Langevin diffusion model, and act as multiplicative noise dynamics like the geometric brownian motion.
Besides, as it is believed that stochasticity plays a role in the generalisation performances of optimisation algorithms [13], there is a need to consider its action precisely. Yet, grasping its overall nature appears to be particularly hard as it mixes properties from the model’s architecture, the data’s distribution and the loss. In a following post, I will try to analyse some simple non-convex models where we can show that the SGD-noise drives the dynamics to possibly “good for generalisation” regions [17,18].
Acknowledgements. I would like to thank Scott Pesme and Etienne Boursier for fruitful discussions, and especially Nicolas Le Hir for proofreading this blog post and making good clarifying suggestions.