Gradient Descent and Numerical Optimization › Example
Machine Learning from ScratchAll chapters Gradient Descent and Numerical Optimization › ExampleSection view · Sections in this chapter6 / 10 ↓
Reading tools Definitions, links & notes
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
We can check the recurrence by hand, then compare its predicted iterates with the update loop.
Take the well-conditioned curvature , start at , and use . The gradient at the start is , so the first update is
The objective falls from to , where and are the parameters before and after the first update, each term is with the curvature and the parameter along coordinate , and the intermediate sums make the arithmetic explicit. Continuing, we can factor the update per coordinate. For coordinate one the multiplier is ; for coordinate two it is , where is the learning rate and are the curvatures along the two coordinates. So after steps
where and are the two coordinates at iteration . With this starting point and rate, coordinate two decays more slowly. After ten steps it is , while coordinate one is . Here the smaller-curvature direction has the larger contraction multiplier. For another rate, we must compare both absolute multipliers rather than assuming that the smallest curvature always controls the error.
Now verify the Newton prediction. The Hessian is constant, so the step from is
since and the gradient is , where:
- : the Hessian with diagonal entries equal to the curvatures and
- : the inverse of the Hessian, here
- : the gradient at the starting point, here
- : the Newton step
Adding gives , the exact minimum, in a single step. Let us confirm these hand calculations with code so nothing rests on arithmetic we did in our heads.
PythonIn [15]Expand
a = np.array([2.0, 1.0])
theta = np.array([1.0, 1.0])
eta = 0.25
first_three = [theta.copy()]
for k in range(3):
grad = a * theta
theta = theta - eta * grad
first_three.append(theta.copy())
# Closed form prediction from the per-coordinate contraction factors
k = 10
pred = np.array([(1 - eta * a[0]) ** k, (1 - eta * a[1]) ** k])
gd_final = run_gd(np.array([1.0, 1.0]), a, eta, k)[0][-1]
assert np.allclose(gd_final, pred, rtol=1e-12, atol=1e-15)
assert np.allclose(first_three[1], [0.5, 0.75])
assert np.isclose(objective(first_three[1], a), 0.53125)ConsoleOut [16]Expand
After 10 steps the closed form gives theta = [0.00097656 0.05631351] direct run gives theta = [0.00097656 0.05631351] Objective there: 1.587e-03 Newton one-step solution: [0. 0.] iterate k=0: theta=[1. 1.], J=1.50000 iterate k=1: theta=[0.5 0.75], J=0.53125 iterate k=2: theta=[0.25 0.5625], J=0.22070 iterate k=3: theta=[0.125 0.42188], J=0.10461
The example also exposes a subtlety about learning rates. With both coordinates contract, and the objective decreases monotonically. Raise to and coordinate one has multiplier , so it oscillates in sign while shrinking; the objective still decreases each step but the path zig-zags. The multiplier for coordinate is , with the learning rate and the curvature along that coordinate. Raise to and the multiplier for coordinate one is , magnitude greater than one, and the run diverges even though coordinate two still converges. The next cell confirms the threshold numerically.
PythonIn [17]Expand
eta_tests = [0.25, 0.75, 1.25]
eta_results = []
for eta_test in eta_tests:
p, h = run_gd(np.array([1.0, 1.0]), a, eta_test, 12)
eta_results.append((eta_test, h[-1], p[-1]))ConsoleOut [18]Expand
eta=0.25: J after 12 steps = 5.018e-04, final theta = [0. 0.032] eta=0.75: J after 12 steps = 5.960e-08, final theta = [0. 0.] eta=1.25: J after 12 steps = 1.683e+04, final theta = [129.746 0. ] Stability requires eta < 2/2.0 = 1.0 for this problem.
The run with diverges because of the steep direction. On a positive-definite quadratic, an unbounded parameter eventually forces an unbounded objective. Over a finite budget, a lightly weighted coordinate can still look small in the objective while its parameter error matters. Inspect the parameter trajectory alongside the loss when that distinction is relevant.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!