What Does AI Actually Learn? Machine Learning with One Straight Line (Tech Explained from Zero #1)
We work out what 'learning' means using nothing but five points and one straight line: a loss that turns misses into a single number, gradient descent down the loss valley, what happens when the learning rate is too big, and a curve that scores perfectly on the data yet fails everywhere else. Every number can be checked by hand. With a companion video.
Key points
- Machine learning replaces hand-written rules with example data plus answers, and lets the machine set the rule (the parameter values).
- Misses are summarized as one number: the mean of the squared differences between prediction and answer (mean squared error). Without squaring, positive and negative misses cancel out.
- In this example the loss is exactly the parabola 11(w−2)²+0.8, and its bottom at w=2 is the best line.
- Gradient descent moves w a little against the slope. With learning rate 0.02 the gap to the bottom shrinks by 0.56 each step; with 0.1 it grows by −1.2 and diverges.
- A quartic curve through all five points has zero training error, yet predicts −14 points for 0 hours and 0 points for 7 hours. The score on the training data is not the goal.
Sources: Deep Learning, Chapter 5: Machine Learning Basics · Efficient BackProp · Linear Models — scikit-learn documentation · numpy.polyfit — NumPy Manual
About this series — Tech Explained from Zero is built so that each part stacks on the previous one: read (or watch) from #1 in order. Every part has a companion video with the same content. Part 1 assumes no background.
From writing rules to showing examples
Say you want software that spots spam. In ordinary programming, a person writes rules: "if the subject contains 'You won', it's spam." But spammers change their wording — "You've won", "Y0u won" — and the rules never stop growing.
Machine learning flips this around. Instead of writing the rule, you show the machine many examples together with the answers, and let it build the rule itself.
| What a person provides | What comes out | |
|---|---|---|
| Ordinary programming | data + rules | answers |
| Machine learning | data + answers | rules |
So what does "the machine builds the rule" mean concretely? In this article we compute it all the way through, using just five points and one straight line.
The example: study time and quiz scores
Here are five students' study hours and scores on a quiz out of 20 (this is made-up data for the explanation).
| Study time (hours) | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Score (points) | 2 | 5 | 5 | 7 | 11 |
We want a rule that predicts the score from the study time. The simplest rule to try is a straight line:
In words, this is a guess that the score is proportional to the study time.
One symbol at a time: is the study time (the input), and ("y-hat") is the predicted score — the hat tells it apart from the true score . is the slope of the line: how many points one extra hour is worth.
This is the knob we want the machine to set. The technical name is a parameter. With , an hour is worth 1 point; with , 3 points. Turning the knob changes the slope of the line.
(To keep things simple, we only consider lines through the origin — "0 hours means 0 points". We come back to adding an intercept at the end.)
"Learning" means finding the value of that fits the data best. But how do we measure "fits best"?
One number for how far off we are: mean squared error
Try first. The predictions are points. The differences (true − predicted) are
We want to boil these five differences down to one number, because with a single number we can say "better" or "worse".
What if we just add them up? That is a trap. Try : the predictions are , and the differences are
which add up to 0. The line misses, yet we would call the error zero — the positive and negative misses cancel out.
So we square each difference first, add them, and divide by the count to get an average. This is the mean squared error (MSE):
In words: square each student's miss, then take the average over everyone.
One symbol at a time: is the loss — the number for how far off we are; is the number of data points (5 here); is student 's true score and the prediction; means "add up over all from 1 to ".
Plugging in numbers:
- : squared differences . Total 59, divided by 5:
- : squared differences . Total 4, divided by 5:
is far smaller, so the line fits the data much better. Squaring also counts big misses more heavily: a miss twice as large costs four times as much.
The loss landscape: how L changes as you turn w
Computing for several values of gives:
| 0 | 1 | 1.5 | 2 | 2.5 | 3 | |
|---|---|---|---|---|---|---|
| 44.8 | 11.8 | 3.55 | 0.8 | 3.55 | 11.8 |
A symmetric valley with its bottom at . In fact, in this example, expanding the formula shows that is exactly a parabola:
In words: the further moves from 2, the more the loss grows, in proportion to the square of that distance.
(The mean of is 11, the mean of is 22, and the mean of is 44.8, so .)
Two things can be read off this formula. At , , so the loss reaches its minimum of 0.8. And it can never go below 0.8: no straight line passes exactly through all five points.
So now learning can be restated as finding the lowest point of the loss valley.
Walking downhill: gradient descent
Because we have the formula, we could see at a glance that the bottom is at . Real AI models, though, have millions or billions of knobs, and nobody can see the whole valley. All you can see is the slope where you are standing.
So you repeat two steps:
- Measure the slope where you are
- Move a little in the opposite direction of the slope (downhill)
This is gradient descent. As a formula, it is one line:
In words: from where is now, move against the slope, by an amount proportional to how steep the slope is.
One symbol at a time: is the rate at which grows when you increase a little — the slope (the derivative). ("eta") is the learning rate, which sets the size of each step. The minus sign makes you go downhill: if the slope is positive (rising to the right) you decrease ; if it is negative you increase .
Differentiating the parabola gives the slope for this example:
At it is : a steep slope falling to the right.
Let's walk with learning rate , starting from :
| Step | Slope | Loss | |
|---|---|---|---|
| 0 | 0 | −44 | 44.8 |
| 1 | 0.88 | −24.64 | 14.60 |
| 2 | 1.3728 | −13.80 | 5.13 |
| 3 | 1.6488 | −7.73 | 2.16 |
| 4 | 1.8033 | −4.33 | 1.23 |
Step 1 is , and the loss drops from 44.8 to 14.60 in one go. The slope gets gentler near the bottom, so the steps shrink on their own.
Look one level deeper and you can see exactly what each step does. Putting the slope into the update gives
so the gap to the bottom is multiplied by every step. With that is . The gap goes , and after 50 steps is essentially 2.0.
The learning rate: what happens if it's too big
Do bigger steps get you there faster? Check with "the gap is multiplied by ":
- : the factor is . goes . The minus sign means it jumps over the bottom to the other side, but the gap shrinks tenfold each time and settles quickly.
- : the factor is . goes . Each jump widens the gap by 1.2×, and is thrown out of the valley (divergence).
The boundary is where the factor hits : . With any larger learning rate, this example never reaches the bottom. With a tiny one, it takes thousands of steps. The learning rate is one of the first numbers people tune when training real AI models.
Even with millions of knobs, the procedure is the same: measure the slope for each knob (all of them lined up together are called the gradient) and nudge every knob a little downhill. The one-line formula above sits at the heart of large-scale training.
In code
Everything so far takes a few lines of Python:
import numpy as np
x = np.array([1, 2, 3, 4, 5.0]) # study hours
y = np.array([2, 5, 5, 7, 11.0]) # scores
w = 0.0 # starting point
for step in range(50):
grad = 2 * np.mean((w * x - y) * x) # slope dL/dw
w -= 0.02 * grad # step downhill, learning rate 0.02
print(round(w, 4)) # → 2.0
The grad line is the derivative of the mean squared error with respect to , , written out directly. At it evaluates to , matching step 1 in the table.
Memorizing is not understanding
Finally, let's build a rule that fits the data "even better": not a line, but a curve that passes exactly through all five points (a quartic):
In words, this is a curve with five knobs instead of one, tuned so that it passes through every one of the five students' points.
Since it passes through every point, its loss on the five students is 0 — supposedly "better" than the line's 0.8.
But compute what it predicts where we have no data:
- Someone who studied 0 hours: the line says 0 points, the curve says −14 points
- Someone who studied 7 hours: the line says 14 points, the curve says 0 points
The curve has simply memorized the five students; it has not captured how study time relates to scores. Shrinking the loss on the data you have is a means, not the goal — the goal is to be right on data you haven't seen yet. This memorizing behavior is called overfitting. How to detect and prevent it is the topic of the next part.
What about an intercept?
We only used lines through the origin. If you add an intercept , making , there are two knobs and the loss valley becomes a bowl (a two-dimensional valley) instead of a parabola. The procedure doesn't change: measure the slope for and for , and step downhill. For this data, the bottom of the bowl is also at (it happens to be data whose best line goes through the origin).
Using it in practice
The procedure in this article (pick a loss → measure the slope → step downhill by the learning rate → check on data you didn't train on) is exactly what data scientists and machine learning engineers do when they build the first baseline model for a new prediction problem. Before moving to a more complex model, measuring how far a straight line (linear regression) can push the loss down gives you something to compare against, so you can tell whether the later model is actually better.
Names you will actually touch
- scikit-learn's
LinearRegression: thefit_interceptargument defaults toTrue(with an intercept). Set it toFalseonly when you want a line through the origin, as in this article - scikit-learn's
SGDRegressor: fits a linear model by gradient descent. The step size is set byeta0, and how it shrinks over time bylearning_rate - PyTorch: the loss is
torch.nn.MSELoss()(it averages by default — the same as here), and the update istorch.optim.SGD(params, lr=...).lris this article's - NumPy's
numpy.polyfit(x, y, deg): raisedegand you can build a "passes through every point" curve like this article's quartic in one line
Pitfalls that cause real accidents
- Changing the input's units alone can make training diverge. The breaking point in this example was set by the mean of (11): the boundary is . Enter study time in minutes instead of hours and becomes 60 times larger, the mean of becomes 3600 times larger (39600), and with the same the factor is . It diverges from the very first step. This is why inputs are brought to a common scale (standardized) before training (Efficient BackProp also recommends normalizing the inputs)
- If the loss turns into
nanorinf, suspect the learning rate first. When keeps bouncing and growing, as in the example, it eventually exceeds the range numbers can represent - Don't judge a model by its loss on the data you trained on. With five points, raising the
polyfitdegree to 4 is enough to drive the training loss to zero. Evaluate on data that was not used for training (how to split it is part #2)
Questions you'll be asked in interviews and on the job
- "The loss isn't going down during training, and partway through it starts rising. What do you check?" — Line of reasoning: first rerun with a smaller learning rate and see whether the loss now falls. If it does, the step was too large for how sharply the valley curves (in this article's terms, the factor ). Next, check whether the inputs are on a common scale
- "You built a model with zero error on the training data. Is it a good model?" — Line of reasoning: that alone tells you nothing. Measure it on data not used for training; a large gap from the training error points to overfitting
- "Why square the errors? Why not just add them?" — Line of reasoning: use this article's example to show that plain addition gives 0. Squaring stops misses from canceling out, and counts big misses more heavily
Summary
- Learning means setting the parameters (knobs) so the model fits the example data
- Fit is measured by a loss; for predicting numbers, the basic one is the mean squared error (square the differences, then average)
- The bottom of the valley is found by gradient descent: move a little against the slope, scaled by the learning rate. Too large a learning rate diverges
- Zero loss on your data doesn't mean you'll be right elsewhere (overfitting)
Next (#2): Memorizing vs. understanding — how to split data into training, validation and test sets, and how to catch overfitting.
Go deeper
- What is machine learning? — a map of supervised, unsupervised and reinforcement learning
- Loss functions and optimization — cross-entropy for classification, SGD
- Gradient descent — ravines, saddle points and the ceiling on the learning rate
Comments
Sign in to comment