Machine Learning from Scratch · Lesson 01 · Part 1

Your First Neuron, by Hand

Why does a model learn? Work it out by hand with five examples, two adjustable numbers and one correction at a time.

Before you begin

You need arithmetic and a pen. No machine learning, calculus or programming is assumed: code comes in Part 2. Plan for about an hour. This is an estimate: stop to calculate rather than skim, even if it takes longer.

By the end you should be able to calculate a prediction and its loss, derive a gradient, apply one update, and recognise a learning rate that is too large.

1. Theory — what does “learning” mean?

From a fixed rule to an adjustable rule

In ordinary programming you write a rule and feed it inputs. In machine learning you choose a family of rules and let examples pick the best member. A model is the calculation that turns an input into an output. Training adjusts the model’s numbers to reduce its mistakes on examples.

Each sample pairs an input (hours studied) with a known target (exam points). Learning from input–target pairs is supervised learning; predicting a number is regression. Our five pairs are (1, 20), (2, 40), (3, 60), (4, 80) and (5, 100). They are invented and perfectly regular, so you can check every calculation by hand.

Parameters, weight, bias and prediction

Picture a straight line through the five points. Two knobs control it: the slope and the vertical offset. A parameter is a number inside the model that training adjusts. The weight ww sets how much the output changes when the input grows by one. The bias bb adds the same offset to every output.

The prediction is the model’s answer. We write the input as xx, the target as yy and the prediction as y^\hat{y} (“y hat”). Keep them apart: a guess is not the target.

y^=wx+b\hat{y} = wx + b

With w=10w = 10 points/hour and b=5b = 5 points, three hours gives 10⋅3+5=3510 \cdot 3 + 5 = 35 points. Raising ww by 1 adds 3 points to this prediction; raising bb by 1 adds 1 point. The weight’s effect grows with the input.

This model is linear regression. We call it a “neuron” because a neuron in a neural network computes the same thing, a weighted input plus a bias, before passing it through a nonlinear function. That function comes in a later lesson.

Loss: turn five mistakes into one score

An error is prediction minus target, so a negative error means the model predicted too low. Averaging signed errors hides mistakes: −10 and +10 cancel out. Instead, square each error and average the squares. The result is the loss: one number saying how badly the model fits. Smaller is better.

This loss is mean squared error (MSE). Squaring stops errors from cancelling and makes big errors count more: an error of 20 contributes four times as much as an error of 10. MSE is measured in squared points, not points or percent.

L(w,b)=1n∑i=1n(wxi+b−yi)2L(w, b) = \frac{1}{n} \sum_{i=1}^{n} \left( w x_i + b - y_i \right)^2

LL is the loss, nn the number of samples (5), and ii numbers the samples. ∑\sum is a reminder that the data stay fixed and only the parameters change the score.

Derivative: how sensitive is the loss to a change?

Turn one knob slightly. Does the loss rise or fall, and how fast? A derivative answers that: it is the rate of change of a function at a point. For a function f(t)f(t), the ratio

f(t+h)−f(t)h\frac{f(t + h) - f(t)}{h}

is the average change over a small step hh. The derivative is the value this ratio approaches as hh shrinks to zero.

For example, let f(t)=t2f(t) = t^2. At t=3t = 3, moving to t=3.01t = 3.01 changes ff from 9 to 9.0601. The ratio is 6.01, and smaller steps bring it closer to 6. Algebra shows why:

(t+h)2−t2h=2t+h  →  h→0    2t\frac{(t + h)^2 - t^2}{h} = 2t + h \;\xrightarrow{\;h \to 0\;}\; 2t

So the derivative of t2t^2 is 2t2t.

Our loss depends on two numbers, ww and bb. A partial derivative changes one and holds the other fixed. We write them ∂L∂w\frac{\partial L}{\partial w} and ∂L∂b\frac{\partial L}{\partial b} (“partial L by w”, “partial L by b”). A negative ∂L∂w\frac{\partial L}{\partial w} means a small increase in ww lowers the loss. It says nothing about the loss itself being negative.

Gradient and gradient descent

The gradient collects all the partial derivatives into one vector:

∇L=(∂L∂w,  ∂L∂b)\nabla L = \left( \frac{\partial L}{\partial w}, \; \frac{\partial L}{\partial b} \right)

Picture the loss as the height of a landscape whose map coordinates are ww and bb. The gradient points uphill, and its negative points most steeply downhill.

Gradient descent is the algorithm that uses it: compute the gradient, take a small step against it, repeat. The learning rate η\eta (“eta”) sets the step size. It is a hyperparameter: a setting you choose, not something the model learns.

wnew=w−η ∂L∂wbnew=b−η ∂L∂b\begin{aligned} w_{\text{new}} &= w - \eta \, \frac{\partial L}{\partial w} \\[6pt] b_{\text{new}} &= b - \eta \, \frac{\partial L}{\partial b} \end{aligned}

Compute both derivatives from the same old values before changing either parameter. A gradient is a local guide, not a promise about a long jump. Too large a rate overshoots the low region and the loss grows; too small a rate makes progress slow; zero changes nothing.

Every update in this lesson uses all five samples. One full pass over the training data is called an epoch, so here one update is one epoch.

Computing the gradient: the chain rule

The loss is built in stages: parameters give predictions, predictions give errors, and errors are squared and averaged. To find how a parameter affects the loss, multiply the rates of change along that chain. This is the chain rule.

For one sample, call its error e=wx+b−ye = wx + b - y. The square e2e^2 changes at rate 2e2e as ee changes. The error changes at rate xx as ww changes, and at rate 1 as bb changes. Multiplying along the chain:

∂(e2)∂w=2e⋅∂e∂w=2ex∂(e2)∂b=2e⋅∂e∂b=2e\begin{aligned} \frac{\partial (e^2)}{\partial w} &= 2e \cdot \frac{\partial e}{\partial w} = 2ex \\[6pt] \frac{\partial (e^2)}{\partial b} &= 2e \cdot \frac{\partial e}{\partial b} = 2e \end{aligned}

So each sample contributes 2ex2ex to the weight’s derivative and 2e2e to the bias’s. Averaging over the five samples gives:

∂L∂w=25∑i=15eixi∂L∂b=25∑i=15ei\begin{aligned} \frac{\partial L}{\partial w} &= \frac{2}{5} \sum_{i=1}^{5} e_i x_i \\[6pt] \frac{\partial L}{\partial b} &= \frac{2}{5} \sum_{i=1}^{5} e_i \end{aligned}

In Part 2, PyTorch does this calculation for you. Doing it by hand first means you know exactly what it computes.

2. Worked example — the first correction

Start at w=0w = 0 and b=0b = 0, so every prediction is zero. Before calculating, predict the signs of both partial derivatives: all targets are positive, so would increasing each knob help?

All five samples at w=b=0w = b = 0
xx (hours)yy (points)y^\hat{y}e=y^−ye = \hat{y} - ye2e^2exex
1200−20400−20
2400−401600−80
3600−603600−180
4800−806400−320
51000−10010000−500

The squared errors sum to 22000, the errors to −300, and the error-times-input terms to −1100. So:

L=220005=4400∂L∂w=2⋅(−1100)5=−440∂L∂b=2⋅(−300)5=−120\begin{aligned} L &= \frac{22000}{5} = 4400 \\[6pt] \frac{\partial L}{\partial w} &= \frac{2 \cdot (-1100)}{5} = -440 \\[6pt] \frac{\partial L}{\partial b} &= \frac{2 \cdot (-300)}{5} = -120 \end{aligned}

With η=0.001\eta = 0.001:

wnew=0−0.001⋅(−440)=0.44bnew=0−0.001⋅(−120)=0.12\begin{aligned} w_{\text{new}} &= 0 - 0.001 \cdot (-440) = 0.44 \\[6pt] b_{\text{new}} &= 0 - 0.001 \cdot (-120) = 0.12 \end{aligned}

Subtracting a negative number increases the parameter. The two corrections differ because the inputs multiply the weight’s effect.

The new predictions are [0.56, 1.00, 1.44, 1.88, 2.32] and the new errors [−19.44, −39.00, −58.56, −78.12, −97.68]. Their squares sum to 20972.304, giving

MSE=20972.3045=4194.4608\text{MSE} = \frac{20972.304}{5} = 4194.4608

The loss went down, but only a little: one correction is not training.

Check the derivative without the formula

Hold b=0b = 0 and raise ww from 0 to 0.001. The loss becomes 4399.560011, so the ratio

4399.560011−44000.001=−439.989\frac{4399.560011 - 4400}{0.001} = -439.989

is close to −440. It isn’t exact because the step is not infinitely small.

Why does the bias rise, when the perfect fit has b=0b = 0?

At the start, both parameters help raise the predictions, so both grow. Later, once the slope is nearly right, a positive bias makes the small inputs overshoot, and it has to shrink again. The perfect fit is w=20w = 20, b=0b = 0, but a limited number of small steps need not reach it. The loss surface is a long, tilted valley: steep across, almost flat along it.

3. Interactive visualization — compare corrections

The simulation below runs the same equations in JavaScript. Before you move the sliders, predict what happens at learning rates 0, 0.001, 0.05 and 0.1. Each change restarts from w=b=0w = b = 0; the step slider shows the model after that many updates.

Horizontal axis: completed updates (0–100). Vertical axis: MSE in points², linear scale. The vertical maximum adjusts to each run; compare the numbers as well as the shape.

The numerical result is provided below the chart.

At update 1 with η = 0.001: w = 0.44, b = 0.12, MSE = 4194.4608.

Fit a line to the five points

First choose manual mode: set w = 10 and b = 5. Predict the effect of increasing only bias by 5, then restore b = 5 and increase only weight by 5. Which changes the slope and which changes the offset? Then select training and move from update 0 to 1, then to 100. Connect the line’s movement to decreasing loss.

Manual parameters (do not change training)

Circles show the five training targets; the solid line is ŷ = wx + b. Fixed axes: 0–6 hours and −60–240 points. Line segments outside this range are not drawn; values remain available in the summary and table. Rescaling therefore cannot hide divergence.

Training points and model line 0123456Hours studied -600100200240 Points

Model after update 1: w = 0.44, b = 0.12; MSE = 4194.4608 points². Predictions for hours 1–5: 0.56, 1, 1.44, 1.88, 2.32.

Targets and predictions for the plotted model
HoursTarget (points)Prediction (points)
1200.56
2401.00
3601.44
4801.88
51002.32

At rate 0 the loss stays at 4400. At 0.001 it falls slowly; at 0.1 it explodes within 100 steps. Watch the numbers as well as the curve: when a run diverges, the vertical scale grows so much that the early losses look like zero. These thresholds hold for this dataset only; there is no universally good learning rate.

4. Exercises

Write your prediction before you calculate, and use the simulation to check your arithmetic.

  1. Start at w=10w = 10, b=5b = 5. Calculate the five predictions, the MSE, both partial derivatives and one update with η=0.001\eta = 0.001. Check the MSE in the manual mode of the plot.
  2. Multiply all targets by two. Before calculating, predict how the initial loss and gradients at w=b=0w = b = 0 change. Explain why the loss and its derivatives scale differently.
  3. In the simulation, find roughly the largest learning rate at which the loss still falls after 100 updates. What does the line in the plot do just above it?
  4. Change the targets to [25, 45, 65, 85, 105]. Which values of ww and bb fit them exactly? Why could a model without a bias never fit these points?
Hints

Reuse e=wx+b−ye = wx + b - y and sum e2e^2, exex and ee separately. At w=b=0w = b = 0, doubling the targets doubles every error; think about what squaring does. For exercise 3, try rates between 0.05 and 0.1. The new targets in exercise 4 follow y=20x+5y = 20x + 5.

5. Assessment — explain why learning happens

Answer without scrolling back. Back each answer with a calculation or an observation, not only a definition.

  1. A colleague says derivative, gradient and gradient descent are the same thing. Use this two-parameter model to show the difference.
  2. At the start, why is the weight’s derivative (−440) more negative than the bias’s (−120)? Why must both be computed before either parameter changes?
  3. In the simulation the bias rises at first, although the perfect fit has b=0b = 0. Why? Do 100 updates guarantee an exact fit?
Reasoning guide — open after writing your answers

A partial derivative measures the sensitivity to one parameter; the gradient collects them; gradient descent uses the gradient to update the parameters. The weight’s derivative sums e⋅xe \cdot x, so larger inputs amplify it. Updating w first and then computing the bias derivative from the new w would be a different algorithm. The bias helps reduce the early underprediction even though its final value is zero; a finite number of steps makes progress but need not converge exactly.

Completion checklist

Keep your hand calculations and a short note connecting errors → gradient → update → lower loss. The checkboxes are reminders for you, not a test.

Saved only in this browser.

Next: Part 2

Part 2 turns these calculations into a program and lets PyTorch compute the gradient for you.

Continue to Part 2: in PyTorch →