Chapter 12
Eligibility Traces
An eligibility trace is the mechanism that turns the one-step methods of the last three chapters into multi-step ones without waiting for the future: a short-term memory vector that runs alongside the long-term weight vector . When a component of helps to produce an estimate, the matching component of is bumped up and then fades; if a TD error arrives before it has faded, that component learns. The -step methods of Chapter 7 already interpolate between one-step TD and Monte Carlo, but they must store the last feature vectors and they learn steps late. A trace needs one vector and learns at once. The chapter develops one idea in several layers. First we define an ideal forward view, the -return, which looks ahead from each state to a geometric mixture of all -step returns. Then we find backward views, algorithms that look back from the current TD error along the trace, and prove that they implement the forward view, approximately for TD() and exactly for true online TD(). Finally we generalize the forward view, to variable bootstrapping and discounting and to off-policy data, and read off the corresponding traces.Richard S. Sutton and Andrew G. Barto. Reinforcement Learning: An Introduction. Adaptive Computation and Machine Learning. MIT Press, Cambridge, MA, second edition, 2018. ISBN 978-0-262-03924-6. URL http://incompleteideas.net/book/the-book-2nd.html
12.1The -return#
Write the -step return of (7.1) for a general approximator, bootstrapping from the most recent weights,
with when . Each is a valid target, and so is any average of them with nonnegative weights summing to one, since averaging preserves the error-reduction property that makes -step targets work. Such a target is a compound update. The -return gives weight proportional to , , to the -step return:
(12.1)In an episode ending at every return with equals , and their weights add to , so
(12.2)At only the one-step return survives and the target is TD(0)’s; at only survives and the target is Monte Carlo’s. The weight on halves every steps (Exercise 12.9). The offline -return algorithm waits for the end of an episode and then makes the semi-gradient updates
(12.3)On the 19-state random walk of Chapter 7 it performs about as well as the best -step method, with the best results, again, at intermediate .
Two identities turn this forward view into something computable. They hold for a fixed weight vector, written with , and the TD error (6.2).
Proposition 12.1 (Recursion and TD-error form of the -return). For a fixed value function,
(12.4)(12.5)with the convention .
Proof. Each -step return splits as for , while . Substituting in (12.1),
using ; the last sum is . For (12.5), subtract from the recursion and add and subtract : . Unrolling to , where , gives the sum. □
The recursion is the -analogue of ((3.5)): at the next state the target bootstraps with probability and continues with probability . The TD-error form says that the -return error is a sum of future one-step errors, each discounted by per step of delay. That sum is what a trace will deliver.
12.1.1In Wolfram Language#
We check the definitions on an episode of length with symbolic rewards and values .
The weights of the episodic -return (12.2) sum to one for any horizon .await show`Simplify[(1 - \[Lambda]) Sum[\[Lambda]^(n - 1), {n, 1, m - 1}] + \[Lambda]^(m - 1)]`const gn = await wl`Function[{t, n}, Sum[\[Gamma]^(k - 1) Subscript[R, t + k], {k, 1, Min[n, 4 - t]}] + If[t + n < 4, \[Gamma]^n Subscript[V, t + n], 0]]`
const glam = await wl`Function[{t}, (1 - \[Lambda]) Sum[\[Lambda]^(n - 1) ${gn}[t, n], {n, 1, 3 - t}] + \[Lambda]^(3 - t) ${gn}[t, 4 - t]]`
await show`Collect[Expand[${glam}[2]], \[Lambda]]`await show`Table[Simplify[${glam}[t] - (Subscript[R, t + 1] + \[Gamma] ((1 - \[Lambda]) If[t + 1 < 4, Subscript[V, t + 1], 0] + \[Lambda] If[t + 1 < 4, ${glam}[t + 1], 0]))], {t, 0, 3}]`const tdErr = await wl`Function[{k}, Subscript[R, k + 1] + \[Gamma] If[k + 1 < 4, Subscript[V, k + 1], 0] - Subscript[V, k]]`
await show`Table[Simplify[${glam}[t] - Subscript[V, t] - Sum[(\[Gamma] \[Lambda])^(k - t) ${tdErr}[k], {k, t, 3}]], {t, 0, 3}]`await show`{${glam}[0] /. \[Lambda] -> 0, Expand[${glam}[0] /. \[Lambda] -> 1]}`12.2TD()#
**TD()** replaces the forward view by a backward one. It keeps an eligibility trace, reset at the start of each episode, incremented by the value gradient and decaying by ,
(12.6)and moves the weights at every step along the trace, in proportion to the current TD error, . This is the accumulating trace; with linear features is simply a decaying sum of past feature vectors.
Algorithm 12.2 (Semi-gradient TD()). Input: the policy to evaluate; a differentiable with . Parameters: step size , trace decay . Initialize: arbitrarily.
Loop for each episode:
- Initialize ; .
- Loop for each step until is terminal:
- choose , take it, observe ;
- ;
- ;
- ; .
At the trace is the current gradient and TD() is semi-gradient TD(0), hence the name. At the credit given to a state steps back falls by , exactly the discount that the reward inside deserves in that state’s return; TD(1) is an incremental, online implementation of Monte Carlo that works for continuing tasks too and learns from an episode while it is still running.
12.2.1The Forward and the Backward View Agree Offline#
The link between the two views is an exchange of the order of summation.
Theorem 12.3 (Offline equivalence of TD() and the -return algorithm). Suppose that during an episode the weights are held fixed at , the TD() increments being accumulated and applied only at the end. Then the total TD() update equals the total update of the offline -return algorithm computed with the same fixed :
Proof. With fixed, . Hence
where the last step is (12.5). □
Online, the weights do change within the episode, and the equivalence becomes approximate, with an error of order per episode. For small step sizes TD() and the offline -return algorithm are practically indistinguishable; for large ones TD() is markedly worse and can become unstable, because a state’s value is adjusted repeatedly with stale TD errors. Section 12.4 removes this defect.
Offline equivalence on a three-step episode with arbitrary linear features and fixed weights: backward minus forward total update.const xsT = await wl`Table[If[t < 3, {Subscript[x, t, 1], Subscript[x, t, 2]}, {0, 0}], {t, 0, 3}]`
const wFix = await wl`{Subscript[w, 1], Subscript[w, 2]}`
await show`Module[{v, d, z, gl, back, fwd}, v = Function[t, ${wFix} . ${xsT}[[t + 1]]]; d = Function[t, Subscript[R, t + 1] + \[Gamma] v[t + 1] - v[t]]; z = Function[t, Sum[(\[Gamma] \[Lambda])^(t - k) ${xsT}[[k + 1]], {k, 0, t}]]; gl = Function[t, (1 - \[Lambda]) Sum[\[Lambda]^(n - 1) (Sum[\[Gamma]^(j - 1) Subscript[R, t + j], {j, 1, n}] + \[Gamma]^n v[t + n]), {n, 1, 2 - t}] + \[Lambda]^(2 - t) Sum[\[Gamma]^(j - 1) Subscript[R, t + j], {j, 1, 3 - t}]]; back = Sum[\[Alpha] d[t] z[t], {t, 0, 2}]; fwd = Sum[\[Alpha] (gl[t] - v[t]) ${xsT}[[t + 1]], {t, 0, 2}]; Simplify[back - fwd]]`12.2.2What TD() Converges To#
On-policy linear TD() converges with probability one under the usual step-size conditions, to a weight vector that depends on . It is the fixed point of a projected operator. Define the TD() operator as the -mixture of iterated Bellman operators,
Then , with the -projection of Section 11.4.John N. Tsitsiklis and Benjamin Van Roy. An Analysis of Temporal-Difference Learning with Function Approximation. IEEE Transactions on Automatic Control, 42(5):674–690, 1997. doi: 10.1109/9.580874
Theorem 12.4 (Error bound for linear TD()). On-policy, with the stationary distribution of and , let . Then
Proof. Stationarity gives (Jensen’s inequality row by row, then ), so : is a -contraction with fixed point , and so is , since is non-expansive. Write . The vector lies in the subspace and is -orthogonal to it, so by Pythagoras
Rearranging gives the first inequality, since . The second follows from and . □
At this is the bound of Theorem 9.7; as it tends to the Monte Carlo optimum. Yet in practice is rarely the best choice: the bound is about the limit, while the speed of learning, which bootstrapping improves, usually matters more.
The contraction factor of along an eigenvalue of : summing the series gives , and at .const serP = await show`Assuming[0 < \[Gamma] < 1 && 0 <= \[Lambda] <= 1 && 0 < p <= 1, Simplify[Sum[(1 - \[Lambda]) \[Lambda]^n \[Gamma]^(n + 1) p^(n + 1), {n, 0, Infinity}]]]`const kap = await wl`${serP} /. p -> 1`
await show`Factor[(1 - \[Gamma] \[Lambda])/(1 - \[Gamma]) - 1/(1 - ${kap}^2)]`12.3Truncated -returns and Redoing Updates#
The offline -return is known only at the end of the episode, and never in a continuing task. Its dependence on distant rewards fades like , so it is natural to truncate it at a horizon , giving the residual weight to the longest available -step return:
(12.7)**Truncated TD()** updates toward with an -step delay, like the methods of Chapter 7, but with all -step returns, , mixed geometrically. It is cheap because the truncated return is again a sum of TD errors, now with each TD error using the weights current at its time (Exercise 12.12).
Truncation forces a compromise: a long horizon approximates the -return well, a short one lets updates influence behavior sooner. The online -return algorithm avoids it by brute force. Whenever the horizon advances from to , it redoes every update of the episode so far, from the initial weights, with the new, better targets:
(12.8)for , starting each row from ; its output is the diagonal , which is also the weight vector the truncated returns bootstrap from. It is fully online, it uses only data available at time , and at the end of the episode it is at least as good as the offline algorithm, because its bootstrapping weights have received more informative updates. Its only defect is cost: updates at step .
12.4True Online TD()#
For linear approximation, , the diagonal of the online -return algorithm can be computed exactly with work per step. The result is **true online TD()**,Harm van Seijen and Richard S. Sutton. True Online TD(). In Proceedings of the 31st International Conference on Machine Learning (ICML 2014), volume 32 of Proceedings of Machine Learning Research, pages 692–700, 2014; and Harm van Seijen, A. Rupam Mahmood, Patrick M. Pilarski, Marlos C. Machado, and Richard S. Sutton. True Online Temporal-Difference Learning. Journal of Machine Learning Research, 17(145):1–40, 2016
(12.9)with as in TD(). The trace in (12.9) is the dutch trace. Compared with the accumulating trace, the increment is reduced along the direction in which the trace already points, : the matrix is exactly what one LMS step on does to the weights, so the trace already knows that the earlier contributions will be partly overwritten. The older replacing trace, defined for binary features by if and otherwise, is a crude version of the same idea (Exercise 12.10).
Algorithm 12.5 (True online TD()). Input: ; features with . Parameters: , . Initialize: .
Loop for each episode:
- Initialize , , , .
- Loop for each step until :
- choose , observe and ;
- , , ;
- ;
- ;
- , .
The update of in the box is (12.9) rearranged: is , saved at the previous step. Memory is that of TD(), and the cost per step grows by one inner product.
Theorem 12.6 (Equivalence of true online TD() and the online -return algorithm). For linear function approximation, the weight vectors produced by Algorithm 12.5 coincide, for every , with the diagonal of the online -return algorithm (12.8).
The general proof (van Seijen et al., appendix) finds a recursion for by comparing two consecutive rows of the triangle and expressing their difference through a trace. Rather than reproduce it, we verify the theorem symbolically. The script below runs the online -return algorithm literally, redoing all updates at each horizon and bootstrapping from the diagonal, and runs true online TD() on the same three-step episode with arbitrary features , rewards, step size and initial weights.
The online -return algorithm, literally: for each horizon , redo all updates from with targets that bootstrap from the diagonal weights; return the diagonal. The display checks the first row: at horizon the algorithm makes exactly one TD(0) step.const xsO = await wl`{{Subscript[x, 0, 1], Subscript[x, 0, 2]}, {Subscript[x, 1, 1], Subscript[x, 1, 2]}, {Subscript[x, 2, 1], Subscript[x, 2, 2]}, {0, 0}}`
const w0O = await wl`{Subscript[w, 1], Subscript[w, 2]}`
const onlineDiag = await wl`Fold[Function[{diag, h}, With[{gn = Function[{t, n}, Sum[\[Gamma]^(j - 1) Subscript[R, t + j], {j, 1, n}] + If[t + n < 3, \[Gamma]^n diag[[t + n]] . ${xsO}[[t + n + 1]], 0]]}, With[{gl = Function[{t}, (1 - \[Lambda]) Sum[\[Lambda]^(n - 1) gn[t, n], {n, 1, h - t - 1}] + \[Lambda]^(h - t - 1) gn[t, h - t]]}, Append[diag, Expand[Fold[Function[{w, t}, w + \[Alpha] (gl[t] - w . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]], ${w0O}, Range[0, h - 1]]]]]]], {${w0O}}, {1, 2, 3}]`
await show`Simplify[${onlineDiag}[[2]] - (${w0O} + \[Alpha] (Subscript[R, 1] + \[Gamma] ${w0O} . ${xsO}[[2]] - ${w0O} . ${xsO}[[1]]) ${xsO}[[1]])]`const trueOnline = await wl`FoldList[Function[{st, t}, Module[{w = st[[1]], z = st[[2]], vo = st[[3]], v, v1, d, zn}, v = w . ${xsO}[[t + 1]]; v1 = w . ${xsO}[[t + 2]]; d = Subscript[R, t + 1] + \[Gamma] v1 - v; zn = \[Gamma] \[Lambda] z + (1 - \[Alpha] \[Gamma] \[Lambda] z . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]; {Expand[w + \[Alpha] (d + v - vo) zn - \[Alpha] (v - vo) ${xsO}[[t + 1]]], zn, v1}]], {${w0O}, {0, 0}, 0}, {0, 1, 2}][[All, 1]]`
await show`Expand[${trueOnline} - ${onlineDiag}]`12.4.1Dutch Traces in Monte Carlo Learning#
Dutch traces are not specific to TD learning: they appear as soon as one implements a forward view efficiently, even with no bootstrapping at all.Hado van Hasselt and Richard S. Sutton. Learning to Predict Independent of Span, 2015 Take linear gradient Monte Carlo (the LMS rule) on an episode whose only reward arrives at the end, with :
Nothing can be done before is known, and the obvious implementation stores all feature vectors and spends at the end. Writing the update as with the fading matrix reveals a better way.
Proposition 12.7 (Dutch traces for Monte Carlo). Let , and, for ,
(12.10)Then the final LMS weights are , computed with work and memory per step.
Proof. Unrolling gives . The first term is , because . Call the sum ; splitting off its last term, , which is (12.10). □
This is the dutch trace (12.9) with .Sutton and Barto define but initialize and apply the recursion only for . That produces , without the factor . Their own definition gives , or equivalently the start used in the proposition. The two coincide when , in particular for the usual ; the second check below gives the error otherwise. Eligibility traces, it seems, are what any efficient implementation of a long-horizon prediction needs, TD or not.
LMS run to the end of a three-step episode versus the dutch-trace recursion (12.10), with arbitrary : the difference vanishes.const lms = await wl`Fold[Function[{w, t}, w + \[Alpha] (G - w . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]], ${w0O}, {0, 1, 2}]`
const dutchMC = await wl`Fold[Function[{st, t}, {st[[1]] - \[Alpha] ${xsO}[[t + 1]] (${xsO}[[t + 1]] . st[[1]]), st[[2]] + (1 - \[Alpha] st[[2]] . ${xsO}[[t + 1]]) ${xsO}[[t + 1]]}], {${w0O}, {0, 0}}, {0, 1, 2}]`
await show`Expand[${lms} - (${dutchMC}[[1]] + \[Alpha] G ${dutchMC}[[2]])]`const aBook = await wl`Fold[Function[{a, t}, a - \[Alpha] ${xsO}[[t + 1]] (${xsO}[[t + 1]] . a)], ${w0O}, {1, 2}]`
await show`Map[Factor, Normal[Series[${lms} - (${aBook} + \[Alpha] G ${dutchMC}[[2]]), {\[Alpha], 0, 1}]]]`12.5Sarsa()#
For action values everything carries over with in place of , the action-value -step returns , and state–action features . **Sarsa()** is TD() with
The advantage over one-step and -step control is easy to see in a gridworld where the only reward is at the goal. When it is reached, one-step Sarsa raises the value of the last action only, and -step Sarsa the last equally; Sarsa() raises every action of the episode, fading with distance from the goal, which is usually the right credit assignment. With tile coding the features are binary and the loops over active features make each step cheap; with the true online construction one obtains the following algorithm, which on Mountain Car outperforms Sarsa() with accumulating and with replacing traces.
Algorithm 12.8 (True online Sarsa()). Input: features with ; a policy , or -greedy selection with respect to for control. Parameters: , . Initialize: .
Loop for each episode:
- Initialize ; choose from ; , , .
- Loop for each step until is terminal:
- take , observe ; choose from ; ;
- , , ;
- ;
- ;
- , , .
12.6Variable and #
The last generalization of the forward view lets bootstrapping and discounting vary from step to step: and . The function is a termination function, and it changes the problem, since the return becomes
(12.11)A terminal state is now just a state with that transitions to the start distribution, so episodic and discounted continuing tasks become one stream of experience; pseudo-termination, predicting a quantity without altering the flow of the process, is included. Varying , on the other hand, changes only the solution method: how much to trust the estimate at each state. The state-based -return generalizes (12.4) to
(12.12)and the action-based one either bootstraps from (Sarsa form) or from (Expected Sarsa form). Watkins already noted that cutting a return at a chosen time amounts to setting there.
12.7Off-policy Traces with Control Variates#
For full -returns there is no practical way to apply importance sampling to the whole return; instead the ratio is applied per decision, with the control variate of Chapter 7:
(12.13)The added term has expectation zero under , because ; it changes nothing on average but cancels much of the variance when is far from one: if the target is the current estimate and no update occurs. For a fixed value function, with , the same telescoping as in Proposition 12.1 gives
(12.14)approximately when the weights change. Summing the forward-view updates over time and exchanging the sums, exactly as in Theorem 12.3,
and the bracketed sum obeys a one-step recursion, the general accumulating trace
(12.15)With this is TD() for on- or off-policy data; with it is Algorithm 12.2. For action values, starting from the Expected Sarsa form, , the same steps give the TD error and the trace
(12.16)an off-policy Expected Sarsa(). Two caveats. At these methods are related to Monte Carlo only in expectation, not episode by episode: they make irrevocable updates along the way, whereas an importance-sampled Monte Carlo update would be canceled entirely by a later zero ratio. Provisional-weight methods, PTD() and PQ(), restore an exact equivalence. And for all of them bootstrap off-policy with function approximation: the deadly triad of Section 11.3 applies. Off-policy traces correct the targets, not the distribution of updates.
The expansion (12.14) is exact for fixed values: an episode of length with arbitrary ratios and constant ( at termination).const gOff = await wl`Function[{t}, Fold[Function[{g, k}, Subscript[\[Rho], k] (Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] ((1 - \[Lambda]) Subscript[V, k + 1] + \[Lambda] g), 0]) + (1 - Subscript[\[Rho], k]) Subscript[V, k]], 0, Range[2, t, -1]]]`
const dOff = await wl`Function[{k}, Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] Subscript[V, k + 1], 0] - Subscript[V, k]]`
await show`Table[Simplify[${gOff}[t] - (Subscript[V, t] + Subscript[\[Rho], t] Sum[${dOff}[k] Product[\[Gamma] \[Lambda] Subscript[\[Rho], i], {i, t + 1, k}], {k, t, 2}])], {t, 0, 2}]`await show`Simplify[Sum[{Subscript[b, 1], 1 - Subscript[b, 1]}[[a]] (({Subscript[\[Pi], 1], 1 - Subscript[\[Pi], 1]}[[a]]/{Subscript[b, 1], 1 - Subscript[b, 1]}[[a]]) {Subscript[g, 1], Subscript[g, 2]}[[a]] + (1 - {Subscript[\[Pi], 1], 1 - Subscript[\[Pi], 1]}[[a]]/{Subscript[b, 1], 1 - Subscript[b, 1]}[[a]]) V), {a, 1, 2}]]`12.8Watkins’s Q() and Tree-Backup()#
**Watkins’s Q()** extends Q-learning by decaying the traces normally while greedy actions are taken and cutting them to zero at the first exploratory action, after which the greedy target policy’s return no longer applies. **Tree-Backup()**, TB(), is the trace version of the tree-backup algorithm of Chapter 7 and, like it, uses no importance sampling. Expanding the Expected Sarsa form of the -return with the tree-backup target,
the derivation of the previous section goes through with replaced by :
(12.17)For a greedy target policy is for a greedy action and otherwise, so (12.17) is Watkins’s trace cutting; TB() is its natural generalization to stochastic target policies, and arguably the true successor of Q-learning. Both are semi-gradient methods and both can diverge with function approximation.
12.9Stable Off-policy Methods with Traces#
The Gradient-TD and Emphatic-TD ideas of Chapter 11 extend to traces, giving linear off-policy methods with convergence guarantees. With , and the trace (12.15) (linear case, ), and a secondary vector with step size :
- **GTD()**, the trace version of TDC: and . At the correction vanishes, as it should: without bootstrapping there is nothing to correct.
- GQ(), the action-value analogue, uses , the trace (12.16) and the target-policy average feature in the correction term; with a target policy that is greedy-ish, it is a stable control method.
- HTD() adds a second, behavior-policy trace and corrects with ; when the two traces coincide and HTD() is TD(), keeping its speed and single step size in the on-policy case.
- **Emphatic TD()** uses the followon trace , the emphasis and the trace in the ordinary update . It is stable for any state-dependent , which is not true even of on-policy TD(), at the price of high variance.
12.10Choosing #
Traces cost little. In the tabular case only the few recently visited states have non-negligible traces, so an implementation can update only those and pay a small multiple of the one-step cost; with function approximation the overhead is roughly a doubling of memory and computation. Empirically, on tasks with many steps per episode, or many steps within the discount horizon, an intermediate is clearly better than , while close to degrades sharply: traces should move TD toward Monte Carlo, not all the way. They are the first line of defense against long-delayed rewards and against partially non-Markov states, where pure bootstrapping is misled by a poor state representation. When data are cheap, as with a fast simulator, the per-sample gain of traces may not pay for their cost, and one-step methods on more data can win.
12.11Beyond the Book: Off-policy Returns at Scale#
The off-policy traces of Section 12.7 multiply ratios that can be large, and the tree-backup traces multiply probabilities that cut learning short even on-policy. The modern answer is to truncate the ratios.Beyond Sutton and Barto (2018). Retrace (Munos et al., 2016) appears in the book only as a reference in its variance discussion, and V-trace (Espeholt et al., 2018) postdates it. The JMLR analysis of true online TD (van Seijen et al., 2016) is the source of Theorem 12.6.
Retrace(). Munos et al. write many return-based algorithms as one operator with “traces” ,Rémi Munos, Tom Stepleton, Anna Harutyunyan, and Marc G. Bellemare. Safe and Efficient Off-Policy Reinforcement Learning. In Advances in Neural Information Processing Systems 29 (NIPS 2016), volume 29, pages 1054–1062. Curran Associates, Inc., 2016. URL https://proceedings.neurips.cc/paper/2016/hash/c3992e9a68c5ae12bd18488bc579b30d-Abstract.html
with for importance sampling, for TB() and for the uncorrected Q(). **Retrace()** takes . They show that any traces with give a -contraction around for arbitrary (safety), Retrace’s traces are never cut on-policy (efficiency) and never exceed one (low variance), and in control, with increasingly greedy , the sampled algorithm converges to without the GLIE assumption, which yields as a corollary the first convergence proof of Watkins’s Q(), open since 1989.
V-trace. In the distributed IMPALA architecture, actors run slightly stale policies and the learner corrects with the V-trace targetLasse Espeholt, Hubert Soyer, Rémi Munos, Karen Simonyan, Volodymyr Mnih, Tom Ward, Yotam Doron, Vlad Firoiu, Tim Harley, Iain Dunning, Shane Legg, and Koray Kavukcuoglu. IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures. In Proceedings of the 35th International Conference on Machine Learning (ICML 2018), volume 80 of Proceedings of Machine Learning Research, pages 1407–1416, 2018
which is (12.14) with two separately clipped ratios. The truncation level of the trace only affects speed (variance); sets the fixed point, which is the value of the policy , between and . On-policy, with , V-trace is the ordinary -step (or TD()) target.
Generalized advantage estimation. The advantage estimator GAE of policy-gradient methods, , is by (12.5) exactly the -return error used as an advantage, computed backward over a batch with the recursion , a trace in all but name; it is treated in Section 13.8.3.
True online learning. Van Seijen et al. gave the full proof of Theorem 12.6, showed how to derive true online versions from modified forward views, and found true online TD() and Sarsa() faster than their classic versions in many domains and never worse.Harm van Seijen, A. Rupam Mahmood, Patrick M. Pilarski, Marlos C. Machado, and Richard S. Sutton. True Online Temporal-Difference Learning. Journal of Machine Learning Research, 17(145):1–40, 2016
Every method so far, with or without traces, has learned values and derived its policy from them. Chapter 13 learns the policy directly; value functions and the traces of this chapter return there as the critic of actor–critic methods.
12.12Exercises#
Exercise 12.9. The weights of the -return decay by the factor per step. Find the half-life , the number of steps after which the weight has halved, and evaluate it for and . What is the half-life of the trace in TD()?
Solution
Solve , so .
The half-life and its value at and .const halfLife = await wl`Log[2]/Log[1/\[Lambda]]`
await show`{Simplify[(\[Lambda]^${halfLife}), 0 < \[Lambda] < 1], N[${halfLife} /. \[Lambda] -> {9/10, 99/100}]}`So and steps. The trace decays by per step, so its half-life is , shorter than when : discounting and bootstrapping shorten the memory together.
Exercise 12.10. Take tabular (one-hot) features, . Show that the dutch trace (12.9) updates the visited component as and decays all others by . Conclude that for it is the replacing trace, and that for small it is close to the accumulating trace.
Solution
One dutch-trace step with in three dimensions, then at .const dutchStep = await wl`Function[{z, x}, \[Gamma] \[Lambda] z + (1 - \[Alpha] \[Gamma] \[Lambda] z . x) x]`
await show`{Collect[${dutchStep}[{Subscript[z, 1], Subscript[z, 2], Subscript[z, 3]}, {1, 0, 0}], Subscript[z, 1], Factor], ${dutchStep}[{Subscript[z, 1], Subscript[z, 2], Subscript[z, 3]}, {1, 0, 0}] /. \[Alpha] -> 1}`Only the visited component is affected by the correction: . With it is reset to , the replacing trace; as it becomes , the accumulating trace. The dutch trace interpolates, with the step size deciding how much of the old credit the new update overwrites; in the tabular case with a visited state’s value is overwritten completely, so its old eligibility is rightly forgotten.
Exercise 12.11. Prove that, for fixed action values , the off-policy Expected Sarsa -return of Section 12.7 satisfies , and use it to derive the trace (12.16).
Solution
Subtract from the recursion: , and unroll. A symbolic check for an episode of length with expected next values :
The action-value expansion is exact for fixed values; the episode terminates after the third reward.const gQ = await wl`Function[{t}, Fold[Function[{g, k}, Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] (Subscript[Vbar, k + 1] + \[Lambda] Subscript[\[Rho], k + 1] (g - Subscript[Q, k + 1])), 0]], 0, Range[2, t, -1]]]`
const dQ = await wl`Function[{k}, Subscript[R, k + 1] + If[k + 1 < 3, \[Gamma] Subscript[Vbar, k + 1], 0] - Subscript[Q, k]]`
await show`Table[Simplify[${gQ}[t] - (Subscript[Q, t] + Sum[${dQ}[k] Product[\[Gamma] \[Lambda] Subscript[\[Rho], i], {i, t + 1, k}], {k, t, 2}])], {t, 0, 2}]`The forward update summed over becomes, after exchanging the sums, with , and splitting off the term gives . Unlike the state-value trace (12.15), the ratio does not multiply the new gradient: the action whose value is updated needs no correction, as in one-step Expected Sarsa.
Exercise 12.12. Let , a TD error whose two values come from consecutive weight vectors. Show that the truncated -return (12.7), with -step returns bootstrapping from , satisfies , even though the weights change. Why does this make truncated TD() cheap?
Solution
Write ; only the values , each state valued by the weights in force when it was reached, appear in the -step returns, and the sum telescopes in them. For , :
Truncated -return with three components, against the sum of the “shifted” TD errors .const gTr = await wl`Function[{n}, Sum[\[Gamma]^(j - 1) Subscript[R, j], {j, 1, n}] + \[Gamma]^n Subscript[V, n, n - 1]]`
const dTr = await wl`Function[{i}, Subscript[R, i + 1] + \[Gamma] Subscript[V, i + 1, i] - Subscript[V, i, i - 1]]`
await show`Simplify[(1 - \[Lambda]) (${gTr}[1] + \[Lambda] ${gTr}[2]) + \[Lambda]^2 ${gTr}[3] - (Subscript[V, 0, -1] + Sum[(\[Gamma] \[Lambda])^i ${dTr}[i], {i, 0, 2}])]`In general, , by the argument of Proposition 12.1 applied to the truncated return. Each is computed once, when and arrive, and the target of the update at time is a running geometric sum of them: work per step beyond the value computations, regardless of .