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, a linear predictor gives y^=wTx. The residual is r=y^−y. The derivative of the loss r2/2 with respect to r is r, and the derivative of r with respect to w is x. The chain rule gives ∇w(r2/2)=rx. For the mean squared loss with the same factor of 1/2, average these gradients over the examples. For a matrix X with features in rows and a target vector y, this is XT(Xw−y)/n.
Gradient descent applies wnext=w−η∇L(w), with learning rate η>0. 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,w=(1,1)T and y=3. The prediction is 1 and the gradient is (−4,2)T. At learning rate 1/10, the update gives w=(7/5,4/5)T, and the loss falls from 2 to 1/2.
2
Follow the error on a quadratic
For L(w)=a(w−w∗)2/2 with a>0, the error e=w−w∗ changes to (1−ηa)e each step. For a nonzero error, its magnitude shrinks exactly when ∣1−ηa∣<1, equivalently 0<η<2/a. When 1<ηa<2, the iterate alternates across the minimiser while converging. At η=2/a and nonzero initial error it oscillates without shrinking.
For a quadratic with positive definite Hessian H, choose an orthonormal eigenbasis of H. Each error coordinate then follows the scalar rule with a equal to its eigenvalue. Stability for all initial errors requires 0<η<2/λmax(H). 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.0for _ inrange(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/2 at w=1.
Show solution✓ Checked
The derivative is 2(2w−3), giving −2.
Exercise 5.2★★Land on the minimum
Find the one-step learning rate for the scalar quadratic with curvature a=5.
Show solution✓ Checked
Set 1−5η=0. The answer is η=1/5.
Exercise 5.3★★★Two speeds
A quadratic has Hessian eigenvalues 1 and 9. At learning rate 1/9, find the two error factors.
Show solution✓ Checked
Apply 1−ηa to each eigenvalue. The factors are 8/9 and 0.
4
Check yourself
1
What is the gradient of the single-example loss with respect to w?
The two derivatives in the chain rule are r and x.
2
For curvature 4, which learning rate is at the upper stability boundary?
The error factor at 2/a is −1.
3
Which eigenvalue limits the step size for a positive definite quadratic?
Every eigenvalue must obey the scalar stability condition.