Practice

Gradient Descent

Exercise Set 26 - Gradient Descent

Companion lesson: Lesson 26 - Gradient Descent.

Source artifact: Lesson 26 exercise images.

Implementation script: implementation.ts.

Concepts Practiced

Notation

SymbolMeaning
θ\thetaA parameter value or parameter vector.
θt\theta_tThe parameter value at step tt.
L(θ)L(\theta)Loss evaluated at parameters θ\theta.
L(θt)\nabla L(\theta_t)Gradient of the loss at the current parameters.
η\etaLearning rate.
Δθ\Delta \thetaParameter update.

The gradient descent update is:

θt+1=θtηL(θt).\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t).

Problems

Problem 1

In your own words, explain the difference between these two statements:

Then identify which object each phrase refers to in the update rule:

θt+1=θtηL(θt).\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t).

Problem 2

Draw a one-dimensional loss curve with a minimum at θ=3\theta = 3.

Mark a current point θt<3\theta_t < 3. Then label:

Problem 3

Let:

L(θ)=(θ4)2.L(\theta) = (\theta - 4)^2.

Then:

L(θ)=2(θ4).\nabla L(\theta) = 2(\theta - 4).

Start with:

θ0=1\theta_0 = 1

and:

η=0.1.\eta = 0.1.

Compute θ1\theta_1, θ2\theta_2, and θ3\theta_3. For each step, write the gradient value before applying the update.

Problem 4

Suppose:

L(θ)=10L(θ).L'(\theta) = 10L(\theta).

Answer:

  1. Does the minimizer of LL' change compared with the minimizer of LL?
  2. What happens to the gradient?
  3. If the original learning rate was η\eta, what learning rate would make gradient descent take the same steps on LL'?

Problem 5

Let:

L(w,b)=(w1)2+9(b+2)2.L(w, b) = (w - 1)^2 + 9(b + 2)^2.

The gradient is:

L(w,b)=[2(w1)18(b+2)].\nabla L(w, b) = \begin{bmatrix} 2(w - 1) \\ 18(b + 2) \end{bmatrix}.

Start at:

(w0,b0)=(0,0)(w_0, b_0) = (0, 0)

with:

η=0.05.\eta = 0.05.

Compute one gradient descent update. Then explain why the bb-coordinate moves more dramatically than the ww-coordinate.

Problem 6

Use the local linear approximation:

L(θ+Δθ)L(θ)+L(θ)TΔθ.L(\theta + \Delta \theta) \approx L(\theta) + \nabla L(\theta)^T \Delta \theta.

Show what happens when:

Δθ=ηL(θ)\Delta \theta = -\eta \nabla L(\theta)

for η>0\eta > 0.

Your answer should explain why the dot product term is non-positive.

Problem 7

A regularized objective has the form:

J(θ)=Ldata(θ)+λθ22.J(\theta) = L_{\text{data}}(\theta) + \lambda \lVert \theta \rVert_2^2.

Answer:

Implementation Problems

Implementation Problem 1

Write a TypeScript function:

type GradientDescentOptions = {
  initialTheta: number[];
  gradient: (theta: number[]) => number[];
  learningRate: number;
  steps: number;
};

function gradientDescent(options: GradientDescentOptions): number[][] {
  // Return every parameter vector, including the initial one.
}

Use .map to compute each parameter update.

Test it on:

L(w,b)=(w2)2+4(b+1)2.L(w, b) = (w - 2)^2 + 4(b + 1)^2.

The gradient is:

L(w,b)=[2(w2)8(b+1)].\nabla L(w, b) = \begin{bmatrix} 2(w - 2) \\ 8(b + 1) \end{bmatrix}.

Start from (0,0)(0, 0), use η=0.1\eta = 0.1, and record 10 steps.

Implementation Problem 2

Add a helper that computes the loss value for each parameter vector in the history.

Check whether the loss decreases at every step for η=0.1\eta = 0.1. Then try η=0.4\eta = 0.4 and describe what changes.

Reflection

Instructor Notes / Review

Problem 3 should move toward θ=4\theta = 4 with shrinking steps:

θ1=1.6,θ2=2.08,θ3=2.464.\theta_1 = 1.6,\quad \theta_2 = 2.08,\quad \theta_3 = 2.464.

Problem 4:

Problem 5:

L(0,0)=[236]\nabla L(0, 0) = \begin{bmatrix} -2 \\ 36 \end{bmatrix}

so:

(w1,b1)=(0,0)0.05(2,36)=(0.1,1.8).(w_1, b_1) = (0, 0) - 0.05(-2, 36) = (0.1, -1.8).

Problem 6:

L(θ)T(ηL(θ))=ηL(θ)20.\nabla L(\theta)^T(-\eta \nabla L(\theta)) = -\eta \lVert \nabla L(\theta) \rVert^2 \leq 0.

This is the compact reason the negative gradient is a descent direction for sufficiently small steps.