Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

Forward & Backward Views of TD(λ)

Why traces implement λ-returns, and true online TD(λ).

Sutton's TD(λ) of 1988 came with an identity for λ=1\lambda = 1: summed over an episode, its updates equal those of the Monte Carlo (Widrow–Hoff) rule. With Watkins's λ-return as the forward view, Sutton and Barto (1998) extended it to every λ\lambda: the backward view's summed updates equal the forward view's. The identity needs the values to stand still, and real TD(λ) moves them at every step, so for a quarter of a century the online algorithm was understood only as an approximation, one that can even blow up. True online TD(λ) (van Seijen & Sutton, 2014) closed the gap exactly, with a new kind of trace. This lesson proves the off-line equivalence in general form, shows how and why the online algorithm drifts, derives dutch traces in the simplest case, and ends with what linear TD(λ) converges to and how good that answer is.

What you’ll learn

  • Prove the off-line equivalence of the forward and backward views under linear function approximation, and identify its λ=1\lambda = 1 cases with every-visit and first-visit Monte Carlo.
  • Explain why online TD(λ) departs from the forward view, and why accumulating traces can make it unstable at large α\alpha and λ\lambda.
  • Define the online λ-return algorithm, state the true online TD(λ) equivalence theorem, and derive the dutch trace in the Monte Carlo case.
  • State Tsitsiklis and Van Roy's convergence theorem for linear TD(λ) and prove its error bound, including how it depends on λ\lambda.

The off-line equivalence, in general

Work with linear values v^(s,w)=w⊤x(s)\hat v(s,\mathbf w) = \mathbf w^\top\mathbf x(s), writing xt=x(St)\mathbf x_t = \mathbf x(S_t) and xT=0\mathbf x_T = \mathbf 0 at the end of the episode; the tabular case is x(s)=es\mathbf x(s) = \mathbf e_s. With w\mathbf w held fixed during an episode:

  • the forward view (off-line λ-return algorithm) sums the increments α(Gtλ−w⊤xt)xt\alpha\big(G_t^\lambda - \mathbf w^\top\mathbf x_t\big)\mathbf x_t;
  • the backward view (off-line TD(λ)) sums α δt zt\alpha\,\delta_t\,\mathbf z_t, with δt=Rt+1+γw⊤xt+1−w⊤xt\delta_t = R_{t+1} + \gamma\mathbf w^\top\mathbf x_{t+1} - \mathbf w^\top\mathbf x_t and the accumulating trace zt=γλ zt−1+xt\mathbf z_t = \gamma\lambda\,\mathbf z_{t-1} + \mathbf x_t, z−1=0\mathbf z_{-1} = \mathbf 0.
DerivationProof
  1. Gtλ−w⊤xt=∑k=tT−1(γλ)k−t δkG_t^\lambda - \mathbf w^\top\mathbf x_t = \sum_{k=t}^{T-1}(\gamma\lambda)^{k-t}\,\delta_k

    The λ-return recursion of n-Step Returns & Eligibility Traces, with w⊤x\mathbf w^\top\mathbf x in place of VV, gives the error of each target as a sum of later TD errors:

  2. ∑t=0T−1xt∑k=tT−1(γλ)k−tδk=∑k=0T−1δk∑t=0k(γλ)k−t xt⏟= zk■\sum_{t=0}^{T-1}\mathbf x_t\sum_{k=t}^{T-1}(\gamma\lambda)^{k-t}\delta_k = \sum_{k=0}^{T-1}\delta_k\underbrace{\sum_{t=0}^{k}(\gamma\lambda)^{k-t}\,\mathbf x_t}_{=\ \mathbf z_k}\qquad\blacksquare

    Multiply by xt\mathbf x_t, sum over tt, and exchange the order of summation over the triangle 0≤t≤k≤T−10 \le t \le k \le T-1:

  3. Nothing about linearity was used beyond the fact that the gradient xt\mathbf x_t does not depend on w\mathbf w, which is why the off-line equality holds for linear (and tabular) values and not for nonlinear ones, whose gradients move with the weights.

Two Monte Carlo methods in disguise. Take the tabular case with λ=1\lambda = 1 and γ=1\gamma = 1. The accumulating trace of a state counts its visits so far, and the theorem turns off-line TD(1) into every-visit Monte Carlo: one increment α(Gt−V(s))\alpha(G_t - V(s)) per visit. The replacing trace sets z(s)=1z(s) = 1 at each visit instead. Each TD error after the first visit then gets weight exactly 1, once, so the total increment is α∑k≥t1δk=α(Gt1−V(s))\alpha\sum_{k \ge t_1}\delta_k = \alpha(G_{t_1} - V(s)), with t1t_1 the first visit: first-visit Monte Carlo (Singh & Sutton, 1996). Their properties, first-visit unbiased and every-visit biased but consistent, are analyzed in Monte Carlo Estimation in Depth.

Check your understanding

Off-line, tabular, λ=γ=1\lambda = \gamma = 1: a state is visited at times 3 and 7 of an episode, and VV is its current estimate. What total update do accumulating and replacing traces produce for it?

  1. Accumulating: α(G3−V)\alpha(G_3 - V); replacing: α[(G3−V)+(G7−V)]\alpha[(G_3 - V) + (G_7 - V)]
  2. Both: α(G3−V)\alpha(G_3 - V)
  3. Accumulating: α[(G3−V)+(G7−V)]\alpha[(G_3 - V) + (G_7 - V)]; replacing: α(G3−V)\alpha(G_3 - V)
  4. Both: α[(G3−V)+(G7−V)]\alpha[(G_3 - V) + (G_7 - V)]
Show the answer

C. Accumulating: α[(G3−V)+(G7−V)]\alpha[(G_3 - V) + (G_7 - V)]; replacing: α(G3−V)\alpha(G_3 - V)

The accumulating trace is 1 between the visits and 2 after the second, so TD errors after time 7 are counted twice: that is one increment per visit, every-visit Monte Carlo. The replacing trace stays at 1 from the first visit on, so every later TD error is counted once, and they sum to G3−VG_3 - V: first-visit Monte Carlo.

Online, the views drift apart

Online TD(λ) updates w\mathbf w at every step, so δk\delta_k is computed with weights already moved by the episode's earlier updates. The exchange of sums still goes through, but the TD errors in it are no longer the ones a fixed w\mathbf w would produce: the online and off-line totals differ by terms of second order in α\alpha. This difference cuts both ways. Bootstrapping from estimates improved earlier in the same episode helps, which is why online TD(λ) often learns faster at small α\alpha. At large α\alpha the same feedback can turn into instability.

The culprit is the accumulating trace of a state that is revisited often. With γλ=1\gamma\lambda = 1 it simply counts visits. On the 19-state random walk a walk from the middle lasts 100 steps on average and visits the middle state 10 times on average, often far more. A TD error that measures the state's own error is applied with the factor αz(s)\alpha z(s): beyond 1 the correction overshoots the target, and beyond 2 it overshoots by more than the error it corrects, so the errors grow. Every-visit Monte Carlo with the same α\alpha applies the same kind of increments one at a time at the end of the walk, each moving V(s)V(s) a fraction α\alpha toward a return in [−1,1][-1, 1], and cannot blow up.

Sutton and Barto's comparison on the same walk (their Figure 12.6) shows the pattern: at step sizes up to each λ\lambda's best, online TD(λ) and the off-line λ-return algorithm perform almost identically; above it, TD(λ) is much worse and can be unstable, while the λ-return algorithm degrades only a little. Is there an online algorithm that keeps the forward view's robustness and TD(λ)'s cost? Yes, but it needs a different forward view and a different trace.

True online TD(λ) and dutch traces

An online forward view. At time hh the data reach only to hh, so a forward view that learns during the episode must use truncated λ-returns, which put the remaining weight on the longest return available:

Gt:hλ=(1−λ)∑n=1h−t−1λn−1Gt:t+n+λh−t−1Gt:h,0≤t<h.G_{t:h}^\lambda = (1-\lambda)\sum_{n=1}^{h-t-1}\lambda^{n-1}G_{t:t+n} + \lambda^{h-t-1}G_{t:h},\qquad 0 \le t < h.

The online λ-return algorithm redoes, at every new horizon hh, all of the episode's updates from its initial weights, wt+1h=wth+α(Gt:hλ−wth⊤xt)xt\mathbf w^h_{t+1} = \mathbf w^h_t + \alpha\big(G_{t:h}^\lambda - \mathbf w^{h\top}_t\mathbf x_t\big)\mathbf x_t for t=0,…,h−1t = 0, \dots, h-1, with the returns bootstrapping from the weights computed along the way. Its weights wh=whh\mathbf w_h = \mathbf w^h_h use all the data so far, but its cost per step grows with the length of the episode. On the 19-state walk it performs slightly better than the off-line λ-return algorithm (Sutton and Barto, Figure 12.8).

DerivationWhere the dutch trace comes from: the Monte Carlo case (van Hasselt & Sutton, 2015)
  1. wt+1=wt+α(G−wt⊤xt)xt=Ft wt+αG xt\mathbf w_{t+1} = \mathbf w_t + \alpha\big(G - \mathbf w_t^\top\mathbf x_t\big)\mathbf x_t = F_t\,\mathbf w_t + \alpha G\,\mathbf x_t

    Take the simplest forward view: linear Monte Carlo with a single final return GG and γ=1\gamma = 1, updating at every step toward GG. With the fading matrix Ft=I−α xtxt⊤F_t = I - \alpha\,\mathbf x_t\mathbf x_t^\top, each update is affine in the weights:

  2. wT=FT−1⋯F0 w0⏟aT−1+αG ∑k=0T−1FT−1⋯Fk+1 xk⏟zT−1\mathbf w_T = \underbrace{F_{T-1}\cdots F_0\,\mathbf w_0}_{\mathbf a_{T-1}} + \alpha G\,\underbrace{\sum_{k=0}^{T-1}F_{T-1}\cdots F_{k+1}\,\mathbf x_k}_{\mathbf z_{T-1}}

    Unroll to the end of the episode. GG is unknown until then, but it multiplies a sum that can be built along the way:

  3. zt=zt−1+(1−α zt−1⊤xt) xt\mathbf z_t = \mathbf z_{t-1} + \big(1 - \alpha\,\mathbf z_{t-1}^\top\mathbf x_t\big)\,\mathbf x_t

    Both vectors update online in order-dd time: at=Ftat−1=at−1−αxt(xt⊤at−1)\mathbf a_t = F_t\mathbf a_{t-1} = \mathbf a_{t-1} - \alpha\mathbf x_t(\mathbf x_t^\top\mathbf a_{t-1}), and the trace satisfies zt=Ftzt−1+xt\mathbf z_t = F_t\mathbf z_{t-1} + \mathbf x_t, that is,

  4. This is the dutch trace with γλ=1\gamma\lambda = 1, and wT=aT−1+αG zT−1\mathbf w_T = \mathbf a_{T-1} + \alpha G\,\mathbf z_{T-1} reproduces the Monte Carlo result exactly without storing the episode. A trace appeared with no TD learning in sight: traces are what efficient online computation of a forward view looks like. The true online derivation follows the same plan with bootstrapping and γλ\gamma\lambda, along the diagonal of the online λ-return algorithm's triangle of weights. ■\blacksquare

Tabular dutch traces. With xt=eSt\mathbf x_t = \mathbf e_{S_t}, the visited state's trace becomes z(St)←(1−α)γλ z(St)+1z(S_t) \leftarrow (1-\alpha)\gamma\lambda\,z(S_t) + 1 while all others decay by γλ\gamma\lambda. With α=1\alpha = 1 this is a replacing trace; with α→0\alpha \to 0 it approaches an accumulating one. With γλ=1\gamma\lambda = 1 it can never exceed 1/α1/\alpha, so αz≤1\alpha z \le 1: no state's own correction can overshoot, which removes the mechanism of the blow-up above. Sutton and Barto now regard replacing traces as crude approximations to dutch traces, which usually perform better and rest on a clearer theory; accumulating traces remain in use with nonlinear function approximation, where no dutch trace is available.

Predict before you look

A state is visited at three consecutive steps, starting from trace 0, with γλ=1\gamma\lambda = 1 and α=0.1\alpha = 0.1. What is its tabular dutch trace after the third visit? (An accumulating trace would be 3, a replacing trace 1.)

Show the answer

2.71

z←0.9z+1z \leftarrow 0.9z + 1 at each visit: 11, then 0.9+1=1.90.9 + 1 = 1.9, then 0.9×1.9+1=2.710.9 \times 1.9 + 1 = 2.71. After many visits it approaches 1/α=101/\alpha = 10 and never passes it, so αz\alpha z stays below 1.

Linear TD(λ): where it converges, and how good the answer is

DerivationProof of the error bound, and a sharper form
  1. κ=(1−λ)∑m=0∞λmγm+1=γ(1−λ)1−γλ≤γ\kappa = (1-\lambda)\sum_{m=0}^\infty\lambda^m\gamma^{m+1} = \frac{\gamma(1-\lambda)}{1-\gamma\lambda} \le \gamma

    Under the stationary distribution, ∥PπV∥D≤∥V∥D\norm{P^\pi V}_D \le \norm{V}_D (Jensen's inequality and stationarity). The linear part of T(λ)\mathcal T^{(\lambda)} is (1−λ)∑mλm(γPπ)m+1(1-\lambda)\sum_m\lambda^m(\gamma P^\pi)^{m+1}, so T(λ)\mathcal T^{(\lambda)} is a contraction in ∥⋅∥D\norm{\cdot}_D with modulus

  2. ∥Φw⋆−Vπ∥D2=∥Φw⋆−ΠVπ∥D2+∥ΠVπ−Vπ∥D2\norm{\Phi\mathbf w^\star - V^\pi}_D^2 = \norm{\Phi\mathbf w^\star - \Pi V^\pi}_D^2 + \norm{\Pi V^\pi - V^\pi}_D^2

    Π\Pi is an orthogonal projection in the same norm, hence non-expansive, so ΠT(λ)\Pi\mathcal T^{(\lambda)} is a κ\kappa-contraction with a unique fixed point Φw⋆\Phi\mathbf w^\star. Now Φw⋆−ΠVπ\Phi\mathbf w^\star - \Pi V^\pi lies in the feature space and ΠVπ−Vπ\Pi V^\pi - V^\pi is DD-orthogonal to it, so Pythagoras applies:

  3. ∥Φw⋆−Vπ∥D≤11−κ2 ∥ΠVπ−Vπ∥D\norm{\Phi\mathbf w^\star - V^\pi}_D \le \frac{1}{\sqrt{1-\kappa^2}}\,\norm{\Pi V^\pi - V^\pi}_D

    Since Vπ=T(λ)VπV^\pi = \mathcal T^{(\lambda)}V^\pi, the first term is ∥ΠT(λ)Φw⋆−ΠT(λ)Vπ∥D2≤κ2∥Φw⋆−Vπ∥D2\norm{\Pi\mathcal T^{(\lambda)}\Phi\mathbf w^\star - \Pi\mathcal T^{(\lambda)}V^\pi}_D^2 \le \kappa^2\norm{\Phi\mathbf w^\star - V^\pi}_D^2. Rearranging:

  4. 11−κ2  ≤  1−γλ1−γ\frac{1}{\sqrt{1-\kappa^2}} \;\le\; \frac{1-\gamma\lambda}{1-\gamma}

    Finally 1−κ2≥1−κ=(1−γ)/(1−γλ)\sqrt{1-\kappa^2} \ge 1 - \kappa = (1-\gamma)/(1-\gamma\lambda), which gives Tsitsiklis and Van Roy's factor; squaring instead, 1/(1−κ2)≤(1−γλ)/(1−γ)1/(1-\kappa^2) \le (1-\gamma\lambda)/(1-\gamma), which is Sutton and Barto's version for the squared value error. ■\blacksquare

Predict before you look

With γ=0.99\gamma = 0.99, Tsitsiklis and Van Roy's factor is 100 for TD(0). What is it for λ=0.9\lambda = 0.9?

Show the answer

10.9

(1−0.99×0.9)/(1−0.99)=0.109/0.01=10.9(1 - 0.99 \times 0.9)/(1 - 0.99) = 0.109/0.01 = 10.9. The sharper factor 1/1−κ21/\sqrt{1-\kappa^2} is about 7.1 for TD(0) and 2.4 for λ=0.9\lambda = 0.9 (κ≈0.908\kappa \approx 0.908). At λ=1\lambda = 1 both equal 1: TD(1) finds the best approximation in the span, ΠVπ\Pi V^\pi.

So λ\lambda trades two kinds of error. Larger λ\lambda moves the fixed point toward the best approximation in the feature space, but makes the updates noisier, and the bound says nothing about how many samples each setting needs. The bound is a worst case: with good features, TD(0)'s fixed point is often much closer than 1/(1−γ)1/(1-\gamma) times the best error. The whole argument needs the stationary distribution of π\pi itself, through ∥PπV∥D≤∥V∥D\norm{P^\pi V}_D \le \norm{V}_D; with off-policy sampling that inequality fails, and so can convergence (The Deadly Triad). The Projected Bellman Equation & LSTD develops the fixed point Φw⋆\Phi\mathbf w^\star and its least-squares solution.

Key takeaways

  • Off-line, for linear or tabular values, the backward view's summed updates ∑αδtzt\sum\alpha\delta_t\mathbf z_t equal the forward view's ∑α(Gtλ−w⊤xt)xt\sum\alpha(G_t^\lambda - \mathbf w^\top\mathbf x_t)\mathbf x_t: one exchange of summations, path by path. With λ=γ=1\lambda = \gamma = 1, accumulating traces give every-visit and replacing traces first-visit Monte Carlo.
  • Online, TD errors use weights that changed earlier in the episode; the views then differ at second order in α\alpha, which helps at small α\alpha and can destabilize accumulating traces at large α\alpha and λ\lambda.
  • True online TD(λ) reproduces the online λ-return algorithm exactly for linear values, in order-dd time, using the dutch trace zt=γλzt−1+(1−αγλzt−1⊤xt)xt\mathbf z_t = \gamma\lambda\mathbf z_{t-1} + (1 - \alpha\gamma\lambda\mathbf z_{t-1}^\top\mathbf x_t)\mathbf x_t; in tables it keeps αz≤1\alpha z \le 1 when γλ=1\gamma\lambda = 1.
  • On-policy linear TD(λ) converges to the fixed point of ΠT(λ)\Pi\mathcal T^{(\lambda)}, a γ(1−λ)/(1−γλ)\gamma(1-\lambda)/(1-\gamma\lambda)-contraction, with error at most 1−γλ1−γ\frac{1-\gamma\lambda}{1-\gamma} (sharper: 1/1−κ21/\sqrt{1-\kappa^2}) times the best approximation error.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What does the proof of the off-line equivalence rest on?

  1. The contraction of the Bellman operator
  2. Writing each λ-return error as a (γλ)(\gamma\lambda)-discounted sum of TD errors computed with one fixed w\mathbf w, then exchanging the order of summation, which collects each TD error's coefficients into the trace
  3. The Markov property of the environment
  4. Taking expectations over the episode
Show the answer

B. Writing each λ-return error as a (γλ)(\gamma\lambda)-discounted sum of TD errors computed with one fixed w\mathbf w, then exchanging the order of summation, which collects each TD error's coefficients into the trace

The identity is algebraic and holds on every sample path: no expectation, no contraction and no Markov property is involved. It needs fixed weights (so that each δk\delta_k is the same number in both views) and gradients xt\mathbf x_t that do not depend on w\mathbf w.

Question 2

On the 19-state walk with λ=1\lambda = 1 and α=0.1\alpha = 0.1, online TD(λ) with accumulating traces blows up, while every-visit Monte Carlo with the same α\alpha does not. Why?

  1. Because Monte Carlo uses a smaller effective discount
  2. Because TD(λ) bootstraps from terminal states
  3. Because TD errors with λ=1\lambda = 1 are biased
  4. Because a frequently revisited state's accumulating trace grows far above 1, so its own TD errors move its value by αz\alpha z times the error, which overshoots once αz>2\alpha z > 2
Show the answer

D. Because a frequently revisited state's accumulating trace grows far above 1, so its own TD errors move its value by αz\alpha z times the error, which overshoots once αz>2\alpha z > 2

With γλ=1\gamma\lambda = 1 the trace counts the visits so far. In long walks the middle states are revisited dozens of times, αz\alpha z passes 2, and each correction overshoots by more than it corrects. Monte Carlo applies each visit's increment separately, always moving a fraction α≤1\alpha \le 1 toward a bounded return.

Question 3

In the tabular case, how does a dutch trace change at a visit, and what does that guarantee when γλ=1\gamma\lambda = 1?

  1. z←z+1z \leftarrow z + 1; it guarantees nothing about the size of zz
  2. z←1z \leftarrow 1; it guarantees z≤1z \le 1
  3. z←(1−α)γλ z+1z \leftarrow (1-\alpha)\gamma\lambda\,z + 1; it guarantees z≤1/αz \le 1/\alpha, so the visited state's own correction αz\alpha z never exceeds 1
  4. z←γλzz \leftarrow \gamma\lambda z; it guarantees that traces vanish between visits
Show the answer

C. z←(1−α)γλ z+1z \leftarrow (1-\alpha)\gamma\lambda\,z + 1; it guarantees z≤1/αz \le 1/\alpha, so the visited state's own correction αz\alpha z never exceeds 1

With xt=eSt\mathbf x_t = \mathbf e_{S_t}, zt=γλzt−1+(1−αγλzt−1(St))eSt\mathbf z_t = \gamma\lambda\mathbf z_{t-1} + (1 - \alpha\gamma\lambda z_{t-1}(S_t))\mathbf e_{S_t}, which for the visited state is (1−α)γλz+1(1-\alpha)\gamma\lambda z + 1. Its fixed point under repeated visits is 1/(1−(1−α)γλ)1/(1 - (1-\alpha)\gamma\lambda), equal to 1/α1/\alpha when γλ=1\gamma\lambda = 1.

Question 4

In Tsitsiklis and Van Roy's bound ∥Φw⋆−Vπ∥D≤1−γλ1−γ∥ΠVπ−Vπ∥D\norm{\Phi\mathbf w^\star - V^\pi}_D \le \frac{1-\gamma\lambda}{1-\gamma}\norm{\Pi V^\pi - V^\pi}_D, what happens as λ\lambda increases from 0 to 1, and what is the catch?

  1. The factor falls from 1/(1−γ)1/(1-\gamma) to 1, so TD(1)'s fixed point is the best approximation ΠVπ\Pi V^\pi; the catch is noisier updates, which the bound does not measure
  2. The factor grows, so larger λ\lambda is always worse
  3. The factor is constant; λ\lambda only changes the speed
  4. The factor falls to 0, so TD(1) recovers VπV^\pi exactly with any features
Show the answer

A. The factor falls from 1/(1−γ)1/(1-\gamma) to 1, so TD(1)'s fixed point is the best approximation ΠVπ\Pi V^\pi; the catch is noisier updates, which the bound does not measure

At λ=1\lambda = 1 the factor is 1 and Φw⋆=ΠVπ\Phi\mathbf w^\star = \Pi V^\pi, the best the features allow. Smaller λ\lambda bootstraps more, which can amplify the approximation error by up to 1/(1−γ)1/(1-\gamma) but reduces variance. In practice intermediate λ\lambda usually learns best.

Further reading

  • R. S. Sutton, "Learning to predict by the methods of temporal differences," Machine Learning 3, 9–44 (1988).
  • S. P. Singh & R. S. Sutton, "Reinforcement learning with replacing eligibility traces," Machine Learning 22, 123–158 (1996).
  • H. van Seijen & R. S. Sutton, "True online TD(λ)," Proceedings of the 31st International Conference on Machine Learning, 692–700 (2014).
  • H. van Seijen, A. R. Mahmood, P. M. Pilarski, M. C. Machado & R. S. Sutton, "True online temporal-difference learning," Journal of Machine Learning Research 17(145), 1–40 (2016).
  • J. N. Tsitsiklis & B. Van Roy, "An analysis of temporal-difference learning with function approximation," IEEE Transactions on Automatic Control 42(5), 674–690 (1997).