Exercise Set 26 - Gradient Descent
Companion lesson: Lesson 26 - Gradient Descent.
Source artifact: Lesson 26 exercise images.
Implementation script: implementation.ts.
Concepts Practiced
- Loss landscapes.
- Gradients as uphill directions.
- Negative gradients as descent directions.
- Learning rate.
- One-dimensional and two-dimensional update steps.
- The difference between an objective and an optimizer.
- Scaling a loss without changing its minimizer.
Notation
| Symbol | Meaning |
|---|---|
| A parameter value or parameter vector. | |
| The parameter value at step . | |
| Loss evaluated at parameters . | |
| Gradient of the loss at the current parameters. | |
| Learning rate. | |
| Parameter update. |
The gradient descent update is:
Problems
Problem 1
In your own words, explain the difference between these two statements:
- “The loss function defines the landscape.”
- “Gradient descent defines the path through the landscape.”
Then identify which object each phrase refers to in the update rule:
Problem 2
Draw a one-dimensional loss curve with a minimum at .
Mark a current point . Then label:
- The sign of the gradient.
- The downhill direction.
- A small learning-rate step.
- A large learning-rate step that might overshoot the minimum.
Problem 3
Let:
Then:
Start with:
and:
Compute , , and . For each step, write the gradient value before applying the update.
Problem 4
Suppose:
Answer:
- Does the minimizer of change compared with the minimizer of ?
- What happens to the gradient?
- If the original learning rate was , what learning rate would make gradient descent take the same steps on ?
Problem 5
Let:
The gradient is:
Start at:
with:
Compute one gradient descent update. Then explain why the -coordinate moves more dramatically than the -coordinate.
Problem 6
Use the local linear approximation:
Show what happens when:
for .
Your answer should explain why the dot product term is non-positive.
Problem 7
A regularized objective has the form:
Answer:
- Which part measures data fit?
- Which part penalizes large parameters?
- Which object would gradient descent use to update : or ?
- How is changing different from changing the learning rate ?
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:
The gradient is:
Start from , use , 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 . Then try and describe what changes.
Reflection
- Which part of the update rule still feels symbolic instead of geometric?
- Did the one-dimensional example or two-dimensional example make the idea clearer?
- What is the clearest difference between regularization strength and learning rate ?
- What should the next lesson reinforce before stochastic gradient descent adds mini-batches?
Instructor Notes / Review
Problem 3 should move toward with shrinking steps:
Problem 4:
- The minimizer does not change.
- The gradient is multiplied by 10.
- The learning rate should be divided by 10 to preserve the same update steps.
Problem 5:
so:
Problem 6:
This is the compact reason the negative gradient is a descent direction for sufficiently small steps.