s1, s2 = 0.0, 0.0
for step in range(num_steps):
g1, g2 = grad_L(theta1, theta2)
s1 = beta * s1 + (1 - beta) * g1**2 # EMA of g²
s2 = beta * s2 + (1 - beta) * g2**2
theta1 -= eta * g1 / (np.sqrt(s1) + eps)
theta2 -= eta * g2 / (np.sqrt(s2) + eps)
Why it helps. g² throws away the sign, so √s only tracks how big a weight’s gradients usually are. If that size stays roughly steady, √s ≈ |g| and g/√s ≈ +1 or −1: only the sign of g survives, and every weight moves about η per step.
• θ₂ (steep, zig-zags): g = +6, −6, +6… flips sign, but g² stays 36 → √s ≈ 6 → steps of ±η instead of ±2.7. Still alternating, but too small to overshoot.
• θ₁ (flat, no zig-zag): g ≈ −0.18 every step → √s ≈ 0.18 → steps of −η instead of −0.08. No sign flip needed: it just walks instead of crawling.
ε ≈ 1e-8 only prevents division by zero.