Posts

Standing waves from two travelling waves

Pluck a guitar string and the wave that races towards the bridge reflects and comes back. The string then carries two identical waves moving in opposite directions, and what you see is neither of them: a pattern that stays in place and only breathes up and down.

Adding the two waves #

Take two waves of the same amplitude AA, wavenumber kk and angular frequency ω\omega, one moving right and one moving left:

y1=Asin⁡(kx−ωt),y2=Asin⁡(kx+ωt). y_1 = A \sin(kx - \omega t), \qquad y_2 = A \sin(kx + \omega t).

Their sum, by the identity sin⁡a+sin⁡b=2sin⁡a+b2cos⁡a−b2\sin a + \sin b = 2 \sin\tfrac{a+b}{2} \cos\tfrac{a-b}{2}, is

y=y1+y2=2Asin⁡(kx) cos⁡(ωt). y = y_1 + y_2 = 2A \sin(kx)\,\cos(\omega t).

The position and the time have separated. Every point of the string moves in step, with an amplitude 2Asin⁡(kx)2A\sin(kx) that depends only on where it is.

Two dashed travelling waves and their solid sum, a standing wave, at one instant
Two travelling waves (dashed) and their sum (solid). With JavaScript, this figure animates.

Nodes and antinodes #

Where sin⁡(kx)=0\sin(kx) = 0, at x=nπ/kx = n\pi/k or every half wavelength, the string never moves: these are the nodes, marked in the figure. Halfway between them the amplitude reaches 2A2A, the antinodes. A string of length LL fixed at both ends can only hold waves that put a node at each end, so its allowed wavelengths are

λn=2Ln,n=1,2,3,… \lambda_n = \frac{2L}{n}, \qquad n = 1, 2, 3, \ldots

which is why a string sounds a fundamental note and its harmonics, and nothing in between.

Why momentum helps gradient descent

Gradient descent is the workhorse of machine learning, and it has a well-known weakness. On a long, narrow valley it bounces from wall to wall while creeping along the floor. Adding momentum, a running memory of past gradients, is a one-line change that often makes it several times faster. This article shows why, on the simplest valley there is.

A quadratic valley #

Take a bowl stretched along one axis,

f(w)=12(λ1w12+λ2w22),λ1=1, λ2=10. f(w) = \tfrac12 \left( \lambda_1 w_1^2 + \lambda_2 w_2^2 \right), \qquad \lambda_1 = 1,\ \lambda_2 = 10.

Its gradient is ∇f(w)=(λ1w1, λ2w2)\nabla f(w) = (\lambda_1 w_1,\ \lambda_2 w_2), and the ratio κ=λ2/λ1=10\kappa = \lambda_2 / \lambda_1 = 10, the condition number, measures how narrow the valley is. Plain gradient descent takes steps of size α\alpha:

wk+1=wk−α∇f(wk). w_{k+1} = w_k - \alpha \nabla f(w_k).

A step large enough to make progress along the shallow direction overshoots in the steep one, and a step small enough for the steep one crawls along the shallow one. The best fixed step, α=2/(λ1+λ2)\alpha = 2/(\lambda_1 + \lambda_2), shrinks the error by a factor of (κ−1)/(κ+1)≈0.82(\kappa - 1)/(\kappa + 1) \approx 0.82 per step.

Adding momentum #

Momentum keeps a velocity zz that accumulates gradients and decays by a factor β\beta each step:

zk+1=βzk+∇f(wk),wk+1=wk−αzk+1. z_{k+1} = \beta z_k + \nabla f(w_k), \qquad w_{k+1} = w_k - \alpha z_{k+1}.

Oscillations across the valley point in alternating directions and cancel in zz; the small, consistent pull along the floor adds up. With the best choice of α\alpha and β\beta the error shrinks by (κ−1)/(κ+1)≈0.52(\sqrt\kappa - 1)/(\sqrt\kappa + 1) \approx 0.52 per step: about 11 steps instead of 35 to reduce it a thousandfold.

Contours of a narrow quadratic valley, with a red plain gradient descent path and a blue path with momentum
Gradient descent without momentum (red) and with it (blue), on the valley above. With JavaScript, this figure has sliders for the step size and momentum.

What to try #

  • Set β=0\beta = 0 and raise α\alpha until the path zig-zags, then diverges past α=0.2\alpha = 0.2.
  • Keep that α\alpha and raise β\beta: the zig-zag damps, and the path reaches the centre sooner.
  • Push β\beta towards 11: too much memory and the path overshoots and circles.

Further reading #

This sample follows the style of Distill articles and borrows its example from Gabriel Goh’s Why Momentum Really Works, which analyses the same valley in far more depth.