Q-learning looks ahead to the best possible next action, even if the behavior policy is still exploring.
Introduction to Reinforcement LearningPart 2 · Learning from experience
Lecture 6Model-free control
From predicting to improving, still without a model: Monte Carlo control and GLIE, SARSA, Q-learning and off-policy learning, the overestimation that double Q-learning fixes, importance sampling and expected SARSA.
Key ideas, one per episode
- Ep. 21Learn q, explore, then shrink the exploration.
- Ep. 22Learn the greedy policy while you explore.
- Ep. 23Don't let the same numbers pick and score.
Based on: Lecture 6: Model-Free Control, 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.
Watch
3 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 21 · 5:12
Learning to act
Bit can tell you how good a policy is. Today he learns to find a better one, with no model at all.
Learn q, explore, then shrink the exploration.
TranscriptEpisode 21
From predicting to acting
Five modules in, Bit can predict. Give him a policy, and he'll tell you how good it is. But nobody wants a critic. We want Bit to act, and to get better at it. Today he learns to act. No model, no map. Just experience. Remember the recipe from dynamic programming? Evaluate the policy. Improve it. Repeat. That loop is called generalised policy iteration. Today we run it without a model.
Why q, not v
Here's the snag. To improve greedily with v, you need to know where each action leads. Which states come next, and how likely each one is. That's a model, and we don't have one. So learn q instead. q of s and a: how good is this action, right here. With q, improving is easy. Look at the row for your state, and take the biggest number. Like choosing a dish from reviews. You don't need to know what happens in the kitchen. Monte Carlo and TD can learn q from experience. Same methods as before, one letter different. So for control, we learn q, not v. That's the whole trick of this module.
Greedy is a trap
But pure greedy is a trap. Say the first action you tried paid off a little. Greedy will pick it forever. The other actions never get a chance to show what they're worth. So we explore. ε-greedy: most of the time, take the best action. Once in a while, a random one. And the theory backs it up. An ε-greedy policy built on your q is never worse than the policy you had. Small detail: the random move can land on the best action too. Exploring includes the obvious.
The cleaning robot
Now, how big should ε be? In class, I used a cleaning robot. Fix ε at point one, and ten percent of the time it does random things in your living room. That's not why you bought it. You want ε to shrink. One over k works well, where k counts the episodes. Day one, it explores a lot. By the two hundredth run, it should know the house. And one over k never quite reaches zero. A sliver of curiosity stays, and keeps shrinking. Nobody wants a robot that's still curious about the couch after a year.
GLIE
That gives us a condition with a funny name: GLIE. Greedy in the limit, with infinite exploration. Two promises. Every state and action gets tried infinitely often. And in the end, the policy becomes greedy. Keep both, and Monte Carlo control finds the optimal action values. A strong guarantee. But notice the second half. I do ask you to stop exploring eventually. At the optimum, every random move is a wrong move.
SARSA
Monte Carlo waits for the end of an episode. We already know the fix: TD. TD on q has a name you can pronounce: SARSA. The name is the data it needs: a state and an action, the reward, then the next state and the next action. Nudge q of S and A toward the reward, plus γ times q of the next pair. Start the table at zeros. The start state is given, like in chess, or random, like the cards you're dealt. Then act ε-greedily on q, and update after every single step. Update the policy after every step, or after every few episodes? Every step is smoother, but noisier. Which is better? There's no way to know in advance. Solve that in general, and that's great. If not, I won't be mad.
Step sizes that behave
Does SARSA find the optimum? Yes, with GLIE, and with step sizes that behave. Two conditions on the step sizes. Their sum must be infinite. The sum of their squares must be finite. α equal to one over t passes both. The harmonic series grows forever. After a million steps it's only about fourteen point four, but it never stops. The squares add up to π squared over six. About one point six four. Big enough to get anywhere. Small enough to settle
Ten lines and a windy gridworld
down. All those slides of maths. And in code, it's about ten lines of Python. Borderline funny how simple it is. That's how good ideas usually look. We ran it on a windy gridworld. In the middle columns, the wind pushes Bit upwards. The first episode takes about seven hundred steps. Bit gets blown around a lot. After a hundred and seventy episodes, about eight thousand steps in all, an episode takes about twenty two steps. The shortest possible path is fifteen. Our own runs: fifty of them, ε at point one, step size one half. The curve gets steeper as Bit learns. Why not fifteen? Because ε still throws in a random move now and then, and the wind punishes every one.
On-policy
SARSA is on-policy. It learns the value of the policy it actually follows, random moves included. That sounds like a detail. It isn't. It changes which path Bit thinks is best. Learn q, explore, shrink the exploration slowly, and update with SARSA. That's model-free control. Next time: learning about one policy while following another. Q-learning, and a walk along a cliff.
Behaviour vs target
SARSA learns about the policy it follows. Today, Bit learns about one policy while following another. The one you follow is the behaviour policy. Call it μ. The one you want to learn about is the target policy, π. When they're the same, that's on-policy. When they differ, off-policy. Why would anyone want that? In class, I gave three reasons. SARSA can't do that. It learns about whatever it does, mistakes included.
Why off-policy?
One. A robot vacuum explores the house with a random walk. But what you want to know is the best route. Two. You want to become a good chess player. But all you do is solve two-move chess puzzles. Three. Learn from someone else: another agent, a human, an oracle. Their experience, your policy. In all three, the behaviour gathers the data, and the target is what we actually want. And one pile of exploration can serve many goals. The same walk teaches the way to the kitchen, and to the living room. The price: when μ and π are very different, learning is slower. Off-policy learning lets one stream of experience teach many lessons.
Q-learning
The most famous off-policy method is Q-learning. SARSA uses the next action Bit actually takes. Q-learning uses the best next action: the max. So the target is greedy, whatever Bit really does next. Exploration doesn't leak into the values. Written out: q of S and A moves toward the reward, plus γ times the biggest q in the next state. It converges to the optimal q, as long as every pair keeps getting visited and the step sizes behave. The behaviour can be anything that keeps exploring. ε-greedy is the usual choice. One word changed. Next action became best action. Let's see what that does.
The cliff
A grid, four rows by twelve. Start bottom left, goal bottom right. Between them, along the bottom edge, a cliff. Step off, and you lose a hundred and go back to the start. Every other step costs one. So the shortest path, hugging the edge, scores minus thirteen. A hundred points of penalty for a single slip. That's a steep price. Both learners explore with ε at point one. One random move in ten. Bit doesn't know any of this. He just walks, falls, and learns. Watch the two paths closely. This is where the two methods part ways.
Our run
Our own run: five hundred episodes, a hundred runs each. Q-learning finds the edge path. Thirteen steps. Optimal. SARSA picks a path away from the edge. Longer, but safe. Now look at the reward while they learn. SARSA earns about minus twenty six per episode. Q-learning earns about minus fifty. Walking the edge, with one random step in ten, it falls about once every three episodes. That's a hundred runs averaged, so the gap is real, not luck. Q-learning learned the better policy. SARSA earns more while exploring. Why the gap? Q-learning's values assume a perfect, greedy walker. But the real walker still makes random moves, right next to the edge. SARSA's values include its own random moves. So it learns that the edge is dangerous, for someone like itself.
Which one?
So which one is better? It depends on whether the falling is real. A real robot next to a real staircase? SARSA's caution is worth a lot. Training in a simulator, then switching exploration off? Q-learning's path is the one you want. And for your own problem, there's no way to know in advance. Many people start with SARSA and switch to Q-learning later. There's no promise that's better either. Methods that choose for you do exist: meta-learning, AutoML. They don't work that well. Yet. Pick one and pray. This course runs on a lot of faith. Faith, and a lot of experiments.
The flip
One more thing about Q-learning. It's very loyal to its first guesses. It keeps exploiting whatever looks best, until the estimates of two actions cross. I call that a flip. Starting from scratch, Q-learning is very abusive to its first guesses. It stays put, until a flip. On the reward curve, it looks like noise for a long time. And then, suddenly, a wave. It doesn't converge, doesn't converge, and after three hours it suddenly works. So much fun. Especially when your bonus depends on it.
How many episodes?
How many episodes is a lot? Fewer than you think. A hundred thousand episodes is reasonable. For a tiny game, that's a few minutes. Chess, at fifty milliseconds a game: about seventy two thousand games an hour on one core. Four cores: close to three hundred thousand. One hospital resource-allocation project ran fifty million episodes over a weekend. And every one of those episodes is free data. Nobody had to label anything. So don't be shy with episodes. Let it run.
Recap
SARSA: on-policy, and careful. Q-learning: off-policy, with a greedy target, and bold. Both are about ten lines of code. Both converge, under the right conditions. And notice: neither one needed a model. Just a table, a step size, and a lot of walking. But Q-learning has a dirty secret. It's too optimistic. Next time, we take it to the casino.
The max of noisy zeros
Q-learning takes the max of its estimates. That sounds harmless. It isn't. Take ten actions, all worth exactly zero. Your estimates are noisy: some a bit high, some a bit low. Now pick the biggest. On average, the biggest of ten noisy zeros is about one point five. Ten noisy zeros, and the winner always looks like a genius. Every action is worth zero, and the max says one and a half. That's maximisation bias. The max picks the luckiest error, every single time. And Q-learning doesn't take the max just once. It takes it at every step, and bootstraps on it. The error compounds.
Two actions
In class, I told it as a story with two actions. Try each one once. The good action has an unlucky draw. The bad one has a lucky draw. Now Q-learning keeps picking the bad one. The good one barely gets tried, so its low estimate never gets fixed. Until, one day, a flip, and the estimates trade places. The root of it: the same numbers choose the action and score it. That's the trap of using your own estimates for everything. You can't be your own referee.
The casino
Now, the casino. Roulette. Bit can bet one dollar on one of a hundred and seventy bets. Or he can stop, and walk away. Every bet loses about five cents on average. Some win big, rarely. Some win small, often. Some bets pay thirty five to one. That's the jackpot Bit remembers. The right answer is to stop. The house always wins. On average. Standard disclaimer: the presenter is not an investment adviser. Q-learning does learn this eventually. It just takes millions of episodes.
Our roulette run
We ran our own version: one state, a hundred and seventy bets, γ at point nine five. Q-learning remembers the wins. After a million bets, it thinks a typical bet is worth about a hundred and six dollars. Not minus five cents. Plus a hundred dollars. It would play forever. Every bet looks like a winner, and stopping is worth exactly zero. So it never stops. Double Q-learning, on the very same bets: about minus twenty five cents. A bit gloomy, but it stops betting. Same data. One learner wants to stay all night. The other one goes home.
Double Q-learning
So how does double Q-learning work? Keep two tables. One table picks the best action. The other one scores it. Each step, flip a coin and update one of them. To act, use their average. If one table overestimates an action, the other has no reason to agree. So the bias washes out. And acting on their average means you never act on one table's lucky guess alone. The same idea scaled up to Atari games, as double DQN. On a scale where a hundred is a typical human, it improved many games. Atari, by the way, is what we played at the arcade. A big wooden cabinet, a joystick, and another coin every time you lost.
Importance sampling
Back to off-policy learning. The data comes from μ, but we want averages under π. A small example. Two actions: left pays one, right pays ten. μ goes left eighty percent of the time. π goes right eighty percent of the time. The plain average of μ's rewards is two point eight. But π's true average is eight point two. The fix: weigh each sample by π over μ. A left counts a quarter. A right counts four times. Now the weighted average is eight point two. A rare action you want more of? Multiply it back up. Formally, you multiply and divide by μ. Multiplying by one never hurt anybody. That's importance sampling. It turns averages under one policy into averages under another. And on average, under μ, the ratio is exactly one. So nothing gets biased.
Monte Carlo vs TD ratios
For Monte Carlo, the correction is a product: one ratio for every step of the episode. Ten steps, with a ratio of four each: that's about a million. One unlikely episode gets a weight of a million. The variance explodes. The ratios multiply, so a few rare episodes can dominate everything. TD needs just one ratio, for one step. Much lower variance. That's one reason off-policy methods built on TD are far more common.
One family
And for q, there's a cleaner trick. Don't sample the next action at all. Average over all the next actions, weighted by π. That's expected SARSA. If π is greedy, that average is just the max. Expected SARSA becomes Q-learning. Expected SARSA costs a little more per step, but it removes the noise of sampling the next action. So it's really one family. SARSA samples, expected SARSA averages, and Q-learning takes the
Module six, done
max. And that's module six, done. Bit can now act without a model. On-policy, off-policy, and without fooling himself. But every method so far keeps a table. One number for every state and action. Three ideas carry over to everything that comes next: exploring, off-policy targets, and double learning. Next module: what to do when the table won't fit. Not in memory. Not in the universe.
Interactive lab
Gridworld lab
Train Q-learning or SARSA on a small gridworld with hazards and walls. Tune ε, the step size and the discount, add slippery actions, and watch the value map and the greedy policy sharpen episode by episode.
The gridworld keeps the idea compact but brings in state, path dependence and delayed rewards. Watch the agent trade immediate moves against long-run return while the learned policy map gradually sharpens. It starts by connecting the bandit of Lecture 2 to sequential decisions.
From isolated action choice to sequential decision making
A bandit can be read as reinforcement learning with only one state: there are multiple actions, but every pull returns you to the same decision point. A gridworld adds many states, delayed consequences, and transitions, so the cost of a choice can propagate several steps into the future.
Bandit = one state, many actions
Exploration asks: which option seems best when feedback is immediate and the decision context never changes?
Gridworld = many states, delayed consequences
Now each move changes the next state. A poor exploratory step can lead toward a trap or away from the goal.
Exploration still matters
The same tension remains, but decisions now shape future opportunities. Learning a policy means learning where each action leads.
How the gridworld learner updates its action values
In reinforcement learning the estimate is attached to a state-action pair. After each move, the learner nudges that estimate toward a target built from the immediate reward and the next state's value.
SARSA updates toward the action the agent actually expects to take next, so it learns under its own exploratory behavior.
Optional epsilon decay makes the learner search broadly at first and then become more stable once it has mapped the environment.
Policy and value map
Cell shading reflects the current value estimate. Icons show the greedy action, and numbered badges mark the latest trajectory.
Episode reward history
Returns should rise as the policy discovers a safer, shorter route to the goal.
Most recent trajectory
This makes the last episode legible as a path rather than only as a scalar reward.
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: One of the learners Echo Heist trains in your browser is a tabular one. Change what Echo can sense or what it is rewarded for, train, and watch the route it learns change.
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.




