Free lab · runs in your browser · no sign-up
Gradient descent from scratch in Python
Gradient descent is how almost every model learns: measure how wrong it is, work out which way is downhill, take a small step, repeat. Here you write it yourself, in plain Python, and run it on this page.
Step 1: minimise a simple curve
f(w) = (w − 3)² is smallest at w = 3. Its gradient (slope) is 2(w − 3). Each step moves w a little against the slope.
Step 2: fit a line to data
The same idea fits a model: find the slope m and intercept b that make predictions closest to the data (mean squared error).
Step 3: break it on purpose
Change lr to 1.1 in step 1 and run it again. The steps overshoot and w runs away: a learning rate that is too big never settles.