Grades 9–12
Building a Network That Learns
This week: the full algorithm behind modern AI, ending on a network that predicts the next word.
Day 1
Through the hidden layer
How did AI get so good?
Every AI tool you used this week was trained the same way: three steps, repeated billions of times.
Forward pass
Push the inputs through the network to make a prediction.
Back propagation
Measure the error and send it back through the network.
Weight update
Nudge every weight to make the next error smaller.
By Friday, you'll have done all three by hand.
High School · Grades 9–12
The forward pass
Each node works out a weighted sum, each input times its weight, added together. Then it adds its bias and applies an activation function.
ReLU turns any negative value into zero.
The nodes in the middle are a hidden layer: their values are never in the data, only worked out on the way.
High School · Grades 9–12
Key vocabulary
- Hidden layer
- Nodes between the inputs and the outputs. Their values are worked out, never given.
- Weighted sum
- Each value coming into a node times its connection's weight, added together.
- Bias
- A number added to a node's weighted sum.
- Activation function
- A function applied to a node's value. Here, ReLU: any negative value becomes zero.
- Forward pass
- Working out every node's value, layer by layer, from inputs to outputs.
High School · Grades 9–12
Activity
Forward through the middle
Each node works out a weighted sum: each input times its weight, added together. Predict each middle node, then the output.
High School · Grades 9–12
Activity
A bias and an activation
The middle node's bias, -10, is the $10 you owe a friend. ReLU holds any negative value at zero.
High School · Grades 9–12
Explore
Read a bigger network
Three inputs, four middle nodes, three outputs. Each road has its own path through a middle node.
- Click on connections to find each road's path.
- What do the 0.8 weights mean?
0.8 of green time for each car waiting.
High School · Grades 9–12
Discussion
- Why does the network need a middle layer at all?
- What did the activation function do in the Birthday Book Predictor?
- If a network gives the wrong answer, which numbers would you change?
High School · Grades 9–12
Day 2
Sending the error back
Who is to blame?
Back propagation sends the error back across each weight. A middle node receives the output's error times the weight connecting them.
A node that feeds several outputs adds up what comes back from each.
The input values never receive an error. Why not?
High School · Grades 9–12
Key vocabulary
- Error
- The output value minus the target value.
- Back propagation
- Sending the error back across each weight, so every node knows its share of the mistake.
- Middle error
- The error a middle node receives: each output's error times the weight between them, added up.
High School · Grades 9–12
Activity
Send the error back
The network predicts $20. The musician really collected $23.
error = output - target
The input values never receive an error, because nothing can change them.
High School · Grades 9–12
Activity
Errors that add up
Two outputs, wrappers and bottles, so each middle node collects two errors and adds them up.
| top middle | bottom middle | |
|---|---|---|
| round 1 | ?-399 | ?-199 |
| round 2 | ?399 | ?199 |
Write each one as a sum of products before you type it.
High School · Grades 9–12
Done early?
Draw it
Draw the City Park network on paper.
Label every connection with the error that travels back along it in round 1.
Then check your labels against the app.
High School · Grades 9–12
Discussion
- Why does the error travel back across the weights?
- Why do the input values never receive an error?
- A City Park middle node got a large error from one output and a small one from the other. Which matters more?
High School · Grades 9–12
Day 3
Where the error stops
No error gets through
A middle node that the activation function held at zero played no part in the output, so no error reaches it.
Changing a weight into that node wouldn't change the output: it would still be held at zero.
In calculus terms: ReLU's slope is zero there, and the error is multiplied by that slope.
High School · Grades 9–12
Key vocabulary
- ReLU
- A negative value becomes zero, and a positive one passes through unchanged.
- Inactive node
- A node the activation function holds at zero. It receives no error.
- Slope
- How much a function's output changes for a small change in its input. ReLU's is 0 below zero, 1 above.
High School · Grades 9–12
Activity
One node at a time
The middle nodes measure the hours of sleep over and the hours short. Only the one above zero receives an error.
| hours over | hours short | |
|---|---|---|
| round 1: 10 slept, 8 needed | ?4 | ?0 |
| round 2: 7 slept, 11 needed | ?0 | ?3 |
High School · Grades 9–12
Activity
A node that never wakes
The bottom middle node's bias, -20, is more than any day's dogs and cats can make up, so it is held at zero every day.
| top middle | bottom middle | |
|---|---|---|
| round 1 | ?-3.5 | ?0 |
| round 2 | ?3.5 | ?0 |
For each zero, write one sentence saying why that node got no error.
High School · Grades 9–12
Discussion
- Why did one yawn node receive no error in each round?
- The pet rescue's bottom node never receives an error. What does that mean for its weights?
- Why is it useful that only the active nodes get an error?
High School · Grades 9–12
Day 4
Gradient descent
Gradient descent
Every connection now knows its share of the error. Now the network uses it to change its weights.
The learning rate keeps each step small. Moving every weight a small step against its error is gradient descent.
High School · Grades 9–12
Key vocabulary
- Weight update
- Subtracting input value × error × learning rate from a weight.
- Learning rate
- A small number that sets how big each weight update is.
- Gradient descent
- Training by moving every weight a small step in the direction that makes the error smaller.
- Overshoot
- Stepping past the best weight, so the error grows instead of shrinking.
High School · Grades 9–12
Activity
Update a weight
2 classes take part. The network predicts 3 bags of cans, but the target is 6. The learning rate is 0.02.
High School · Grades 9–12
Activity
Watch it settle
Keep stepping through the 8 drives, and note the output error each time.
Why does it never reach zero?
It shrinks to within about 1 bag and stays there: the bags are never exactly 3 a class.
High School · Grades 9–12
Activity
Find the learning rate
At a learning rate of 0.2, this network's weights blow up.
- Watch it train at 0.2. What goes wrong?
- Try the other learning rates, and record what each one does.
- Find the one that settles.
What goes wrong above it? And below it?
High School · Grades 9–12
Sketch the error
Sketch the error against the weight as a valley. Draw each learning rate as steps down it.
Too small
Tiny steps that never reach the bottom.
Just right
Steps that land at the bottom.
Too large
Steps that jump across and climb the other side.
High School · Grades 9–12
Discussion
- Why multiply by the input value in the update rule?
- What happens when the learning rate is too large? Too small?
- Why is it called gradient descent?
High School · Grades 9–12
Day 5
Training, testing and the next word
Thousands of steps at once
This week you did every step by hand. Today the networks do thousands of steps at once, in swift mode.
A network can fit its training data without learning the pattern, like memorizing answers without understanding them. That's overfitting.
So we judge it on testing data it has never seen.
High School · Grades 9–12
Key vocabulary
- Training data
- The examples a network learns from.
- Testing data
- Examples the network never learned from, used to check whether it really learned.
- Overfitting
- Fitting the training data so closely that the network does worse on new data.
- Swift mode
- Running the network's steps quickly, without showing each calculation.
- Language model
- A network that predicts the next word from the words before it.
High School · Grades 9–12
Activity
Train, then test
The Harbor Predictor learns swimmers and sailboats from sunshine and wind, through a middle layer.
- Train it in swift mode across all the training days.
- Switch to the testing data, days it has never seen.
- Which answer did it learn better?
The sailboats: off by about 0.7 a testing day, against about 1.1 for the swimmers.
High School · Grades 9–12
Activity
A deeper network
Two hidden layers, and 255 steps a round.
- Train it in swift mode.
- Switch to the testing data.
- Did it learn correctly?
High School · Grades 9–12
Together
Predict the next word
A tiny language model that learns from 7 short sentences, like "the dog is cute" and "the sky is blue".
After training, which words does it expect after "is"?
cute, big, blue and hot: the words that followed "is" in its training sentences.
High School · Grades 9–12
Discussion
- Why did the Harbor Predictor learn the sailboats better than the swimmers?
- The Next Word Predictor only knows 10 words. What would change for a real language model?
- The Next Word Predictor only says what it saw in training. What does that mean for real AI?
High School · Grades 9–12
How does an AI model learn?
Draw a network of your own: two inputs, two middle nodes, one output. Choose its weights.
- Run one forward pass.
- Invent a target and send the error back.
- Do one weight update at a learning rate of 0.1.
In a paragraph: how does an AI model learn?
High School · Grades 9–12