Practice / Initialisation and optimisers

Momentum, RMSProp and Adam: optimiser updates by hand

Ten problems on optimiser update rules: gradient descent on a quadratic and the 2/λ_max learning-rate limit, momentum unrolled as a weighted sum of past gradients and its 1/(1 − β) step, the EMA form, Nesterov, AdaGrad and RMSProp, Adam's bias correction and its first step, two Adam steps by hand, and L2 penalty versus AdamW's decoupled weight decay, with worked solutions and the mistakes that change the step size without anyone noticing.

Before you start

An optimiser turns a sequence of gradients into a sequence of parameter updates, and every popular one is a short recurrence that can be run by hand. Running it by hand is how you find out what the step size really is. These ten problems start with plain gradient descent on a quadratic, where the learning-rate limit comes from the Hessian's largest eigenvalue, then unroll momentum into a weighted sum of past gradients, compare its two common forms and Nesterov's variant, follow AdaGrad and RMSProp under a constant gradient, derive Adam's bias correction, take two Adam steps with numbers, and separate an L2 penalty from AdamW's weight decay. The five mistakes at the end each change the effective step size by a constant factor: a stability bound from the wrong eigenvalue, a momentum step taken to be the plain step, a learning rate carried between momentum conventions, an Adam step without bias correction, and an L2 penalty read as weight decay.

  • The parameters are θ∈Rd\theta \in \mathbb{R}^d and the loss is L(θ)L(\theta). θ0\theta_0 is the starting point and θt\theta_t the parameters after step t=1,2,…t = 1, 2, \dots. The gradient used at step tt is gt=∇L(θt−1)g_t = \nabla L(\theta_{t-1}), taken at the parameters before the step. η>0\eta > 0 is the learning rate.
  • Gradient descent: θt=θt−1−ηgt\theta_t = \theta_{t-1} - \eta g_t.
  • Momentum (the "classic" or summed form): v0=0v_0 = 0, vt=βvt−1+gtv_t = \beta v_{t-1} + g_t, θt=θt−1−ηvt\theta_t = \theta_{t-1} - \eta v_t, with 0≤β<10 \le \beta < 1. This is PyTorch's torch.optim.SGD with momentum=β\beta and the default dampening=0. The EMA form is m0=0m_0 = 0, mt=βmt−1+(1−β)gtm_t = \beta m_{t-1} + (1 - \beta) g_t, θt=θt−1−η′mt\theta_t = \theta_{t-1} - \eta' m_t.
  • Nesterov momentum, in the form PyTorch uses with nesterov=True: vtv_t as above, and θt=θt−1−η (gt+βvt)\theta_t = \theta_{t-1} - \eta\,(g_t + \beta v_t).
  • AdaGrad: G0=0G_0 = 0, Gt=Gt−1+gt2G_t = G_{t-1} + g_t^2, θt=θt−1−η gt/(Gt+ϵ)\theta_t = \theta_{t-1} - \eta\, g_t/(\sqrt{G_t} + \epsilon). RMSProp: s0=0s_0 = 0, st=ρst−1+(1−ρ)gt2s_t = \rho s_{t-1} + (1 - \rho) g_t^2, θt=θt−1−η gt/(st+ϵ)\theta_t = \theta_{t-1} - \eta\, g_t/(\sqrt{s_t} + \epsilon), with 0<ρ<10 < \rho < 1.
  • Adam: m0=v0=0m_0 = v_0 = 0, mt=β1mt−1+(1−β1)gtm_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t, vt=β2vt−1+(1−β2)gt2v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2, the bias-corrected estimates m^t=mt/(1−β1t)\hat m_t = m_t/(1 - \beta_1^t) and v^t=vt/(1−β2t)\hat v_t = v_t/(1 - \beta_2^t), and θt=θt−1−η m^t/(v^t+ϵ)\theta_t = \theta_{t-1} - \eta\,\hat m_t/(\sqrt{\hat v_t} + \epsilon). PyTorch's defaults are η=10−3\eta = 10^{-3}, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ϵ=10−8\epsilon = 10^{-8}. Here β1t\beta_1^t is β1\beta_1 to the power tt; vtv_t in Adam is its second-moment estimate, not momentum's vtv_t, and each problem says which one it uses.
  • In the adaptive methods, gt2g_t^2, ⋅\sqrt{\cdot}, ∣⋅∣|\cdot| and the division act entry by entry, and ϵ>0\epsilon > 0 is a small constant that keeps the denominator away from 00.
  • A quadratic loss is L(θ)=12θ⊤Aθ−b⊤θL(\theta) = \tfrac12\theta^\top A\theta - b^\top\theta with AA symmetric positive definite, so ∇L(θ)=Aθ−b\nabla L(\theta) = A\theta - b and the Hessian is AA. Its eigenvalues are 0<λmin⁡≤⋯≤λmax⁡0 < \lambda_{\min} \le \dots \le \lambda_{\max}, and θ∗=A−1b\theta^* = A^{-1}b is the minimiser.

Builds on: Regression gradients: linear, logistic and softmax

Problems

  1. ·

    Gradient descent on the quadratic L(θ)=12θ⊤Aθ−b⊤θL(\theta) = \tfrac12\theta^\top A\theta - b^\top\theta. Let et=θt−θ∗e_t = \theta_t - \theta^* be the error after step tt. Show that et=(I−ηA) et−1e_t = (I - \eta A)\,e_{t-1} and hence write ete_t in terms of e0e_0.

  2. ··

    Show that gradient descent on the quadratic converges to θ∗\theta^* from every starting point exactly when 0<η<2/λmax⁡0 < \eta < 2/\lambda_{\max}. For A=(3113)A = \begin{pmatrix} 3 & 1 \\ 1 & 3\end{pmatrix}, find that range, and find the η\eta that makes the worst-case error factor per step as small as possible.

  3. ···

    Momentum, classic form, with any gradients g1,g2,…g_1, g_2, \dots treated as given. Show that vt=∑s=1tβt−sgsv_t = \sum_{s=1}^{t}\beta^{t-s}g_s and that θt=θ0−η∑s=1t1−βt−s+11−β gs\theta_t = \theta_0 - \eta\sum_{s=1}^{t}\dfrac{1 - \beta^{t-s+1}}{1 - \beta}\,g_s.

  4. ·

    Classic momentum with a constant gradient, gt=gg_t = g for every tt. Find vtv_t and the size of the step as t→∞t \to \infty. How large is it for β=0.9\beta = 0.9 compared with plain gradient descent at the same η\eta?

  5. ··

    The EMA form of momentum is mt=βmt−1+(1−β)gtm_t = \beta m_{t-1} + (1 - \beta) g_t, θt=θt−1−η′mt\theta_t = \theta_{t-1} - \eta' m_t, with m0=0m_0 = 0. Show that mt=(1−β)vtm_t = (1 - \beta)v_t for the classic vtv_t fed the same gradients, and find the η\eta for which the classic form produces exactly the same parameters as the EMA form with learning rate η′\eta'.

  6. ··

    One parameter, L(θ)=θ2L(\theta) = \theta^2, θ0=1\theta_0 = 1, η=0.1\eta = 0.1, β=0.9\beta = 0.9. Compute θ1\theta_1 and θ2\theta_2 with classic momentum and with Nesterov momentum in the form θt=θt−1−η (gt+βvt)\theta_t = \theta_{t-1} - \eta\,(g_t + \beta v_t).

  7. ··

    One parameter with a constant gradient g≠0g \neq 0, and ϵ=0\epsilon = 0. Find the step size ∣θt−θt−1∣|\theta_t - \theta_{t-1}| of AdaGrad and of RMSProp as functions of tt. What happens to each as t→∞t \to \infty, and what is RMSProp's first step for ρ=0.99\rho = 0.99?

  8. ··

    Adam. Show that mt=(1−β1)∑s=1tβ1t−sgsm_t = (1 - \beta_1)\sum_{s=1}^{t}\beta_1^{t-s}g_s, whose weights sum to 1−β1t1 - \beta_1^t, so that a constant gradient gg gives m^t=g\hat m_t = g and v^t=g2\hat v_t = g^2 exactly. Then show that, whatever the gradients, Adam's first step is θ1−θ0=−η g1/(∣g1∣+ϵ)\theta_1 - \theta_0 = -\eta\,g_1/(|g_1| + \epsilon), entry by entry.

  9. ···

    One parameter, L(θ)=θ2L(\theta) = \theta^2, θ0=1\theta_0 = 1, Adam with η=0.1\eta = 0.1, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ=0\epsilon = 0. Compute θ1\theta_1 and θ2\theta_2, rounding to four decimal places at the end.

  10. ···

    Two parameters, θ0=(1,1)\theta_0 = (1, 1), first gradient of the data loss g1=(0,4)g_1 = (0, 4), η=10−3\eta = 10^{-3}, λ=10−2\lambda = 10^{-2}, ϵ=10−8\epsilon = 10^{-8}. Compute θ1\theta_1 for (a) Adam with an L2 penalty, which replaces gtg_t by gt+λθt−1g_t + \lambda\theta_{t-1} before the moment updates (PyTorch's Adam with weight_decay=λ\lambda), and (b) AdamW, which sets θt=(1−ηλ) θt−1−η m^t/(v^t+ϵ)\theta_t = (1 - \eta\lambda)\,\theta_{t-1} - \eta\,\hat m_t/(\sqrt{\hat v_t} + \epsilon) with the moments built from gtg_t alone (PyTorch's AdamW). Write the decay part of the step in each.

Worked solutions

Problem 1

Gradient descent on the quadratic L(θ)=12θ⊤Aθ−b⊤θL(\theta) = \tfrac12\theta^\top A\theta - b^\top\theta. Let et=θt−θ∗e_t = \theta_t - \theta^* be the error after step tt. Show that et=(I−ηA) et−1e_t = (I - \eta A)\,e_{t-1} and hence write ete_t in terms of e0e_0.

  1. ∇L(θ∗)=Aθ∗−b=0\nabla L(\theta^*) = A\theta^* - b = 0, so b=Aθ∗b = A\theta^*.θ∗=A−1b\theta^* = A^{-1}b is where the gradient vanishes; writing bb this way lets the gradient be expressed through the error.
  2. gt=Aθt−1−b=Aθt−1−Aθ∗=Aet−1g_t = A\theta_{t-1} - b = A\theta_{t-1} - A\theta^* = A e_{t-1}.The gradient of the quadratic is Aθ−bA\theta - b; substitute step 1 and factor out AA.
  3. et=θt−1−ηgt−θ∗=et−1−ηAet−1=(I−ηA) et−1e_t = \theta_{t-1} - \eta g_t - \theta^* = e_{t-1} - \eta A e_{t-1} = (I - \eta A)\,e_{t-1}.Subtract θ∗\theta^* from both sides of the update and use step 2.
  4. et=(I−ηA)t e0e_t = (I - \eta A)^t\,e_0Step 3 applied tt times. On a quadratic, gradient descent is a fixed linear map applied to the error over and over, so whether it converges depends only on the eigenvalues of I−ηAI - \eta A (Problem 2).

Problem 2

Show that gradient descent on the quadratic converges to θ∗\theta^* from every starting point exactly when 0<η<2/λmax⁡0 < \eta < 2/\lambda_{\max}. For A=(3113)A = \begin{pmatrix} 3 & 1 \\ 1 & 3\end{pmatrix}, find that range, and find the η\eta that makes the worst-case error factor per step as small as possible.

  1. A=QΛQ⊤A = Q\Lambda Q^\top with QQ orthogonal and Λ=diag⁡(λ1,…,λd)\Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_d), so I−ηA=Q(I−ηΛ)Q⊤I - \eta A = Q(I - \eta\Lambda)Q^\top.A symmetric matrix has an orthonormal basis of eigenvectors, and I=QQ⊤I = QQ^\top, so II and AA are diagonal in the same basis.
  2. With ct=Q⊤etc_t = Q^\top e_t, ct,i=(1−ηλi)t c0,ic_{t,i} = (1 - \eta\lambda_i)^t\,c_{0,i}.Problem 1 in the eigenvector basis: each coordinate is multiplied by its own factor 1−ηλi1 - \eta\lambda_i at every step, independently of the others.
  3. et→0e_t \to 0 for every e0e_0 iff ∣1−ηλi∣<1|1 - \eta\lambda_i| < 1 for every ii, iff 0<ηλi<20 < \eta\lambda_i < 2 for every ii.∥et∥=∥ct∥\|e_t\| = \|c_t\| because QQ is orthogonal. A start along eigenvector ii has only c0,i≠0c_{0,i} \neq 0, and rt→0r^t \to 0 exactly when ∣r∣<1|r| < 1.
  4. Every λi>0\lambda_i > 0, so the condition is 0<η<2/λmax⁡0 < \eta < 2/\lambda_{\max}.The largest eigenvalue gives the tightest upper bound; the others are then satisfied automatically.
  5. det⁡(A−λI)=(3−λ)2−1=0\det(A - \lambda I) = (3 - \lambda)^2 - 1 = 0 gives λ=2\lambda = 2 and λ=4\lambda = 4.3−λ=±13 - \lambda = \pm 1.
  6. The worst factor is max⁡(∣1−2η∣,∣1−4η∣)\max(|1 - 2\eta|, |1 - 4\eta|); it is smallest where 1−2η=−(1−4η)1 - 2\eta = -(1 - 4\eta), at η=13\eta = \tfrac13, giving 13\tfrac13.As η\eta grows, ∣1−2η∣|1 - 2\eta| falls while ∣1−4η∣|1 - 4\eta| rises once η>14\eta > \tfrac14; the maximum of the two is least where they cross. In general the crossing is at η=2/(λmin⁡+λmax⁡)\eta = 2/(\lambda_{\min} + \lambda_{\max}), with factor (λmax⁡−λmin⁡)/(λmax⁡+λmin⁡)(\lambda_{\max} - \lambda_{\min})/(\lambda_{\max} + \lambda_{\min}).
  7. Converges from every start iff 0<η<2/λmax⁡0 < \eta < 2/\lambda_{\max}; for this AA, 0<η<120 < \eta < \tfrac12, and η=13\eta = \tfrac13 gives the smallest worst-case factor, 13\tfrac13 per stepThe steep direction (λmax⁡\lambda_{\max}) caps the learning rate and the flat direction (λmin⁡\lambda_{\min}) then sets the speed; the ratio λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min}, the condition number, is what momentum and the adaptive methods below try to work around.

Problem 3

Momentum, classic form, with any gradients g1,g2,…g_1, g_2, \dots treated as given. Show that vt=∑s=1tβt−sgsv_t = \sum_{s=1}^{t}\beta^{t-s}g_s and that θt=θ0−η∑s=1t1−βt−s+11−β gs\theta_t = \theta_0 - \eta\sum_{s=1}^{t}\dfrac{1 - \beta^{t-s+1}}{1 - \beta}\,g_s.

  1. v1=βv0+g1=g1v_1 = \beta v_0 + g_1 = g_1.v0=0v_0 = 0; this matches the claimed sum for t=1t = 1, which has the single term β0g1\beta^0 g_1.
  2. If vt−1=∑s=1t−1βt−1−sgsv_{t-1} = \sum_{s=1}^{t-1}\beta^{t-1-s}g_s, then vt=∑s=1t−1βt−sgs+gt=∑s=1tβt−sgsv_t = \sum_{s=1}^{t-1}\beta^{t-s}g_s + g_t = \sum_{s=1}^{t}\beta^{t-s}g_s.Multiplying by β\beta raises every exponent by one, and the new gradient enters with weight β0=1\beta^0 = 1. Induction on tt completes the first claim.
  3. θt=θ0−η∑k=1tvk=θ0−η∑k=1t∑s=1kβk−sgs\theta_t = \theta_0 - \eta\sum_{k=1}^{t}v_k = \theta_0 - \eta\sum_{k=1}^{t}\sum_{s=1}^{k}\beta^{k-s}g_s.Each step subtracts ηvk\eta v_k, so after tt steps the parameters have moved by the sum of all of them.
  4. ∑k=1t∑s=1kβk−sgs=∑s=1tgs∑k=stβk−s\sum_{k=1}^{t}\sum_{s=1}^{k}\beta^{k-s}g_s = \sum_{s=1}^{t}g_s\sum_{k=s}^{t}\beta^{k-s}.Swap the order of summation: the pairs with 1≤s≤k≤t1 \le s \le k \le t can be listed by ss first, with kk running from ss to tt.
  5. ∑k=stβk−s=∑j=0t−sβj=1−βt−s+11−β\sum_{k=s}^{t}\beta^{k-s} = \sum_{j=0}^{t-s}\beta^{j} = \dfrac{1 - \beta^{t-s+1}}{1 - \beta}.A geometric series with t−s+1t - s + 1 terms; β<1\beta < 1 so the denominator is not zero.
  6. vt=∑s=1tβt−sgsv_t = \sum_{s=1}^{t}\beta^{t-s}g_s and θt=θ0−η∑s=1t1−βt−s+11−β gs\theta_t = \theta_0 - \eta\sum_{s=1}^{t}\frac{1 - \beta^{t-s+1}}{1 - \beta}\,g_sStep 2 and steps 3 to 5. An old gradient's total effect on θ\theta approaches η/(1−β)\eta/(1 - \beta) times the gradient: every gradient is eventually applied 1/(1−β)1/(1 - \beta) times over, spread across the following steps.

Problem 4

Classic momentum with a constant gradient, gt=gg_t = g for every tt. Find vtv_t and the size of the step as t→∞t \to \infty. How large is it for β=0.9\beta = 0.9 compared with plain gradient descent at the same η\eta?

  1. vt=∑s=1tβt−sg=g∑j=0t−1βjv_t = \sum_{s=1}^{t}\beta^{t-s}g = g\sum_{j=0}^{t-1}\beta^{j}.Problem 3 with every gs=gg_s = g; substitute j=t−sj = t - s.
  2. vt=1−βt1−β gv_t = \dfrac{1 - \beta^t}{1 - \beta}\,g.A geometric series with tt terms.
  3. βt→0\beta^t \to 0, so ηvt→η1−β g\eta v_t \to \dfrac{\eta}{1 - \beta}\,g.0≤β<10 \le \beta < 1.
  4. vt=1−βt1−β gv_t = \frac{1 - \beta^t}{1 - \beta}\,g, and the step tends to η1−β g\frac{\eta}{1 - \beta}\,g: ten times the plain step ηg\eta g when β=0.9\beta = 0.9On a long stretch where the gradient barely changes, momentum moves 1/(1−β)1/(1 - \beta) times as far per step as gradient descent with the same learning rate. Where the gradient flips sign from step to step, the terms of step 1 alternate and partly cancel instead, which is what damps oscillation across a narrow valley.

Problem 5

The EMA form of momentum is mt=βmt−1+(1−β)gtm_t = \beta m_{t-1} + (1 - \beta) g_t, θt=θt−1−η′mt\theta_t = \theta_{t-1} - \eta' m_t, with m0=0m_0 = 0. Show that mt=(1−β)vtm_t = (1 - \beta)v_t for the classic vtv_t fed the same gradients, and find the η\eta for which the classic form produces exactly the same parameters as the EMA form with learning rate η′\eta'.

  1. m0=0=(1−β)v0m_0 = 0 = (1 - \beta)v_0.Both start at zero.
  2. If mt−1=(1−β)vt−1m_{t-1} = (1 - \beta)v_{t-1}, then mt=(1−β)βvt−1+(1−β)gt=(1−β)(βvt−1+gt)=(1−β)vtm_t = (1 - \beta)\beta v_{t-1} + (1 - \beta)g_t = (1 - \beta)(\beta v_{t-1} + g_t) = (1 - \beta)v_t.Substitute and factor out 1−β1 - \beta; the bracket is the classic recurrence. Induction gives the claim for every tt, as long as both are fed the same gtg_t.
  3. η′mt=η′(1−β)vt\eta' m_t = \eta'(1 - \beta)v_t, so the two updates agree when η=(1−β)η′\eta = (1 - \beta)\eta'.With the same starting point and the same steps, the two runs pass through the same parameters, so their gradients gtg_t are the same too and step 2 keeps applying.
  4. mt=(1−β)vtm_t = (1 - \beta)v_t; the classic form with η=(1−β)η′\eta = (1 - \beta)\eta' reproduces the EMA form exactlyWith β=0.9\beta = 0.9, an EMA-form learning rate of 0.10.1 is a classic-form learning rate of 0.010.01. Problem 4's factor 1/(1−β)1/(1 - \beta) lives in the classic form; the EMA form has already divided it out, since its weights (1−β)βt−s(1 - \beta)\beta^{t-s} sum to 1−βt≤11 - \beta^t \le 1.

Problem 6

One parameter, L(θ)=θ2L(\theta) = \theta^2, θ0=1\theta_0 = 1, η=0.1\eta = 0.1, β=0.9\beta = 0.9. Compute θ1\theta_1 and θ2\theta_2 with classic momentum and with Nesterov momentum in the form θt=θt−1−η (gt+βvt)\theta_t = \theta_{t-1} - \eta\,(g_t + \beta v_t).

  1. gt=2θt−1g_t = 2\theta_{t-1}.ddθθ2=2θ\tfrac{d}{d\theta}\theta^2 = 2\theta, evaluated before the step.
  2. Classic, step 1: g1=2g_1 = 2, v1=2v_1 = 2, θ1=1−0.1⋅2=0.8\theta_1 = 1 - 0.1 \cdot 2 = 0.8.v1=β⋅0+g1v_1 = \beta \cdot 0 + g_1.
  3. Classic, step 2: g2=1.6g_2 = 1.6, v2=0.9⋅2+1.6=3.4v_2 = 0.9 \cdot 2 + 1.6 = 3.4, θ2=0.8−0.34=0.46\theta_2 = 0.8 - 0.34 = 0.46.v2=βv1+g2v_2 = \beta v_1 + g_2, then θ2=θ1−ηv2\theta_2 = \theta_1 - \eta v_2.
  4. Nesterov, step 1: g1=2g_1 = 2, v1=2v_1 = 2, g1+βv1=3.8g_1 + \beta v_1 = 3.8, θ1=1−0.38=0.62\theta_1 = 1 - 0.38 = 0.62.The buffer is updated exactly as in the classic form; only the direction used for the step changes.
  5. Nesterov, step 2: g2=1.24g_2 = 1.24, v2=1.8+1.24=3.04v_2 = 1.8 + 1.24 = 3.04, g2+βv2=1.24+2.736=3.976g_2 + \beta v_2 = 1.24 + 2.736 = 3.976, θ2=0.62−0.3976=0.2224\theta_2 = 0.62 - 0.3976 = 0.2224.g2=2⋅0.62g_2 = 2 \cdot 0.62, then the same two lines as step 4.
  6. Classic: θ1=0.8\theta_1 = 0.8, θ2=0.46\theta_2 = 0.46; Nesterov: θ1=0.62\theta_1 = 0.62, θ2=0.2224\theta_2 = 0.2224Nesterov's direction is gt+βvt=(1+β)gt+β2vt−1g_t + \beta v_t = (1 + \beta)g_t + \beta^2 v_{t-1}: the newest gradient counts 1+β1 + \beta times and the older history is damped by one more factor of β\beta, so the step leans towards where momentum is about to carry the parameters. Under a constant gradient both forms tend to the same step, ηg/(1−β)\eta g/(1 - \beta), because g+βg/(1−β)=g/(1−β)g + \beta g/(1 - \beta) = g/(1 - \beta); they differ when the gradient changes.

Problem 7

One parameter with a constant gradient g≠0g \neq 0, and ϵ=0\epsilon = 0. Find the step size ∣θt−θt−1∣|\theta_t - \theta_{t-1}| of AdaGrad and of RMSProp as functions of tt. What happens to each as t→∞t \to \infty, and what is RMSProp's first step for ρ=0.99\rho = 0.99?

  1. AdaGrad: Gt=∑s=1tg2=t g2G_t = \sum_{s=1}^{t} g^2 = t\,g^2, so Gt=t ∣g∣\sqrt{G_t} = \sqrt t\,|g|.GG adds the squared gradient at every step and never forgets.
  2. AdaGrad step: η ∣g∣/(t ∣g∣)=η/t\eta\,|g|/(\sqrt t\,|g|) = \eta/\sqrt t.The gradient's size cancels; only the count of steps is left.
  3. RMSProp: st=(1−ρ)∑s=1tρt−sg2=(1−ρt) g2s_t = (1 - \rho)\sum_{s=1}^{t}\rho^{t-s}g^2 = (1 - \rho^t)\,g^2.Problem 3's unrolling with ρ\rho for β\beta and the extra factor 1−ρ1 - \rho, then the geometric sum ∑j=0t−1ρj=(1−ρt)/(1−ρ)\sum_{j=0}^{t-1}\rho^j = (1 - \rho^t)/(1 - \rho).
  4. RMSProp step: η ∣g∣/(1−ρt ∣g∣)=η/1−ρt\eta\,|g|/(\sqrt{1 - \rho^t}\,|g|) = \eta/\sqrt{1 - \rho^t}.st=1−ρt ∣g∣\sqrt{s_t} = \sqrt{1 - \rho^t}\,|g|.
  5. At t=1t = 1 with ρ=0.99\rho = 0.99: η/0.01=10η\eta/\sqrt{0.01} = 10\eta; as t→∞t \to \infty the step falls to η\eta.ρt→0\rho^t \to 0, and 1−ρt1 - \rho^t is smallest at t=1t = 1.
  6. AdaGrad: ∣Δθt∣=η/t→0|\Delta\theta_t| = \eta/\sqrt t \to 0; RMSProp: ∣Δθt∣=η/1−ρt→η|\Delta\theta_t| = \eta/\sqrt{1 - \rho^t} \to \eta, with a first step of 10η10\eta for ρ=0.99\rho = 0.99AdaGrad's sum grows without limit, so its steps keep shrinking even when the gradient does not; RMSProp's moving average forgets, so its step settles at η\eta. Neither depends on ∣g∣|g|: multiplying the loss by a constant leaves both updates unchanged when ϵ=0\epsilon = 0. RMSProp's oversized early steps come from s0=0s_0 = 0 biasing sts_t towards zero, which is the bias Adam corrects (Problem 8).

Problem 8

Adam. Show that mt=(1−β1)∑s=1tβ1t−sgsm_t = (1 - \beta_1)\sum_{s=1}^{t}\beta_1^{t-s}g_s, whose weights sum to 1−β1t1 - \beta_1^t, so that a constant gradient gg gives m^t=g\hat m_t = g and v^t=g2\hat v_t = g^2 exactly. Then show that, whatever the gradients, Adam's first step is θ1−θ0=−η g1/(∣g1∣+ϵ)\theta_1 - \theta_0 = -\eta\,g_1/(|g_1| + \epsilon), entry by entry.

  1. mt=(1−β1)∑s=1tβ1t−sgsm_t = (1 - \beta_1)\sum_{s=1}^{t}\beta_1^{t-s}g_s.Problem 5, step 2 shows the EMA recurrence is (1−β1)(1 - \beta_1) times the classic one, and Problem 3 unrolls the classic one.
  2. (1−β1)∑s=1tβ1t−s=(1−β1) 1−β1t1−β1=1−β1t(1 - \beta_1)\sum_{s=1}^{t}\beta_1^{t-s} = (1 - \beta_1)\,\dfrac{1 - \beta_1^t}{1 - \beta_1} = 1 - \beta_1^t.Geometric series with tt terms. The weights fall short of 11 by β1t\beta_1^t, the weight that m0=0m_0 = 0 would have carried.
  3. With gs=gg_s = g: mt=(1−β1t) gm_t = (1 - \beta_1^t)\,g, so m^t=mt/(1−β1t)=g\hat m_t = m_t/(1 - \beta_1^t) = g.Step 2 times gg. Dividing by the sum of the weights turns a sum weighted towards zero into a true weighted average.
  4. The same steps for vtv_t, with β2\beta_2 and g2g^2: vt=(1−β2t) g2v_t = (1 - \beta_2^t)\,g^2 and v^t=g2\hat v_t = g^2.vtv_t is the same recurrence applied to gt2g_t^2.
  5. At t=1t = 1 for any g1g_1: m1=(1−β1)g1m_1 = (1 - \beta_1)g_1 and v1=(1−β2)g12v_1 = (1 - \beta_2)g_1^2, so m^1=g1\hat m_1 = g_1 and v^1=g12\hat v_1 = g_1^2, v^1=∣g1∣\sqrt{\hat v_1} = |g_1|.One gradient is a constant sequence of length one, so step 3 applies; x2=∣x∣\sqrt{x^2} = |x| entry by entry.
  6. For a constant gradient, m^t=g\hat m_t = g and v^t=g2\hat v_t = g^2; for any gradients, θ1−θ0=−η g1/(∣g1∣+ϵ)\theta_1 - \theta_0 = -\eta\,g_1/(|g_1| + \epsilon)Each entry of the first step is −η-\eta times the sign of its gradient (slightly less when ∣g1∣|g_1| is comparable to ϵ\epsilon), whatever the gradient's size. Adam's learning rate is therefore close to the actual distance each parameter moves early in training, which is why η\eta does not need retuning when the loss is rescaled.

Problem 9

One parameter, L(θ)=θ2L(\theta) = \theta^2, θ0=1\theta_0 = 1, Adam with η=0.1\eta = 0.1, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ=0\epsilon = 0. Compute θ1\theta_1 and θ2\theta_2, rounding to four decimal places at the end.

  1. g1=2g_1 = 2, m1=0.1⋅2=0.2m_1 = 0.1 \cdot 2 = 0.2, v1=0.001⋅4=0.004v_1 = 0.001 \cdot 4 = 0.004.g=2θg = 2\theta at θ0=1\theta_0 = 1; both moments start at 00.
  2. m^1=0.2/0.1=2\hat m_1 = 0.2/0.1 = 2, v^1=0.004/0.001=4\hat v_1 = 0.004/0.001 = 4, step =0.1⋅2/4=0.1= 0.1 \cdot 2/\sqrt 4 = 0.1, so θ1=0.9\theta_1 = 0.9.1−β11=0.11 - \beta_1^1 = 0.1 and 1−β21=0.0011 - \beta_2^1 = 0.001; the first step is η\eta times the sign, as Problem 8 predicts.
  3. g2=1.8g_2 = 1.8, m2=0.9⋅0.2+0.1⋅1.8=0.36m_2 = 0.9 \cdot 0.2 + 0.1 \cdot 1.8 = 0.36, v2=0.999⋅0.004+0.001⋅3.24=0.007236v_2 = 0.999 \cdot 0.004 + 0.001 \cdot 3.24 = 0.007236.g=2⋅0.9g = 2 \cdot 0.9 and 1.82=3.241.8^2 = 3.24; then the two recurrences.
  4. m^2=0.36/0.19≈1.89474\hat m_2 = 0.36/0.19 \approx 1.89474, v^2=0.007236/0.001999≈3.61981\hat v_2 = 0.007236/0.001999 \approx 3.61981.1−0.92=0.191 - 0.9^2 = 0.19 and 1−0.9992=0.0019991 - 0.999^2 = 0.001999.
  5. Step =0.1⋅1.89474/3.61981≈0.1⋅1.89474/1.90258≈0.09959= 0.1 \cdot 1.89474/\sqrt{3.61981} \approx 0.1 \cdot 1.89474/1.90258 \approx 0.09959.3.61981≈1.90258\sqrt{3.61981} \approx 1.90258.
  6. θ1=0.9\theta_1 = 0.9 and θ2≈0.8004\theta_2 \approx 0.8004The second step is 0.09960.0996, again almost exactly η\eta, although the gradient fell by 10%10\%: early on m^t/v^t\hat m_t/\sqrt{\hat v_t} is a ratio of two averages of the same few gradients and stays near 11. Gradient descent with η=0.1\eta = 0.1 would have moved 0.20.2 and then 0.160.16.

Problem 10

Two parameters, θ0=(1,1)\theta_0 = (1, 1), first gradient of the data loss g1=(0,4)g_1 = (0, 4), η=10−3\eta = 10^{-3}, λ=10−2\lambda = 10^{-2}, ϵ=10−8\epsilon = 10^{-8}. Compute θ1\theta_1 for (a) Adam with an L2 penalty, which replaces gtg_t by gt+λθt−1g_t + \lambda\theta_{t-1} before the moment updates (PyTorch's Adam with weight_decay=λ\lambda), and (b) AdamW, which sets θt=(1−ηλ) θt−1−η m^t/(v^t+ϵ)\theta_t = (1 - \eta\lambda)\,\theta_{t-1} - \eta\,\hat m_t/(\sqrt{\hat v_t} + \epsilon) with the moments built from gtg_t alone (PyTorch's AdamW). Write the decay part of the step in each.

  1. (a) The gradient Adam sees is g~1=g1+λθ0=(0.01, 4.01)\tilde g_1 = g_1 + \lambda\theta_0 = (0.01,\ 4.01).The penalty λ2∥θ∥2\tfrac\lambda2\|\theta\|^2 has gradient λθ\lambda\theta, added to the data gradient before anything else happens.
  2. (a) θ1=θ0−η g~1/(∣g~1∣+ϵ)≈(1−0.001, 1−0.001)=(0.999, 0.999)\theta_1 = \theta_0 - \eta\,\tilde g_1/(|\tilde g_1| + \epsilon) \approx (1 - 0.001,\ 1 - 0.001) = (0.999,\ 0.999).Problem 8: the first step is −η-\eta times the sign of the gradient it is given; 0.01/(0.01+10−8)0.01/(0.01 + 10^{-8}) and 4.01/(4.01+10−8)4.01/(4.01 + 10^{-8}) are both 11 to six decimal places.
  3. (a) In general m^t\hat m_t contains a weighted average of the past λθ\lambda\theta terms, and that share of the step is divided entry by entry by v^t+ϵ\sqrt{\hat v_t} + \epsilon, exactly like the data gradient.mtm_t is linear in the gradients it is fed, so it splits into a data part and a penalty part; the division by v^t+ϵ\sqrt{\hat v_t} + \epsilon applies to both, with v^t\hat v_t built from the squared total gradient.
  4. (b) m^1=g1\hat m_1 = g_1, v^1=∣g1∣=(0,4)\sqrt{\hat v_1} = |g_1| = (0, 4), so the Adam part is −η (0/(0+10−8), 4/(4+10−8))≈(0, −0.001)-\eta\,(0/(0 + 10^{-8}),\ 4/(4 + 10^{-8})) \approx (0,\ -0.001).Problem 8 with the data gradient alone; the first entry's gradient is exactly 00, so its Adam step is 00.
  5. (b) θ1=(1−10−5)(1,1)+(0,−0.001)=(0.99999, 0.99899)\theta_1 = (1 - 10^{-5})(1, 1) + (0, -0.001) = (0.99999,\ 0.99899).ηλ=10−5\eta\lambda = 10^{-5}: each weight shrinks by the same fraction before the Adam step.
  6. (a) Adam + L2: θ1≈(0.999, 0.999)\theta_1 \approx (0.999,\ 0.999), with the decay divided by v^t+ϵ\sqrt{\hat v_t} + \epsilon; (b) AdamW: θ1=(0.99999, 0.99899)\theta_1 = (0.99999,\ 0.99899), decay part ηλθ\eta\lambda\thetaWith L2, the weight that has no data gradient moved 10−310^{-3}, a hundred times the ηλθ=10−5\eta\lambda\theta = 10^{-5} that "weight decay" suggests, while for the weight with a large gradient the penalty barely changed the step. Dividing by v^t\sqrt{\hat v_t} makes the decay strongest on weights with small gradient history and weakest on those with large ones. AdamW keeps the decay a fixed fraction ηλ\eta\lambda of every weight, which is why its λ\lambda (PyTorch's default is 0.010.01) behaves like weight decay in plain SGD.

Where this goes wrong

1. Learning-rate limit from the smallest eigenvalue

The minimum lies along the flat directions, and it is natural to tune the step to the direction that has furthest to go.

  1. The error along eigenvector ii is multiplied by 1−ηλi1 - \eta\lambda_i at each stepRight so far: Problem 2, step 2.
  2. “The slow direction is the bottleneck, so the learning rate should be set by λmin⁡\lambda_{\min}.”The reasoning that causes the mistake: the flat direction sets how fast gradient descent can go, but the steep direction sets whether it converges at all.
  3. For AA with eigenvalues 22 and 44, any η<2/λmin⁡=1\eta < 2/\lambda_{\min} = 1 converges, so take η=0.9\eta = 0.9At η=0.9\eta = 0.9 the steep direction's factor is 1−0.9⋅4=−2.61 - 0.9 \cdot 4 = -2.6: the error there flips sign and grows by 2.6×2.6\times every step. The limit is 2/λmax⁡=122/\lambda_{\max} = \tfrac12 (Problem 2); the flat direction's slowness has to be fixed by momentum or preconditioning, not by a larger η\eta.

2. Momentum step taken to equal the plain step ηg

Adding momentum to a working SGD setup looks like adding smoothing, not like changing the learning rate.

  1. vt=βvt−1+gtv_t = \beta v_{t-1} + g_t, θt=θt−1−ηvt\theta_t = \theta_{t-1} - \eta v_tRight so far: classic momentum, PyTorch's SGD with momentum=β\beta.
  2. “Momentum averages the recent gradients, so the step is still about ηg\eta g, only smoother.”The analogy that causes the mistake: reading the classic form as if it were the EMA form, whose weights sum to at most 11.
  3. Switching from plain SGD at η=0.1\eta = 0.1 to momentum 0.90.9 at η=0.1\eta = 0.1 keeps the step size at about 0.1 g0.1\,gThe classic buffer sums the gradients, so on a steady gradient the step tends to ηg/(1−β)=g\eta g/(1 - \beta) = g, ten times larger (Problem 4). A learning rate that was near the stability limit of Problem 2 is now well past it; to keep the step the same, multiply η\eta by 1−β1 - \beta.

3. Same learning rate for the EMA and summed momentum forms

Papers and libraries write momentum both ways, and η\eta is called the learning rate in both.

  1. mt=(1−β)vtm_t = (1 - \beta)v_tRight so far: Problem 5, step 2.
  2. “Both forms are momentum with the same β\beta, so a learning rate from one carries over to the other.”The shortcut that causes the mistake: treating the factor 1−β1 - \beta in the EMA recurrence as a detail of the bookkeeping rather than a scale on every step.
  3. An EMA-form run with η′=0.1\eta' = 0.1, β=0.9\beta = 0.9 is reproduced by the classic form with η=0.1\eta = 0.1The classic form needs η=(1−β)η′=0.01\eta = (1 - \beta)\eta' = 0.01 (Problem 5). With η=0.1\eta = 0.1 every step is ten times too large; going the other way, from classic to EMA with the number unchanged, every step is ten times too small and training looks merely slow.

4. First Adam step computed without the bias correction

The bias correction looks like a small fix-up that matters only for a few steps, so a hand-written Adam often leaves it out.

  1. m1=(1−β1)g1m_1 = (1 - \beta_1)g_1, v1=(1−β2)g12v_1 = (1 - \beta_2)g_1^2Right so far: the first moment updates from zero.
  2. “The correction only matters for small tt; use mtm_t and vtv_t directly.”The shortcut that causes the mistake: assuming the two biases are similar and cancel in the ratio mt/vtm_t/\sqrt{v_t}.
  3. θ1−θ0=−η m1/v1=−η 0.1 g10.001 ∣g1∣≈−3.16 η sign⁡(g1)\theta_1 - \theta_0 = -\eta\,m_1/\sqrt{v_1} = -\eta\,\dfrac{0.1\,g_1}{\sqrt{0.001}\,|g_1|} \approx -3.16\,\eta\,\operatorname{sign}(g_1)The biases do not cancel: m1m_1 is shrunk by 1−β1=0.11 - \beta_1 = 0.1, v1\sqrt{v_1} by 1−β2≈0.0316\sqrt{1 - \beta_2} \approx 0.0316. The uncorrected first step is 3.163.16 times η\eta instead of η\eta (Problem 8), and the factor (1−β1t)/1−β2t(1 - \beta_1^t)/\sqrt{1 - \beta_2^t} by which every uncorrected step is too large rises to about 6.56.5 near t=10t = 10 and is still about 1.261.26 at t=1000t = 1000, so the early steps are oversized just when the parameters are furthest from sensible.

5. Adam's L2 penalty taken as a decay of ηλθ

In plain SGD an L2 penalty and weight decay are the same thing: the penalty's gradient λθ\lambda\theta times η\eta shrinks each weight by ηλθ\eta\lambda\theta.

  1. The gradient Adam sees is gt+λθt−1g_t + \lambda\theta_{t-1}Right so far: Problem 10, step 1, PyTorch's Adam with weight_decay=λ\lambda.
  2. “Adding λθ\lambda\theta to the gradient is weight decay, so each step shrinks the weights by ηλθ\eta\lambda\theta as in SGD.”The analogy that causes the mistake: SGD multiplies the whole gradient by the same η\eta; Adam divides it entry by entry by v^t+ϵ\sqrt{\hat v_t} + \epsilon.
  3. In Problem 10, Adam with weight_decay=0.01 moves the weight with zero data gradient by ηλθ=10−5\eta\lambda\theta = 10^{-5}It moves by 10−310^{-3}, a full Adam step, because the penalty gradient is normalised by its own size (Problem 10, step 2). Weights with small gradients are decayed far more than ηλθ\eta\lambda\theta and weights with large gradients far less; AdamW applies ηλθ\eta\lambda\theta separately, outside the normalisation.

Print this set: momentum-rmsprop-and-adam.pdf (problems, answers, and worked solutions on separate pages).