Gradient descent on a quadratic
Minimise using with . See gradient-descent.
def grad(theta):
return 2 * (theta - 3)
def descend(eta, steps=20, theta=0.0):
for _ in range(steps):
theta -= eta * grad(theta)
return theta
for eta in (0.01, 0.1, 0.5, 1.1):
print(f"eta={eta:<4} -> theta after 20 steps = {descend(eta):.4f}")A small is slow, lands on the minimum at once, and diverges.