How a puppy learns: reinforcement learning

Lesson6 min read

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:

1. Pick a movecurious or best-known2. The world respondsit moves to a new spot3. Reward or penaltytreat +10, mud −2, move −0.14. Update the notesthat move was good or badrepeat
Every move goes round this loop. After thousands of loops, the puppy’s notes point to the best moves.
  1. Pick a move: up, down, left or right.
  2. The world responds: the puppy ends up on a new square (or bumps into a fence and stays put).
  3. 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.
  4. 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:

It is like following a trail of breadcrumbs that the puppy lays down in reverse.

For when you're ready for the maths

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.

Tries so far: 0. Found the treat in 0 of the last 0. Curiosity: 100%. The treat is worth 10 points, each move costs 0.1.

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:

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:

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

  1. Does anyone tell the puppy the way to the treat?

    Show the answer

    No. It finds out by trying moves and getting rewards.

  2. 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.

  3. 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.

  4. 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.