Back to topic
Optional
Gradient descent on a quadratic
Descent on ½‖Aw−b‖²: loss path and step-size condition. Self-check in the notebook.
- Version
- v1
- Minutes
- ~20
- Evidence
- Self-check in the notebook
Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.
Download the notebook and run it locally. Its environment requirements are listed inside.
Notebook contents
Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.
ML · Градиентный спуск на квадратике / GD on a quadratic
Activity version: 1 · Seed: n/a · Данные: , минимум в .
Ожидаемые числа (, , 40 шагов)
| Quantity | Value | Tolerance |
|---|---|---|
| final | ≈ 0.87148784, 1.00000000 |
abs ≤ 1e-5 |
| final | < 0.01 |
|
| decreases | yes |
Самопроверка в ноутбуке
Не влияет на mastery.
import numpy as np
def loss(w):
return 0.5 * (w[0] - 1.0) ** 2 + 5.0 * (w[1] - 1.0) ** 2
def grad(w):
return np.array([w[0] - 1.0, 10.0 * (w[1] - 1.0)])
w = np.zeros(2)
lr = 0.05
losses = []
for _ in range(40):
w = w - lr * grad(w)
losses.append(loss(w))
print("w", w, "L", losses[-1])
assert np.allclose(w, [0.87148784, 1.00000000], atol=1e-5)
assert losses[-1] < 0.01
assert losses[-1] < losses[0]
print("OK: gradient descent quadratic")
Самопроверка в ноутбуке
- финальный рядом с
- loss убывает и