Skip to content
BIASED
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 .ipynb

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 · Данные: L(w)=12(w1−1)2+5(w2−1)2L(w)=\tfrac12(w_1-1)^2 + 5(w_2-1)^2, минимум в (1,1)(1,1).

Ожидаемые числа (w0=0w_0=0, η=0.05\eta=0.05, 40 шагов)
Quantity Value Tolerance
final ww ≈ 0.87148784, 1.00000000 abs ≤ 1e-5
final LL < 0.01
LL 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")
 
Самопроверка в ноутбуке
  • финальный ww рядом с (1,1)(1,1)
  • loss убывает и <0.01<0.01