Introduction to Reinforcement LearningPart 2 · Learning from experience

Lecture 5Model-free prediction

Learning the value of a policy from experience, with no model: Monte Carlo, temporal-difference learning, how the two differ on the same data, multi-step returns, and eligibility traces. With a first look at function approximation.

By the end you should be able to compute Monte Carlo and TD updates by hand, explain when each is better, and say what λ and eligibility traces do.

Key ideas, one per episode

  • Ep. 17Play, see what happened, average. No model needed.
  • Ep. 18Learn online, from one step: reward now plus your best guess.
  • Ep. 19Trust your state? TD. Don't? Monte Carlo.
  • Ep. 20Look a few steps ahead, or look back with traces.

Based on: Lecture 5: Model-Free Prediction, DeepMind × UCL Reinforcement Learning Lecture Series (2021), by Hado van Hasselt. 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 5: Model-free prediction

51 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.6 MB.

Watch

4 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 17 · 5:08

Monte Carlo learning

No rules, no model. Just play, see what happened, and average. It's enough to learn Blackjack.

Play, see what happened, average. No model needed.

TranscriptEpisode 17

No model

For two whole modules, Bit knew the rules of the world: p, every probability, every reward. Today we take that away. No model. Bit doesn't know how the world works. He only gets to play. There's a theorem: sample the world forever, and you'll know it exactly. Very nice. Good luck sampling even a small game perfectly. So we need methods that learn well from a finite pile of experience. First goal: prediction. Given a policy, learn its values, from samples only. Two methods, this episode and the next: Monte Carlo, and temporal differences. Today, Monte Carlo.

Monte Carlo

Method one is named after a casino: Monte Carlo. The idea is almost embarrassingly simple. Play. See what happened. Average. The name? The casino in Monaco. Rolling dice, many, many times. That's the whole method. You already did this, in module two. A bandit: for each handle, average the rewards you got. That's Monte Carlo. Now add states. Each state is its own little bandit. The episodes still last one step, and actions don't change the next state. Like showing an ad to a user: the user is the state, the ad is the action, a click is the reward. Average the clicks for each kind of user and each ad, and you have a value for every pair.

Features

But with millions of users, you can't keep a table with one number per user. So describe each state with a few features: numbers like age, time of day, past clicks. Call that vector x of s. And make the value a weighted sum of the features: v of s is w times x of s. A linear value function. And here's a nice surprise: a table is a special case. Give each state its own feature, one for that state, zero for everything else. One-hot features, and the weights are exactly the table entries. A linear model sounds weak. With good features, it goes surprisingly far.

Learning the weights

How do we learn w? Minimise the squared error between what happened and what we predicted. Take a gradient step: move w by α, times the error, times the features. Error times feature. Features that were on get the blame, or the credit. Same idea as supervised learning, with the return as the label. And with one-hot features, this is exactly the incremental average from module two. Same formula, new clothes.

Whole episodes

Now, real sequences. Bit plays a whole episode with his policy: state, action, reward, state, and so on, until it ends. For every state he visited, compute the return from there: everything that came after. Then nudge that state's value toward the return. Or just average all the returns you ever saw from it. Count a state once per episode, or every time it appears. First visit, or every visit. Both converge. And the step size: one over the number of visits gives the exact average. A constant α slowly forgets old games, which helps if the world changes. By the law of large numbers, from module two, the average converges to the true value. No p anywhere.

Blackjack

Let's try it on Blackjack. This is the example we played with in class. It's a nice fit: every game is an episode, it always ends, and the rules are a pain to write down as probabilities. You want your cards to sum closer to twenty-one than the dealer's, without going over. The state: your sum, from twelve to twenty-one. The dealer's visible card, ace to ten. And whether you hold a usable ace. Ten times ten times two: two hundred states. Below twelve, you always take a card; you can't go bust. Three numbers describe the whole situation. Near enough, that's the Markov state of Blackjack. Actions: stick, or take another card. The reward comes only at the end: plus one for a win, zero for a draw, minus one for a loss. The policy we'll evaluate: stick on twenty or twenty-one, otherwise always take a card. Bold. Maybe too bold.

What it learned

We ran it. After ten thousand games, the values are bumpy. Especially with a usable ace: those states are rare, so there's little data. After half a million games, the surface is smooth. Half a million games, in seconds, on a laptop. Try that at a real casino. And look: it's only high at twenty and twenty-one. Everywhere else, this policy loses money. Hitting on nineteen is not a great plan. And when the dealer shows an ace, everything drops. Monte Carlo found all of that without knowing a single rule of the game. Two hundred states, and ten thousand games still leave holes. Now imagine chess.

The catch

So what's the catch? Monte Carlo has to wait until the episode ends. Only then is the return known. Long episodes: slow learning. Episodes that never end: no learning at all. And because each return adds up many random steps, it's noisy. Can we learn before we know how the story ends? Yes. Next time: temporal differences. With me so far?

Interactive lab

TD vs Monte Carlo lab

A random walk over five states, A to E. Monte Carlo and TD(0) learn from the very same episodes. Compare their estimates with the true values, and their error curves averaged over a hundred runs.

The random walk of Sutton and Barto (Example 6.2): every episode starts in the middle, at C, and steps left or right with equal chance. Leaving on the right pays 1, on the left 0, so the true values are 1/6, 2/6, …, 5/6.

Simple math

Same episodes, two ways to learn from them

All rewards are 0 except leaving on the right, which pays 1. No discounting (γ = 1). Both estimates start at 0.5 in every state.

Monte Carlo
V(s) ← V(s) + α (G − V(s))

Wait until the episode ends, then move every visited state towards the return G it actually got (every visit counts).

TD(0)
V(s) ← V(s) + α (r + V(s′) − V(s))

Update after every step, towards the reward plus the current estimate of the next state: learning a guess from a guess.

The error
RMS = √( mean over A–E of (V(s) − v(s))² )

The true values v are 1/6 … 5/6. Constant step sizes never settle exactly, so the error levels off instead of reaching 0.

Episodes
0
RMS error, TD
–
RMS error, MC
–
Last episode
–

Press 1 episode to watch a walk.

Estimates and true values

Value of each state after the episodes so far.

true valueTD(0)Monte Carlo

Error over episodes

Your run, one episode at a time.

TD(0)Monte Carlo