How a puppy learns: reinforcement learning
Trial, error and rewards. See how a computer can learn a maze with nobody telling it the way.
Imagine a puppy dropped into a maze. It has no map. Somewhere there is a treat. Nobody tells it the way. How could it ever learn a route?
The answer is the same way real puppies learn: by trying things, and noticing what pays off. Computers can learn like this too. It is called reinforcement learning.
The learning loop
Every move the puppy makes goes round the same loop:
- Pick a move: up, down, left or right.
- The world responds: the puppy ends up on a new square (or bumps into a fence and stays put).
- Reward or penalty: a treat is worth a big reward. Mud costs points. Every move costs a tiny bit, so wasting time is a little bad.
- Update the notes: the puppy adjusts what it believes about that move.
Then it starts again. After many loops, the puppy's notes point toward the treat.
The puppy's notes
What exactly does the puppy remember? For every square in the maze, it keeps four notes, one for each move. Each note says: "how good do I think it is to go this way from here?"
At the start, every note is 0. The puppy knows nothing. After each move, it improves one note, like this:
new note = old note + a little step toward (the reward I just got + the best note from the square I landed on)
Look at that second part. The puppy does not only count the reward it just got. It also counts how good the next square looks. That way, a good score slowly spreads backward from the treat:
- The square next to the treat learns "going to the treat is great".
- Then the square before it learns "going to the square next to the treat is good".
- And so on, all the way back to the start.
It is like following a trail of breadcrumbs that the puppy lays down in reverse.
This method is called Q-learning. The update is Q ← Q + α × (reward + γ × best Q of the next square − Q). In the game, α (the step size) is 0.5 and γ (patience) is 0.95. That means a reward one move away counts a little less than one right now, which is why the puppy prefers short routes.
Try it
Watch a tiny puppy learn a tiny maze. Press Train a few times, and watch the arrows appear and settle.
The puppy knows nothing yet. Press Train and watch the arrows appear.
The arrow in each square is the move the puppy now thinks is best. Notice how the arrows nearest the treat appear first, and the ones near home appear last.
Curiosity: explore or play it safe?
If the puppy always picked its best-known move from the start, it could get stuck. Its first guess might lead to a long route, and it would never find a shortcut because it never tried anything else.
So early on, the puppy is curious. Often it picks a random move, just to see what happens. As it learns, the curiosity fades, and it mostly follows its best notes. You can see the curiosity percentage in the game.
Scientists call this the balance between exploring (trying new things) and exploiting (using what you know). Too much exploring wastes time, too little and you miss better options. People do this too, when we choose between our favourite restaurant and a new one.
Why does the puppy find the shortest way?
Two small things push it toward short routes:
- Each move costs a little. A long route piles up more costs.
- Later rewards count a little less. A treat ten moves away is worth slightly less than one two moves away.
You never told the puppy "find the shortest route". The rewards did that for you.
How big is this in real life?
The puppy keeps 4 notes for each of about 35 squares: roughly 140 numbers. That is tiny. Real systems use the same idea with far bigger "notes", stored in neural networks:
- Programs that play board games and video games at expert level.
- Robots that learn to walk or grab things, often practising in a simulation first.
- Chat assistants, which are fine-tuned using human ratings as rewards.
Words to know
- Reinforcement learning
- Learning by trial and error from rewards and penalties.
- Reward
- Points the learner gets when something goes well.
- Penalty
- Points taken away when something goes badly.
- Exploring
- Trying new things to find out what happens.
- Exploiting
- Using the best option you already know.
- Q-learning
- A classic reinforcement learning method that keeps a score for each move in each situation.
Check yourself
Does anyone tell the puppy the way to the treat?
Show the answer
No. It finds out by trying moves and getting rewards.
Why is the puppy random at the start?
Show the answer
So it explores and finds out what happens. If it always chose its first guess, it might never find a better route.
Why does a good score spread backward from the treat?
Show the answer
Each square's notes include how good the next square looks. So squares near the treat learn first, then the ones before them, and so on.
Name two things that make the puppy prefer the shortest route.
Show the answer
Every move costs a little, and rewards that are further away count a little less.