Answer key · 12 questions

Python gradient descent, step by step Exercises With Answers

All 12 questions for Python gradient descent, step by step in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python gradient descent, step by step, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does a gradient-descent update subtract from the parameter?

  1. The gradient times the learning ratecorrect
  2. The number of steps
  3. The intercept
  4. The loss

Answer: The gradient times the learning rate

Why: The gradient gives the direction of increasing loss; scaling it by the learning rate controls the step size.

02 · Multiple choice

If the gradient is negative, what happens to w after a normal update?

  1. It stays exactly the same
  2. It decreases
  3. It increasescorrect
  4. It becomes zero immediately

Answer: It increases

Why: Subtracting a negative gradient adds a positive amount to w.

03 · Multiple choice

What is the main risk of a learning rate that is too large?

  1. The loss becomes an integer
  2. The updates can overshoot and divergecorrect
  3. The gradient disappears
  4. Python cannot multiply floats

Answer: The updates can overshoot and diverge

Why: A large step can jump past the minimum and make the next gradient even larger.

04 · Multiple choice

What is the output?

def gradient(w, xs, ys):
    return 2 * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

print(gradient(0, [1, 2], [1, 2]))
  1. -2.5
  2. -5.0correct
  3. 5.0
  4. 0.0

Answer: -5.0

Why: At w = 0, each residual is negative, so the gradient is negative.

05 · Fill in the blank

Complete the gradient-descent update.

def gradient(w, xs, ys):
    return 2 * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

w = 1.0
w -= 0.1 * ____(w, [1, 2], [2, 4])
print(w)

Answer: gradient

Why: The update subtracts the learning rate times the gradient from w.

06 · Fill in the blank

Complete the factor from differentiating the squared loss.

def gradient(w, xs, ys):
    return ____ * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

print(gradient(1, [1, 2], [2, 4]))

Answer: 2

Why: Differentiating (w*x - y) squared brings down a factor of 2.

07 · Fill in the blank

Complete the mean in the loss.

def loss(w, xs, ys):
    return sum((w * x - y) ** 2 for x, y in zip(xs, ys)) / ____(xs)

print(loss(1, [1, 2], [2, 4]))

Answer: len

Why: The total squared error is divided by the number of observations.

08 · Fill in the blank

Complete the loop so it takes three updates.

def gradient(w, xs, ys):
    return 2 * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

w = 1.0
for _ in range(____):
    w -= 0.1 * gradient(w, [1, 2], [2, 4])
print(round(w, 3))

Answer: 3

Why: range(3) produces three iterations.

09 · Predict output

What does this code print?

def loss(w, xs, ys):
    return sum((w * x - y) ** 2 for x, y in zip(xs, ys)) / len(xs)

print(loss(2, [1, 2], [2, 4]))

Answer: 0.0

Why: At w = 2 every prediction matches the target, so the loss is 0.0.

10 · Predict output

What does the program output?

def gradient(w, xs, ys):
    return 2 * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

print(gradient(2, [1, 2], [2, 4]))

Answer: 0.0

Why: The model already fits the data, so every residual is zero and the gradient is 0.0.

11 · Predict output

What appears on the screen?

def gradient(w, xs, ys):
    return 2 * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

w = 1.0
w -= 0.1 * gradient(w, [1, 2], [2, 4])
print(w)

Answer: 1.5

Why: The gradient is -5.0, so subtracting 0.1 times -5.0 raises w from 1.0 to 1.5.

12 · Predict output

What is printed?

def gradient(w, xs, ys):
    return 2 * sum((w * x - y) * x for x, y in zip(xs, ys)) / len(xs)

w = 1.0
for _ in range(2):
    w -= 0.1 * gradient(w, [1, 2], [2, 4])
print(w)

Answer: 1.75

Why: The first update reaches 1.5 and the second reaches 1.75.

Try the questions first

Open the practice page for Python gradient descent, step by step and answer the same 12 questions with instant feedback.

Practice this lesson