skip to content
← Scientific Spaces English

Making Model Training More Scientific, Part 5: Fine-Tuning Learning Rates Based on Gradients

In the previous four articles, we explored a series of convergence results for SGD ranging from bounded to unbounded domains, and from average loss to last-iterate loss. Some readers might feel: after all is said and done, it is still just SGD—aren't these results from the "ancient era"? Far from it! For instance, the core identity that the fourth article, Making Model Training More Scientific, Part 4: A New Identity, a New Learning Rate, relies on dates back no further than 2023; the results of the third article, Making Model Training More Scientific, Part 3: Convergence of the Final Loss in SGD, are only slightly earlier, dating back to 2020.

Also in the fourth article, we derived the common practical learning rate schedule "linear decay," demonstrating that this series of theoretical derivations is not merely "armchair theory," but can provide effective guidance for practice. Next, we will discuss finer-grained learning rate schedules based on gradients, which help us understand the principles of learning rate scheduling and also serve as the foundation for various adaptive learning rate optimizers.

The Very Starting Point #

If we carefully review the previous proofs, we will find that the starting point of this entire series of results is an inconspicuous identity:

The reason it is called "inconspicuous" is that it is utterly ordinary—any passing high school student could understand and prove it. Yet it is precisely such a simple identity that underpins a whole series of results in stochastic optimization, leading one to marvel at how the greatest truth is often the simplest, returning to pure fundamentals.

To understand this identity, we need to pay attention to two aspects of "arbitrariness." First, is arbitrary. In most cases, we set it to the theoretical optimal solution to obtain the most valuable results, but this does not change the arbitrariness of itself. We already know that in both Making Model Training More Scientific, Part 3: Convergence of the Final Loss in SGD and Making Model Training More Scientific, Part 4: A New Identity, a New Learning Rate, one of the key steps in the derivations was substituting and .

Second, is arbitrary. At first glance, this might seem a bit surprising, since the previous articles all assumed by default that is the gradient of the loss function, , but in fact, the only purpose of setting it to the gradient is to leverage 's convexity to obtain

thereby connecting the identity to the loss function. If we do not need this connection, or if we have another way to establish it, then setting it to the gradient is not strictly necessary. This arbitrariness is crucial: it allows us to construct more complex update rules and is also key to extending our results to non-SGD optimizers later on.

Classical Results #

Slightly rearranging Eq. yields

Next, the "old-school" approach is to first divide both sides by , and then sum over from . The advantage of this is that the left-hand side can directly establish an inequality relation with the loss via convexity; the disadvantage is that it requires introducing assumptions of a bounded domain and a non-increasing learning rate in order to bound the right-hand side relatively easily. So far, we have only used the "old-school" method in Part 1, Making Model Training More Scientific, Part 1: Convergence of the Average Loss in SGD, with the final result being

This time, we explicitly retain , rather than assuming for further simplification. Since we assumed a non-increasing learning rate, we have

Letting , the condition for equality is . That is to say, for inequality , the right-hand side at the constant learning rate attains its minimum, which is the learning rate that yields the fastest convergence. However, this result violates causality: at time step , we cannot "foresee" the gradient norms of subsequent steps. One way to modify it into a causal form is

After this modification, we need to reprove that

Substituting this back into inequality yields

The result exceeds the ideal by only 50%, which is already quite impressive; crucially, it does not violate causality, making it a practically viable learning rate schedule. It is also the prototype of the AdaGrad optimizer. Applying Eq. element-wise to each component, we obtain standard AdaGrad, which we will return to discuss later.

Be Careful with Expectations #

Starting from Part 2, Making Model Training More Scientific, Part 2: Extending the Results to Unbounded Domains, we adopted a "new-school" approach, which directly sums Eq. over both sides:

Clearly, the advantage of the "new-school" approach is that it assumes neither monotonicity of the learning rate nor a bounded domain; the terms on the right-hand side cancel out naturally, at the cost of the left-hand sum acquiring an extra weight . This weight is not a major issue for SGD, but for adaptive learning rate optimizers it is almost fatal. Thus, although the new-school approach has many subtle merits, almost none of them can be extended to adaptive learning rate optimizers; when the time comes, we will still have no choice but to use the "old-school" method.

Without digressing too far, let us return to the analysis of SGD. Applying convexity to the left-hand side of the above equation yields

In the first three articles, the step immediately following the above equation was taking the mathematical expectation on both sides, but here we must be very careful! This is because the expectation is taken over all , whereas in the first three articles, we always assumed that the learning rate is independent of the data, so that holds, thereby establishing the connection between the left-hand side and the target loss. However, this article considers gradient-dependent learning rates, and cannot simply be factored out.

A relatively simple remedy is to assume that depends at most on ; in this case, the expectation with respect to can be evaluated separately, for instance, , but this is somewhat too restrictive, so we simply still assume that is independent of the data. Does this make it impossible to adjust the learning rate based on gradients? Not at all; rather, we can only achieve adjustments based on the root-mean-square gradient norm , namely

Still the Old Result #

With inequality , subsequent derivations are fairly straightforward. We will extend the conclusions from the second, third, and fourth articles one by one to see what new insights they offer for learning rate adjustment. The second article, Making Model Training More Scientific, Part 2: Extending the Results to Unbounded Domains, has two conclusions. The first conclusion assumes non-increasing learning rates, where on the left-hand side is uniformly replaced by , and substituting yields

where . Since the learning rate is non-increasing, it is easy to prove that the right-hand side also attains its minimum under a constant learning rate, in which case

Of course, this result also violates causality, so we can only modify it to

However, in the denominator is also impractical, as it implies running multiple repetitions to compute the average. We directly drop as an approximation to the formula above, or equivalently assume that has very small variance, so a single sample is accurate enough; for all subsequent results, we adopt this operation by default. In short, after removing the expectation , the result is essentially the same as , offering no new insights for now.

Inversely Proportional to the Gradient #

Making Model Training More Scientific, Part 2: Extending the Results to Unbounded Domains's second conclusion is an inequality in weighted average form:

Using the Cauchy–Schwarz inequality, we get

The equality condition is , that is, . Substituting the above into the right-hand side of , we have

The overall condition for equality is finally , where . The most distinctive aspect of this result is that it tells us the learning rate should be inversely proportional to the squared gradient norm. This can be used to explain the necessity of warmup, because the gradient norm is relatively large in the early stages of training, decreases later, and remains approximately constant for a fairly long time. Modifying it into a form that respects causality gives

After the modification, we also need to reprove it:

Substituting these two results into yields

The core difference from the optimal solution is the presence of an extra logarithmically growing factor , which is a common phenomenon when transitioning from a static learning rate to a dynamic one.

Focusing on the Present #

Starting from the third post, Making Model Training More Scientific, Part 3: Convergence of the Final Loss in SGD, our conclusions began addressing last-iterate loss convergence. Extending the core conclusion of that post to dynamic gradients, the result is

Since this result also requires the learning rate to be non-increasing, it is easy to prove that the minimum value on the right-hand side is , which is attained at the constant learning rate:

This learning rate is likewise very intriguing. It shares a denominator similar to that of the "mini-version" AdaGrad learning rate , where in both cases takes the form of a sum of squared gradients; the difference here is that the gradient at step is weighted by , which means paying more attention to gradients close to the current step. This is a new characteristic brought about by shifting from average loss to last-iterate loss, and it is already remarkably close to the practice in RMSProp, Adam, etc., of updating second moments via EMA.

Intuitively, the causal version would be , but this is not sufficiently accurate; the correct version should be , where is 's running maximum. Taking the envelope is necessary: itself is not necessarily non-decreasing, whereas Equation requires the learning rate to be non-increasing; is automatically non-decreasing, thereby ensuring this property. Substituting into Equation 's right-hand side yields

The first inequality uses (i.e., ), and the second inequality uses . Compared to the optimal solution, it likewise picks up an extra logarithmically growing factor . In particular, when itself is non-decreasing (for example, when is non-decreasing), , and the result is completely identical to the original formula.

The Grand Synthesis #

In Making Model Training More Scientific, Part 4: A New Identity, a New Learning Rate, we obtained our strongest last-iterate loss convergence result to date. Extending it to dynamic gradients yields the "grand synthesis" of Eq. and Eq. :

This result looks simple, but in reality it is very complex; at least, we cannot intuitively see under which learning rate schedule the minimum of the right-hand side is attained. However, in the previous article I provided an approach: solving it via the calculus of variations after a continuum approximation. Here we can try it again: let , then for , we have . Consistently using to approximate and approximating sums by integrals, the right-hand side of the above equation is approximated as (substituting )

By definition, . Fixing as well, the integral part becomes a variational problem with fixed boundaries. The Euler–Lagrange equation gives , that is, . Integrating both sides and combining this with yields . Substituting this back into Eq. gives , where the minimum is attained at . Finally, according to the approximation , we ultimately obtain

Restoring discretization, we can guess that the optimal learning rate roughly takes the form of

, where is the previously defined . In fact, this "guess" is indeed the correct answer! However, verifying it by substituting it back into Eq. is extremely cumbersome, because the denominator of Eq. is rather than , so there is no guarantee that is bounded. Establishing bounds is exceptionally difficult; I tried for a week to no avail. Here, we will not pursue it further, but will instead wait until the next article to prove it through a more ingenious construction.

Now let us appreciate Equation . Notice that is precisely the optimal learning rate of , characterized by being proportional to , which, as mentioned earlier, can be used to explain warmup; Equation multiplies this by an additional factor of , which is monotonically decreasing; under suitable assumptions it decays linearly, so this term can be used to explain decay. Therefore, Equation corresponds precisely to the classic "warmup-decay-type" learning rate schedule.

Even more interestingly, the optimal learning rate of the previous section's told us to pay more attention to the gradient at the current step, yet Equation is even more extreme: it depends only on the gradients of the current and future steps, completely independent of past gradients. This undoubtedly violates causality. How can we modify it to respect causality? can be replaced with , and as for , its can be written as , and then we can consider replacing with , thus obtaining

This appears very reasonable, but substituting it back into Equation to prove that it is genuinely effective is no easy task either.

Summary #

Starting with this article, we consider gradient-based learning rate schedules, which help us understand the principles behind learning rate strategies such as warmup and decay, and also provide a valuable reference for various adaptive learning rate optimizers.