Same Start, Same Stride

OptimizersHard+30 XP+30

Same Start, Same Stride

The chapter raced plain descent against momentum and watched the paths. Here you run that race yourself, over several different landscapes, and count how long each optimizer takes to settle.

Two weights (x,y)(x, y)(x,y) are trained on the loss

L(x,y)=p x2+2q xy+r y2L(x, y) = p\,x^2 + 2q\,xy + r\,y^2L(x,y)=px2+2qxy+ry2

which is passed in as the symmetric matrix M = [[p, q], [q, r]]. Every M you will be given is a bowl whose single lowest point is (0,0)(0, 0)(0,0), where L=0L = 0L=0. When q=0q = 0q=0 the bowl's axes line up with xxx and yyy. When q≠0q \ne 0q=0 the valley runs diagonally. The gradient is

∂L∂x=2(p x+q y),∂L∂y=2(q x+r y)\frac{\partial L}{\partial x} = 2(p\,x + q\,y), \qquad \frac{\partial L}{\partial y} = 2(q\,x + r\,y)∂x∂L​=2(px+qy),∂y∂L​=2(qx+ry)

Plain descent. Measure both partial derivatives at the current point, then move both weights:

x←x−lr ∂L∂x,y←y−lr ∂L∂yx \leftarrow x - \text{lr}\,\frac{\partial L}{\partial x}, \qquad y \leftarrow y - \text{lr}\,\frac{\partial L}{\partial y}x←x−lr∂x∂L​,y←y−lr∂y∂L​

Momentum. Keep a velocity (vx,vy)(v_x, v_y)(vx​,vy​) that starts at (0,0)(0, 0)(0,0). On every update, measure the gradient at the current point, then

vx←β vx−lr ∂L∂x,vy←β vy−lr ∂L∂yv_x \leftarrow \beta\,v_x - \text{lr}\,\frac{\partial L}{\partial x}, \qquad v_y \leftarrow \beta\,v_y - \text{lr}\,\frac{\partial L}{\partial y}vx​←βvx​−lr∂x∂L​,vy​←βvy​−lr∂y∂L​

x←x+vx,y←y+vyx \leftarrow x + v_x, \qquad y \leftarrow y + v_yx←x+vx​,y←y+vy​

Here β\betaβ (beta) is how much of the previous motion is kept. At β=0\beta = 0β=0 this is exactly plain descent.

Both runs start from the same point start = [x, y], use the same lr, and last exactly 1000 updates. The two runs are independent: momentum starts from start with zero velocity.

When has a run settled? A falling loss is not enough, because momentum's loss can dip below a threshold and then climb back over it. A run's settling count is the smallest number of updates ttt such that the loss after ttt updates, and after every later update up to the 1000th, is strictly below tol. The loss after 000 updates is the loss at start, so a run that begins below tol and stays there has a settling count of 000. If the loss after the 1000th update is not below tol, the run never settled, and its count is None.

For example, suppose a run's losses after 0,1,2,3,4,5,…0, 1, 2, 3, 4, 5, \dots0,1,2,3,4,5,… updates were 5.0, 0.8, 0.03, 0.2, 0.04, 0.009,…5.0,\ 0.8,\ 0.03,\ 0.2,\ 0.04,\ 0.009, \dots5.0, 0.8, 0.03, 0.2, 0.04, 0.009,… and every later loss stayed below 0.050.050.05. With tol = 0.05, its settling count is 444, not 222: update 2 dipped under, but update 3 climbed back out.

Task: write race_to_settle(M, start, lr, beta, tol), returning [plain_count, momentum_count]. Each entry is a whole number or None.

You can rely on lr >0> 0>0, 0≤0 \le0≤ beta <1< 1<1 and tol >0> 0>0.

Nothing in these rules favours either optimizer. Which one wins, and by how much, depends entirely on the landscape and the settings, and that is the reason to race them.

with momentum1.1 from the bottom

Momentum lets the side-to-side wobbles cancel out while the forward motion builds up.

Examples

M = [[0.1, 0.0], [0.0, 10.0]]
start = [8.0, 1.0]
lr = 0.09
beta = 0.9
tol = 0.001
returns [242, 87]
M = [[1.0, 0.0], [0.0, 1.0]]
start = [3.0, -4.0]
lr = 0.25
beta = 0.9
tol = 0.001
returns [8, 94]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run