VALLEY_CURVATURES = (0.2, 4.0)
h1, h2 = VALLEY_CURVATURES # Curvatures of the valley along x1 and x2
def f_valley(x1, x2):
"""The ill-conditioned quadratic valley h1/2 x1^2 + h2/2 x2^2, with
(h1, h2) = VALLEY_CURVATURES."""
h1, h2 = VALLEY_CURVATURES
return h1 / 2 * x1 ** 2 + h2 / 2 * x2 ** 2
def valley_grad(x1, x2):
"""Gradient of f_valley."""
h1, h2 = VALLEY_CURVATURES
return h1 * x1, h2 * x2
def train_2d(trainer, f_grad, steps=20, **hparams):
"""Run a 2D update rule from (-5, -2) and return the iterates."""
x1, x2, s1, s2 = -5, -2, 0, 0 # s1, s2: state of stateful optimizers
results = [(x1, x2)]
for _ in range(steps):
x1, x2, s1, s2 = trainer(x1, x2, s1, s2, f_grad, **hparams)
results.append((x1, x2))
print(f'step {steps}, x1: {float(x1):f}, x2: {float(x2):f}')
return results
def gd_2d(x1, x2, s1, s2, f_grad, eta):
"""One step of gradient descent in 2D; the state stays zero."""
g1, g2 = f_grad(x1, x2)
return x1 - eta * g1, x2 - eta * g2, 0, 0