K-Means step by step
Assign, update, assign… Follow the centroids until convergence, and discover where K-Means goes wrong: bad start, stretched clusters, rings.
English interface, partly translated: navigation, lab, projects, series, about and contact are; the home page sections, the site search and the content itself (articles, tutorials, research) are still in French.
Three optimizers start from the same point on a two-variable surface. Compare their trajectories over the level curves: zigzag, momentum, adaptive step.
| optimizer | η | f(θ) | distance to minimum |
|---|---|---|---|
| Gradient descent | 0.09 | 30.5 | 4.272 |
| Momentum | 0.02 | 30.5 | 4.272 |
| Adam | 0.3 | 30.5 | 4.272 |
On a two-variable surface, the gradient is a vector pointing towards the steepest ascent. The three optimizers use it differently.
Each step follows the local slope, with no memory. In a narrow valley, the slope is steep across and gentle along the floor: the point zigzags from one wall to the other and crawls towards the minimum. A larger speeds up the floor but makes the cross direction diverge.
The velocity accumulates past gradients. Components that flip sign at every step (across the valley) cancel out; those that keep their sign (along the floor) add up. With , a constant gradient is amplified up to ten times. The price: the trajectory may overshoot the minimum and spiral back.
Adam divides each coordinate of the step by an estimate of the recent magnitude of its gradient ( and are the averages corrected for their start-up bias). Every direction thus gets its own effective step, of order whatever the steepness: the narrow valley hardly bothers it.
No optimizer is "the best": on figure 2, compare speed (slope of the curve) and stability (oscillations).
Assign, update, assign… Follow the centroids until convergence, and discover where K-Means goes wrong: bad start, stretched clusters, rings.
Move the points and watch the least squares line follow. Then learn the same line by gradient descent and compare.
Slide a 3 × 3 kernel over an image: blur, sharpen, edge detection. Edit the weights and follow the computation of one pixel, term by term.