On-Policy Prediction with Approximation
Every tabular method so far stored one number per state, which fails once the state space is large or continuous. We replace the table with a parameterized value function , define the mean squared value error it should minimize under the on-policy distribution, and derive stochastic- and semi-gradient learning rules — the semi-gradient TD(0) update that bootstraps and so is not a true gradient.
╌╌╌╌
Every method up to here has stored the value function as a table: one entry per state for , one per state–action pair for . That representation is exact, but it does not scale. A backgammon position is one of ; a robot's state is a vector of real-valued joint angles and velocities, so there are uncountably many. No table has that many rows, and even if it did, the agent would have to visit each state to fill its entry — there is never enough experience to learn them one at a time. This is the curse of dimensionality: the number of states grows exponentially in the number of state variables, and tabular learning grows right along with it.
The alternative is to stop treating states as independent. We represent the approximate value not as a lookup but as a parameterized functional form with a weight vector ,1
where — far fewer weights than states. Changing one weight now changes the estimated value of many states at once. When the agent updates at one state, the change generalizes to other states, and a value can be assigned to states never seen at all. The same move that lets us cope with enormous state spaces also lets us handle partial observability: if simply cannot depend on some aspect of the state, learning proceeds exactly as if that aspect were hidden.1
Generalization cuts both ways. With genuine approximation, improving the estimate at one state usually worsens it at others, because they share weights. We can no longer make every state's value exactly right, so we must say which states we care about most, and by how much. Prediction becomes an optimization problem, and this lesson sets it up: the objective, the gradient methods that descend it, the special structure of the linear case, and the features that feed it.
The prediction objective
Before we can descend toward a good , we must define the objective. Since we can no longer make every state's value exactly right, we need a weighting that says how much an error at each state costs. That weighting is the prediction objective.
In the tabular setting we never needed an explicit objective. The values at different states were decoupled — an update at one state left every other untouched — so each could converge exactly to its target and there was nothing to trade off. Approximation destroys that independence. Because updating one state spills onto others, and because there are far more states than weights, no makes every state correct. We are forced to weigh the errors against each other.2
We do this with a state distribution , , representing how much we care about the error at each state . Weighting the squared error at each state by gives the Mean Squared Value Error:
Its square root, the root , is a rough measure of how far the approximate values stray from the true ones, and it is what appears on the plots in this chapter. The choice of is not incidental — it decides which states the approximation bends to fit and which it is allowed to get wrong.
The on-policy distribution
Which ? Throughout this chapter it is the on-policy distribution: the fraction of time the agent actually spends in each state while following . States the policy visits often matter proportionally more, as they should: accuracy where you are is worth more than accuracy where you never go. In a continuing task this is the stationary distribution under ; in an episodic task it depends on how episodes start. Let be the probability an episode begins in , and the expected number of time steps spent in per episode. Time is spent in if episodes start there, or if a transition enters it from some predecessor :
Solve this linear system for the visit counts , and the on-policy distribution is that count normalized to sum to one:
Emphasizing the on-policy distribution is what makes the central convergence result of this chapter hold. Update states in these proportions and bootstrapping is stable; update them in some other proportion and the same method can diverge, a failure we return to under the deadly triad.2
One caveat: is not obviously the right objective — our real aim is a better policy, and the value function best for control is not necessarily the one minimizing . But it is the best we can currently formalize, and minimizing it is a well-posed goal. For simple (linear) approximators we can even hope for a global optimum with everywhere; for complex ones (neural nets) we settle for a local optimum, and sometimes for merely staying near one.
Every update is a training example
Before deriving the learning rule, notice a reframing that connects reinforcement learning to ordinary supervised learning. Every prediction method here updates an estimated value at some state toward a backed-up value, an update target. Write a single update as : the estimate at should move toward . Reading off the targets from earlier lessons:
| Method | Update | Target |
|---|---|---|
| Monte Carlo | the actual return | |
| TD(0) | one reward + bootstrap | |
| -step TD | truncated return | |
| DP | expected backup |
Each pair is a labelled example: input , desired output . Any supervised-learning method that takes examples and produces a function can serve as the value approximator — we simply hand it the updates as training data.3 But not every such method fits. Reinforcement learning demands methods that learn online from incrementally acquired data, and that tolerate nonstationary targets, because in bootstrapping the target shifts as changes. Batch methods that assume a fixed training set are poorly suited. This narrows us, in practice, to gradient methods.
Stochastic-gradient methods
Assume is a differentiable function of the weight vector , and suppose on each step we observe an example — a state and, for now, its true value. Stochastic gradient descent (SGD) adjusts after each example by a small step down the gradient of that example's squared error:4
where is a positive step size and is the column vector of partial derivatives . The step is proportional to the negative gradient, the direction of steepest descent on the squared error, so many small steps reduce an average error like . It matters that the step is small: we do not want to eliminate the error on any single example, because that would unbalance the fit across the other states that share the weights. Correcting each example only a fraction of the way is what finds a balance.
In reality we do not know ; we have only a possibly-noisy target standing in for it. Substituting gives the general SGD method for state-value prediction:
If is an unbiased estimate — — then is guaranteed to converge to a local optimum under the usual decreasing- conditions. The Monte Carlo return qualifies: it is by definition an unbiased sample of . So gradient Monte Carlo is a genuine SGD method and inherits its convergence guarantee.4
- 1input: a policy , a differentiable , step size
- 2arbitrary (e.g. )
- 3for each episode do
- 4generate an episode following :
- 5for do
- 6
Semi-gradient methods
The SGD guarantee assumed the target was a fixed label. A bootstrapped target is not fixed — it is itself computed from the weights being adjusted, so it moves as they move. This section covers what a true gradient would require with such a target, and the practical shortcut we take instead.
The guarantee breaks the instant the target bootstraps. Consider using the TD(0) target . It depends on the current weights — the very quantity we are differentiating. The clean step from the squared-error gradient to the update above relied on the target being independent of ; here it is not. A true gradient of would include a term from differentiating the target. We drop that term, keeping only the gradient of the estimate at :4
Because it accounts for the effect of changing on the estimate but ignores its effect on the target, this is only half of a gradient. We call such methods semi-gradient methods. The one above is semi-gradient TD(0).
Semi-gradient methods do not converge as robustly as full-gradient ones, but in the important linear case (next) they converge reliably, and they carry two practical advantages that usually make them preferred. They typically learn significantly faster, as bootstrapping did in the tabular TD and -step chapters. And they are continual and online: no need to wait for the end of an episode, so they apply to continuing tasks.4
- 1input: a policy , a differentiable with , step size
- 2arbitrary
- 3for each episode do
- 4initialize
- 5repeat
- 6choose , take it, observe ,
- 7
- 8
- 9until is terminal
State aggregation is the simplest such approximator: group states into blocks, give each block one weight, and estimate a state's value as its block's weight. It is a special case of SGD in which is for the active block's component and elsewhere. Run gradient Monte Carlo with state aggregation on a 1000-state random walk and it recovers a staircase approximation to the true value function — piecewise constant within each block, close to the minimum — visibly biased in the outer blocks toward the values of the states within them that the on-policy distribution weights most.4
Worked example: state aggregation on the 1000-state walk
Make the bias concrete. The task is a random walk on states laid in a line, plus two terminal states off each end. From any state the agent jumps to one of the neighbors on its left or the on its right, each with equal probability (a jump that would cross an end lands in the terminal there, and jumps are truncated so the probabilities on the short side pile up on the near terminal). Reaching the left terminal pays , the right terminal , every other reward is , and . By symmetry the true value rises almost linearly from about near state to about near state , passing through at the center.
Aggregate the states into blocks of : states – share weight , states – share , and so on. There are weights standing in for values. What value does gradient Monte Carlo settle on for a block? Each block's weight converges to the -weighted average of the true values of the states inside it, because that average is the constant that minimizes . Setting the derivative to zero gives over the block — a -weighted mean. In the interior blocks is nearly flat and nearly linear, so the weight lands near the block's midpoint value: block (states –) settles around , block (states –) around . The staircase there hugs the true line.
The outer blocks are where the bias shows. Under this walk is not flat: the agent starts at state and the near-terminal jumps make the states just inside each end rarer to occupy but the states a little further in more heavily visited, so within block (states –) leans toward the states nearer state , whose true values are higher (closer to ) than the states near state (closer to ). The -weighted mean is pulled up: block converges to about rather than the midpoint value near , and the estimated value flattens at the edges instead of continuing down to . That upward flattening in the leftmost block and the matching downward flattening in the rightmost is the signature of state aggregation — the approximation bends toward the states the on-policy distribution weights most, and the edges pay for it.
Linear methods
The most important special case is the one where is linear in the weights. To each state attach a real-valued feature vector
with the same number of components as . Each is a feature, and the approximate value is the inner product of weights and features:5
The features are basis functions — they span the set of value functions the model can represent — and constructing good features is where a designer injects prior knowledge about the task. In the linear case the gradient is trivially the feature vector itself,
so the general SGD update collapses to a clean form,
The linear case is favored because it is the one we can analyze. There is a single optimum (or a connected set of equally good optima), so any method that reaches a local optimum reaches the global one — gradient Monte Carlo under linear features converges to the global minimum.
The TD fixed point
Gradient Monte Carlo converges to the best possible . Semi-gradient TD(0) converges to a different point, stable but slightly biased. The next few equations locate that point. At convergence the average update must be zero, and solving that condition for gives one specific weight vector, the TD fixed point.
Semi-gradient TD(0) also converges under linear approximation, but not to the global minimum, and the argument needs its own machinery. Write . The linear semi-gradient TD(0) update is
Once the process reaches steady state, the expected next weight vector is
with
If the iteration converges, it must converge to the weight vector where the expected update is zero, , that is,
This is the TD fixed point, and linear semi-gradient TD(0) does converge to it. Rewriting the expected update as shows that only governs stability: convergence needs positive definite. Under the on-policy distribution factors as , where is the diagonal matrix of the and the transition matrix; the key matrix has all its column sums equal to , which is positive, and that positivity secures positive definiteness — and the existence of .5 This is why the on-policy distribution matters. Update states in some other proportion and the column-sum argument collapses; may cease to be positive definite, and the iteration can diverge.
The TD fixed point's stability comes with a bias. At ,
The asymptotic TD error is at most times the smallest error any linear approximation can achieve — the error the Monte Carlo method reaches in the limit. When is near the factor is large, so TD can settle for a noticeably worse asymptote. In exchange it usually has far lower variance and learns much faster, which is why it is so often preferred; the winner depends on the problem and on how long learning runs.5
Worked example: solving for the TD fixed point by hand
For example, take a two-state chain with a single feature per state, so and are scalars. State transitions to state with reward ; state transitions back to with reward ; discount . Give the scalar feature and the feature . The on-policy distribution is uniform, , because the two states alternate.
Compute by averaging over the two transition types under . From : , next feature , so the term is . From : , next feature , so the term is . Averaging,
Compute the same way. From the reward is , contributing ; from the reward is , contributing . Averaging, . The TD fixed point is
giving and . Note — it is positive, which is what makes a stable attractor: the expected update pulls any toward for any . Had the features been chosen so that came out negative, the same iteration would push to infinity. The positive is the one-dimensional case of the positive-definiteness that the column-sum argument guarantees under the on-policy distribution.
Footnotes
- Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), §9.1 — Value-function Approximation: the approximate value as a parameterized form with weight vector , ; how a single update generalizes across states, and why this makes reinforcement learning applicable to partially observable problems. ↩ ↩2
- Sutton & Barto, §9.2 — The Prediction Objective (): the Mean Squared Value Error (9.1) weighted by a state distribution , the on-policy distribution and the episodic-task visit equations (9.2)–(9.3), and the distinction between global and local optima. ↩ ↩2
- Sutton & Barto, §9.1 — the / framing of every update as a supervised-learning example, the Monte Carlo, TD(0), -step, and DP update targets, and the online and nonstationarity requirements that rule out static batch methods. ↩
- Sutton & Barto, §9.3 — Stochastic-gradient and Semi-gradient Methods: the SGD update (9.4)–(9.7), the unbiasedness condition for convergence, the Gradient Monte Carlo box, the semi-gradient argument (dropping the target's gradient because it bootstraps), the Semi-gradient TD(0) box, and state aggregation (Example 9.1, 1000-state random walk). ↩ ↩2 ↩3 ↩4 ↩5
- Sutton & Barto, §9.4 — Linear Methods: the feature vector and (9.8), the linear SGD update, the expected-update matrices and (9.9)–(9.11), the TD fixed point (9.12), the positive-definiteness / column-sum proof of convergence, and the bound (9.14). ↩ ↩2 ↩3
╌╌ END ╌╌