# Python 3.10+; standard library; deterministic; no network.
from math import isclose

def loss(w):
    return (w * 2.0 - 3.0) ** 2 / 2

w, learning_rate = 1.0, 0.1
gradient = (w * 2.0 - 3.0) * 2.0
h = 1e-5
numeric = (loss(w + h) - loss(w - h)) / (2 * h)
assert isclose(gradient, numeric, abs_tol=1e-8)
w_new = w - learning_rate * gradient
assert isclose(w_new, 1.2)
assert isclose(loss(w_new), 0.18)
assert loss(w_new) < loss(w)
print("gradient=-2.0; weight=1.2; loss=0.18")
