Introduction to Reinforcement LearningPart 3 · Deep RL and planning

Lecture 9Planning and models

Letting the agent learn how the world works and plan with it: models and their errors, Dyna, whether experience replay is a model, forward search and Monte Carlo tree search, and the road from AlphaGo to AlphaZero.

By the end you should be able to run Dyna-Q and one round of MCTS by hand.

Key ideas, one per episode

  • Ep. 31Learn the dynamics once; plan with them for any goal.
  • Ep. 32A model turns a little real data into a lot of imagined data.
  • Ep. 33Real steps teach the model; imagined steps teach the values.
  • Ep. 34A buffer remembers; a model imagines.
  • Ep. 35Plan for the state you're in, not the whole world.
  • Ep. 36Every node is a bandit; the visits are the answer.
  • Ep. 37A policy is good; a policy plus search is better.

Based on: Planning and Models, DeepMind × UCL Reinforcement Learning Lecture Series (2021), by Matteo Hessel; and, for MCTS and AlphaZero, Lecture 14: Monte Carlo Tree Search, Stanford CS234 (Spring 2024), by Emma Brunskill. The slides follow that order of topics, in our own words, figures and examples; they are not official material of the original course.

The title slide of Lecture 9: Planning and models

50 slides, written to be read on your own, with worked examples, “try it” boxes, small simulations and check-yourself questions (answers at the end). PDF, 1.4 MB.

Watch

7 short episodes of ACML Explains for this lecture. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 31 · 5:13

Learn the world first

Learn how the world moves once, and plan with it for any goal you like.

Learn the dynamics once; plan with them for any goal.

TranscriptEpisode 31

Recorded forever

I'm recording this. So everything silly I say is saved forever. Well, at least two years. Forever or two years, whichever comes first. Module nine: planning, models and search. A quick recap. So far, we told Bit nothing about the world. It tried things, got rewards, and learned values. Remember the ninety nine and the hundred gold coins? Bit learned they're related. But what gold actually is, it has no idea. Close to its data, it does fine. Far from its data, it can't extrapolate. But we know a tool that's great at exactly that. Show it data, and it finds the hidden pattern. Supervised learning.

Best of both

So far we had two worlds. Dynamic programming: hand me the model, and I'll solve it. But on big problems, it explodes. Model-free learning: assume nothing, and learn values straight from experience. What if we take the best of both? Learn a model from experience, then plan with it. That's model-based reinforcement learning. The loop changes a little. Experience no longer teaches the values directly. It teaches a model, and the model teaches the values. Careful with words, by the way. Until now, the model usually meant the policy. Today, a model means a model of the world.

Why bother

Let's start with the downside. We added a stage. And what happens when you add stages? I see engineers in the audience. More failure points. The model can be wrong, and then the values built on top of it are wrong too. Two sources of error instead of one. Now the upsides. Supervised learning is very good at this job. And many models can tell you how confident they are. Low confidence somewhere interesting? Maybe invest more effort there. Sounds familiar? That was the bandits. And the big one: fewer real samples. Simulating a thousand poker games is cheap. Running a robot is not. A robot run needs a robot, a room, sensors and people. On a bad day, the robot breaks. Asking a model costs a few chips and some electricity.

What's a model

Formally, a model approximates the dynamics of the MDP, with weights we call eta. Give it a state and an action, and it predicts the reward and the next state. Can every world be learned like this? In class, I played a game with a student. I say numbers, you guess the next one. The trick: every new run is longer than everything before it. Whatever pattern you learn, the next run breaks it. No function keeps up. Luckily, the real world lives in R n. It's a metric space, and there it works. This is my course, so I decide what's true.

Dynamics, not policy

So learning a model is supervised learning. The inputs: a state and an action. The targets: the reward and the next state. The data is our experience. A history is one episode. Experience is many of them. Notice what we're learning here. Not the policy. The dynamics. Many people get this wrong. Predicting two outputs at once is painful regression. So we train two models: one for the reward, one for the next state. That's all the genius.

Dynamics vs desires

Here's why keeping them apart matters. The reward depends on what I want. The transition does not. Teach a robot one task, and along the way it learns how the world moves. Now ask it for a different task. Do you learn the dynamics again? No. You got them for free. Chess puzzles teach you how the pieces move, and that carries over to a full game. Some worlds are deterministic, like chess. Some are not, like ice skating. Trust me on this one. You fall, randomly, and hard.

Into the wall

The simplest model predicts the expected next state. And expectations can bite. Say an action sends you left of a wall or right of it, half the time each. The expected next state? Right inside the wall. On a grid, the expectation can land half a cell away. A place that doesn't exist. Linear models suffer less here. A straight line has fewer ways to surprise you. And with linear values, the expected next state even gives the right expected value.

Expectations lie

Another one. A world gives minus a million once, plus a million once, and zero nine hundred and ninety eight times. The expectation: zero. The model did its job faithfully. And it's still useless. Sample one of those millions, and everything shifts. So make the model stochastic. Feed it some noise, and let it predict a next state from that noise. If you've met diffusion models, it's the same spirit. Start from noise, and shape it into something that fits the condition. A stochastic model can be chained, step after step, even when it's non-linear. It just adds some noise along the way.

The A-B table

The humblest model of all is a table. Count how often each state and action led where, and average the rewards. The A B example, eight episodes. A, zero, B, zero. Then six times B, one. And once, B, zero. From A, we always went to B, with reward zero. From B, reward one six times out of eight. Seventy five percent. That's a whole model, learned by counting. No action choices in this one. Things just happen. Next time, we plan with it. And find out it can lie to us.

Interactive lab

MCTS lab

Play tic-tac-toe against Bit, who plans with Monte Carlo tree search: random play-outs, and UCB to choose which branch to try next. Change how many simulations it gets and look inside its last search.

Before each move Bit runs a number of simulations from the current board. Each one walks down the tree by UCB, adds a node, plays randomly to the end and backs the result up. Then Bit plays the move it visited most.

Simple math

Search a little, with random play-outs

Each simulation has four steps. More simulations mean better estimates near the top of the tree.

1. Select
Q/N + c √( ln N(parent) / N )

From the root, go down to the child with the highest UCB score: good so far (Q/N), or tried too little.

2. Expand, 3. Play out
Add one new child, then play random moves to the end of the game.

No knowledge of tic-tac-toe is used beyond its rules: the play-out is completely random.

4. Back up
Win 1 draw ½ loss 0

Add the result to N and Q of every node on the path, each from the view of the player who made that move. Then play the most visited move.

You
0
Bit
0
Draws
0
Last search
–

Your move: you are X.

The game

Inside the last search

For each possible move: how often it was visited (N) and its value for the player to move (Q/N).

Yellow: the move chosen (the most visited). Darker blue: more visits.

Play

A game that goes with this lecture, and what to look for while you play.

Echo Heist

Teach a little robot. Steal back a city.

In this lecture: Among Echo Heist’s learners is a planning one. Try it on a mission and compare it with learning purely from trial and error.

Choose what your partner Echo senses, which actions it can take and what success means, then let a real learner train in your browser: tabular, planning, policy-gradient, deep-RL or imitation. When the door opens, the policy is frozen and Echo runs with what it learned.