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.

By the end you should be able to write MC and TD updates with features, explain why TD can diverge, and name the parts of DQN.

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.

The title slide of Lecture 7: Function approximation

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

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.

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.

Simple math

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.

Dynamics
v ← v + 0.001 a − 0.0025 cos(3x)
x ← x + v, with a ∈ {−1, 0, +1}

Bounded to x ∈ [−1.2, 0.5] and |v| ≤ 0.07. The episode ends at x = 0.5; every step costs −1.

Tile coding
q(s, a) = Σ over active tiles of wᵢ

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.

Semi-gradient SARSA
w ← w + α (r + q(s′, a′) − q(s, a)) ∇q(s, a)

The gradient is 1 on the active tiles and 0 elsewhere, so each step changes only a few weights. Here α = (step-size numerator) / tilings.

Episodes
0
Steps, last episode
–
Fewest steps
–
Active features
–

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.