Function Approximation & Deep Q-Learning
Generalizing across huge state spaces, from linear features to deep networks.
Go has about legal positions, and a robot's joint angles are real numbers that never repeat exactly. No agent can keep a table with one value per state, let alone visit every entry. This lesson shows how agents learn values that carry over to states they have never seen, from hand-built features to deep networks that learned Atari games from pixels, and why that same generalization can make learning blow up.
What you’ll learn
- Explain why one-value-per-state tables fail in large and continuous problems, and what generalization buys instead.
- Describe a value estimate built from features, and how tile coding turns a continuous state into features.
- Name the three ingredients of the deadly triad, and watch them make a value estimate diverge.
- Explain why deep Q-networks needed experience replay and a target network.
- Describe what distributional RL learns in place of an average return.
Too many states to count
Tabular methods such as Monte Carlo, SARSA and Q-learning keep one number for every state (or every state–action pair) and change a state's number only when the agent visits it. That works for a gridworld with fifty cells. It fails everywhere else, for three reasons.
- Memory. Go has about legal positions (Tromp, 2016), far more than the roughly atoms in the observable universe.
- Experience. Even a table that fit in memory would be mostly empty: the agent visits a vanishing fraction of the states, and a table learns nothing about a state it has never seen.
- Continuous states. A car's position and speed are real numbers. No two moments are exactly alike, so a table would never see the same state twice.
The way out is generalization: states that look alike are usually worth about the same, so experience in one state should inform the estimates of similar ones. Function approximation replaces the table by a formula with a set of adjustable weights , far fewer weights than states. Changing one weight changes the estimates of many states at once.
A robot arm's state is 12 numbers: 6 joint angles and 6 joint speeds. Chop each number into just 10 bins and make a table with one cell per combination. How many cells is that, as a power of ten?
Show the answer
12 power of 10
Each number multiplies the count by 10, so the table has cells, a trillion. Visiting each cell once at a million steps per second would take about 11.6 days, and estimating a value needs many visits per cell. This explosion with the number of state variables is the curse of dimensionality, and 10 bins is still a coarse view of a joint angle.
Features and tile coding
The simplest approximation describes each state by a few numbers, its features , and estimates the value as a weighted sum:
This is linear function approximation. Learning nudges the weights. When the agent sees a target for state , such as the return that followed it, each weight moves in proportion to its feature:
Only weights whose features are active in move, and every other state that shares those features moves with them.
Which features? For a continuous state, a robust answer is tile coding, an idea that goes back to Albus's CMAC of the 1970s. Lay a grid over the state space: each cell is a feature, equal to 1 if the state is in it and 0 otherwise. One grid alone is a coarse table. Now lay several grids, each shifted a little. A state falls in exactly one tile of each grid, and its value is the sum of those tiles' weights. Two nearby states share most of their tiles and get similar values; two distant states share none. Overlap gives broad generalization, and the offsets give fine resolution.
With 8 tilings, the agent makes one update in state . Whose value estimates change?
- Only the estimate of
- Every state's estimate, by the same amount
- States with the same position as , whatever their velocity
- States that share at least one tile with , and those sharing more tiles change more
Show the answer
D. States that share at least one tile with , and those sharing more tiles change more
The update moves the 8 weights of the tiles active in . Any state that lies in some of those tiles has those weights in its sum, so its estimate moves by an amount proportional to the number of tiles it shares with . Nearby states change a lot, distant ones not at all. That is generalization, and its width is set by the tile size.
When learning blows up: the deadly triad
Generalization has a dark side. An update aimed at one state also moves others, including the states whose estimates the agent uses as targets. Two more ingredients turn that into trouble:
- Bootstrapping: updating a guess toward a target built from another guess, as TD learning and Q-learning do with .
- Off-policy learning: learning about one policy (say, the greedy one) from data generated by another (say, an exploring one).
Function approximation, bootstrapping and off-policy training together are the deadly triad (Sutton & Barto, 2018): with all three, the estimates can grow without bound. With any two of them, the standard methods stay stable, at least with linear features.
Deep Q-networks: learning the features too
Tiles need a designer who knows which state variables matter. For a video game the state is a screen of pixels, and nobody can hand-design good features for every game. A neural network learns its own features: its hidden layers transform the raw input, and its last layer is a weighted sum of what they compute, so it is a linear approximator on features that are themselves learned.
The deep Q-network (DQN) of Mnih et al. (2015) read the last 4 frames of an Atari screen (downscaled to 84×84 gray pixels) and output one Q-value per joystick action, trained by Q-learning. With the same architecture and settings for every game (a separate network was trained on each), it learned 49 games from pixels and the score alone, reaching more than 75% of a professional human tester's score on more than half of them. It did very poorly where rewards are rare, such as Montezuma's Revenge.
DQN contains the whole triad: a network, bootstrapped targets , and off-policy data (it learns the greedy policy while acting -greedily). Two tricks kept it in check:
- Experience replay (Lin, 1992). Store the last million transitions and train on random mini-batches of them. Consecutive frames are nearly identical, and training on them in order would push the network back and forth; random samples look more like the independent examples that supervised learning expects, and each transition is reused many times.
- A target network. Compute the targets with a frozen copy of the network, refreshed only every 10,000 updates. Between refreshes the target stays put, so each stretch of training is an ordinary regression problem instead of a chase after a moving target.
Why does computing targets with a frozen copy of the network make learning more stable?
- The frozen copy is more accurate than the network being trained
- An update no longer moves its own target, so the network regresses toward fixed numbers until the next refresh
- It removes the need for exploration
- It makes the data on-policy
Show the answer
B. An update no longer moves its own target, so the network regresses toward fixed numbers until the next refresh
With a single network, every update changes the estimates used in the next targets, as in the -and- chase. Freezing the target network breaks that loop for thousands of updates at a time. A large study of DQN variants (van Hasselt et al., 2018) found values beyond any achievable return in 61% of runs without a target network and in 14% with one; unbounded divergence never occurred.
Distributional RL: the whole distribution of returns
A value is an average. A lottery ticket and a steady paycheck can have the same average, and an agent that keeps only averages cannot tell them apart. Distributional RL (Bellemare, Dabney & Munos, 2017) learns the whole probability distribution of the return instead, the return distribution .
The Bellman equation has a distributional version: the return from is distributed like the reward plus times the return from wherever you land next. A distributional backup therefore takes the next state's distribution, shrinks it by , shifts it by the reward, and projects it back onto a fixed set of values called atoms. The C51 agent uses 51 atoms per action.
Distributional agents can act on risk, preferring the paycheck to the lottery, and in deep RL learning the distribution also tended to improve performance on Atari, even when actions were still chosen by the mean. Why it helps is still debated: one leading explanation is that predicting many quantities shapes better learned features.
In the Bootstrapping lessons you'll derive the machinery: Linear Value Function Approximation (semi-gradient TD and where it converges, and feature construction from polynomials to tile coding), The Deadly Triad (exactly why off-policy bootstrapping diverges, and the repairs), Deep Q-Networks (the full algorithm and its improvements), and Distributional RL (categorical and quantile methods, and risk).
Key takeaways
- Tables fail in large or continuous state spaces: they cannot be stored, and they learn nothing about unvisited states. Function approximation generalizes with far fewer weights than states.
- A linear approximator sums the weights of active features; tile coding builds those features from overlapping, offset grids, so nearby states share weights.
- Function approximation, bootstrapping and off-policy training together form the deadly triad: estimates can diverge even when every reward is 0, while any two of the three are safe for linear methods.
- Deep Q-networks learn features from pixels; experience replay and a frozen target network were the tricks that kept their off-policy bootstrapping in check.
- Distributional RL learns the whole return distribution, shrinking, shifting and projecting it in each backup, which supports risk-sensitive decisions.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Why can't tabular Q-learning learn to play Go from board positions?
- Go has no rewards until the game ends
- There are far too many positions to store, and a table learns nothing about the positions it has not visited
- Go is not a Markov decision process
- Q-learning only works with continuous states
Show the answer
B. There are far too many positions to store, and a table learns nothing about the positions it has not visited
With about positions, the table cannot be stored, and the agent would see a negligible fraction of it. Learning has to generalize from visited positions to new ones, which is what function approximation provides.
A tile coder uses 8 tilings, each a grid of 8 × 8 tiles. How many weights are added up to compute for one state?
- 1
- 8
- 64
- 512
Show the answer
B. 8
Each tiling contributes exactly one active tile, the one containing , so the estimate sums 8 weights. That keeps updates cheap: an update touches 8 weights however many tiles there are in total.
In Baird's counterexample, off-policy semi-gradient TD diverges. Which change makes the same linear learner stable?
- A much smaller step size
- Adding more features
- Distributing the updates over states the way the target policy itself would visit them
- Replacing sampled updates by exact expected updates
Show the answer
C. Distributing the updates over states the way the target policy itself would visit them
Divergence comes from the mismatch between the states being updated and the policy being evaluated. Updating states in proportion to the target policy's own (on-policy) distribution makes linear TD stable. A smaller step size only slows the divergence, and expected updates diverge too.
What does experience replay do in DQN?
- It trains on random mini-batches of stored past transitions, breaking the correlation between consecutive frames and reusing data
- It replays the best episode over and over until the agent imitates it
- It makes the agent's data on-policy
- It replaces the reward with the change in the score
Show the answer
A. It trains on random mini-batches of stored past transitions, breaking the correlation between consecutive frames and reusing data
Consecutive frames are nearly identical, so learning from them in order gives highly correlated updates. Sampling at random from a large buffer decorrelates them and lets each transition be used many times. The price is that the replayed data come from older policies, which makes learning even more off-policy.
Action A always returns 5. Action B returns 0 or 10 with equal probability. What can a distributional agent tell apart that an expected-value agent cannot?
- Nothing: both actions are worth 5
- That B is better, because it can return 10
- That B's return is spread out while A's is not, so a risk-averse agent can prefer A
- That A is better, because its mean is higher
Show the answer
C. That B's return is spread out while A's is not, so a risk-averse agent can prefer A
Both means are 5, so an expected-value agent is indifferent. The distributions differ: A is a single spike at 5, B two spikes at 0 and 10. A risk measure such as CVaR, the average of the worst outcomes, separates them: the worst half of B's outcomes average 0, A's average 5.
Go deeper
- Linear Value Function Approximation · Features, semi-gradient TD and tile coding.
- The Deadly Triad · Function approximation, bootstrapping and off-policy data: why the three together can diverge.
- Deep Q-Networks · Experience replay, target networks and learning Atari from pixels.
- Distributional RL · Learning the whole distribution of returns, not just its average.
Further reading
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 9–11 (MIT Press, 2nd ed., 2018).
- V. Mnih et al., "Human-level control through deep reinforcement learning", Nature 518 (2015).