Search

Search papers and lessons

Function Approximation

Level 3 · 15 min read

Function approximation estimates a value for every state from a short list of learned weights, so an agent can make a sensible guess about states it has never visited.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Reinforcement learning
  • Neural network basics
  • Calculus
  • Linear algebra
  • ReadReady to readNot yet

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 2567056256^{7056} 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 1017010^{170} 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 ss is described by a feature list x(s)=(1,s)x(s) = (1, s). The estimate is

v^(s)=w1⋅1+w2⋅s,\hat v(s) = w_1 \cdot 1 + w_2 \cdot s,

and we start with w=(1,0.5)w = (1, 0.5). So v^(1)=1.5\hat v(1) = 1.5, v^(2)=2.0\hat v(2) = 2.0, v^(3)=2.5\hat v(3) = 2.5, v^(4)=3.0\hat v(4) = 3.0, and v^(5)=3.5\hat v(5) = 3.5.

  1. The agent is in position 3 and takes a step, receiving reward R=1R = 1 and landing in position 4. Its estimate for position 3 is 2.52.5. Check: 1+0.5⋅3=2.51 + 0.5 \cdot 3 = 2.5.
  2. Form a target. A better guess for position 3 is the reward just received plus the discounted estimate of where we landed. With discount γ=0.9\gamma = 0.9, the target is 1+0.9⋅v^(4)=1+0.9⋅3.0=3.71 + 0.9 \cdot \hat v(4) = 1 + 0.9 \cdot 3.0 = 3.7. This is a one-step look ahead, the idea behind Temporal-Difference Learning.
  3. Measure the error. The TD error is the target minus the estimate: δ=3.7−2.5=1.2\delta = 3.7 - 2.5 = 1.2. The estimate for position 3 is too low by 1.2.
  4. 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 (1,3)(1, 3), so both weights should move in proportion to 11 and 33. Check: the second feature is larger, so the second weight moves more.
  5. Take a small step. With step size α=0.1\alpha = 0.1, the change is 0.1⋅1.2⋅(1,3)=(0.12,0.36)0.1 \cdot 1.2 \cdot (1, 3) = (0.12, 0.36). The new weights are w=(1.12,0.86)w = (1.12, 0.86).
  6. Look at the result for position 3. v^(3)=1.12+0.86⋅3=3.70\hat v(3) = 1.12 + 0.86 \cdot 3 = 3.70. The estimate moved from 2.52.5 to 3.73.7, exactly the target. That is not luck: the step changes v^(3)\hat v(3) by α δ ∥x(3)∥2=0.1⋅1.2⋅10=1.2\alpha \, \delta \, \|x(3)\|^2 = 0.1 \cdot 1.2 \cdot 10 = 1.2, because ∥x(3)∥2=12+32=10\|x(3)\|^2 = 1^2 + 3^2 = 10. This step size lands exactly on the target. A smaller step would land partway.
  7. Look at the side effects. Position 4 was not the one we updated, but its estimate is now 1.12+0.86⋅4=4.561.12 + 0.86 \cdot 4 = 4.56, up from 3.03.0. Position 2 went from 2.02.0 to 2.842.84. 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.
01234560246positionestimated value
  • before the step
  • after the step
One TD step moves the whole line, not just the point at position 3. The marker shows the target 3.7 for position 3.

The math

Setting

Let StS_t be the state at time tt, Rt+1R_{t+1} the reward after the step, and γ\gamma the discount factor, a number with 0≤γ≤10 \le \gamma \le 1. The agent follows a fixed Policy (Reinforcement Learning) π\pi, and vπ(s)v_\pi(s) is the true value of state ss under that policy, the expected discounted return from ss. We want an estimate v^(s,w)\hat v(s, w) that depends on a vector of weights w=(w1,…,wd)w = (w_1, \dots, w_d).

A feature vector x(s)=(x1(s),…,xd(s))x(s) = (x_1(s), \dots, x_d(s)) is a list of numbers describing state ss. In the walkthrough, x(s)=(1,s)x(s) = (1, s), so d=2d = 2.

Step 1 in symbols: a linear estimate

The simplest choice is a weighted sum of features, which is the same form as Linear Regression:

v^(s,w)=w⊤x(s)=∑i=1dwi xi(s).\hat v(s, w) = w^\top x(s) = \sum_{i=1}^{d} w_i \, x_i(s).

Here w⊤xw^\top x is the Dot Product of the weight vector and the feature vector. In the walkthrough, v^(3,(1,0.5))=1⋅1+0.5⋅3=2.5\hat v(3, (1, 0.5)) = 1 \cdot 1 + 0.5 \cdot 3 = 2.5.

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 μ(s)\mu(s) for that frequency. The objective is

J(w)=12 E ⁣[(vπ(S)−v^(S,w))2],J(w) = \tfrac{1}{2} \, \mathbb{E}\!\left[ \big( v_\pi(S) - \hat v(S, w) \big)^2 \right],

where the expectation is over states drawn with frequency μ\mu. The Mean Squared Error is the usual loss for this kind of fitting. Taking the Gradient of JJ with respect to ww and using ∇wv^(s,w)=x(s)\nabla_w \hat v(s, w) = x(s) gives

∇wJ(w)=− E ⁣[(vπ(S)−v^(S,w)) x(S)].\nabla_w J(w) = -\,\mathbb{E}\!\left[ \big( v_\pi(S) - \hat v(S, w) \big) \, x(S) \right].

A Gradient Descent step moves ww opposite this gradient, one sample at a time:

w←w+α(vπ(St)−v^(St,w)) x(St).w \leftarrow w + \alpha \big( v_\pi(S_t) - \hat v(S_t, w) \big) \, x(S_t).

Step 3 in symbols: the semi-gradient TD update

In reinforcement learning we do not know vπ(St)v_\pi(S_t). We replace it with a target. The TD target is Rt+1+γv^(St+1,w)R_{t+1} + \gamma \hat v(S_{t+1}, w), so the TD error is

δt=Rt+1+γ v^(St+1,w)−v^(St,w).\delta_t = R_{t+1} + \gamma \, \hat v(S_{t+1}, w) - \hat v(S_t, w).

The update becomes

w←w+α δt x(St).w \leftarrow w + \alpha \, \delta_t \, x(S_t).

This is called a semi-gradient update. The target depends on ww, but we treat it as a constant when we differentiate. Only the estimate at StS_t 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, δ=1.2\delta = 1.2, α=0.1\alpha = 0.1 and x(3)=(1,3)x(3) = (1, 3), so Δw=0.1⋅1.2⋅(1,3)=(0.12,0.36)\Delta w = 0.1 \cdot 1.2 \cdot (1, 3) = (0.12, 0.36).

Step 4 in symbols: why nearby states move

The change in the estimate at any other state s′s' after the update is

v^(s′,wnew)−v^(s′,w)=α δt (x(St)⊤x(s′)).\hat v(s', w_{\text{new}}) - \hat v(s', w) = \alpha \, \delta_t \, \big( x(S_t)^\top x(s') \big).

So the size of the side effect depends on how similar the two feature vectors are. In the walkthrough, x(3)⊤x(4)=1+12=13x(3)^\top x(4) = 1 + 12 = 13 and x(3)⊤x(2)=1+6=7x(3)^\top x(2) = 1 + 6 = 7. Both are positive, so both estimates rise, and position 4, with the larger dot product, rises the most. Check: 0.1⋅1.2⋅13=1.560.1 \cdot 1.2 \cdot 13 = 1.56 is the predicted change in v^(4)\hat v(4), and the actual change is 4.56−3.0=1.564.56 - 3.0 = 1.56. The same formula gives 0.1⋅1.2⋅7=0.840.1 \cdot 1.2 \cdot 7 = 0.84 for position 2, and indeed 2.84−2.0=0.842.84 - 2.0 = 0.84.

Special case: the table

If each state gets its own indicator feature, so x(s)x(s) is a vector with a 1 in the position of ss and 0 elsewhere, then v^(s,w)=ws\hat v(s, w) = w_s. The update changes only the entry for StS_t. 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

Example
Example 1: one weight, one update

A value estimate uses a single weight: v^(s)=w⋅s\hat v(s) = w \cdot s. Start with w=1w = 1. The TD target for state s=2s = 2 is 66.

  1. The estimate is v^(2)=1⋅2=2\hat v(2) = 1 \cdot 2 = 2.
  2. The error is target minus estimate: 6−2=46 - 2 = 4.
  3. The feature is x=2x = 2, and the step size is α=0.1\alpha = 0.1.
  4. The change in the weight is α⋅error⋅x=0.1⋅4⋅2=0.8\alpha \cdot \text{error} \cdot x = 0.1 \cdot 4 \cdot 2 = 0.8.
  5. The new weight is w=1+0.8=1.8w = 1 + 0.8 = 1.8.
  6. The new estimate is 1.8⋅2=3.61.8 \cdot 2 = 3.6. It moved from 22 toward 66, 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 α=0.1\alpha = 0.1 the new error is 2.42.4, down from 44.

Example
Example 2: the table is a special case

Two states, AA and BB, use one-hot features, so v^(A)=wA\hat v(A) = w_A and v^(B)=wB\hat v(B) = w_B. Current values: wA=0.5w_A = 0.5 and wB=0.8w_B = 0.8. The agent moves from AA to BB, receives reward 00, with γ=0.9\gamma = 0.9 and α=0.5\alpha = 0.5. (A one-hot feature vector is a list of zeros with a single 1, marking which state is active.)

  1. The target is 0+0.9⋅v^(B)=0.9⋅0.8=0.720 + 0.9 \cdot \hat v(B) = 0.9 \cdot 0.8 = 0.72.
  2. The TD error is δ=0.72−0.5=0.22\delta = 0.72 - 0.5 = 0.22.
  3. The feature vector for AA is (1,0)(1, 0), so only wAw_A changes.
  4. The change is αδ=0.5⋅0.22=0.11\alpha \delta = 0.5 \cdot 0.22 = 0.11.
  5. The new value is v^(A)=0.5+0.11=0.61\hat v(A) = 0.5 + 0.11 = 0.61.
  6. The value of BB is unchanged at 0.80.8, 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.

Example
Example 3: a delivery robot with three features

A delivery robot has three features per state: x(s)=(1,b,d)x(s) = (1, b, d), where bb is battery as a fraction of full and dd is distance to the charger in kilometres. The learned weights are w=(0.5,2.0,−0.4)w = (0.5, 2.0, -0.4). The robot is in state ss with b=0.6b = 0.6 and d=1.5d = 1.5, receives reward R=0.1R = 0.1 for moving toward its next drop-off, and lands in s′s' with b′=0.55b' = 0.55 and d′=1.0d' = 1.0. Use γ=0.95\gamma = 0.95 and α=0.05\alpha = 0.05.

  1. Features at ss: x(s)=(1,0.6,1.5)x(s) = (1, 0.6, 1.5). Estimate: v^(s)=0.5+2.0⋅0.6−0.4⋅1.5=0.5+1.2−0.6=1.1\hat v(s) = 0.5 + 2.0 \cdot 0.6 - 0.4 \cdot 1.5 = 0.5 + 1.2 - 0.6 = 1.1.
  2. Features at s′s': x(s′)=(1,0.55,1.0)x(s') = (1, 0.55, 1.0). Estimate: v^(s′)=0.5+1.1−0.4=1.2\hat v(s') = 0.5 + 1.1 - 0.4 = 1.2.
  3. Target: 0.1+0.95⋅1.2=0.1+1.14=1.240.1 + 0.95 \cdot 1.2 = 0.1 + 1.14 = 1.24.
  4. TD error: δ=1.24−1.1=0.14\delta = 1.24 - 1.1 = 0.14.
  5. Step: αδ=0.05⋅0.14=0.007\alpha \delta = 0.05 \cdot 0.14 = 0.007. Change in the weights: 0.007⋅(1,0.6,1.5)=(0.007,0.0042,0.0105)0.007 \cdot (1, 0.6, 1.5) = (0.007, 0.0042, 0.0105).
  6. New weights: w=(0.507,2.0042,−0.3895)w = (0.507, 2.0042, -0.3895).
  7. New estimate at ss: 0.507+2.0042⋅0.6−0.3895⋅1.5=1.12530.507 + 2.0042 \cdot 0.6 - 0.3895 \cdot 1.5 = 1.1253, up slightly from 1.11.1.
  8. Side effect at s′s': its estimate changes by 0.007⋅1+0.0042⋅0.55+0.0105⋅1.0=0.01980.007 \cdot 1 + 0.0042 \cdot 0.55 + 0.0105 \cdot 1.0 = 0.0198, so it rises to about 1.21981.2198.

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 w=(0.5,2.0,−0.4)w = (0.5, 2.0, -0.4), compute the TD error for a transition from state x=(1,0.4,2.0)x = (1, 0.4, 2.0) with reward R=0.1R = 0.1 into x′=(1,0.35,2.0)x' = (1, 0.35, 2.0), with γ=0.95\gamma = 0.95.

Answer: v^(x)=0.5+0.8−0.8=0.5\hat v(x) = 0.5 + 0.8 - 0.8 = 0.5 and v^(x′)=0.5+0.7−0.8=0.4\hat v(x') = 0.5 + 0.7 - 0.8 = 0.4. The target is 0.1+0.95⋅0.4=0.480.1 + 0.95 \cdot 0.4 = 0.48, so δ=0.48−0.5=−0.02\delta = 0.48 - 0.5 = -0.02. 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, v^(s,w)=w⊤x(s)\hat v(s, w) = w^\top x(s).
  • 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 w←w+αδtx(St)w \leftarrow w + \alpha \delta_t x(S_t)?Show answer

The update is not the exact gradient of a single objective. The target Rt+1+γv^(St+1,w)R_{t+1} + \gamma \hat v(S_{t+1}, w) depends on ww, but it is treated as a constant when differentiating. Only the estimate at StS_t contributes its gradient, x(St)x(S_t).

QuestionCompute: v^(s)=w1+w2x\hat v(s) = w_1 + w_2 x with weights w=(0.5,2)w = (0.5, 2), α=0.1\alpha = 0.1. A state with feature x=1x = 1 has v^=2.5\hat v = 2.5, and its TD target is 3.53.5. What is v^\hat v at that state after one update?Show answer

The error is 3.5−2.5=13.5 - 2.5 = 1. The feature vector is (1,1)(1, 1), so each weight moves by αδ\alpha \delta times its feature: w1w_1 becomes 0.5+0.1=0.60.5 + 0.1 = 0.6 and w2w_2 becomes 2+0.1=2.12 + 0.1 = 2.1. The new estimate is 0.6+2.1⋅1=2.70.6 + 2.1 \cdot 1 = 2.7. Check: the total change is αδ(12+12)=0.2\alpha \delta (1^2 + 1^2) = 0.2, which takes 2.52.5 to 2.72.7.

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.