Linear models and gradient descent
Fit a line by minimizing a loss, step by step, and see how the learning rate decides whether training works.
- Write a linear model and its mean squared error loss
- Explain gradient descent and the role of the learning rate
- Implement gradient descent with numpy
A linear model predicts with a weighted sum of features plus a bias: with one feature, . Training means finding the and that make predictions closest to the targets. “Closest” is measured by a loss function; for regression the usual choice is mean squared error:
Gradient descent minimizes it by repeatedly stepping downhill. The gradient points uphill, so each step moves the parameters against it, scaled by the learning rate :
This same loop - predict, measure loss, compute gradients, step - trains everything from linear regression to large language models.
Try it
Gradient descent, step by step
Start at . With learning rate 0.01, take 100 steps a few times and watch the loss fall. Then try 0.1, and finally 0.6. What happens when the steps are too big?
1import numpy as np
2x = np.array([0.0, 1.0, 2.0])
3y = np.array([1.0, 3.0, 5.0])
4w, b = 0.0, 0.0
5error = w * x + b - y
6print(2 * np.mean(error * x), 2 * np.mean(error))-8.666666666666666 -6.0
Key takeaways
A linear model predicts ; training minimizes a loss like mean squared error.
Gradient descent repeatedly steps against the gradient, scaled by the learning rate .
Too large a learning rate diverges; too small crawls. Scaled features help.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Mean squared error
Line 1 is w b; line 2 holds x values; line 3 holds y values. Print the mean squared error of with 4 decimals, using numpy.
- A rough line
- A perfect line
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Fit a line with gradient descent
Line 1 is LEARNING_RATE STEPS; line 2 x values; line 3 y values. Start at and run gradient descent on mean squared error, using
Print step S: loss L (4 decimals) after steps 1, 10, 100 and the last step (each once, if ≤ STEPS), then w=W b=B (3 decimals). If the loss ever exceeds , print diverged at step S and stop.
- Converges
- Learning rate too big
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…