Bounded to x ∈ [−1.2, 0.5] and |v| ≤ 0.07. The episode ends at x = 0.5; every step costs −1.
Introduction to Reinforcement LearningPart 2 · Learning from experience
Lecture 7Function approximation
When there are too many states for a table: features and gradient descent, linear value functions and tile coding, control with approximation, convergence and the deadly triad, batch methods such as LSTD, and a first look at deep Q-networks.
Key ideas, one per episode
- Ep. 24Generalise: let similar states share what they learn.
- Ep. 25Good features take a line a long way.
- Ep. 26Approximation, bootstrapping, off-policy: handle with care.
Based on: Lecture 7: Function Approximation in Reinforcement Learning, 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 24 · 5:15
Too many states
Ten to the hundred and seventy states. One per atom, and you run out of atoms. Time to stop using tables.
Generalise: let similar states share what they learn.
TranscriptEpisode 24
The Q-table
Module seven. We're past halfway now. So far, every value lived in a table. Rows are states, columns are actions, and each cell holds a q value. The upside: it's child's play. The downsides: it's sparse, because most actions make no sense in most states. It's huge. And it may not fit in memory. And worst of all: the table has no idea that two neighbouring states are related.
Too many states
The sentence to remember from this module: too many states to visit them all. Backgammon, a fairly simple game, has about ten to the twenty states. The observable universe has about ten to the eighty atoms. Go has about ten to the hundred and seventy states. Give each state its own atom, and you run out of atoms. And a robot's sensors are continuous: infinitely many states. Chop them into bins, and the count explodes anyway. So we can't visit every state. Not even close. We'll only ever see a sliver of them.
The gold bag
Here's the example I use in class. A computer game. You keep finding a bag with ninety nine gold coins. You found it many times, and the reward averaged ninety nine. Now you find a bag with a hundred coins. What's the reward? Everyone says a hundred. Everyone. You just made two hidden assumptions. One: more gold is better. Who told you that? Two: the relation is roughly linear, at least nearby. Ask about a thousand coins, and a room of economists will say less than a thousand. Diminishing returns. Mathematically, it's not true. Practically, it's excellent. Notice what you did: you generalised from states you saw, to a state you never saw.
Neighbourhoods
A table can't do that. State ninety nine, visited a hundred times, tells it nothing about state one hundred. They're just two rows. I never defined any relation between them. Functions have neighbourhoods. Anyone remember calculus one? Any calculus one traumas? A neighbourhood is exactly what a table doesn't have. So we write v of s as a function with weights w, and we learn the weights. Nothing changes except the representation. Monte Carlo and TD still work. Pick the weights well, and one learned number covers a whole neighbourhood of states.
Aggregation and the ladder
The simplest function is state aggregation. Ninety to a hundred coins? Call it one state. Groups of ten make the problem ten times smaller. But good groups are hard to find in practice. In the gold game, a bag of ninety one and a bag of ninety nine become the same thing. Close enough. Then linear functions: a weight for every feature. Then anything differentiable, like a neural network. Tables and aggregation are special cases of linear. Each step up generalises more, and promises less.
The rubber band
And reinforcement learning data is weird. Here's a game from class. Map a room in an hour, by taking random steps. Walk long enough, and you cover every corner. Now play again, tied to one corner with a rubber band. The farther you go, the harder it pulls. We simulated both: twenty thousand steps, in a room of twenty one by twenty one squares. Free, Bit visits every square. Tied, he reaches about a third of the room, and never the far corner. The bias is in the sampling, not in the room. Chess is the same. One game is shaped by its opening. You can't jump to a random board. And Bit chooses his own data. If he exploits, he samples around his current policy, and nowhere else. In supervised learning, someone hands you the data. Here, the learner and the data collector are the same agent. How to sample well, when you're tied to your own experience, is still an open research problem.
Moving data
And the data keeps moving. Supervised learning assumes the data doesn't care what you predict. In reinforcement learning, the policy changes as you learn, so the data changes too. Bootstrapping makes the targets move. And sometimes the world itself moves. Warehouse robots in the holiday rush live in a different world than in the quiet season. A student once suggested putting time into the state. Great idea. Doesn't work. So: not independent, and not stationary. Two assumptions of supervised learning, both gone.
Gradient descent
So how do we learn the weights? Gradient descent. You know the picture: hills, valleys, and a ball rolling down. Not every course has to reinvent the wheel. The hill is the squared error between the true value and ours. We step downhill, a little at a time. We don't know the true value, so we sample. The update is the step size, times the error, times the gradient. That's stochastic gradient descent. One sample, one small step. And small steps matter. Big ones overshoot the valley. With Monte Carlo, the target is the real return. With TD, it's a reward plus our own guess. Remember that. It'll bite us later.
Recap
Too many states, so we generalise. A table remembers. A function guesses, and guessing well is the whole game. Next time: features. Circles, tiles, a car stuck in a valley, and the most dangerous line in statistics.
Feature vectors
A linear value function needs features. A feature vector, x of s, describes the state with numbers. The distance of a robot from some landmarks. Trends in the stock market. Which pieces stand where on a chessboard. Then the value is a weighted sum: each feature, times its weight. In the gold game, one feature is enough: the number of coins. Learn a weight of one, and a hundred coins means a hundred. Choose the features, and you choose what the learner can and can't notice.
The update
The update is beautifully simple: the step size, times the prediction error, times the feature vector. A classic. Features that were active get the blame, or the credit. The error says how far the prediction missed. The features say which weights were involved. For linear functions, the error surface is a bowl. Monte Carlo slides to the bottom: the best fit there is. You could also log states and returns, and fit a regression afterwards. People do. But the returns are noisy. It converges, but it's noisy. High variance, as always with Monte Carlo.
Life is not linear
But life is not linear. Anyone who tells you it's linear is lying. Hooke's law: pull a spring, and the force grows linearly. Up to a point. Pull harder, and the spring deforms, then breaks. Heat it, and the constant changes. Then you meet the second order differential equation, and find out you've been lied to your whole life. So linear only works with good features. And choosing them is the hard part. Physics teachers start with the linear version for a reason. It's a great first approximation. Just not the last one.
x(s) in code
A practical note: x of s doesn't really exist in code. There's no function called map to vector. You write the state in your chosen representation from the start. In the gold game, the state already is a number of coins. The feature vector is just that number.
Coarse coding
One classic way to build features is coarse coding. Scatter circles over the state space. Each circle is one feature: one if the state is inside it, zero if not. A state lights up a few overlapping circles. Learn from it, and nearby states that share those circles learn too. It's a bit like a mixture of Gaussians, laid over the states. Small circles: narrow, precise generalisation. Big circles: broad and rough, with fewer features to learn. Stretch the circles, and they generalise more in one direction than the other. And the size of the circles is a design choice, made before learning starts.
Tile coding
Tile coding is the tidy version. Lay a grid over the space. Then another one, shifted a little. Then another. A state falls into exactly one tile per grid. Eight grids, eight active features. Always. Each grid on its own is coarse. Together, they pin the state down much more finely. And the number of active features never changes, so every update costs the same. Cheap, fast, and surprisingly good. That's why tile coding was a favourite for years, before deep networks took over.
One point, infinitely many lines
Now, a warning. Fit a line through one point. How many lines are there? Infinitely many. Your solver hands you one of them: a random line. The most dangerous kind of thing: technically possible, practically terrible. When the variance blows up, the code crashes, and you notice. This one runs fine and returns garbage. And then you debug it forever. With features, the lesson is that each weight needs data from enough different states. A feature that fires in only one state has nothing to generalise from. So when your results look too clean, check how many states each feature has actually seen.
Mountain Car
Let's try it on a classic: Mountain Car. A weak car, stuck in a valley. The engine can't climb the hill directly. The car has to rock back and forth to build up speed. The state is position and velocity: two continuous numbers. A table can't hold that. The car starts near the bottom of the valley, with zero speed. Every step costs one, until it reaches the top on the right. Our run: linear SARSA, with eight tilings of eight by eight, averaged over ten runs. All the weights start at zero. Since every step costs one, zero looks optimistic. So the car explores on its own, without ε. The first episode takes about sixteen hundred steps. After three hundred episodes: about a hundred and twenty five. The learned cost to go peaks at the bottom of the valley, standing still. It drops steeply wherever the car already has enough swing. One global line would be terrible here. Many small tiles, each with its own weight, do the job.
The best you can do
With function approximation, control reaches the best your approximator can do. A perfect approximator gets you the optimum. Anything less, and it doesn't. That's why, from linear on, I stop promising you the optimum. But the best your approximator can do is often very, very good. And the better the features, the closer that gets to the real thing. Next time: what happens when learning doesn't just miss the optimum, but blows up.
The ladder of promises
Every step up the ladder buys power and costs promises. Today, let's be precise about the cost. Tables: a global optimum, guaranteed. The only place in your academic life where anyone ever promised you a global optimum. Linear sits in the middle: reasonable theory, as long as your features are good. Linear: decent answers, especially when states are sampled densely. Any smooth function looks linear up close. Neural networks: the cool kid on the block. Great in practice. No promises at all. Why none? Formally, you could be approximating a function that's continuous everywhere and differentiable nowhere. The Weierstrass function. In class, I first called it Dirichlet. Even the lecturer makes mistakes. So the further up the ladder, the more we lean on practice, and the less on proofs.
Monte Carlo converges
Prediction first. Linear Monte Carlo always converges, to the weights with the smallest value error. Unless nothing happens at all. No dynamics, nothing to learn. Monte Carlo sees whole episodes. It has the global picture. Its answer depends only on the data and the features. The state doesn't even have to be Markov. That makes Monte Carlo the safe default, when you can afford to wait.
TD chases its tail
TD is different. Its target, a reward plus our own guess, depends on the very weights we're updating. So TD isn't really rolling downhill. It's chasing its own tail. Remember the ball rolling downhill? For TD, the hill moves every time the ball does. Mostly, that's fine. TD belongs to a broader family of stochastic approximation methods, and those usually converge. Usually. Plain gradient descent always converges, with small enough steps and a fixed target. TD's target isn't fixed. When linear TD does converge, it lands near the Monte Carlo answer, not on it. Often faster,
w and 2w
though. Here's the classic counterexample. Two states. The first has feature one, the second has feature two. There's one weight, w, so their values are w and two w. The reward between them is zero, so the true values are zero. Now update only the first state, again and again. Its target is γ times two w. If γ is above one half, the target is bigger than w. So w grows. Which makes the target grow. With γ at point nine and a step size of point one, w grows eight percent per update. After a hundred updates, it's twenty two hundred times bigger. The truth is zero. Watch it compound. Every update multiplies w by one point zero eight. After ten updates, w has more than doubled. Nothing in the data is wrong. The rewards are all zero. The explosion comes entirely from the update. And this isn't a rare corner case. Two states, one weight, and it already breaks.
What saves it
What saves it? Update both states, on-policy, along the episode. The second state's update pulls w back down, harder. Now w shrinks, by about a third every episode. At γ point nine, that's a multiplier of about point seven per episode. Almost nothing left after twenty episodes. Or use tabular features: one weight per state. Then it's just regression. No explosion. Or look further ahead. With a λ-return, it's safe once λ is above four ninths. Same features, same rewards: converge or explode. It depends on which states you update, and how often.
The deadly triad
What went wrong is a combination of three things. Function approximation: states share weights. Bootstrapping: targets use our own guesses. Off-policy: the updates don't follow the episode. Together, they're called the deadly triad. Any two of them are fine. All three, and learning may diverge. Think of a fire triangle: heat, fuel and oxygen. Take one away, and nothing burns. Off-policy shows up even when you don't plan it. Replay old data, and you're off-policy.
The one table
If you remember one thing from this module, let it be this table. Monte Carlo converges everywhere: table, linear, nonlinear, on-policy or off. TD on-policy: fine with tables and linear features. Not guaranteed with neural networks. TD off-policy: only the table is safe. For control, the picture is similar. Tabular Q-learning is safe. With function approximation, all bets are off.
Over the hill
Why does Monte Carlo do better? Picture a knight galloping up a hill, in thick fog. At the top, is there a higher hill, or a valley? TD is that knight. It only sees one step ahead. Monte Carlo already rode the whole way. It knows what came after the hill. But Monte Carlo needs episodes. If your task never ends, define one anyway: a day, or a week. Or use TD, carefully, and keep an eye on the triad. I once worked on a traffic light project. One junction. An episode was one day. Complaints about that junction: not to me. The day ends, the traffic resets, and you get a return. Imperfect, but usable.
Tracking
For control with approximation, the theory is even thinner. And often we don't even want convergence. We want tracking: a policy that keeps adapting as the world changes. That makes sense: the world drifts, and a policy that stopped learning would slowly get worse. Next time: squeeze more out of every sample with batches and replay. And meet DQN.
Interactive lab
Mountain Car lab
An underpowered car must rock back and forth to climb a hill. Train linear SARSA on tile-coded features, watch the steps per episode drop, look at the learned cost-to-go, and replay the greedy policy.
Tile coding turns the two numbers of the state, position and velocity, into a handful of active features out of thousands, and the value is a weighted sum of them. The setup follows Sutton and Barto (Example 10.1): −1 per step, weights start at zero (which is optimistic), and the agent acts greedily.
A car, two numbers, and thousands of tiles
The engine is too weak to drive straight up the right-hand hill, so the agent must learn to swing back first. The state is the position x and the velocity v.
Each tiling cuts the (x, v) square into 8 × 8 tiles, shifted a little; exactly one tile per tiling is active, so the features are sparse.
The gradient is 1 on the active tiles and 0 elsewhere, so each step changes only a few weights. Here α = (step-size numerator) / tilings.
The weights start at 0, which is optimistic: every step costs −1, so untried actions look better than tried ones.
The hill
The car starts in the valley. Replay shows the greedy policy from x = −0.5 at rest.
Steps per episode
On a log scale; lower is better.
Learned cost-to-go
Steps to the goal, as the agent estimates them: −maxₐ q(x, v). Darker is further. The line is the last replay.



