Neural Networks From Zero: Two Tiny Examples, Every Step Explained
Where neural networks are used, why almost every use reduces to predicting a number or choosing a category, and how a single neuron learns both. Every term is defined before it is used, and every step is shown with real numbers.

Part 1: The big picture
Where neural networks are used
| Real use | What goes in | What comes out |
|---|---|---|
| Price estimate for a house | size, rooms, location (numbers) | a price (a number) |
| Spam filter | features of an email (numbers) | spam or not spam |
| Face unlock | pixel values of a photo | this is you or not |
| Weather forecast | past temperatures, pressure | tomorrow's temperature |
| Chat assistant | the words so far, as numbers | the next word |
Every row has the same shape: numbers go in, a decision or a number comes out. Photos, text and sound all get converted to numbers first.
Two kinds of output
Look at the "comes out" column. There are only two kinds:
- A number. Price, temperature. This is called regression.
- A choice between categories. Spam or not, you or not, which next word. This is called classification. The simplest case is yes/no.
Even the chat assistant is classification: it chooses one word out of a list of thousands.
Why this article uses exactly these two
The two kinds of output need slightly different parts inside the network. If we understand one example of each, we understand the full picture. Everything else is a bigger version.
| Example A | Example B | |
|---|---|---|
| Kind | Regression (output is a number) | Classification (output is yes/no) |
| Data | AND gate (below) | |
| Hidden rule | output is 1 only when both inputs are 1 |
Why such tiny data
So that every number can be checked by hand. Real networks do exactly the same steps with millions of numbers.
Part 2: The problem: learning from examples instead of rules
Normally, a programmer writes the rule: "multiply by 2, add 1."
Here the rule is hidden. We only have examples, and the computer must find the rule.
Example A: examples
| Input | Correct output |
|---|---|
| 1 | 3 |
| 2 | 5 |
| 3 | 7 |
Example B: examples (AND gate)
| Input 1 | Input 2 | Correct output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Words we will use:
- Input: the numbers we give the model.
- Output: the number the model gives back.
- Target: the correct output from the examples. We write it in A and in B.
- Prediction: what the model outputs. We write it in A and in B.
Part 3: What is a "model"?
A model is a rule with adjustable numbers inside it. Start with the simplest rule that can fit Example A:
The two adjustable numbers are called parameters:
- (weight): multiplies the input. It controls how strongly the input affects the output.
- (bias): added at the end. It shifts the output up or down regardless of input.
Why we need both
Try input with different parameters:
| Output | Correct is 5 | ||
|---|---|---|---|
| 0 | 0 | 0 | far |
| 2 | 0 | 4 | close, still short by 1 |
| 2 | 1 | 5 | correct |
With only , we could not get exactly 5 and also 3 and 7 for the other inputs. The bias gives the second adjustable number. Learning means finding the right and .
This is already a neuron
A neuron is this exact thing: take inputs, multiply each by its weight, add them up, add the bias. The result of that arithmetic is called the weighted sum, and we write it :
For Example A there is one input, so . That is the entire neuron for regression.
Example B needs two changes
Change 1: two inputs. The AND gate has two inputs, so we have two weights:
Change 2: the output must be a probability. For yes/no we want a number between 0 and 1 (0 = surely no, 1 = surely yes, 0.5 = unsure). But can be any number, like -7 or 13. We need something that converts any number into the range 0 to 1.
That something is called an activation function: a function applied to after the weighted sum. The one we use is sigmoid:
You do not need to memorise the formula. Look at what it does:
| -4 | -2 | 0 | 2 | 4 | |
|---|---|---|---|---|---|
| 0.018 | 0.119 | 0.500 | 0.881 | 0.982 |
Large negative becomes close to 0. Zero becomes exactly 0.5. Large positive becomes close to 1. So sigmoid turns any number into a probability.
Example A has no activation function, because a price or a value can be any number.
Summary of the two neurons
| Example A | Example B | |
|---|---|---|
| Weighted sum | ||
| Activation | none | sigmoid |
| Prediction | ||
| Parameters |
Part 4: The learning loop
Learning is the same 5-step loop in both examples:
- Forward pass: make a prediction with the current parameters.
- Loss: measure how wrong it is.
- Gradient: find which direction to change each parameter.
- Update: change the parameters in that direction.
- Repeat.
We will do one full pass of this loop by hand on one sample:
- A: , target
- B: , target
Step 0: Starting values
All parameters start at 0.
| Example A | Example B | |
|---|---|---|
| Parameters |
Why start with wrong values? The model has not seen anything yet, so no values are known to be right. Any starting point works for a single neuron. (For bigger networks, starting values need to differ, but that is a later topic.)
Step 1: Forward pass
Forward pass means: push the input through the neuron to get a prediction. "Forward" because numbers flow from input to output.
| Example A | Example B | |
|---|---|---|
| Weighted sum | ||
| Prediction | ||
| Target | 5 | 1 |
Both predictions are wrong. But "wrong" is just a feeling. The computer needs a number that says how wrong. That is the next step.
Step 2: Loss
The loss is a single number measuring how far the prediction is from the target. Lower is better, and 0 means perfect.
Example A: squared error
Our values: .
Why squared? Two reasons. The difference could be negative (predicting 8 when the target is 5 gives -(-3)), and squaring makes it positive. Squaring also makes big mistakes count much more than small ones: off by 1 costs 1, off by 4 costs 16.
Loss for sample with and different :
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| Prediction | 0 | 2 | 4 | 6 | 8 |
| Loss | 25 | 9 | 1 | 1 | 9 |
The loss is lowest near (where the prediction would be exactly 5). That is where we want to end up.
Example B: binary cross-entropy
When the target is , this becomes . Look at what it does:
| Prediction (target is 1) | 0.99 | 0.9 | 0.5 | 0.1 | 0.01 |
|---|---|---|---|---|---|
| Loss | 0.010 | 0.105 | 0.693 | 2.303 | 4.605 |
If the model is confident and right, loss is tiny. If it is unsure (0.5), loss is 0.693. If it is confident and wrong (0.01 when the answer is 1), loss is huge.
Why not use squared error here too? Cross-entropy punishes confident wrong answers much harder, which is exactly the mistake a classifier must avoid. It also gives better learning signals with sigmoid (this shows up in Step 3).
Our values: . This is the loss of a pure coin flip. The model knows nothing yet.
Step 3: Gradient
Now we know how wrong the model is. The question is: how should we change and to reduce the loss?
We could try every possible value, but with millions of parameters that is impossible. Instead we use the gradient.
What the gradient is. The gradient of the loss with respect to a parameter says: if I increase this parameter a tiny bit, how much does the loss change? It is the slope.
Check with real numbers (Example A). Current , loss 25. Increase by 0.01:
| Prediction | 0 | 0.02 |
| Loss | 25 |
Loss changed by for a change of , so the slope is about , roughly -20.
- A negative gradient means: increasing the parameter decreases the loss. So increase it.
- A positive gradient means the opposite: decrease the parameter.
- A large gradient means the loss is very sensitive to that parameter.
We can get this exactly, without trying values, using the chain rule:
If A depends on B, and B depends on C, then how fast A changes with C = (how fast A changes with B) × (how fast B changes with C).
In our case, loss depends on the prediction, and the prediction depends on .
Example A
| Link | Calculation | Value |
|---|---|---|
| Loss changes with prediction | -10 | |
| Prediction changes with | , so slope is | 2 |
| Prediction changes with | slope is 1 | 1 |
| Gradient for | -20 | |
| Gradient for | -10 |
The -20 matches our hand-check of about -19.96.
Example B
| Link | Calculation | Value |
|---|---|---|
| Loss changes with | for sigmoid with cross-entropy this simplifies to | -0.5 |
| changes with | slope is | 1 |
| changes with | slope is | 1 |
| changes with | slope is 1 | 1 |
| Gradients | each | -0.5, -0.5, -0.5 |
The pattern. In both examples:
A's error signal is and B's is . The rest is identical. That is why the same learning method works for both. (The sigmoid + cross-entropy pair is chosen so the error signal is the clean .)
Why input matters. If an input is 0, its weight gets gradient 0. That weight did not contribute to the prediction, so it is not blamed.
Step 4: Update
Move each parameter against its gradient (opposite direction, since the gradient points toward higher loss):
Learning rate (lr) is a number we choose. It controls how big a step we take.
| Example A (lr = 0.01) | Example B (lr = 1) | |
|---|---|---|
| , | ||
Why not just jump straight to the answer? The gradient only tells the local slope, not the exact destination. A step too large overshoots. Example A with lr = 0.3 instead of 0.01:
| Value | |
|---|---|
| New | |
| New | |
| New prediction | |
| New loss | , worse than the starting 25 |
Too small a learning rate learns very slowly. Too large overshoots. It is a number you tune.
Step 5: Check that it worked
Run the same sample again with the new parameters.
| Example A | Example B | |
|---|---|---|
| New weighted sum | ||
| New prediction | 0.5 | |
| Target | 5 | 1 |
| New loss | ||
| Old loss | 25 | 0.693 |
Both losses dropped after one update. That is the whole idea.
Part 5: Repeating on all the data
One step on one sample only improves that sample. The real loop does this:
- Run every sample through the model.
- Compute the gradient for each sample, then average them.
- Update the parameters once using that average.
- Repeat.
Why average? If the update followed only one sample, it could damage the others. The average moves in a direction that helps the whole dataset.
One full pass over all samples is called an epoch.
Example A (lr = 0.05, 3 samples)
| Epoch | Loss | ||
|---|---|---|---|
| 1 | 1.133 | 0.500 | 5.489 |
| 5 | 2.006 | 0.889 | 0.0098 |
| 20 | 2.035 | 0.921 | 0.0009 |
| 100 | 2.013 | 0.970 | 0.00013 |
| 200 | 2.004 | 0.991 | 0.00001 |
Hidden rule was . The model reached , without being told.
Notice the loss falls fast at first and then slowly. As the model gets close, gradients get smaller, so steps get smaller.
Example B (lr = 1, 4 samples)
| Epoch | Loss | Outputs for (0,0), (0,1), (1,0), (1,1) | ||
|---|---|---|---|---|
| 1 | 0.000 | -0.250 | 0.638 | 0.438, 0.438, 0.438, 0.438 |
| 10 | 0.514 | -1.263 | 0.460 | 0.221, 0.321, 0.321, 0.442 |
| 100 | 3.049 | -4.791 | 0.143 | 0.008, 0.149, 0.149, 0.787 |
| 1000 | 7.414 | -11.294 | 0.017 | 0.000, 0.020, 0.020, 0.972 |
Look at epoch 1: the weights did not move, only the bias did. Sample (1,0) pushes down and sample (1,1) pushes it up by the same amount, so the average cancels. After the bias changes, the outputs differ and the weights begin to move.
Part 6: Using the trained model
Example A: feed , a value the model never saw.
Prediction: . The correct answer is 9. The model learned the rule, not a lookup table.
Example B: outputs after training are 0.000, 0.020, 0.020, 0.972. Choose a threshold of 0.5 (above it we say yes, below it no). That gives 0, 0, 0, 1, which is exactly the AND gate.
Part 7: Same and different
| Part | Same in A and B? | Why |
|---|---|---|
| Weighted sum | Same | both combine inputs with weights and a bias |
| Activation | Different | A's output can be any number; B's must be a probability |
| Loss | Different | A measures distance; B measures confident mistakes |
| Gradient pattern (error × input) | Same | the chain rule works the same way |
| Update rule | Same | step against the gradient |
| Learning rate | Different | tuned per problem |
Part 8: What a single neuron cannot do
A single neuron separates the data with one straight line. AND is separable by one line. XOR (output 1 when inputs differ) is not. To solve XOR, we stack neurons into layers, where the first layer's outputs become the second layer's inputs. The same loop still trains it, only the chain rule gets longer. The follow-up experiment tests this.
Glossary
| Term | Meaning |
|---|---|
| Parameter | an adjustable number inside the model |
| Weight | a parameter multiplying one input |
| Bias | a parameter added at the end |
| Weighted sum () | inputs × weights, added up, plus bias |
| Activation function | function applied to (e.g. sigmoid) |
| Prediction | the model's output |
| Target | the correct output |
| Loss | one number: how wrong the prediction is |
| Gradient | slope of the loss with respect to a parameter |
| Learning rate | size of each update step |
| Epoch | one pass over all training samples |
| Regression | predicting a number |
| Classification | predicting a category |
Full code
import numpy as np
sig = lambda z: 1/(1+np.exp(-z))
# Example A: regression, y = 2x + 1
x = np.array([1, 2, 3.]); y = np.array([3, 5, 7.])
w = b = 0.; lr = 0.05
for epoch in range(200):
p = w*x + b # forward pass
d = 2*(p - y)/len(x) # error signal, averaged
w -= lr*(d*x).sum() # gradient = error x input, then update
b -= lr*d.sum()
print(w, b, w*4 + b)
# Example B: AND gate
X = np.array([[0,0],[0,1],[1,0],[1,1.]]); t = np.array([0,0,0,1.])
w = np.zeros(2); b = 0.; lr = 1.
for epoch in range(1000):
p = sig(X@w + b) # forward pass
d = (p - t)/4 # error signal, averaged
w -= lr*X.T@d # gradient = error x input, then update
b -= lr*d.sum()
print(w, b, sig(X@w + b).round(3))
Try: change lr to 0.3 in Example A and watch the loss blow up, like the overshoot table in Step 4.
More from the lab

Gradient Descent: The One Loop Behind Every AI, From a Tiny Model to an LLM
Gradient descent is the loop that trains almost every AI model, including large language models. We run it on two tiny examples with real numbers: finding one best number, and teaching a model to pick the right next word. Then we see what changes when the same loop is scaled up to an LLM.

What Is a Neuron? Trying 7 Kinds on 3 Tiny Puzzles
A neuron is a tiny calculator: it takes numbers in, adds them up with weights, and gives one number out. We try 7 kinds of neuron, from the simplest to a small group of neurons, on three tiny puzzles (AND, OR, XOR). The kind of neuron barely mattered. What mattered was having more than one neuron working together.
Foundational