Learn › Tech Explained from Zero (video series)
·FREE·11 min read

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

ModalitytextTaskbasics

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 xx (hours) 1 2 3 4 5
Score yy (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:

y^=w x\hat{y} = w\,x

In words, this is a guess that the score is proportional to the study time.

One symbol at a time: xx is the study time (the input), and y^\hat{y} ("y-hat") is the predicted score — the hat tells it apart from the true score yy. ww is the slope of the line: how many points one extra hour is worth.

This ww is the knob we want the machine to set. The technical name is a parameter. With w=1w=1, an hour is worth 1 point; with w=3w=3, 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 ww that fits the data best. But how do we measure "fits best"?

One number for how far off we are: mean squared error

Try w=1w=1 first. The predictions are y^=1,2,3,4,5\hat{y} = 1, 2, 3, 4, 5 points. The differences (true − predicted) are

2−1=1,5−2=3,5−3=2,7−4=3,11−5=62-1=1,\quad 5-2=3,\quad 5-3=2,\quad 7-4=3,\quad 11-5=6

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 w=2w=2: the predictions are 2,4,6,8,102, 4, 6, 8, 10, and the differences are

0,1,−1,−1,10,\quad 1,\quad -1,\quad -1,\quad 1

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):

L=1N∑i=1N(yi−y^i)2L = \frac{1}{N}\sum_{i=1}^{N}\left(y_i - \hat{y}_i\right)^2

In words: square each student's miss, then take the average over everyone.

One symbol at a time: LL is the loss — the number for how far off we are; NN is the number of data points (5 here); yiy_i is student ii's true score and y^i\hat{y}_i the prediction; ∑\sum means "add up over all ii from 1 to NN".

Plugging in numbers:

w=2w=2 is far smaller, so the w=2w=2 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 LL for several values of ww gives:

ww 0 1 1.5 2 2.5 3
LL 44.8 11.8 3.55 0.8 3.55 11.8

A symmetric valley with its bottom at w=2w=2. In fact, in this example, expanding the formula shows that LL is exactly a parabola:

L(w)=11 (w−2)2+0.8L(w) = 11\,(w-2)^2 + 0.8

In words: the further ww moves from 2, the more the loss grows, in proportion to the square of that distance.

(The mean of x2x^2 is 11, the mean of x⋅yx\cdot y is 22, and the mean of y2y^2 is 44.8, so L=11w2−44w+44.8=11(w−2)2+0.8L = 11w^2 - 44w + 44.8 = 11(w-2)^2 + 0.8.)

Two things can be read off this formula. At w=2w=2, (w−2)2=0(w-2)^2 = 0, 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 w=2w=2. 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:

  1. Measure the slope where you are
  2. Move ww a little in the opposite direction of the slope (downhill)

This is gradient descent. As a formula, it is one line:

wnew=wold−η⋅dLdww_{\text{new}} = w_{\text{old}} - \eta \cdot \frac{dL}{dw}

In words: from where ww is now, move against the slope, by an amount proportional to how steep the slope is.

One symbol at a time: dLdw\frac{dL}{dw} is the rate at which LL grows when you increase ww a little — the slope (the derivative). η\eta ("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 ww; if it is negative you increase ww.

Differentiating the parabola gives the slope for this example:

dLdw=22 (w−2)\frac{dL}{dw} = 22\,(w-2)

At w=0w=0 it is 22×(0−2)=−4422 \times (0-2) = -44: a steep slope falling to the right.

Let's walk with learning rate η=0.02\eta = 0.02, starting from w=0w=0:

Step ww Slope Loss LL
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 0−0.02×(−44)=0.880 - 0.02 \times (-44) = 0.88, 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

wnew−2=(1−22η) (wold−2)w_{\text{new}} - 2 = (1 - 22\eta)\,(w_{\text{old}} - 2)

so the gap to the bottom is multiplied by (1−22η)(1-22\eta) every step. With η=0.02\eta = 0.02 that is 1−0.44=0.561 - 0.44 = 0.56. The gap goes −2→−1.12→−0.627→⋯-2 \to -1.12 \to -0.627 \to \cdots, and after 50 steps ww 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 (1−22η)(1-22\eta)":

The boundary is where the factor hits −1-1: η=2/22≈0.091\eta = 2/22 \approx 0.091. 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.

FIG 1Raise the learning rate η and the ball starts bouncing back and forth over the bottom of the valley; past a certain value it is thrown out of the valley (divergence). This valley has a different shape from the article's parabola, so the breaking point is a different number, but the behavior is the same

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 ww, 2N∑(y^i−yi) xi\frac{2}{N}\sum(\hat{y}_i - y_i)\,x_i, written out directly. At w=0w=0 it evaluates to −44-44, 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):

y^=−524x4+3512x3−33124x2+32512x−14\hat{y} = -\tfrac{5}{24}x^4 + \tfrac{35}{12}x^3 - \tfrac{331}{24}x^2 + \tfrac{325}{12}x - 14

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:

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 bb, making y^=wx+b\hat{y} = wx + b, 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 ww and for bb, and step downhill. For this data, the bottom of the bowl is also at w=2, b=0w=2,\ b=0 (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

Pitfalls that cause real accidents

Questions you'll be asked in interviews and on the job

Summary

Next (#2): Memorizing vs. understanding — how to split data into training, validation and test sets, and how to catch overfitting.

Go deeper

🎬 Watch this explained (Manim, captioned)

Sources

  1. Deep Learning, Chapter 5: Machine Learning Basics. deeplearningbook.orgOpen(retrieved 2026-10-27)
  2. Efficient BackProp. yann.lecun.comOpen(retrieved 2026-10-27)
  3. Linear Models — scikit-learn documentation. scikit-learn.orgOpen(retrieved 2026-10-27)
  4. numpy.polyfit — NumPy Manual. numpy.orgOpen(retrieved 2026-10-27)

Definitions and figures in this article were checked against the primary sources above. Where they differ, the source is authoritative.

Published by TechAthletes · Editorial policy · How we cite · Corrections

Comments

Sign in to comment