Approximate Solution Methods/Eligibility Traces

Lesson 3.72,018 words

Eligibility Traces

n-step methods unify TD and Monte Carlo by storing the last n feature vectors; eligibility traces do the same job with a single short-term memory vector. The λ-return averages every n-step return under a geometric weighting; the forward view looks ahead to that average, and the backward view produces nearly the same updates online through a decaying trace vector.

╌╌╌╌

n-step bootstrapping already unified temporal-difference learning and Monte Carlo: one integer slides from a one-step bootstrap to the full return, and an intermediate usually beats both extremes. But the -step algorithm costs memory and lag: to form the target for time it must wait steps and keep the last feature vectors on hand. Eligibility traces produce nearly the same updates from a single short-term memory vector, updated online, with the memory of just one feature vector.1

The mechanism is a vector , the same shape as the weight vector . Where is a long-term memory accumulating over the life of the agent, is short-term, lasting less than one episode. When a component of helps produce an estimate, the matching component of is bumped up; then it fades. If a nonzero TD error arrives before the trace decays away, that component of is eligible for learning, by an amount proportional to its remaining trace. The trace-decay parameter sets the fade rate; the same reappears below to index the family of returns.

n-step methods versus eligibility traces. The n-step algorithm buffers the last feature vectors and waits steps before it can form a target; the trace collapses all of that history into one vector updated every step, so it needs the memory of a single feature vector and no lag.

Two views of the same idea run through the whole lesson. The forward view is theoretical: to update a state, look ahead to all the future rewards and combine them. The backward view is mechanistic: at each step, propagate the current TD error back to the recently visited states, in proportion to their remaining traces. The forward view specifies the target; the backward view is how a real algorithm computes it, online and with bounded memory.

The λ-return

The -step return for a parameterized value function bootstraps after real rewards,

where is the approximate value under weights . A valid update can aim at any one of these returns — but it can also aim at an average of several. Any weighted average of -step returns is a legitimate target, as long as the weights are nonnegative and sum to , because the average inherits the error-reduction property of its components. An update toward such an average is a compound update, and averaging over different opens a whole new range of algorithms.

A compound update averages several -step returns. Each backup diagram (one, two, three steps deep) yields a target; a weighted average of them, with nonnegative weights summing to 1, is itself a valid target. The λ-return is the special compound update that averages all with geometric weights.

The λ-return is one particular compound update: it averages every-step return, weighting the -step return by and normalizing by so the weights sum to ,

The one-step return gets the largest weight, ; the two-step return gets ; the three-step return ; and so on. Each additional step of lookahead is discounted by another factor of . Because , the whole thing is a genuine weighted average.

Weighting in the λ-return. Each -step return receives weight , a geometric decay by a factor of per step; the areas sum to . After termination at all remaining weight, , collapses onto the actual full return .

The sum simplifies at termination. Once the episode ends at step , every -step return with equals the ordinary full return . Peeling those post-termination terms out of the infinite sum collapses their combined weight onto ,

This finite form makes the two boundary cases obvious. At the whole main sum vanishes except its first term, and — the one-step TD target. At the main sum goes to zero and the residual term takes over, — the Monte Carlo return. The parameter dials continuously between them, playing exactly the role that the integer played before, but averaging over all horizons at once rather than committing to a single one.

Worked example: a λ-return by hand

Take an undiscounted episode () that terminates after three steps, with rewards , , , and current value estimates , (with of the terminal state ). Compute the λ-return for with .

First the -step returns from . The one-step return bootstraps after : . The two-step return: . The three-step return reaches the terminal, so it is the full return: , and every return equals this same .

Now use the finite post-termination form with , , :

That is . Check the weights sum to one: on the one-step return, on the two-step, and the residual on the full return, total . The λ-return sits between the aggressive one-step target and the full return , leaning toward the shorter horizons because decays quickly. Set and the same formula collapses to ; set and it collapses to — the two extremes drop out of the one expression.

The offline λ-return algorithm uses this target directly. It changes nothing during the episode; then, once is known for every , it makes the usual semi-gradient updates,

On the 19-state random walk this offline algorithm performs about as well as the best -step method, and slightly better at the best settings — an intermediate beats both and , just as an intermediate beat both its extremes.

The forward view

The offline λ-return algorithm is the theoretical, or forward, view of a learning method. For each state visited, we look forward in time to all the future rewards and decide how to combine them into a single target. From each state in the sequence, we look forward once to determine its update, then move on and never touch that state again. Future states, by contrast, are processed repeatedly, once from every vantage point that precedes them.

The forward view. Standing at , the algorithm looks ahead along the trajectory to the future rewards and states , combining them into the target . Each state is updated once from its own forward look; every later state is viewed again from each earlier vantage point.

Forward views are always somewhat awkward to implement, because the update at depends on rewards and states that do not exist yet at time . You can compute only after the episode ends. That is the practical defect the backward view repairs: it produces nearly the same updates — and, for true online TD(λ), exactly the same updates — using only quantities available at the current step.

TD(λ) and the backward view

TD(λ) is the backward-view counterpart. It improves on the offline algorithm in three ways at once. It updates on every step rather than only at the end, so estimates get better sooner; its computation is spread evenly across time rather than piled up at termination; and it applies to continuing problems, not just episodic ones.

TD(λ) maintains the trace vector. Initialize it to zero at the start of the episode, increment it each step by the value gradient, and fade the old value by ,

The trace keeps a running record of which components of have contributed to recent state valuations, where recent means measured against the decay. In the linear case is just the feature vector , so the trace is a sum of past, fading, input vectors — a moving snapshot of the features the agent has lately depended on.

The reinforcing event that drives learning is the moment-by-moment one-step TD error,

and the weight vector moves proportional to the scalar error times the whole trace vector,

The single scalar is broadcast back across every component with a nonzero trace. A component with a large remaining trace (one that helped value a recently visited state) moves a lot; a component whose trace has mostly decayed barely moves at all. This is the backward view in one equation: a present error, credited to the past in proportion to how recently each state contributed.

The backward, or mechanistic, view of TD(λ). At the current TD error is fed back along the trajectory to recently visited states, each carrying a trace that has faded by per step. Nearer states hold a larger trace and are credited more; distant states, whose traces have decayed, are barely touched.

The trace update explains both limits directly. At the trace at reduces to the current gradient , because the factor kills everything older. The update then changes only the one state just visited, and TD(λ) collapses to the one-step semi-gradient TD(0) of the previous chapter. This is why the one-step method is written TD(0): it is TD(λ) with the trace switched off. For more of the preceding states are changed, but each more distant one gets less credit, because its trace is smaller — earlier states are given less credit for the TD error.

At the credit given to earlier states falls only by per step — Monte Carlo behavior. A reward carried back steps needs discounting by , and a trace decaying by per step supplies that . With (and, for an undiscounted episodic task, ) the trace never decays with time and the method behaves as Monte Carlo does — but incrementally and online, so it can even run on continuing tasks where classical Monte Carlo cannot. This case is also called TD(1), and unlike offline Monte Carlo it can learn from and alter its own behavior during an episode rather than only after it ends.

Algorithm:Semi-Gradient-TD(λ)\textsc{Semi-Gradient-TD}(\lambda) — estimate v^vπ\hat v \approx v_\pi
  1. 1
    input: a policy π\pi, a differentiable v^\hat v with v^(terminal,)=0\hat v(\text{terminal},\cdot) = 0
  2. 2
    w\mathbf{w} \gets arbitrary (e.g. 0\mathbf{0})
  3. 3
    for each episode do
  4. 4
    initialize SS
  5. 5
    z0\mathbf{z} \gets \mathbf{0}
  6. 6
    repeat
  7. 7
    choose Aπ(S)A \sim \pi(\cdot \mid S), take it, observe RR, SS'
  8. 8
    zγλz+v^(S,w)\mathbf{z} \gets \gamma\lambda\,\mathbf{z} + \nabla\hat v(S,\mathbf{w})
  9. 9
    δR+γv^(S,w)v^(S,w)\delta \gets R + \gamma\,\hat v(S',\mathbf{w}) - \hat v(S,\mathbf{w})
  10. 10
    ww+αδz\mathbf{w} \gets \mathbf{w} + \alpha\,\delta\,\mathbf{z}
  11. 11
    SSS \gets S'
  12. 12
    until SS is terminal

On the random walk, TD(λ) closely tracks the offline λ-return algorithm when is chosen well for each. At larger-than-optimal step sizes TD(λ) degrades more, and can even become unstable, but those settings are not ones you would use anyway. In the on-policy linear case, TD(λ) is proven to converge, to a weight vector whose asymptotic error is bounded by times the smallest achievable error — a bound that tightens toward the minimum as .

The bias-variance tradeoff in . Small leans on the bootstrap: low variance but biased by the current (wrong) value estimates. Large leans on real returns: unbiased but high variance. Prediction error, the sum of the two, is lowest at an intermediate , which is why an in-between value usually wins.

Worked example: a trace over three steps

Take a single scalar feature that is at every visited state (so each step), , . Start . Step : . Step : . Step : . Step : . The trace grows because the same feature keeps firing, but each past contribution is discounted: the increment from step has faded to by step , the step- increment to , and so on. When a TD error arrives, the weight moves by — the whole trajectory's accumulated eligibility scaled by one scalar error. A component whose feature last fired three steps ago still contributes, but only through the surviving fraction of its original bump.

Accumulating, replacing, and dutch traces

The trace above is the accumulating trace, , which keeps adding the feature each visit and so can grow past for a feature that fires repeatedly. That unbounded growth is sometimes a problem: a state revisited many times in quick succession piles up trace and can overshoot. The older fix, defined only for binary features, is the replacing trace — instead of adding, reset each active component to :2

A replacing trace caps every component at , so a repeatedly visited feature no longer accumulates without bound. The dutch trace used in true online TD(λ) below,

interpolates between the two: it adds the feature but scaled down by a term depending on the step size and the surviving trace — exactly the correction that makes the backward view match the forward view identically (the true online TD(λ) below). Modern practice treats the replacing trace as a crude approximation to the dutch trace, which usually performs better; accumulating traces stay relevant mainly for nonlinear approximation, where the dutch trace has no derivation.

The three trace types for one binary feature that fires at steps 0, 1, 2 then stops (decay ). The accumulating trace (blue) adds 1 each visit and climbs above 1; the replacing trace (red) resets to 1 on each visit and never exceeds it; the dutch trace (black) sits between them. All decay identically once the feature stops firing.

Forward and backward, side by side

The forward and backward views are two descriptions of the same computation: what the update is for (aim each state at its λ-return) versus how it happens (spread each incoming TD error back across the states the trace remembers). Neither is more correct; the whole chapter is the claim that they nearly coincide.

Forward viewBackward view
Charactertheoretical, conceptualmechanistic, algorithmic
Targetthe λ-return the current TD error
Directionlooks ahead to future rewardscredits past states
State of memoryneeds the whole futureone trace vector
Timingupdate computable only at episode endupdate on every step, online

For the plain TD(λ) above, the equivalence is only approximate: its total updates over an episode approximate, but do not equal, the offline λ-return algorithm's, and the gap grows as grows. Exact equivalence, and the extension of the trace apparatus to control, continues in true online TD(λ) and Sarsa(λ).

Footnotes

  1. Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), Ch. 12 — Eligibility Traces: the trace vector as a short-term memory paralleling (chapter intro). §12.1 — The λ-return: the compound average (12.2), its finite post-termination form (12.3), the weighting figure (Fig. 12.2), and the offline λ-return update (12.4). §12.2 — TD(λ): the accumulating trace (12.5), the TD error (12.6), the weight update (12.7), and the forward/backward views (Figs. 12.4–12.5).
  2. Sutton & Barto, §12.5 — the accumulating trace (12.5) versus the replacing trace (12.12, binary-feature only) versus the dutch trace (12.11), and the modern view of replacing traces as crude approximations to dutch traces, with accumulating traces retained for nonlinear approximation.

╌╌ END ╌╌