TL;DR
Function approximation replaces a big table of values, one entry per state, with a formula that has a small number of adjustable weights. The agent then estimates the value of any state by computing the formula, including states it has never visited, because nearby states share weights. This is what makes reinforcement learning possible when the world has millions of states, or states that are continuous numbers and never repeat exactly.
Builds on: Value Function, Linear Regression, Feedforward Neural Network, Temporal-Difference Learning · Leads to: Deep Q-networks, Actor-Critic Methods
Where it's used
Backgammon, 1992. Gerald Tesauro's TD-Gammon learned to play backgammon by playing against itself. It estimated how good each board position was with a small neural network, not a table. A backgammon board has far too many positions to store one value for each, and the network generalised from the positions it had seen. The program reached a level of play close to strong human players of its time.
Atari games, 2015. The DQN system of Mnih and colleagues learned to play dozens of Atari 2600 games from raw screen pixels. The screen was downscaled to a single frame of 84 by 84 grey pixels, with 256 shades each, which has possible values, so no table could hold them. A convolutional network estimated the value of each action from the screen, and the same weights served every frame the game produced.
Game-playing value networks, 2016. AlphaGo used a deep network that, given a Go board, output a single number estimating the chance that the player to move would win. Go has about legal positions, so the value of a position had to be computed by a function, not looked up.
How it works
Take a tiny world: five positions on a line, numbered 1 to 5. The agent wants the value of each position, which is the expected discounted reward from there. Instead of a table with five entries, we use a formula with two weights. Each position is described by a feature list . The estimate is
and we start with . So , , , , and .
- The agent is in position 3 and takes a step, receiving reward and landing in position 4. Its estimate for position 3 is . Check: .
- Form a target. A better guess for position 3 is the reward just received plus the discounted estimate of where we landed. With discount , the target is . This is a one-step look ahead, the idea behind Temporal-Difference Learning.
- Measure the error. The TD error is the target minus the estimate: . The estimate for position 3 is too low by 1.2.
- Work out which weights to move. The estimate is a sum of weights times features, so raising the estimate means raising each weight in proportion to its feature. Position 3 has features , so both weights should move in proportion to and . Check: the second feature is larger, so the second weight moves more.
- Take a small step. With step size , the change is . The new weights are .
- Look at the result for position 3. . The estimate moved from to , exactly the target. That is not luck: the step changes by , because . This step size lands exactly on the target. A smaller step would land partway.
- Look at the side effects. Position 4 was not the one we updated, but its estimate is now , up from . Position 2 went from to . Nearby positions moved too, because they share weights. This sharing is the whole point of function approximation, and it is also the source of its risks.
- before the step
- after the step
The math
Setting
Let be the state at time , the reward after the step, and the discount factor, a number with . The agent follows a fixed Policy (Reinforcement Learning) , and is the true value of state under that policy, the expected discounted return from . We want an estimate that depends on a vector of weights .
A feature vector is a list of numbers describing state . In the walkthrough, , so .
Step 1 in symbols: a linear estimate
The simplest choice is a weighted sum of features, which is the same form as Linear Regression:
Here is the Dot Product of the weight vector and the feature vector. In the walkthrough, .
Step 2 in symbols: the error we want to shrink
We would like the estimate to match the true value on the states we care about. A standard measure is the mean squared error, weighted by how often the agent visits each state. Write for that frequency. The objective is
where the expectation is over states drawn with frequency . The Mean Squared Error is the usual loss for this kind of fitting. Taking the Gradient of with respect to and using gives
A Gradient Descent step moves opposite this gradient, one sample at a time:
Step 3 in symbols: the semi-gradient TD update
In reinforcement learning we do not know . We replace it with a target. The TD target is , so the TD error is
The update becomes
This is called a semi-gradient update. The target depends on , but we treat it as a constant when we differentiate. Only the estimate at contributes to the step. This is not the exact gradient of any single objective, which is why the convergence theory is more delicate than for ordinary gradient descent. In the walkthrough, , and , so .
Step 4 in symbols: why nearby states move
The change in the estimate at any other state after the update is
So the size of the side effect depends on how similar the two feature vectors are. In the walkthrough, and . Both are positive, so both estimates rise, and position 4, with the larger dot product, rises the most. Check: is the predicted change in , and the actual change is . The same formula gives for position 2, and indeed .
Special case: the table
If each state gets its own indicator feature, so is a vector with a 1 in the position of and 0 elsewhere, then . The update changes only the entry for . The approximation is exact, with one weight per state. This is the tabular TD method, and it is the case function approximation generalises.
Worked examples
A value estimate uses a single weight: . Start with . The TD target for state is .
- The estimate is .
- The error is target minus estimate: .
- The feature is , and the step size is .
- The change in the weight is .
- The new weight is .
- The new estimate is . It moved from toward , but not all the way, because the step size is small.
Interpretation: the update pushes the weight in the direction that reduces the error, and the step size controls how far it moves. With the new error is , down from .
Two states, and , use one-hot features, so and . Current values: and . The agent moves from to , receives reward , with and . (A one-hot feature vector is a list of zeros with a single 1, marking which state is active.)
- The target is .
- The TD error is .
- The feature vector for is , so only changes.
- The change is .
- The new value is .
- The value of is unchanged at , because its feature entry was 0.
Interpretation: with a table, each update touches exactly one entry. The general formula gives the same answer, so the table is not a different method.
A delivery robot has three features per state: , where is battery as a fraction of full and is distance to the charger in kilometres. The learned weights are . The robot is in state with and , receives reward for moving toward its next drop-off, and lands in with and . Use and .
- Features at : . Estimate: .
- Features at : . Estimate: .
- Target: .
- TD error: .
- Step: . Change in the weights: .
- New weights: .
- New estimate at : , up slightly from .
- Side effect at : its estimate changes by , so it rises to about .
Interpretation: one step about one state changed the estimates of its neighbours, because they share the three weights. A table with one entry per state would have needed to visit each state separately.
Try it: using the original weights , compute the TD error for a transition from state with reward into , with .
Answer: and . The target is , so . The estimate was slightly too high.
Common mistakes and tradeoffs
Mistake 1: "Better function approximation always gives better values." It is tempting to think that a bigger network or more features always helps. In fact, more flexible approximators can make the error on the states you visit smaller while making the answers on other states worse, because the weights are shared. Each extra weight is also something the data must pin down, so the usual Bias-Variance Tradeoff applies.
Mistake 2: "If the TD target is right, the answer converges to the truth." It is tempting to think that bootstrapping from the estimate at the next state is harmless. In fact, the target itself is built from the approximation, so errors can feed back into each other. Combined with off-policy learning (updating from data the current policy did not generate) and a large function approximator, learning can diverge: estimates grow without bound. Leemon Baird's small counterexample in 1995 showed this clearly.
Mistake 3: "One update only changes the state it was computed on." In a table, that is true. With shared weights, the update moves every state with similar features, as the walkthrough showed. Learning a value for one state can correct another, or it can spoil it.
Mistake 4: "The feature list hardly matters." For a linear approximator, the features are the whole model. If the true value depends on something the features cannot express, no amount of training fixes it. This limit is the point of the Universal Approximation Theorem: a network with enough units can represent a wide class of functions, but it still needs data to find the right weights.
When to use it. Use function approximation when the number of states is too large to store, or when states are continuous. Images, sensor readings and board positions are typical.
When not to use it. If the table fits in memory and every state is visited often, a table is exact, simpler, and free from the divergence risk. The table has no approximation error at all.
Alternatives. A linear approximator with hand-designed features, such as tile coding (overlapping grids of binary features), is easier to analyse than a neural network. A Feedforward Neural Network learns its own features, at the cost of more tuning and less predictable behaviour. Deep methods often add a replay buffer (a store of past transitions reused for training) and a slowly updated target network (a copy of the weights that is refreshed only every few thousand steps) to make training more stable.
History
In the late 1950s, Arthur Samuel's checkers program adjusted the weights of a scoring polynomial so that its scores matched values found by looking ahead in the game, an early example of learning a value estimate by function approximation. In the early 1990s, Gerald Tesauro's TD-Gammon combined a neural network with Temporal-Difference Learning and reached strong backgammon play. Researchers then found that the same methods could fail on simple problems, and Tsitsiklis and Van Roy proved in 1997 that linear temporal-difference learning converges when updates follow the policy being evaluated. The field was stuck for years on the divergence problem, and the 2015 DQN paper by Mnih and colleagues showed that a deep network could work on many Atari games with tricks such as experience replay and a target network. Those tricks made deep function approximation practical.
Recap
- A value table with one entry per state is replaced by a formula with a few weights, .
- Each step builds a TD target from the reward and the next estimate, then measures the TD error against the current estimate.
- The update moves the weights in the direction of the features of the current state, scaled by the error and the step size.
- Because weights are shared, updating one state moves the estimates of similar states. This generalises, and it can also destabilise.
- A table is the special case where each feature vector is a one-hot indicator.
- Read next: Deep Q-networks, which use a neural approximator with replay and a target network, and Actor-Critic Methods, which uses an approximated value to guide a learned policy.
Check yourself
QuestionRecall: what does the word 'semi-gradient' mean in the update ?Show answer
The update is not the exact gradient of a single objective. The target depends on , but it is treated as a constant when differentiating. Only the estimate at contributes its gradient, .
QuestionCompute: with weights , . A state with feature has , and its TD target is . What is at that state after one update?Show answer
The error is . The feature vector is , so each weight moves by times its feature: becomes and becomes . The new estimate is . Check: the total change is , which takes to .
QuestionWhy: why does sharing weights between states make function approximation more powerful than a table, and why does it make it riskier?Show answer
Sharing lets one update inform many states, so the agent can estimate states it has never visited. The same sharing means an update to one state changes others, possibly in the wrong direction, so errors can spread. Tables have no such side effects, which is why they are safer but cannot scale.
QuestionApplied: a game agent has 10 million board positions and must choose moves. Should it use a table of values?Show answer
No. A table needs one entry for each position, about 10 million numbers that must each be visited, and most positions will never be seen in training. A function approximator with far fewer weights can generalise from visited positions to similar ones. The tradeoff is approximation error and possible divergence, so the agent should monitor learning closely.
Sources
- Sutton and Barto, Reinforcement Learning: An Introduction (2nd ed., 2018), Chapter 9, on prediction with approximation.
- Mnih et al., "Human-level control through deep reinforcement learning," Nature, 2015.
- Reinforcement learning, Wikipedia.