Function Approximation

Too many states for a table. Tile coding generalises between nearby states, a car learns to climb a mountain, and Baird’s counterexample shows learning blow up.

Function Approximation

A table with one entry per state works for a gridworld, not for a car whose position and speed are real numbers. Replace the table with a function that generalises, so that learning about one state teaches you about its neighbours. It is powerful — and, as the last demo shows, it can break learning in surprising ways.

Tile Coding

Tap the left square to see the one tile each tiling activates, and drop samples to teach the learner the hidden function. Compare one coarse tiling with eight fine offset ones: watch how far a single update reaches.

4
features active (one per tiling)
196
total features (weights)
0
training samples
0%
of the square moved by the last update
0.000
RMS error vs target
Tap anywhere on the left square. The point falls in exactly one tile of each of the 4 tilings, so its feature vector has 4 ones among 196 zeros, and the prediction is the sum of those 4 weights.
Target function
Tiling offsets
Show tilings

Left: the hidden target (emerald positive, rose negative), each tiling’s grid in its own colour, and the active tile of each tiling outlined around the orange crosshair. Right: the linear learner’s prediction, with the region the most recent update changed tinted orange. Try one tiling of 3 × 3 tiles, drop a single sample, then switch to 8 tilings of 8 × 8. Changing the features or target replays your samples once into fresh weights.

Go deeper:Linear Features and Coarse Coding

Linear function approximation writes the value as v̂(s, w) = wᵀx(s) = Σᵢ wᵢ xᵢ(s): a feature vector x(s) chosen by hand, and a weight vector w learned from data. The gradient of v̂ with respect to w is simply x(s), so the stochastic-gradient update for a target U is w ← w + α[U − v̂(s, w)] x(s). With binary features only the active weights move, and every other state that shares one of those features moves with them. That is generalisation, and the shape of the features decides its shape.

Coarse coding uses many overlapping receptive fields; tile coding is its most practical form. Each tiling is a grid that partitions the space, so exactly one tile per tiling is active and the number of active features is always k, the number of tilings. That makes the step size easy to set: α/k per weight moves the prediction at the sample a fraction α of the way to its target, which is why α = 1 hits the target in one step. The idea goes back to James Albus’s CMAC (cerebellar model articulation controller) of 1975, and Richard Sutton used it heavily in reinforcement learning, for example in his 1996 paper “Generalization in reinforcement learning: successful examples using sparse coarse coding”.

Resolution comes from the overlap, not the tile size: k tilings offset from one another cut each tile into many smaller cells, while each update still generalises over a whole tile. How the tilings are offset matters. If every tiling is shifted by the same amount in every dimension (uniform offsets), the pattern of generalisation has diagonal artefacts; Sutton and Barto (chapter 9) recommend asymmetric offsets, shifting tiling t by t/k of a tile times the first odd integers (1, 3, 5, …), which gives more symmetric, compact blobs. Toggle the offsets with several tilings and a single sample to see the difference.

In high dimensions the number of tiles explodes, so practical tile coders hash tiles into a fixed-size table: collisions are rare where the agent actually goes, and the memory no longer depends on the size of the space.

Mountain Car

Press Play and watch the car learn to rock back and forth up the hill while the cost-to-go surface grows its spiral. Then switch to You drive and find the strategy yourself.

Cost-to-go −maxₐ q̂(x, v, a) · drag to orbit
1
episode
0
steps this episode
—
steps last episode
—
mean of last 10 episodes
0
total steps learned from
Press Play. Every action value starts at 0, but every real step costs −1, so whatever the car has already tried looks worse than what it has not. That optimism alone makes it explore, even though ε = 0.

Semi-gradient SARSA, γ = 1, ε = 0, one weight vector per action over 8 tilings of 8 × 8 tiles with asymmetric offsets. The surface and the phase-plane heatmap show the learned cost-to-go (deeper rose = more steps expected; dark = still at the optimistic starting value of 0). The orange line is the agent’s latest episode, sky blue is yours. Episodes are cut off at 10,000 steps.

Go deeper:Semi-Gradient SARSA

Mountain car comes from Andrew Moore’s 1990 PhD thesis; this version follows Sutton and Barto’s Example 10.1. The state is position x ∈ [−1.2, 0.5] and velocity v ∈ [−0.07, 0.07], the action a ∈ {−1, 0, +1} is full reverse, coast or full throttle, and vₜ₊₁ = bound(vₜ + 0.001aₜ − 0.0025 cos 3xₜ), xₜ₊₁ = bound(xₜ + vₜ₊₁), on the hill y = sin 3x. Every step costs −1 until the car reaches x = 0.5. Gravity beats the engine, so the only way up is to back up the left slope first and use the swing.

The learner keeps one tile-coded weight vector per action, here 8 tilings of 8 × 8 over (x, v), and applies episodic semi-gradient SARSA: w ← w + α[R + γ q̂(S′, A′, w) − q̂(S, A, w)] ∇q̂(S, A, w), with γ = 1 and α = 0.5/8 as in the book. It is called semi-gradient because the target R + γq̂(S′, A′, w) also depends on w, yet the update treats it as a constant and only differentiates the prediction. That is not the gradient of any fixed objective, but with linear features and on-policy data it is stable and usually faster than a true gradient method.

There is no ε-greedy exploration (ε = 0). Instead, all weights start at 0 while every true value is negative, so initial estimates are optimistic: each time the car tries something, that action’s estimate drops below the untried ones, and the greedy choice moves on. This drives systematic exploration until the car stumbles on a swing large enough to escape.

The surface is −maxₐ q̂(x, v, a), the estimated number of steps to go, as in the book’s Figure 10.1. Its spiral mirrors the dynamics: the car escapes by pumping energy into the swing, so its trajectory in the phase plane spirals outward from the valley floor, and the states on later, bigger loops are closer to the goal.

The Deadly Triad

Press Play and watch eight weights fly off to infinity on a problem whose answer is zero. Then switch off any one of the three legs of the triad, or choose TDC, and run it again.

The deadly triad — the weights diverge only with all three on
0
updates
10.3
‖w‖
5.32
value error √Σμ v̂²
8.22
(projected) Bellman error
…
verdict
All three legs of the triad are on. Every true value is 0, and w = 0 would represent that exactly. Press Play and watch what semi-gradient TD does instead.

Circles show v̂(s) (emerald positive, rose negative, colour on a log scale); the formula under each is its linear feature combination. The chart plots all 8 weights on a symmetric log scale: sky blue w₁…w₆, amber w₇, violet w₈. γ = 0.99 and all rewards are 0, so every true value is 0. Monte Carlo mode regresses on the return, which is exactly 0 here; on-policy mode learns the value of b from b’s own transitions. TDC uses β = 0.05. Changing any setting starts a fresh run.

Go deeper:Baird’s Counterexample

Leemon Baird published this example in 1995; the version here is Sutton and Barto’s Example 11.1. Seven states, two actions, all rewards zero and γ = 0.99, so the true value of every state is 0, and the eight linear features can represent that exactly with w = 0. The behaviour policy picks dashed (to a random one of states 1–6) with probability 6/7 and solid (to state 7) with probability 1/7; the target policy always picks solid. Off-policy TD weights each update by the importance-sampling ratio ρ = π(a | s)/b(a | s), which is 7 for solid and 0 for dashed. Starting from w = (1, 1, 1, 1, 1, 1, 10, 1), semi-gradient TD sends the weights to infinity for any positive step size — and so do the expected, DP-style updates, which remove sampling noise entirely.

Sutton and Barto call the combination of function approximation, bootstrapping and off-policy training the deadly triad: the danger of instability arises when all three are present, and removing any one avoids it. Tabular methods, even off-policy, converge. Monte Carlo targets do not bootstrap; here every return is exactly zero, so the update is ordinary regression toward zero. And on-policy linear TD is guaranteed to converge: Tsitsiklis and Van Roy proved in 1997 that it converges with probability one, to a fixed point whose error is within a bounded factor of the best the features allow.

The fix is to follow a true gradient. Gradient-TD methods such as TDC (Sutton, Maei and colleagues, 2009) minimise the projected Bellman error, keeping a second weight vector v that estimates the expected TD error in feature space and using it to correct the update: w ← w + αρ[δx − γx′(xᵀv)], v ← v + βρ[δ − vᵀx]x. They are stable off-policy with linear features, at the price of a second step size and slower learning. In this example the features can represent every value function, so the projected Bellman error equals the Bellman error; TDC drives it down while the value error falls only slowly.

Deep reinforcement learning lives with all three legs at once: neural networks, bootstrapped targets and replayed off-policy data. That is why DQN needs its stabilisers, experience replay and target networks. See also: the Deep Q-Learning lesson.

Key Takeaways

  • A function approximator replaces the table, so every update also changes the values of similar states.
  • Tile coding gives k active binary features per state; many offset tilings give fine resolution and smooth generalisation.
  • Semi-gradient SARSA with tile coding and optimistic initial values teaches the mountain car to rock its way out.
  • Function approximation, bootstrapping and off-policy training together can diverge; drop one, or use gradient TD.

Spot a mistake or have an idea?

Search