Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Optimisation

Module 5Gradients and learning rates

Compute the gradient for a linear predictor and see how curvature controls a stable step size.

70 minYou need Derivatives, partial derivatives and matrix multiplication

In this module you will

  • Compute a gradient by the chain rule
  • Differentiate a squared loss with respect to a weight vector
  • Derive the stable learning-rate interval for a positive quadratic
1

Differentiate a prediction loss

For one example with feature vector x,x\mathclose{\text{,}} a linear predictor gives y^=wTx.\hat y=w^\mathsf Tx\mathclose{\text{.}} The residual is r=y^−y.r=\hat y-y\mathclose{\text{.}} The derivative of the loss r2/2r^2/2 with respect to rr is r,r\mathclose{\text{,}} and the derivative of rr with respect to ww is x.x\mathclose{\text{.}} The chain rule gives ∇w(r2/2)=rx.\nabla_w(r^2/2)=rx\mathclose{\text{.}} For the mean squared loss with the same factor of 1/2,1/2\mathclose{\text{,}} average these gradients over the examples. For a matrix XX with features in rows and a target vector y,y\mathclose{\text{,}} this is XT(Xw−y)/n.X^\mathsf T(Xw-y)/n\mathclose{\text{.}}

Gradient descent applies wnext=w−η∇L(w),w_{\rm next}=w-\eta\nabla L(w)\mathclose{\text{,}} with learning rate η>0.\eta>0\mathclose{\text{.}} The gradient gives the first-order change in the loss for a displacement. It describes the local slope, so a sufficiently short descent step lowers the loss where the gradient is nonzero. For a large step, curvature matters.

Example 5.1One update✓ Checked

Take x=(2,−1)T,x=(2,-1)^\mathsf T\mathclose{\text{,}} w=(1,1)Tw=(1,1)^\mathsf T and y=3.y=3\mathclose{\text{.}} The prediction is 11 and the gradient is (−4,2)T.(-4,2)^\mathsf T\mathclose{\text{.}} At learning rate 1/10,1/10\mathclose{\text{,}} the update gives w=(7/5,4/5)T,w=(7/5,4/5)^\mathsf T\mathclose{\text{,}} and the loss falls from 22 to 1/2.1/2\mathclose{\text{.}}

2

Follow the error on a quadratic

For L(w)=a(w−w∗)2/2L(w)=a(w-w_*)^2/2 with a>0,a>0\mathclose{\text{,}} the error e=w−w∗e=w-w_* changes to (1−ηa)e(1-\eta a)e each step. For a nonzero error, its magnitude shrinks exactly when ∣1−ηa∣<1,\lvert 1-\eta a\rvert<1\mathclose{\text{,}} equivalently 0<η<2/a.0<\eta<2/a\mathclose{\text{.}} When 1<ηa<2,1<\eta a<2\mathclose{\text{,}} the iterate alternates across the minimiser while converging. At η=2/a\eta=2/a and nonzero initial error it oscillates without shrinking.

For a quadratic with positive definite Hessian H,H\mathclose{\text{,}} choose an orthonormal eigenbasis of H.H\mathclose{\text{.}} Each error coordinate then follows the scalar rule with aa equal to its eigenvalue. Stability for all initial errors requires 0<η<2/λmax⁡(H).0<\eta<2/\lambda_{\max}(H)\mathclose{\text{.}} Small eigenvalues can still leave slow directions. Deep-network losses are generally nonlinear, so this quadratic argument describes a local model near a point with positive curvature.

Try it

With the diagonal curvatures 1 and 4, try learning rates 0.25, 0.5 and 0.6. Compare the paths and the two error factors at the stable step, the boundary and the unstable step.

Compute itObserve contraction and overshoot

Start with error one and curvature four. Compare a stable step that alternates signs with one beyond the stability boundary.

Python
for rate in (0.375, 0.625):
    error = 1.0
    for _ in range(3):
        error *= 1 - 4 * rate
    print(f"{error:.3f}")
Output
-0.125
-3.375
3

Exercises

Exercise 5.1★A scalar derivative

Differentiate L(w)=(2w−3)2/2L(w)=(2w-3)^2/2 at w=1.w=1\mathclose{\text{.}}

Show solution✓ Checked

The derivative is 2(2w−3),2(2w-3)\mathclose{\text{,}} giving −2.-2\mathclose{\text{.}}

Exercise 5.2★★Land on the minimum

Find the one-step learning rate for the scalar quadratic with curvature a=5.a=5\mathclose{\text{.}}

Show solution✓ Checked

Set 1−5η=0.1-5\eta=0\mathclose{\text{.}} The answer is η=1/5.\eta=1/5\mathclose{\text{.}}

Exercise 5.3★★★Two speeds

A quadratic has Hessian eigenvalues 11 and 9.9\mathclose{\text{.}} At learning rate 1/9,1/9\mathclose{\text{,}} find the two error factors.

Show solution✓ Checked

Apply 1−ηa1-\eta a to each eigenvalue. The factors are 8/98/9 and 0.0\mathclose{\text{.}}

4

Check yourself

1
What is the gradient of the single-example loss with respect to w?w\mathclose{\text{?}}
2
For curvature 4,4\mathclose{\text{,}} which learning rate is at the upper stability boundary?
3
Which eigenvalue limits the step size for a positive definite quadratic?
Finished this module?