← Lesson
Gradient Descent
Joeven
Run
Reset
Python loads on first run
xs = [1.0, 2.0, 3.0, 4.0] ys = [2.0, 4.0, 6.0, 8.0] # true w = 2 def mse(w): return sum((w * x - y) ** 2 for x, y in zip(xs, ys)) / len(xs) def grad_analytic(w): n = len(xs) return sum(2 * (w * x - y) * x for x, y in zip(xs, ys)) / n def grad_finite(w, eps=1e-5): return (mse(w + eps) - mse(w - eps)) / (2 * eps) w = 0.0 lr = 0.02 print("step", 0, "w", round(w, 4), "loss", round(mse(w), 4)) for step in range(1, 26): g = grad_analytic(w) if step == 1: print("analytic grad", round(g, 4), "finite grad", round(grad_finite(w), 4)) w = w - lr * g if step in {1, 5, 10, 25}: print("step", step, "w", round(w, 4), "loss", round(mse(w), 4), "grad", round(g, 4))
Run to execute this in your browser. Nothing is sent to a server.