Skip to content
BIASED

Gradient descent

Block 1 of 8~2 min

Walk instead of solve

The topic Least squares ended with two problems of the closed form. The normal equations need the d×dd \times d matrix X⊤XX^\top X and a solve that costs O(nd2+d3)O(nd^2 + d^3), out of reach for a million features. And they exist only for squared error: the Huber loss has no formula for its minimizer, and neither does any classification loss in this course.

Both problems have one answer. Instead of solving ∇Q(w)=0\nabla Q(w) = 0, walk towards that point. Start anywhere, take a small step against the gradient, repeat:

w(k+1)=w(k)−η ∇Q(w(k)).w^{(k+1)} = w^{(k)} - \eta\, \nabla Q\big(w^{(k)}\big).

The negative gradient is the direction of steepest descent, and η>0\eta > 0 is the step size, or learning rate. Both come from the topic Unconstrained optimization of Mathematics for ML. Here we look at what they do to a model.

The loss is the mean squared error from Least squares, Q(w)=1n∥Xw−y∥2Q(w) = \frac1n \lVert Xw - y \rVert^2. Its gradient is

∇Q(w)=2nX⊤(Xw−y).\nabla Q(w) = \frac2n X^\top (Xw - y).

That is two matrix–vector products: first the residuals Xw−yXw - y, then X⊤X^\top times them. A step costs O(nd)O(nd), and only the number of nonzero entries when XX is sparse. Nothing of size d×dd \times d is ever formed.

Take four points x=(0,1,2,3)x = (0, 1, 2, 3), y=(1,3,2,5)y = (1, 3, 2, 5) and the model y^=w1+w2x\hat y = w_1 + w_2 x, so XX has the columns 11 and xx. The minimum is w^=(1.1,1.1)\hat w = (1.1, 1.1) with Q(w^)=0.675Q(\hat w) = 0.675. At w(0)=0w^{(0)} = 0 we have Q=9.75Q = 9.75 and ∇Q=(−5.5,−11)\nabla Q = (-5.5, -11). With η=0.2\eta = 0.2:

step ww Q(w)Q(w)
0 (0,0)(0, 0) 9.759.75
1 (1.1,2.2)(1.1, 2.2) 4.914.91
2 (0.44,0.66)(0.44, 0.66) 2.6592.659

The loss falls. But the slope jumped from 00 past its target 1.11.1 to 2.22.2, then back to 0.660.66. The next block explains the overshoot.

Answers are saved on the server and tied to this browser until you sign in.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Gradient descent on a quadraticoptional

    Descent on ½‖Aw−b‖²: loss path and step-size condition. Self-check in the notebook.

    ~20 min · easier on a computer · Gradient descent topic read.

    Self-check in the notebook — not part of mastery or the certificate.