Chapter 11

Off-policy Methods with Approximation

Tabular Q-learning makes off-policy learning look easy: learn about the greedy policy while behaving in any sufficiently exploratory way, and convergence is guaranteed. Chapter 10 carried on-policy control over to function approximation and stopped short of Q-learning. With function approximation the off-policy idea becomes treacherous. The difficulty has two parts. The first, already met in the tabular case, concerns the targets of the updates: returns generated by a behavior policy bb must be corrected, by importance sampling or by expectations, to say something about a target policy π\pi. The second part is new and is the real subject of this chapter. It concerns the distribution of the updates. In Chapter 9 the stability of semi-gradient TD rested, silently, on the fact that states were updated in proportion to how often π\pi visits them. Off-policy, they are updated as often as bb visits them, and that is enough to make the simplest linear TD method diverge. We first locate the mechanism of divergence exactly, in one matrix inequality; then we look at the geometry of linear value functions to see which objectives could be optimized instead; and we end with the two families of stable algorithms, Gradient-TD and Emphatic-TD, that this analysis produces.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

11.1Semi-gradient Off-policy Methods

The first part of the challenge is handled exactly as in the tabular case. Each tabular off-policy algorithm of Chapter 6 and Chapter 7 becomes a semi-gradient algorithm by replacing the update of a table entry with an update of the weight vector along the gradient of the estimate. Most of them use the per-step importance sampling ratio, the one-factor case of (5.2):

ρt≐ρt:t=π(At∣St)b(At∣St). \rho_t \doteq \rho_{t:t} = \frac{\pi(A_t \mid S_t)}{b(A_t \mid S_t)}. (11.1)

Algorithm 11.1 (Semi-gradient off-policy TD(0)). Input: target policy π\pi, behavior policy bb with b(a∣s)>0b(a \mid s) > 0 whenever π(a∣s)>0\pi(a \mid s) > 0; a differentiable v^(s,𝐰)\hat v(s, \mathbf{w}) with v^(terminal,⋅)=0\hat v(\text{terminal}, \cdot) = 0. Parameter: step size α>0\alpha > 0. Initialize: 𝐰∈ℝd\mathbf{w} \in \mathbb{R}^d arbitrarily.

Loop for each step, from the current state SS:

  1. Choose A∼b(⋅∣S)A \sim b(\cdot \mid S); take it, observe R,S′R, S'.
  2. ρ←π(A∣S)/b(A∣S)\rho \leftarrow \pi(A \mid S)/b(A \mid S) and δ←R+γv^(S′,𝐰)−v^(S,𝐰)\delta \leftarrow R + \gamma\hat v(S', \mathbf{w}) - \hat v(S, \mathbf{w}).
  3. 𝐰←𝐰+αρ δ ∇v^(S,𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha\rho\,\delta\,\nabla\hat v(S, \mathbf{w}); S←S′S \leftarrow S'.

In the average-reward setting of Section 10.3, δ\delta is replaced by the differential TD error.

For action values the one-step algorithm needs no ratio at all: semi-gradient Expected Sarsa uses δt=Rt+1+γ∑aπ(a∣St+1)q^(St+1,a,𝐰t)−q^(St,At,𝐰t)\delta_t = R_{t+1} + \gamma\sum_a \pi(a \mid S_{t+1})\hat q(S_{t+1}, a, \mathbf{w}_t) - \hat q(S_t, A_t, \mathbf{w}_t), because the only sampled action, AtA_t, is the one whose value is being learned.With a table this argument is airtight. With function approximation it is less so: every pair (s,a)(s, a) contributes to a shared 𝐰\mathbf{w}, and one could argue that pairs should be weighted by how often π\pi, not bb, would produce them. Sutton and Barto leave the question open; the emphatic weighting of Section 11.8 is one answer. Multi-step versions need products ρt+1⋯ρt+n−1\rho_{t+1}\cdots\rho_{t+n-1}, or, in the tree-backup form, products of target probabilities π(Ai∣Si)\pi(A_i \mid S_i) instead of ratios. All of these correct the targets. None of them touch the distribution of states at which updates are made, and therefore none of them is guaranteed to be stable.

11.1.1The Expected Update

From now on the approximation is linear, v^(s,𝐰)=𝐰⊤𝐱(s)\hat v(s, \mathbf{w}) = \mathbf{w}^\top\mathbf{x}(s), with 𝐱t≐𝐱(St)\mathbf{x}_t \doteq \mathbf{x}(S_t). Collect the feature vectors as the rows of the ∣𝒮∣×d|\mathcal{S}| \times d matrix 𝐗\mathbf{X}, write μ\mu for the distribution of states at which updates are made (here, the stationary distribution of the behavior policy), 𝐃=diag⁡(μ)\mathbf{D} = \operatorname{diag}(\mu), 𝐏π\mathbf{P}_\pi for the state-transition matrix under π\pi, and 𝐫π(s)=𝔼π[Rt+1∣St=s]\mathbf{r}_\pi(s) = \mathbb{E}_\pi[R_{t+1} \mid S_t = s]. Conditioning on St=sS_t = s, the ratio turns the behavior expectation into a target expectation, ∑ab(a∣s)π(a∣s)b(a∣s)(⋯ )=∑aπ(a∣s)(⋯ )\sum_a b(a \mid s)\frac{\pi(a \mid s)}{b(a \mid s)}(\cdots) = \sum_a \pi(a \mid s)(\cdots), so

𝔼b[ρtδt𝐱t]=∑sμ(s) 𝐱(s)(rπ(s)+γ∑s′Pπ(s,s′) 𝐱(s′)⊤𝐰−𝐱(s)⊤𝐰)=𝐛−𝐀𝐰, \begin{aligned} \mathbb{E}_b[\rho_t\delta_t\mathbf{x}_t] &= \sum_s \mu(s)\,\mathbf{x}(s)\Bigl(r_\pi(s) + \gamma\sum_{s'}P_\pi(s, s')\,\mathbf{x}(s')^\top\mathbf{w} - \mathbf{x}(s)^\top\mathbf{w}\Bigr) \\ &= \mathbf{b} - \mathbf{A}\mathbf{w}, \end{aligned} (11.2)𝐀≐𝐗⊤𝐃(𝐈−γ𝐏π)𝐗,𝐛≐𝐗⊤𝐃 𝐫π. \mathbf{A} \doteq \mathbf{X}^\top\mathbf{D}(\mathbf{I} - \gamma\mathbf{P}_\pi)\mathbf{X}, \qquad \mathbf{b} \doteq \mathbf{X}^\top\mathbf{D}\,\mathbf{r}_\pi .

This is the same pair (𝐀,𝐛)(\mathbf{A}, \mathbf{b}) as in the on-policy analysis of Chapter 9, with one change: 𝐃\mathbf{D} now holds the behavior distribution while 𝐏π\mathbf{P}_\pi is still the target dynamics. The expected update 𝐰k+1=𝐰k+α(𝐛−𝐀𝐰k)\mathbf{w}_{k+1} = \mathbf{w}_k + \alpha(\mathbf{b} - \mathbf{A}\mathbf{w}_k) has the TD fixed point 𝐀𝐰TD=𝐛\mathbf{A}\mathbf{w}_{\mathrm{TD}} = \mathbf{b} of (9.5) as its only candidate limit, and it converges for small α\alpha exactly when every eigenvalue of 𝐀\mathbf{A} has positive real part. When does that hold? The following result answers the question for every weighting at once.The column-sum argument below is the one Sutton, Mahmood and White (2016) use for emphatic TD; applied to the on-policy weighting it gives a three-line proof of the positive definiteness that Tsitsiklis and Van Roy (1997) obtained from the norm inequality ∥𝐏π𝐯∥μ≤∥𝐯∥μ\lVert\mathbf{P}_\pi\mathbf{v}\rVert_\mu \le \lVert\mathbf{v}\rVert_\mu.Richard S. Sutton, A. Rupam Mahmood, and Martha White. An Emphatic Approach to the Problem of Off-Policy Temporal-Difference Learning. Journal of Machine Learning Research, 17(73):1–29, 2016; and 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 11.2 (The key-matrix test). Let 𝐏\mathbf{P} be a nonnegative matrix with row sums at most 11, γ∈[0,1)\gamma \in [0, 1), and 𝐦≥0\mathbf{m} \ge 0 a weighting of the states. Put 𝐊≐diag⁡(𝐦)(𝐈−γ𝐏)\mathbf{K} \doteq \operatorname{diag}(\mathbf{m})(\mathbf{I} - \gamma\mathbf{P}). If the column sums of 𝐊\mathbf{K} are nonnegative,

𝐟⊤≐𝟏⊤𝐊=𝐦⊤(𝐈−γ𝐏)≥0, \mathbf{f}^\top \doteq \mathbf{1}^\top\mathbf{K} = \mathbf{m}^\top(\mathbf{I} - \gamma\mathbf{P}) \ge 0, (11.3)

then 𝐯⊤𝐊𝐯≥0\mathbf{v}^\top\mathbf{K}\mathbf{v} \ge 0 for every 𝐯\mathbf{v}, with strict inequality for 𝐯≠0\mathbf{v} \neq 0 when 𝐦>0\mathbf{m} > 0. Consequently, if 𝐗\mathbf{X} has full column rank, every eigenvalue of 𝐗⊤𝐊𝐗\mathbf{X}^\top\mathbf{K}\mathbf{X} has positive real part.

Proof. The diagonal of 𝐊\mathbf{K} is m(s)(1−γPss)≥0m(s)(1 - \gamma P_{ss}) \ge 0 and its off-diagonal entries −γ m(s)Pss′-\gamma\,m(s)P_{ss'} are ≤0\le 0. The symmetric matrix 𝐒=𝐊+𝐊⊤\mathbf{S} = \mathbf{K} + \mathbf{K}^\top inherits both signs, and row ss of 𝐒\mathbf{S} sums to (row sum of 𝐊\mathbf{K}) ++ (column sum of 𝐊\mathbf{K}) =m(s)(1−γ∑s′Pss′)+f(s)≥m(s)(1−γ)+f(s)≥0= m(s)\bigl(1 - \gamma\sum_{s'}P_{ss'}\bigr) + f(s) \ge m(s)(1 - \gamma) + f(s) \ge 0. Because the off-diagonal entries are nonpositive, this row sum equals Sss−∑s′≠s∣Sss′∣S_{ss} - \sum_{s' \neq s}\lvert S_{ss'}\rvert, so every Gershgorin disc of 𝐒\mathbf{S} lies in [0,∞)[0, \infty), and 𝐯⊤𝐊𝐯=12𝐯⊤𝐒𝐯≥0\mathbf{v}^\top\mathbf{K}\mathbf{v} = \frac{1}{2}\mathbf{v}^\top\mathbf{S}\mathbf{v} \ge 0. If 𝐦>0\mathbf{m} > 0 the row sums are strictly positive and 𝐒\mathbf{S} is positive definite. Finally, if 𝐗𝐮=𝟎\mathbf{X}\mathbf{u} = \mathbf{0} only for 𝐮=𝟎\mathbf{u} = \mathbf{0} and 𝐗⊤𝐊𝐗𝐮=λ𝐮\mathbf{X}^\top\mathbf{K}\mathbf{X}\mathbf{u} = \lambda\mathbf{u} with 𝐮∈ℂd\mathbf{u} \in \mathbb{C}^d of unit length, then Re⁡λ=Re⁡((𝐗𝐮)∗𝐊(𝐗𝐮))=12(𝐗𝐮)∗𝐒(𝐗𝐮)>0\operatorname{Re}\lambda = \operatorname{Re}\bigl((\mathbf{X}\mathbf{u})^*\mathbf{K}(\mathbf{X}\mathbf{u})\bigr) = \frac{1}{2}(\mathbf{X}\mathbf{u})^*\mathbf{S}(\mathbf{X}\mathbf{u}) > 0. □

The test singles out the on-policy weighting. If μπ\mu_\pi is stationary for 𝐏π\mathbf{P}_\pi, then μπ⊤(𝐈−γ𝐏π)=(1−γ)μπ⊤≥0\mu_\pi^\top(\mathbf{I} - \gamma\mathbf{P}_\pi) = (1 - \gamma)\mu_\pi^\top \ge 0; in the episodic case, with η\eta the expected number of visits and 𝐡\mathbf{h} the start distribution (η⊤=𝐡⊤+η⊤𝐏π\eta^\top = \mathbf{h}^\top + \eta^\top\mathbf{P}_\pi), the column sums are (1−γ)η⊤+γ𝐡⊤≥0(1 - \gamma)\eta^\top + \gamma\mathbf{h}^\top \ge 0. This is why on-policy linear TD is stable (Theorem 9.4). For a behavior distribution there is no such identity: μ⊤𝐏π\mu^\top\mathbf{P}_\pi can put far more mass on a state than μ\mu does, and then a column sum goes negative. Intuitively, a state that π\pi flows into heavily, but that is rarely updated, is a state whose value is used as a target much more often than it is corrected.

11.2Examples of Off-policy Divergence

11.2.1From ww to 2w2w

Take two states with scalar features 11 and 22, so their estimates are ww and 2w2w, and a single deterministic transition from the first to the second with reward 00. The TD error of that transition is δt=0+γ 2wt−wt=(2γ−1)wt\delta_t = 0 + \gamma\,2w_t - w_t = (2\gamma - 1)w_t. The first state has only one action, so ρt=1\rho_t = 1, and the off-policy update multiplies ww by a constant.The example and the next one are due to Tsitsiklis and Van Roy (1996); van Hasselt et al. (2018) use the same two states to open their study of the deadly triad in deep networks.John N. Tsitsiklis and Benjamin Van Roy. Feature-Based Methods for Large Scale Dynamic Programming. Machine Learning, 22(1–3):59–94, 1996. doi: 10.1007/BF00114724

One semi-gradient update on the transition from the ww-state to the 2w2w-state: the weight is multiplied by 1+α(2γ−1)1 + \alpha(2\gamma - 1).
const upd2w = await wl`Function[{w}, w + \[Alpha] 1 (0 + \[Gamma] 2 w - w) 1]`
await show`Factor[${upd2w}[w]]`
wt+1=w(1−α+2αγ) w_{t+1} = w (1-\alpha +2 \alpha \gamma ) The factor exceeds 11 in absolute value, for every step size in (0,1)(0, 1), exactly when γ>1/2\gamma > 1/2.
await show`Reduce[0 < \[Alpha] < 1 && 0 <= \[Gamma] <= 1 && Abs[1 + \[Alpha] (2 \[Gamma] - 1)] > 1, \[Gamma]]`
0<α<1∧12<γ≤1 0<\alpha <1\land \frac{1}{2}<\gamma \leq 1

Divergence does not depend on how small α\alpha is: a smaller step only slows it down. The essential ingredient is that this one transition is updated over and over, while the transitions out of the 2w2w state are not. Under π\pi they would have to occur, and each one would pull ww back down unless it led to a state of still higher value; a promise made by bootstrapping must eventually be paid by rewards. Off-policy, bb may wander off with an action π\pi never takes, the ratio is zero, and the promise is forgotten. In the language of Theorem 11.2: the 2w2w state receives flow from the first one but is never updated, so its column sum is −γ μ(1)<0-\gamma\,\mu(1) < 0.

11.2.2Tsitsiklis and Van Roy’s Counterexample

Complete the fragment into an episodic chain: from the 2w2w state the process stays put with probability 1−ε1 - \varepsilon and terminates with probability ε\varepsilon, all rewards zero. Now vπ=0v_\pi = 0 is representable (w=0w = 0), and we can ask whether doing more than a semi-gradient step helps. Suppose that at each iteration we fit ww by least squares, with equal weight on the two states, to the expected one-step targets computed from wkw_k:

wk+1=arg min⁡w∈ℝ[(w−2γwk)2+(2w−2(1−ε)γwk)2]. w_{k+1} = \operatorname*{arg\,min}_{w \in \mathbb{R}}\Bigl[\bigl(w - 2\gamma w_k\bigr)^2 + \bigl(2w - 2(1 - \varepsilon)\gamma w_k\bigr)^2\Bigr]. (11.4)The exact least-squares DP step on the two-state chain, solved in closed form.
await show`Factor[x /. First[Solve[D[(x - 2 \[Gamma] w)^2 + (2 x - 2 (1 - \[Epsilon]) \[Gamma] w)^2, x] == 0, x]]]`
wk+1=−25wγ(−3+2ϵ) w_{k+1} = -\frac{2}{5} w \gamma (-3+2 \epsilon )

So wk+1=6−4ε5γ wkw_{k+1} = \frac{6 - 4\varepsilon}{5}\gamma\,w_k, which diverges as soon as γ>5/(6−4ε)\gamma > 5/(6 - 4\varepsilon): even a perfect fit at every step does not save dynamic programming from the wrong weighting. The same threshold appears for expected semi-gradient TD. With weights μ(1),μ(2)\mu(1), \mu(2) the scalar AA of (11.2) is μ(1)⋅1⋅(1−2γ)+μ(2)⋅2⋅(2−2γ(1−ε))\mu(1)\cdot 1\cdot(1 - 2\gamma) + \mu(2)\cdot 2\cdot\bigl(2 - 2\gamma(1 - \varepsilon)\bigr).

The scalar A(μ1,μ2)A(\mu_1, \mu_2) under uniform weighting and under the on-policy weighting, which visits the second state 1/ε1/\varepsilon times per episode.
const aChain = await wl`Function[{m1, m2}, m1 (1 - 2 \[Gamma]) + m2 2 (2 - 2 \[Gamma] (1 - \[Epsilon]))]`
await show`{Simplify[${aChain}[1/2, 1/2]], Simplify[${aChain}[\[Epsilon]/(1 + \[Epsilon]), 1/(1 + \[Epsilon])]]}`
{52+γ(−3+2ϵ),4+2γ(−2+ϵ)+ϵ1+ϵ} \left\{\frac{5}{2}+\gamma (-3+2 \epsilon ),\frac{4+2 \gamma (-2+\epsilon )+\epsilon }{1+\epsilon }\right\}

Under the uniform weighting A<0A < 0 exactly when γ>5/(6−4ε)\gamma > 5/(6 - 4\varepsilon), the same condition; under the on-policy weighting A=(4−4γ+ε(1+2γ))/(1+ε)>0A = \bigl(4 - 4\gamma + \varepsilon(1 + 2\gamma)\bigr)/(1 + \varepsilon) > 0 for all γ∈[0,1]\gamma \in [0, 1] and ε∈(0,1]\varepsilon \in (0, 1]. The model, the features and the algorithm are identical; only the distribution of updates differs.

11.2.3Baird’s Counterexample

Example 11.3 (Baird’s counterexample). Seven states, two actions, all rewards zero, γ=0.99\gamma = 0.99. The dashed action moves to one of the states 1,…,61, \dots, 6 uniformly; the solid action moves to state 77. The behavior policy takes dashed with probability 6/76/7 and solid with probability 1/71/7, so that its next-state distribution, and hence μ\mu, is uniform. The target policy always takes solid, so 𝐏π\mathbf{P}_\pi sends every state to state 77. The features are 𝐱(i)=2𝐞i+𝐞8\mathbf{x}(i) = 2\mathbf{e}_i + \mathbf{e}_8 for i≤6i \le 6 and 𝐱(7)=𝐞7+2𝐞8\mathbf{x}(7) = \mathbf{e}_7 + 2\mathbf{e}_8 in ℝ8\mathbb{R}^8: linearly independent, more weights than states, vπ=0v_\pi = 0 exactly representable. Ratios are ρ=7\rho = 7 after solid and ρ=0\rho = 0 after dashed.Leemon C. Baird. Residual Algorithms: Reinforcement Learning with Function Approximation. In Proceedings of the Twelfth International Conference on Machine Learning (ICML 1995), pages 30–37. Morgan Kaufmann, 1995. doi: 10.1016/B978-1-55860-377-6.50013-X

Everything about this problem looks favorable, yet the weights of semi-gradient TD diverge from almost any start. Because the setting is fully specified we can compute 𝐀\mathbf{A} of (11.2) exactly and look at the spectrum of the expected-update matrix −𝐀-\mathbf{A}.

Build 𝐗\mathbf{X} (7×87 \times 8), 𝐏π\mathbf{P}_\pi and 𝐃=𝐈/7\mathbf{D} = \mathbf{I}/7, and factor the characteristic polynomial of −𝐀-\mathbf{A} symbolically in γ\gamma.
const Xb = await wl`Join[Table[2 UnitVector[8, i] + UnitVector[8, 8], {i, 6}], {UnitVector[8, 7] + 2 UnitVector[8, 8]}]`
const Pb = await wl`ConstantArray[UnitVector[7, 7], 7]`
const Ab = await wl`Transpose[${Xb}] . (IdentityMatrix[7]/7) . (IdentityMatrix[7] - \[Gamma] ${Pb}) . ${Xb}`
await show`Factor[CharacteristicPolynomial[-${Ab}, \[Lambda]]]`
λ(4+7λ)5(26−26γ+105λ−119γλ+49λ2)823543 \frac{\lambda (4+7 \lambda )^5 \left(26-26 \gamma +105 \lambda -119 \gamma \lambda +49 \lambda ^2\right)}{823543}

The factor λ\lambda belongs to the direction 𝐰∝(1,1,1,1,1,1,4,−2)⊤\mathbf{w} \propto (1, 1, 1, 1, 1, 1, 4, -2)^\top that 𝐗\mathbf{X} maps to zero; five eigenvalues equal −4/7-4/7 and are harmless. The remaining pair solves 49λ2+(105−119γ)λ+26(1−γ)=049\lambda^2 + (105 - 119\gamma)\lambda + 26(1 - \gamma) = 0: its product 26(1−γ)/4926(1 - \gamma)/49 is positive, so both roots have positive real part as soon as their sum (119γ−105)/49(119\gamma - 105)/49 is positive.

The exact range of discounts for which the expected semi-gradient TD update on Baird’s problem has an unstable mode.
const bairdQuad = await wl`49 \[Lambda]^2 + (105 - 119 \[Gamma]) \[Lambda] + 26 (1 - \[Gamma])`
await show`Reduce[0 <= \[Gamma] < 1 && Max[Re[\[Lambda] /. Solve[${bairdQuad} == 0, \[Lambda]]]] > 0, \[Gamma]]`
1517<γ<1 \frac{15}{17}<\gamma <1 At the book’s γ=0.99\gamma = 0.99, the distinct eigenvalues of −𝐀-\mathbf{A}: two are positive, so every start with a component along them blows up exponentially.
await show`Union[N[Eigenvalues[-${Ab} /. \[Gamma] -> 99/100], 3]]`
spec⁡(−𝐀)∣γ=0.99={−0.571,0,0.0222,0.239} \operatorname{spec}(-\mathbf{A})\big|_{\gamma = 0.99} = \{-0.571,0,0.0222,0.239\}

Baird’s problem therefore diverges for every γ>15/17\gamma > 15/17 and is stable below.Sutton and Barto state the instability only for γ=0.99\gamma = 0.99. The threshold 15/1715/17 follows from the factorization above: above it the expected update diverges for every step size α>0\alpha > 0; below it, it converges for every α\alpha small enough that the stable modes contract. The divergence has nothing to do with sampling. Replace the sampled update by the synchronous, DP-style sweep 𝐰k+1=𝐰k+α∑s(𝔼π[Rt+1+γv^(St+1,𝐰k)∣St=s]−v^(s,𝐰k))𝐱(s)\mathbf{w}_{k+1} = \mathbf{w}_k + \alpha\sum_s\bigl(\mathbb{E}_\pi[R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}_k) \mid S_t = s] - \hat v(s, \mathbf{w}_k)\bigr)\mathbf{x}(s): this is 𝐰k+7α(𝐛−𝐀𝐰k)\mathbf{w}_k + 7\alpha(\mathbf{b} - \mathbf{A}\mathbf{w}_k), with the same unstable eigenvalues (Figure 11.1, left). Nothing is random and nothing is asynchronous, yet the simplest bootstrapping method with the simplest approximator fails, only because the sweep weights the states uniformly. The key-matrix test shows where: the column sum of state 77 is μ⊤(𝐈−γ𝐏π)𝐞7=17−γ\mu^\top(\mathbf{I} - \gamma\mathbf{P}_\pi)\mathbf{e}_7 = \frac{1}{7} - \gamma, negative for any γ>1/7\gamma > 1/7, because all seven states feed state 77 under π\pi while it receives only a seventh of the updates. (The test is sufficient, not necessary, which is why the actual threshold is the larger 15/1715/17.)

Three panels of weight trajectories on Baird's counterexample over 1000 expected updates: semi-gradient TD with w8 and w1 to w6 growing without bound, TDC with the square root of PBE falling to zero while the weights level off, emphatic TD with the weights converging to a zero value function
Figure 11.1: Deterministic expected-update trajectories on Baird’s counterexample (γ=0.99\gamma = 0.99, uniform μ\mu, 𝐰0=(1,1,1,1,1,1,10,1)⊤\mathbf{w}_0 = (1, 1, 1, 1, 1, 1, 10, 1)^\top), computed by iterating the expectations of the updates, not by sampling; gray w1=⋯=w6w_1 = \dots = w_6, blue w7w_7, orange w8w_8. Left: expected semi-gradient TD, 𝐰←𝐰+α(𝐛−𝐀𝐰)\mathbf{w} \leftarrow \mathbf{w} + \alpha(\mathbf{b} - \mathbf{A}\mathbf{w}) with α=0.01\alpha = 0.01, grows along the unstable modes. Center: expected TDC (α=0.005\alpha = 0.005, β=0.05\beta = 0.05); PBE̅\sqrt{\overline{\mathrm{PBE}}} (dashed) falls to nearly zero, but the weights level off far from a solution with v^=0\hat v = 0 and approach it only along an extremely slow mode (eigenvalue ≈−2×10−5\approx -2 \times 10^{-5} in the spectrum computed below). Right: expected one-step emphatic TD (α=0.005\alpha = 0.005) converges to 𝐰≈(1.69,…,1.69,6.77,−3.38)⊤\mathbf{w} \approx (1.69, \dots, 1.69, 6.77, -3.38)^\top, for which v^=0\hat v = 0 in every state.

Counterexamples of the same kind exist for Q-learning with linear features. Two classic escape routes are partial. Least-squares fitting at each DP step fails, as we saw. Averagers, approximators such as nearest neighbors or kernel smoothing whose output at a state is a convex combination of stored targets, never extrapolate and are stable for any update distribution, because Π𝐁π\Pi\mathbf{B}_\pi is then a sup-norm contraction; but tile coding and neural networks are not averagers.Geoffrey J. Gordon. Stable Function Approximation in Dynamic Programming. In Proceedings of the Twelfth International Conference on Machine Learning (ICML 1995), pages 261–268. Morgan Kaufmann, 1995. doi: 10.1016/B978-1-55860-377-6.50040-2

11.3The Deadly Triad

The examples share three ingredients, and instability appears exactly when all three are present. Sutton calls them the deadly triad: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

  1. Function approximation: a representation, linear or nonlinear, that generalizes across states and has far fewer parameters than there are states.
  2. Bootstrapping: targets that contain current estimates, as in DP and TD, rather than complete returns.
  3. Off-policy training: updates distributed differently from the target policy’s own trajectories. A DP sweep that updates all states uniformly is off-policy in this sense.

Any two are safe. Tabular Q-learning bootstraps off-policy without generalization. On-policy linear TD generalizes and bootstraps, and Theorem 11.2 with 𝐦=μπ\mathbf{m} = \mu_\pi guarantees its stability. Gradient Monte Carlo with importance sampling generalizes off-policy without bootstrapping, and is plain stochastic gradient descent on a fixed regression objective. Neither control nor learning is part of the problem: the instability appears in prediction, and in planning with a known model.

Which element could be given up? Not function approximation: it is what makes large problems tractable at all, and its cheaper substitutes (state aggregation, nonparametric memories, quadratic-cost LSTD) do not scale. Bootstrapping can be dropped, but at a large price in data efficiency, as the nn-step and λ\lambda experiments of Chapter 7 and Chapter 12 show, and in computation, since Monte Carlo must store a trajectory until its return is known. Off-policy learning is dispensable for a single control task (use Sarsa instead of Q-learning), but it is essential for the larger ambition of learning many predictions in parallel from one stream of experience: one behavior cannot coincide with many target policies. The rest of the chapter keeps all three and asks what must change in the algorithm.

11.4The Geometry of Linear Value Functions

Think of a value function as a vector in ℝ∣𝒮∣\mathbb{R}^{|\mathcal{S}|} and of the linear approximator as the subspace {𝐗𝐰}\{\mathbf{X}\mathbf{w}\}. Distances are measured in the weighted norm

∥𝐯∥μ2≐∑sμ(s) v(s)2=𝐯⊤𝐃𝐯, \lVert\mathbf{v}\rVert_\mu^2 \doteq \sum_s \mu(s)\,v(s)^2 = \mathbf{v}^\top\mathbf{D}\mathbf{v},

so that VE̅(𝐰)=∥𝐗𝐰−vπ∥μ2\overline{\mathrm{VE}}(\mathbf{w}) = \lVert\mathbf{X}\mathbf{w} - v_\pi\rVert_\mu^2 as in (9.1). The closest representable function to 𝐯\mathbf{v} is its projection

Π𝐯≐𝐗𝐰∗,𝐰∗=arg min⁡𝐰∥𝐯−𝐗𝐰∥μ2,Π=𝐗(𝐗⊤𝐃𝐗)−1𝐗⊤𝐃, \begin{aligned} \Pi\mathbf{v} &\doteq \mathbf{X}\mathbf{w}^*, \quad \mathbf{w}^* = \operatorname*{arg\,min}_{\mathbf{w}}\lVert\mathbf{v} - \mathbf{X}\mathbf{w}\rVert_\mu^2, \\ \Pi &= \mathbf{X}(\mathbf{X}^\top\mathbf{D}\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{D}, \end{aligned} (11.5)

an orthogonal projection for the inner product ⟨𝐮,𝐯⟩μ=𝐮⊤𝐃𝐯\langle\mathbf{u}, \mathbf{v}\rangle_\mu = \mathbf{u}^\top\mathbf{D}\mathbf{v} (Π2=Π\Pi^2 = \Pi and 𝐃Π=Π⊤𝐃\mathbf{D}\Pi = \Pi^\top\mathbf{D}). Monte Carlo methods converge to Πvπ\Pi v_\pi. Bootstrapping methods aim at the Bellman operator

𝐁π𝐯≐𝐫π+γ𝐏π𝐯, \mathbf{B}_\pi\mathbf{v} \doteq \mathbf{r}_\pi + \gamma\mathbf{P}_\pi\mathbf{v},

whose unique fixed point is vπv_\pi (the Bellman equation (3.9) in vector form). Applied to a representable 𝐯𝐰=𝐗𝐰\mathbf{v}_\mathbf{w} = \mathbf{X}\mathbf{w} it usually leaves the subspace. The gap is the Bellman error vector

δ¯𝐰≐𝐁π𝐯𝐰−𝐯𝐰,δ¯𝐰(s)=𝔼π[Rt+1+γv^(St+1,𝐰)−v^(St,𝐰)∣St=s], \bar\delta_\mathbf{w} \doteq \mathbf{B}_\pi\mathbf{v}_\mathbf{w} - \mathbf{v}_\mathbf{w}, \qquad \bar\delta_\mathbf{w}(s) = \mathbb{E}_\pi\bigl[R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}) - \hat v(S_t, \mathbf{w}) \mid S_t = s\bigr],

the expected TD error at each state. Four objectives can now be distinguished.

  1. VE̅(𝐰)=∥𝐯𝐰−vπ∥μ2\overline{\mathrm{VE}}(\mathbf{w}) = \lVert\mathbf{v}_\mathbf{w} - v_\pi\rVert_\mu^2, minimized by Πvπ\Pi v_\pi.

  2. The mean squared Bellman error BE̅(𝐰)=∥δ¯𝐰∥μ2\overline{\mathrm{BE}}(\mathbf{w}) = \lVert\bar\delta_\mathbf{w}\rVert_\mu^2, zero only at vπv_\pi.

  3. The mean squared projected Bellman error, the size of the part of the Bellman error that the approximator can see:

    PBE̅(𝐰)≐∥Πδ¯𝐰∥μ2=(𝐗⊤𝐃δ¯𝐰)⊤(𝐗⊤𝐃𝐗)−1(𝐗⊤𝐃δ¯𝐰). \overline{\mathrm{PBE}}(\mathbf{w}) \doteq \lVert\Pi\bar\delta_\mathbf{w}\rVert_\mu^2 = \bigl(\mathbf{X}^\top\mathbf{D}\bar\delta_\mathbf{w}\bigr)^\top\bigl(\mathbf{X}^\top\mathbf{D}\mathbf{X}\bigr)^{-1}\bigl(\mathbf{X}^\top\mathbf{D}\bar\delta_\mathbf{w}\bigr). (11.6)
  4. The mean squared TD error TDE̅(𝐰)=∑sμ(s) 𝔼π[δt2∣St=s]=𝔼b[ρtδt2]\overline{\mathrm{TDE}}(\mathbf{w}) = \sum_s\mu(s)\,\mathbb{E}_\pi[\delta_t^2 \mid S_t = s] = \mathbb{E}_b[\rho_t\delta_t^2].

The second form of (11.6) follows from Π⊤𝐃Π=𝐃𝐗(𝐗⊤𝐃𝐗)−1𝐗⊤𝐃\Pi^\top\mathbf{D}\Pi = \mathbf{D}\mathbf{X}(\mathbf{X}^\top\mathbf{D}\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{D}. Its middle factor is 𝐂≐𝐗⊤𝐃𝐗=𝔼[𝐱t𝐱t⊤]\mathbf{C} \doteq \mathbf{X}^\top\mathbf{D}\mathbf{X} = \mathbb{E}[\mathbf{x}_t\mathbf{x}_t^\top], and its outer factor is the expected TD update, 𝐗⊤𝐃δ¯𝐰=𝔼b[ρtδt𝐱t]=𝐛−𝐀𝐰\mathbf{X}^\top\mathbf{D}\bar\delta_\mathbf{w} = \mathbb{E}_b[\rho_t\delta_t\mathbf{x}_t] = \mathbf{b} - \mathbf{A}\mathbf{w}. Hence

PBE̅(𝐰)=(𝐛−𝐀𝐰)⊤𝐂−1(𝐛−𝐀𝐰), \overline{\mathrm{PBE}}(\mathbf{w}) = (\mathbf{b} - \mathbf{A}\mathbf{w})^\top\mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}), (11.7)

which vanishes exactly at the TD fixed point 𝐀𝐰TD=𝐛\mathbf{A}\mathbf{w}_{\mathrm{TD}} = \mathbf{b}, the point where 𝐯=Π𝐁π𝐯\mathbf{v} = \Pi\mathbf{B}_\pi\mathbf{v}. In the on-policy case Π𝐁π\Pi\mathbf{B}_\pi is a γ\gamma-contraction in ∥⋅∥μ\lVert\cdot\rVert_\mu (Π\Pi is non-expansive and ∥𝐏π𝐯∥μ≤∥𝐯∥μ\lVert\mathbf{P}_\pi\mathbf{v}\rVert_\mu \le \lVert\mathbf{v}\rVert_\mu), which is the deeper reason for the bound of Theorem 9.7. Off-policy, ∥𝐏π∥μ\lVert\mathbf{P}_\pi\rVert_\mu may exceed 1/γ1/\gamma; the projected iteration then expands, exactly as in (11.4), and 𝐰TD\mathbf{w}_{\mathrm{TD}}, though it still exists when 𝐀\mathbf{A} is invertible, may be arbitrarily far from Πvπ\Pi v_\pi.

The two forms of the projected Bellman error agree identically: here for three states, two features and arbitrary weights μi\mu_i and Bellman errors δi\delta_i.
const Xg = await wl`{{1, 0}, {1, 1}, {0, 2}}`
await show`Module[{d = DiagonalMatrix[{Subscript[\[Mu], 1], Subscript[\[Mu], 2], Subscript[\[Mu], 3]}], dl = {Subscript[\[Delta], 1], Subscript[\[Delta], 2], Subscript[\[Delta], 3]}, proj}, proj = ${Xg} . Inverse[Transpose[${Xg}] . d . ${Xg}] . Transpose[${Xg}] . d; Simplify[{proj . proj - proj, (proj . dl) . d . (proj . dl) - (Transpose[${Xg}] . d . dl) . Inverse[Transpose[${Xg}] . d . ${Xg}] . (Transpose[${Xg}] . d . dl)}]]`
{(000000000),0} \left\{\left( \begin{array}{ccc} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),0\right\}

11.5Gradient Descent in the Bellman Error

Semi-gradient methods are not gradient methods, and that is where their fragility comes from. True stochastic gradient descent on a fixed objective can only go downhill and is stable under any data distribution. Which objective should we descend? The most immediate candidate is the TDE̅\overline{\mathrm{TDE}}, which is an expectation under bb and can therefore be sampled. Its stochastic gradient step,

𝐰t+1=𝐰t−12α∇(ρtδt2)=𝐰t+αρtδt(∇v^(St,𝐰t)−γ∇v^(St+1,𝐰t)), \begin{aligned} \mathbf{w}_{t+1} &= \mathbf{w}_t - \tfrac{1}{2}\alpha\nabla(\rho_t\delta_t^2) \\ &= \mathbf{w}_t + \alpha\rho_t\delta_t\bigl(\nabla\hat v(S_t, \mathbf{w}_t) - \gamma\nabla\hat v(S_{t+1}, \mathbf{w}_t)\bigr), \end{aligned} (11.8)

is semi-gradient TD plus the missing term −γ∇v^(St+1)-\gamma\nabla\hat v(S_{t+1}). This naive residual-gradient algorithm converges robustly, but to the wrong place.Leemon C. Baird. Residual Algorithms: Reinforcement Learning with Function Approximation. In Proceedings of the Twelfth International Conference on Machine Learning (ICML 1995), pages 30–37. Morgan Kaufmann, 1995. doi: 10.1016/B978-1-55860-377-6.50013-X

Example 11.4 (The A-split example). Episodes start in AA, move with reward 00 to BB or CC with probability 12\frac{1}{2} each, and then terminate with reward 11 from BB and 00 from CC; γ=1\gamma = 1, on-policy, tabular values a,b,ca, b, c. The true values are a=12a = \frac{1}{2}, b=1b = 1, c=0c = 0.Peter Dayan. The Convergence of TD(λ\lambda) for General λ\lambda. Machine Learning, 8(3–4):341–362, 1992. doi: 10.1007/BF00992701

Minimize the mean squared TD error over the two transitions of an episode: the minimizer is not the true value function.
const tdeSplit = await wl`Function[{a, b, c}, (1/2) ((1/2) (b - a)^2 + (1/2) (c - a)^2) + (1/2) ((1/2) (1 - b)^2 + (1/2) (0 - c)^2)]`
await show`Minimize[${tdeSplit}[a, b, c], {a, b, c}]`
{116,{a→12,b→34,c→14}} \left\{\frac{1}{16},\left\{a\to \frac{1}{2},b\to \frac{3}{4},c\to \frac{1}{4}\right\}\right\}

At the true values the TD errors are ±12\pm\frac{1}{2} on the first step and 00 on the second, a TDE̅\overline{\mathrm{TDE}} of 18\frac{1}{8}; the values b=34b = \frac{3}{4}, c=14c = \frac{1}{4} spread the error as ±14\pm\frac{1}{4} over both steps and achieve 116\frac{1}{16}. Penalizing every TD error produces temporal smoothing, not prediction.

The natural repair is to square the expected TD error, that is, to descend the BE̅\overline{\mathrm{BE}}. Conditioning on StS_t,

−12∇(𝔼b[ρtδt]2)=(𝔼b[ρt(Rt+1+γv^(St+1,𝐰))]−v^(St,𝐰))×(∇v^(St,𝐰)−γ 𝔼b[ρt∇v^(St+1,𝐰)]), \begin{aligned} -\tfrac{1}{2}\nabla\bigl(\mathbb{E}_b[\rho_t\delta_t]^2\bigr) = {} &\Bigl(\mathbb{E}_b\bigl[\rho_t(R_{t+1} + \gamma\hat v(S_{t+1}, \mathbf{w}))\bigr] - \hat v(S_t, \mathbf{w})\Bigr) \\ &\times \Bigl(\nabla\hat v(S_t, \mathbf{w}) - \gamma\,\mathbb{E}_b\bigl[\rho_t\nabla\hat v(S_{t+1}, \mathbf{w})\bigr]\Bigr), \end{aligned}

a product of two expectations over the same next state. One sample of St+1S_{t+1} gives an unbiased estimate of each factor but a biased estimate of the product (it estimates 𝔼[XY]\mathbb{E}[XY], not 𝔼[X]𝔼[Y]\mathbb{E}[X]\mathbb{E}[Y]); this is the double-sampling problem. The residual-gradient algorithm is unbiased only if the environment is deterministic, or if a simulator can be reset to draw a second, independent St+1S_{t+1} from the same StS_t. Then it converges, for linear and nonlinear approximators, to a minimum of the BE̅\overline{\mathrm{BE}}. But three objections remain. It is slow. It still finds bad values: in the A-presplit variant, where two start states A1A_1 (always going to BB) and A2A_2 (always going to CC) share one feature, all transitions are deterministic, so BE̅=TDE̅\overline{\mathrm{BE}} = \overline{\mathrm{TDE}}, and the BE̅\overline{\mathrm{BE}} minimizer is again b=34b = \frac{3}{4}, c=14c = \frac{1}{4} while semi-gradient TD finds 11 and 00. And, most seriously, the next section shows that the BE̅\overline{\mathrm{BE}} cannot be learned from data at all.

11.6The Bellman Error Is Not Learnable

Call a quantity learnable if it is a function of the distribution of the observable data, the sequence of feature vectors, actions and rewards, so that it could be estimated, with enough experience, without access to the underlying states. This is learnability in principle, not in the PAC sense of efficiency.

The VE̅\overline{\mathrm{VE}} is not learnable. A single state emitting rewards 00 and 22 at random, and two states with deterministic rewards 00 and 22 that switch at random, produce indistinguishable data streams when both look alike to the features; with γ=0\gamma = 0 and w=1w = 1 the first has VE̅=0\overline{\mathrm{VE}} = 0 and the second VE̅=1\overline{\mathrm{VE}} = 1. Yet its minimizer is learnable, because it coincides with that of the mean squared return error, which is an expectation of observables.

Proposition 11.5 (Return error and value error). On-policy, RE̅(𝐰)≐𝔼[(Gt−v^(St,𝐰))2]=VE̅(𝐰)+𝔼[(Gt−vπ(St))2]\overline{\mathrm{RE}}(\mathbf{w}) \doteq \mathbb{E}\bigl[(G_t - \hat v(S_t, \mathbf{w}))^2\bigr] = \overline{\mathrm{VE}}(\mathbf{w}) + \mathbb{E}\bigl[(G_t - v_\pi(S_t))^2\bigr], so the two objectives have the same minimizers.

Proof. Write Gt−v^(St,𝐰)=(Gt−vπ(St))+(vπ(St)−v^(St,𝐰))G_t - \hat v(S_t, \mathbf{w}) = \bigl(G_t - v_\pi(S_t)\bigr) + \bigl(v_\pi(S_t) - \hat v(S_t, \mathbf{w})\bigr) and square. The cross term has conditional expectation 2(vπ(St)−v^(St,𝐰)) 𝔼[Gt−vπ(St)∣St]=02\bigl(v_\pi(S_t) - \hat v(S_t, \mathbf{w})\bigr)\,\mathbb{E}[G_t - v_\pi(S_t) \mid S_t] = 0, because vπ(s)=𝔼[Gt∣St=s]v_\pi(s) = \mathbb{E}[G_t \mid S_t = s]. Taking expectations over St∼μS_t \sim \mu leaves VE̅\overline{\mathrm{VE}} plus a term free of 𝐰\mathbf{w}. □

The Bellman error has no such escape: two processes can generate the same data and still have different BE̅\overline{\mathrm{BE}} minimizers.

Example 11.6 (Two MRPs with the same data and different BE minimizers). First MRP: states A,BA, B with distinct features. AA goes to BB with reward 00; from BB, with probability 12\frac{1}{2} each, stay in BB with reward −1-1 or go to AA with reward 11. Second MRP: states A,B,B′A, B, B', where BB and B′B' share a feature. AA goes to BB or B′B' (probability 12\frac{1}{2} each, reward 00); BB goes to BB or B′B' (probability 12\frac{1}{2} each, reward −1-1); B′B' goes to AA with reward 11. In both, the observer sees an AA with reward 00, then kk apparent BB’s, each followed by −1-1 except the last, followed by 11, with Pr⁡{k}=2−k\Pr\{k\} = 2^{-k}; the weights are μ=(13,23)\mu = (\frac{1}{3}, \frac{2}{3}) in the first and (13,13,13)(\frac{1}{3}, \frac{1}{3}, \frac{1}{3}) in the second. Weights are 𝐰=(wA,wB)\mathbf{w} = (w_A, w_B).

In the first MRP vπ=0v_\pi = 0 is representable and minimizes the BE̅\overline{\mathrm{BE}} for every γ\gamma. In the second, with 𝐰=(a,b)\mathbf{w} = (a, b), the Bellman errors are γb−a\gamma b - a at AA, −1+(γ−1)b-1 + (\gamma - 1)b at BB and 1+γa−b1 + \gamma a - b at B′B'.

Minimize the BE̅\overline{\mathrm{BE}} of the second MRP in closed form.
const be2 = await wl`Function[{a, b}, ((\[Gamma] b - a)^2 + (-1 + (\[Gamma] - 1) b)^2 + (1 + \[Gamma] a - b)^2)/3]`
const wBE = await show`Simplify[{a, b} /. First[Solve[{D[${be2}[a, b], a] == 0, D[${be2}[a, b], b] == 0}, {a, b}]]]`
𝐰BE̅={−γ1+γ+γ2,γ2(1+γ+γ2)} \mathbf{w}_{\overline{\mathrm{BE}}} = \left\{-\frac{\gamma }{1+\gamma +\gamma ^2},\frac{\gamma }{2 \left(1+\gamma +\gamma ^2\right)}\right\} Its limit as γ→1\gamma \to 1; at γ=1\gamma = 1 itself only the difference a−b=−12a - b = -\frac{1}{2} is determined.
await show`Limit[${wBE}, \[Gamma] -> 1]`
lim⁡γ→1𝐰BE̅={−13,16} \lim_{\gamma \to 1}\mathbf{w}_{\overline{\mathrm{BE}}} = \left\{-\frac{1}{3},\frac{1}{6}\right\} Why the limit is not the minimizer at γ=1\gamma = 1: there the BE̅\overline{\mathrm{BE}} takes its minimum 12\frac{1}{2} on the whole line a−b=−12a - b = -\frac{1}{2}, while the determinant of its Hessian, positive for every γ<1\gamma < 1, vanishes at γ=1\gamma = 1.
await show`{Simplify[${be2}[b - 1/2, b] /. \[Gamma] -> 1], Factor[Det[D[${be2}[a, b], {{a, b}, 2}]]]}`
(BE̅∣γ=1, a=b−1/2, det⁡∇2BE̅)={12,89(−1+γ)2(1+γ+γ2)} \Bigl(\overline{\mathrm{BE}}\big|_{\gamma = 1,\, a = b - 1/2},\ \det\nabla^2\overline{\mathrm{BE}}\Bigr) = \left\{\frac{1}{2},\frac{8}{9} (-1+\gamma )^2 \left(1+\gamma +\gamma ^2\right)\right\}

So the BE̅\overline{\mathrm{BE}} minimizer depends on structure that the data do not reveal.Sutton and Barto give the limit as (−12,0)⊤(-\frac{1}{2}, 0)^\top; the computation above gives (−13,16)⊤(-\frac{1}{3}, \frac{1}{6})^\top. For every γ<1\gamma < 1 the BE̅\overline{\mathrm{BE}} is a strictly convex quadratic, so its minimizer is unique, and these minimizers converge to (−13,16)⊤(-\frac{1}{3}, \frac{1}{6})^\top. At γ=1\gamma = 1 the objective sees only wA−wBw_A - w_B and is minimized on the whole line wA−wB=−12w_A - w_B = -\frac{1}{2}, which contains both points; (−12,0)⊤(-\frac{1}{2}, 0)^\top is the minimizer when wBw_B is held at 00, in line with the book’s remark that the value of BB is approximately zero, but it is not the limit. The value of AA is pulled negative because the deterministic reward 11 on entering AA from B′B' says that B′B' should be worth one more than AA, and B′B' must share its value with BB. Contrast the projected Bellman error: 𝐀\mathbf{A}, 𝐛\mathbf{b} and 𝐂\mathbf{C} in (11.7) are expectations of observables (ρt𝐱t(𝐱t−γ𝐱t+1)⊤\rho_t\mathbf{x}_t(\mathbf{x}_t - \gamma\mathbf{x}_{t+1})^\top, ρtRt+1𝐱t\rho_tR_{t+1}\mathbf{x}_t, 𝐱t𝐱t⊤\mathbf{x}_t\mathbf{x}_t^\top), so the PBE̅\overline{\mathrm{PBE}} and its minimizer are learnable, and so is the TDE̅\overline{\mathrm{TDE}}.

The matrix 𝐀\mathbf{A} computed from each MRP’s hidden structure (rows: features, transitions, rewards, μ\mu): they coincide, as they must, being expectations of observables.
const aOf = await wl`Function[{X, P, mu}, Simplify[Transpose[X] . DiagonalMatrix[mu] . (IdentityMatrix[Length[mu]] - \[Gamma] P) . X]]`
await show`{${aOf}[IdentityMatrix[2], {{0, 1}, {1/2, 1/2}}, {1/3, 2/3}], ${aOf}[{{1, 0}, {0, 1}, {0, 1}}, {{0, 1/2, 1/2}, {0, 1/2, 1/2}, {1, 0, 0}}, {1/3, 1/3, 1/3}]}`
{(13−γ3−γ32−γ3),(13−γ3−γ32−γ3)} \left\{\left( \begin{array}{cc} \frac{1}{3} & -\frac{\gamma }{3} \\ -\frac{\gamma }{3} & \frac{2-\gamma }{3} \\ \end{array} \right),\left( \begin{array}{cc} \frac{1}{3} & -\frac{\gamma }{3} \\ -\frac{\gamma }{3} & \frac{2-\gamma }{3} \\ \end{array} \right)\right\}

Both MRPs also give 𝐛=𝟎\mathbf{b} = \mathbf{0} (the rewards −1-1 and 11 out of BB average out), so the TD fixed point is 𝐰=𝟎\mathbf{w} = \mathbf{0} in both. The BE̅\overline{\mathrm{BE}} can be minimized only with access to the underlying states, which is exactly what the double sample of the residual-gradient algorithm provides. This, more than its slowness, is why attention turns to the PBE̅\overline{\mathrm{PBE}}.

11.7Gradient-TD Methods

Gradient-TD methods are stochastic gradient methods for the PBE̅\overline{\mathrm{PBE}} with O(d)O(d) cost per step.Richard S. Sutton, Hamid Reza Maei, Doina Precup, Shalabh Bhatnagar, David Silver, Csaba Szepesvári, and Eric Wiewiora. Fast Gradient-Descent Methods for Temporal-Difference Learning with Linear Function Approximation. In Proceedings of the 26th International Conference on Machine Learning (ICML 2009), pages 993–1000, 2009. doi: 10.1145/1553374.1553501 From (11.7),

∇PBE̅(𝐰)=−2𝐀⊤𝐂−1(𝐛−𝐀𝐰)=−2 𝔼[ρt(𝐱t−γ𝐱t+1)𝐱t⊤] 𝔼[𝐱t𝐱t⊤]−1 𝔼[ρtδt𝐱t]. \begin{aligned} \nabla\overline{\mathrm{PBE}}(\mathbf{w}) &= -2\mathbf{A}^\top\mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}) \\ &= -2\,\mathbb{E}\bigl[\rho_t(\mathbf{x}_t - \gamma\mathbf{x}_{t+1})\mathbf{x}_t^\top\bigr]\,\mathbb{E}\bigl[\mathbf{x}_t\mathbf{x}_t^\top\bigr]^{-1}\,\mathbb{E}[\rho_t\delta_t\mathbf{x}_t]. \end{aligned} (11.9)

The first and last factors both involve 𝐱t+1\mathbf{x}_{t+1}, so multiplying single samples would be biased again; storing the d×dd \times d matrices would cost O(d2)O(d^2). The way out is to learn the product of the last two factors as a second weight vector,

𝐯≈𝐂−1(𝐛−𝐀𝐰)=𝔼[𝐱t𝐱t⊤]−1𝔼[ρtδt𝐱t], \mathbf{v} \approx \mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}) = \mathbb{E}[\mathbf{x}_t\mathbf{x}_t^\top]^{-1}\mathbb{E}[\rho_t\delta_t\mathbf{x}_t],

which is the least-squares solution for predicting ρtδt\rho_t\delta_t from 𝐱t\mathbf{x}_t, and is found by the LMS rule 𝐯t+1=𝐯t+βρt(δt−𝐯t⊤𝐱t)𝐱t\mathbf{v}_{t+1} = \mathbf{v}_t + \beta\rho_t(\delta_t - \mathbf{v}_t^\top\mathbf{x}_t)\mathbf{x}_t. Substituting 𝐯t\mathbf{v}_t into −12∇PBE̅=𝐀⊤𝐯-\frac{1}{2}\nabla\overline{\mathrm{PBE}} = \mathbf{A}^\top\mathbf{v} and sampling gives GTD2. Alternatively, first use 𝔼[ρt𝐱t𝐱t⊤]=𝐂\mathbb{E}[\rho_t\mathbf{x}_t\mathbf{x}_t^\top] = \mathbf{C} (because 𝔼b[ρt∣St]=1\mathbb{E}_b[\rho_t \mid S_t] = 1) to write 𝐀⊤𝐂−1(𝐛−𝐀𝐰)=(𝐛−𝐀𝐰)−γ 𝔼[ρt𝐱t+1𝐱t⊤]𝐂−1(𝐛−𝐀𝐰)\mathbf{A}^\top\mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}) = (\mathbf{b} - \mathbf{A}\mathbf{w}) - \gamma\,\mathbb{E}[\rho_t\mathbf{x}_{t+1}\mathbf{x}_t^\top]\mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}); sampling this form gives semi-gradient TD plus a correction, TDC (TD with gradient correction, also called GTD(0)).

Algorithm 11.7 (TDC and GTD2 (linear, off-policy)). Input: π\pi, bb, features 𝐱(⋅)\mathbf{x}(\cdot) with 𝐱(terminal)=𝟎\mathbf{x}(\text{terminal}) = \mathbf{0}. Parameters: step sizes α,β>0\alpha, \beta > 0, typically β≫α\beta \gg \alpha. Initialize: 𝐰\mathbf{w} arbitrary, 𝐯=𝟎\mathbf{v} = \mathbf{0}.

Loop for each step, from SS with features 𝐱=𝐱(S)\mathbf{x} = \mathbf{x}(S):

  1. Choose A∼b(⋅∣S)A \sim b(\cdot \mid S), observe R,S′R, S'; 𝐱′←𝐱(S′)\mathbf{x}' \leftarrow \mathbf{x}(S'), ρ←π(A∣S)/b(A∣S)\rho \leftarrow \pi(A \mid S)/b(A \mid S).
  2. δ←R+γ𝐰⊤𝐱′−𝐰⊤𝐱\delta \leftarrow R + \gamma\mathbf{w}^\top\mathbf{x}' - \mathbf{w}^\top\mathbf{x} and u←𝐯⊤𝐱u \leftarrow \mathbf{v}^\top\mathbf{x} (computing uu first keeps the cost O(d)O(d)).
  3. TDC: 𝐰←𝐰+αρ(δ 𝐱−γ u 𝐱′)\mathbf{w} \leftarrow \mathbf{w} + \alpha\rho\bigl(\delta\,\mathbf{x} - \gamma\,u\,\mathbf{x}'\bigr). GTD2 instead: 𝐰←𝐰+αρ u (𝐱−γ𝐱′)\mathbf{w} \leftarrow \mathbf{w} + \alpha\rho\,u\,(\mathbf{x} - \gamma\mathbf{x}').
  4. 𝐯←𝐯+βρ (δ−u) 𝐱\mathbf{v} \leftarrow \mathbf{v} + \beta\rho\,(\delta - u)\,\mathbf{x}; S←S′S \leftarrow S'.

The two learning processes form a cascade: the secondary vector 𝐯\mathbf{v} tracks a quantity defined by 𝐰\mathbf{w}, while 𝐰\mathbf{w} relies on 𝐯\mathbf{v} being accurate. Convergence proofs use two time scales, βt→0\beta_t \to 0 and αt/βt→0\alpha_t/\beta_t \to 0, so that 𝐯\mathbf{v} is effectively always at its target. For GTD2, however, stability of the expected dynamics holds for any ratio of step sizes, and the proof is short. Write β=ηα\beta = \eta\alpha; the expected update of (𝐰,𝐯)(\mathbf{w}, \mathbf{v}) is α\alpha times

(𝐰˙𝐯˙)=𝐆(𝐰𝐯)+const,𝐆GTD2=(𝟎𝐀⊤−η𝐀−η𝐂),𝐆TDC=(−𝐀−(𝐂−𝐀⊤)−η𝐀−η𝐂). \begin{aligned} \begin{pmatrix}\dot{\mathbf{w}} \\ \dot{\mathbf{v}}\end{pmatrix} &= \mathbf{G}\begin{pmatrix}\mathbf{w} \\ \mathbf{v}\end{pmatrix} + \text{const}, \\ \mathbf{G}_{\mathrm{GTD2}} &= \begin{pmatrix}\mathbf{0} & \mathbf{A}^\top \\ -\eta\mathbf{A} & -\eta\mathbf{C}\end{pmatrix}, \\ \mathbf{G}_{\mathrm{TDC}} &= \begin{pmatrix}-\mathbf{A} & -(\mathbf{C} - \mathbf{A}^\top) \\ -\eta\mathbf{A} & -\eta\mathbf{C}\end{pmatrix}. \end{aligned} (11.10)

Theorem 11.8 (Stability of expected GTD2). If 𝐀\mathbf{A} is nonsingular and 𝐂\mathbf{C} is positive definite, every eigenvalue of 𝐆GTD2\mathbf{G}_{\mathrm{GTD2}} has negative real part, for every η>0\eta > 0 and whatever the sign structure of 𝐀\mathbf{A}. The unique equilibrium is 𝐰=𝐀−1𝐛=𝐰TD\mathbf{w} = \mathbf{A}^{-1}\mathbf{b} = \mathbf{w}_{\mathrm{TD}}, 𝐯=𝟎\mathbf{v} = \mathbf{0}.

Proof. Let 𝐆(𝐮,𝐲)=λ(𝐮,𝐲)\mathbf{G}(\mathbf{u}, \mathbf{y}) = \lambda(\mathbf{u}, \mathbf{y}) with (𝐮,𝐲)∈ℂ2d(\mathbf{u}, \mathbf{y}) \in \mathbb{C}^{2d} nonzero, that is, 𝐀⊤𝐲=λ𝐮\mathbf{A}^\top\mathbf{y} = \lambda\mathbf{u} and −η𝐀𝐮−η𝐂𝐲=λ𝐲-\eta\mathbf{A}\mathbf{u} - \eta\mathbf{C}\mathbf{y} = \lambda\mathbf{y}. If λ=0\lambda = 0, the first equation gives 𝐲=𝟎\mathbf{y} = \mathbf{0} and then the second gives 𝐮=𝟎\mathbf{u} = \mathbf{0}, a contradiction. Multiply the second equation on the left by 𝐲∗\mathbf{y}^* and use 𝐲∗𝐀𝐮=(𝐀⊤𝐲)∗𝐮=λ¯∥𝐮∥2\mathbf{y}^*\mathbf{A}\mathbf{u} = (\mathbf{A}^\top\mathbf{y})^*\mathbf{u} = \bar\lambda\lVert\mathbf{u}\rVert^2:

−ηλ¯∥𝐮∥2−η 𝐲∗𝐂𝐲=λ∥𝐲∥2, -\eta\bar\lambda\lVert\mathbf{u}\rVert^2 - \eta\,\mathbf{y}^*\mathbf{C}\mathbf{y} = \lambda\lVert\mathbf{y}\rVert^2,

and taking real parts,

Re⁡λ (η∥𝐮∥2+∥𝐲∥2)=−η 𝐲∗𝐂𝐲. \operatorname{Re}\lambda\,\bigl(\eta\lVert\mathbf{u}\rVert^2 + \lVert\mathbf{y}\rVert^2\bigr) = -\eta\,\mathbf{y}^*\mathbf{C}\mathbf{y}.

If 𝐲=𝟎\mathbf{y} = \mathbf{0} then λ𝐮=𝟎\lambda\mathbf{u} = \mathbf{0} with λ≠0\lambda \neq 0, impossible; so 𝐲≠𝟎\mathbf{y} \neq \mathbf{0}, the right side is negative, and Re⁡λ<0\operatorname{Re}\lambda < 0. At an equilibrium 𝐀⊤𝐯=𝟎\mathbf{A}^\top\mathbf{v} = \mathbf{0} forces 𝐯=𝟎\mathbf{v} = \mathbf{0} and then 𝐀𝐰=𝐛\mathbf{A}\mathbf{w} = \mathbf{b}. □

The contrast with semi-gradient TD is the whole point: there, stability needed 𝐀\mathbf{A} itself to be positive (stable), which the off-policy distribution can destroy; here 𝐀\mathbf{A} enters only through the block structure, and only its invertibility matters. TDC is usually the faster of the two in practice, but its stability needs the secondary process to be fast enough. Both facts can be seen on the scalar chain of Section 11.2.2, with 𝐀=a\mathbf{A} = a and 𝐂=c>0\mathbf{C} = c > 0 scalars.

The eigenvalues of the scalar GTD2 matrix: for a≠0a \neq 0, c,η>0c, \eta > 0 their real parts are negative whatever the sign of aa.
const gtd2M = await wl`{{0, a}, {-\[Eta] a, -\[Eta] c}}`
await show`Eigenvalues[${gtd2M}]`
{12(−cη−−4a2η+c2η2),12(−cη+−4a2η+c2η2)} \left\{\frac{1}{2} \left(-c \eta -\sqrt{-4 a^2 \eta +c^2 \eta ^2}\right),\frac{1}{2} \left(-c \eta +\sqrt{-4 a^2 \eta +c^2 \eta ^2}\right)\right\} Trace and determinant of the scalar TDC matrix: it is stable if and only if a+ηc>0a + \eta c > 0, so a divergent a<0a < 0 requires η>−a/c\eta > -a/c.
const tdcM = await wl`{{-a, -(c - a)}, {-\[Eta] a, -\[Eta] c}}`
await show`Simplify[{Tr[${tdcM}], Det[${tdcM}]}]`
{−a−cη,a2η} \left\{-a-c \eta ,a^2 \eta \right\}

On Baird’s counterexample 𝐂=𝐗⊤𝐃𝐗\mathbf{C} = \mathbf{X}^\top\mathbf{D}\mathbf{X} and 𝐀\mathbf{A} are singular (rank 77 in ℝ8\mathbb{R}^8), so one direction is neutral and the theorem applies on the complement; the remaining eigenvalues of 𝐆TDC\mathbf{G}_{\mathrm{TDC}} have negative real parts.

The spectrum of the expected TDC dynamics on Baird’s problem, γ=0.99\gamma = 0.99, η=β/α=10\eta = \beta/\alpha = 10, summarized: the largest real part, the number of zero eigenvalues (the two null directions of 𝐗\mathbf{X}, one for 𝐰\mathbf{w} and one for 𝐯\mathbf{v}), and the largest real part among the others.
const Cb = await wl`Transpose[${Xb}] . (IdentityMatrix[7]/7) . ${Xb}`
const tdcBaird = await wl`ArrayFlatten[{{-${Ab}, -(${Cb} - Transpose[${Ab}])}, {-\[Eta] ${Ab}, -\[Eta] ${Cb}}}] /. {\[Gamma] -> 99/100, \[Eta] -> 10}`
await show`With[{ev = Chop[N[Eigenvalues[${tdcBaird}]], 10^-10]}, {Max[Re[ev]], Count[ev, 0], Max[Re[DeleteCases[ev, 0]]]}]`
(max⁡Re⁡λ, #{λ=0}, max⁡λ≠0Re⁡λ)={0,2,−0.0000237465} \Bigl(\max\operatorname{Re}\lambda,\ \#\{\lambda = 0\},\ \max_{\lambda \ne 0}\operatorname{Re}\lambda\Bigr) = \{0,2,-0.0000237465\}

As Figure 11.1 shows, the PBE̅\overline{\mathrm{PBE}} goes to zero quickly but the weights approach a good value function very slowly: on Baird’s problem the TD solution set is a line, and the PBE̅\overline{\mathrm{PBE}} is nearly flat along the way to it. Gradient-TD methods are today the best understood stable off-policy learners. They extend to action values and control (GQ), to eligibility traces (GTD(λ\lambda), GQ(λ\lambda), Section 12.9), and to nonlinear approximators, and hybrid versions (HTD) reduce to ordinary TD when b=πb = \pi.

11.8Emphatic-TD Methods

The second strategy keeps the cheap semi-gradient update and repairs the distribution: reweight the updates so that their effective weighting 𝐦\mathbf{m} passes the test of Theorem 11.2. Which weighting? Think of discounting as pseudo-termination: with probability 1−γ1 - \gamma per step the process “ends” and immediately restarts from where it is. Any distribution obtained by starting somewhere and then following π\pi until (pseudo-)termination is an on-policy distribution in the sense required by the test, and the starts are ours to choose. If every state visited by bb is treated as a start with interest It=i(St)≥0I_t = i(S_t) \ge 0, and the subsequent “on-policy continuation” is tracked by importance sampling, the weight that state StS_t should receive is the followon traceRichard S. Sutton, A. Rupam Mahmood, and Martha White. An Emphatic Approach to the Problem of Off-Policy Temporal-Difference Learning. Journal of Machine Learning Research, 17(73):1–29, 2016

Ft=γρt−1Ft−1+It,F−1=0. F_t = \gamma\rho_{t-1}F_{t-1} + I_t, \qquad F_{-1} = 0. (11.11)

Algorithm 11.9 (One-step emphatic TD (linear)). As Algorithm 11.1, with interest i(s)≥0i(s) \ge 0 and a scalar M←0M \leftarrow 0 at the start. At each step, after observing R,S′R, S' and computing ρ\rho and δ\delta:

  1. M←M+i(S)M \leftarrow M + i(S) (the emphasis; here it equals the followon trace).
  2. 𝐰←𝐰+αMρ δ 𝐱(S)\mathbf{w} \leftarrow \mathbf{w} + \alpha M\rho\,\delta\,\mathbf{x}(S).
  3. M←γρMM \leftarrow \gamma\rho M; S←S′S \leftarrow S'.

Proposition 11.10 (Emphatic TD passes the key-matrix test). Let f(s)=lim⁡tμ(s) 𝔼b[Ft∣St=s]f(s) = \lim_t \mu(s)\,\mathbb{E}_b[F_t \mid S_t = s]. Then 𝐟⊤=(μ∘𝐢)⊤(𝐈−γ𝐏π)−1\mathbf{f}^\top = (\mu \circ \mathbf{i})^\top(\mathbf{I} - \gamma\mathbf{P}_\pi)^{-1}, the expected update of Algorithm 11.9 is 𝐗⊤diag⁡(𝐟)(𝐫π+γ𝐏π𝐗𝐰−𝐗𝐰)\mathbf{X}^\top\operatorname{diag}(\mathbf{f})(\mathbf{r}_\pi + \gamma\mathbf{P}_\pi\mathbf{X}\mathbf{w} - \mathbf{X}\mathbf{w}), and 𝐦=𝐟\mathbf{m} = \mathbf{f} satisfies (11.3). In particular, if 𝐢>0\mathbf{i} > 0 and 𝐗\mathbf{X} has full column rank, expected emphatic TD is stable.

Proof. Ft−1F_{t-1} depends only on the past, so given St−1=sS_{t-1} = s it is independent of At−1A_{t-1}, and 𝔼b[ρt−1𝟙{St=s′}∣St−1=s]=∑aπ(a∣s) p(s′∣s,a)=Pπ(s,s′)\mathbb{E}_b[\rho_{t-1}\mathbb{1}\{S_t = s'\} \mid S_{t-1} = s] = \sum_a \pi(a \mid s)\,p(s' \mid s, a) = P_\pi(s, s'). Taking expectations in (11.11) at stationarity, f(s′)=μ(s′)i(s′)+γ∑sf(s)Pπ(s,s′)f(s') = \mu(s')i(s') + \gamma\sum_s f(s)P_\pi(s, s'), that is, 𝐟⊤(𝐈−γ𝐏π)=(μ∘𝐢)⊤\mathbf{f}^\top(\mathbf{I} - \gamma\mathbf{P}_\pi) = (\mu \circ \mathbf{i})^\top. The same independence gives 𝔼b[Ftρtδt𝐱t∣St=s]=𝔼b[Ft∣St=s] 𝐱(s)δ¯𝐰(s)\mathbb{E}_b[F_t\rho_t\delta_t\mathbf{x}_t \mid S_t = s] = \mathbb{E}_b[F_t \mid S_t = s]\,\mathbf{x}(s)\bar\delta_\mathbf{w}(s), hence the expected update. Its column sums are μ∘𝐢≥0\mu \circ \mathbf{i} \ge 0, and 𝐟≥μ∘𝐢>0\mathbf{f} \ge \mu \circ \mathbf{i} > 0 (Neumann series of a nonnegative matrix), so Theorem 11.2 applies. □

The emphatic weighting on Baird’s problem, with uniform interest: state 77, into which π\pi funnels everything, gets weight (1+6γ)/(7−7γ)(1 + 6\gamma)/(7 - 7\gamma).
const fB = await show`Simplify[ConstantArray[1/7, 7] . Inverse[IdentityMatrix[7] - \[Gamma] ${Pb}]]`
{17,17,17,17,17,17,1+6γ7−7γ} \left\{\frac{1}{7},\frac{1}{7},\frac{1}{7},\frac{1}{7},\frac{1}{7},\frac{1}{7},\frac{1+6 \gamma }{7-7 \gamma }\right\} Column sums under the emphatic weighting are μ∘𝐢=17\mu \circ \mathbf{i} = \frac{1}{7}; under the behavior weighting the seventh is 17−γ\frac{1}{7} - \gamma.
await show`Simplify[{${fB} . (IdentityMatrix[7] - \[Gamma] ${Pb}), ConstantArray[1/7, 7] . (IdentityMatrix[7] - \[Gamma] ${Pb})}]`
(1717171717171717171717171717−γ) \left( \begin{array}{ccccccc} \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} \\ \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7} & \frac{1}{7}-\gamma \\ \end{array} \right) The spectrum of the expected emphatic update −𝐗⊤diag⁡(𝐟)(𝐈−γ𝐏π)𝐗-\mathbf{X}^\top\operatorname{diag}(\mathbf{f})(\mathbf{I} - \gamma\mathbf{P}_\pi)\mathbf{X} on Baird’s problem: one zero (the null direction of 𝐗\mathbf{X}), all others with negative real part for every γ<1\gamma < 1.
const AeB = await wl`Transpose[${Xb}] . DiagonalMatrix[${fB}] . (IdentityMatrix[7] - \[Gamma] ${Pb}) . ${Xb}`
await show`Factor[CharacteristicPolynomial[-${AeB}, \[Lambda]]]`
λ(4+7λ)5(26+156γ+105λ+126γλ+49λ2)823543 \frac{\lambda (4+7 \lambda )^5 \left(26+156 \gamma +105 \lambda +126 \gamma \lambda +49 \lambda ^2\right)}{823543}

So emphatic TD converges on Baird’s problem in expectation (Figure 11.1, right). In practice it is another matter. The weight of state 77 relative to its frequency is f(7)/μ(7)=(1+6γ)/(1−γ)≈694f(7)/\mu(7) = (1 + 6\gamma)/(1 - \gamma) \approx 694 at γ=0.99\gamma = 0.99, and it is delivered by products of ratios that are 77 or 00: the followon trace is almost always small and occasionally enormous. Its variance is so large that sampled runs on this problem are erratic, although the method is convergent in theory.Full convergence proofs for emphatic TD, with traces and state-dependent λ\lambda, are due to Yu (2015); Hallak et al. (2016) analyzed its variance and proposed a bounded variant.Huizhen Yu. On Convergence of Emphatic Temporal-Difference Learning. In Proceedings of The 28th Conference on Learning Theory (COLT 2015), volume 40 of Proceedings of Machine Learning Research, pages 1724–1751, 2015; and Assaf Hallak, Aviv Tamar, Rémi Munos, and Shie Mannor. Generalized Emphatic Temporal Difference Learning: Bias-Variance Analysis. In Proceedings of the Thirtieth AAAI Conference on Artificial Intelligence (AAAI-16), pages 1631–1637. AAAI Press, 2016. doi: 10.1609/aaai.v30i1.10227

11.9Reducing Variance

Off-policy learning is inherently noisier than on-policy learning: the further bb is from π\pi, the less each sample says about π\pi, and in the limit nothing at all. Importance ratios have expectation 11 but products of them have high, sometimes infinite, variance, and in stochastic gradient methods they multiply the step size. A single huge step can carry the weights to a region with a different gradient. Several remedies help, none solves the problem: small or adaptive per-parameter step sizes, momentum and Polyak–Ruppert averaging, “importance-weight-aware” updates, approximations of weighted importance sampling with O(d)O(d) cost, and methods that avoid ratios altogether as tree backup does, or clip them, as Retrace and V-trace do (Section 12.11). Another option is to let the target policy depend on the behavior, so that ratios are bounded by construction, as in the recognizers of Precup et al.Doina Precup, Richard S. Sutton, Cosmin Paduraru, Anna Koop, and Satinder Singh. Off-Policy Learning with Options and Recognizers. In Advances in Neural Information Processing Systems 18 (NIPS 2005), pages 1097–1104. MIT Press, 2006 The positive side of the coin is the raison d’être of off-policy learning: any policy has many neighbors, policies that visit similar states and take similar actions, and one stream of experience can teach a great deal about all of them.

11.10Beyond the Book: The Deadly Triad in Deep Reinforcement Learning

Since 2015 almost every large-scale value-based agent has embodied all three elements of the triad: deep networks, bootstrapped targets, and replayed off-policy data. Why does it work at all?Beyond Sutton and Barto (2018). The book mentions DQN only as an application. The analyses below (van Hasselt et al., 2018; Liu et al., 2015, in the saddle-point form; Kumar et al., 2020) relate the triad to deep and offline reinforcement learning, which developed after or alongside the second edition.

DQN’s two stabilizers. The deep Q-network of Mnih et al. (Section 15.5) learns by Q-learning from minibatches drawn from a replay memory, with targets computed by a target network that is copied from the online network only every CC steps.Volodymyr Mnih, Koray Kavukcuoglu, David Silver, Andrei A. Rusu, Joel Veness, Marc G. Bellemare, Alex Graves, Martin Riedmiller, Andreas K. Fidjeland, Georg Ostrovski, Stig Petersen, Charles Beattie, Amir Sadik, Ioannis Antonoglou, Helen King, Dharshan Kumaran, Daan Wierstra, Shane Legg, and Demis Hassabis. Human-Level Control through Deep Reinforcement Learning. Nature, 518(7540):529–533, 2015. doi: 10.1038/nature14236 Between copies this is an ordinary regression problem with fixed targets, so each phase is a true gradient method, a stochastic version of the least-squares DP of (11.4). The target network removes the immediate feedback loop of the w→2ww \to 2w example, in which raising ww also raises its own target; it does not remove the triad, because the outer iteration can still expand (Section 15.5.1 checks this on the same two states). Replay decorrelates consecutive samples and averages the behavior over many past policies, which makes the data more off-policy, not less.

How often does the triad bite? Van Hasselt et al. trained many DQN variants on Atari and tracked the largest action value, calling values beyond the largest attainable return 1/(1−γ)=1001/(1 - \gamma) = 100 soft divergence.Hado van Hasselt, Yotam Doron, Florian Strub, Matteo Hessel, Nicolas Sonnerat, and Joseph Modayil. Deep Reinforcement Learning and the Deadly Triad, 2018 Soft divergence was common, but unbounded divergence never occurred. Its frequency fell sharply with longer multi-step returns (one-step Q-learning soft-diverged in 94%94\% of runs, 1010-step in 21%21\%), fell with target networks and with double Q-learning’s correction of overestimation, and rose with stronger prioritized replay, which pushes the update distribution further from the on-policy one; larger networks were, against expectation, slightly less stable. Each element of the triad thus behaves as a dial, not a switch, in the direction that Theorem 11.2 suggests.

Gradient-TD as a saddle point. The secondary vector 𝐯\mathbf{v} of Algorithm 11.7 has a clean interpretation. For fixed 𝐰\mathbf{w},

max⁡𝐯[𝐯⊤(𝐛−𝐀𝐰)−12𝐯⊤𝐂𝐯]=12(𝐛−𝐀𝐰)⊤𝐂−1(𝐛−𝐀𝐰)=12PBE̅(𝐰), \max_{\mathbf{v}}\Bigl[\mathbf{v}^\top(\mathbf{b} - \mathbf{A}\mathbf{w}) - \tfrac{1}{2}\mathbf{v}^\top\mathbf{C}\mathbf{v}\Bigr] = \tfrac{1}{2}(\mathbf{b} - \mathbf{A}\mathbf{w})^\top\mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}) = \tfrac{1}{2}\overline{\mathrm{PBE}}(\mathbf{w}),

attained at 𝐯=𝐂−1(𝐛−𝐀𝐰)\mathbf{v} = \mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}). Minimizing the PBE̅\overline{\mathrm{PBE}} is therefore a convex–concave saddle-point problem, and GTD2 is stochastic gradient descent in 𝐰\mathbf{w} and ascent in 𝐯\mathbf{v} on it (Exercise 11.12). Liu et al. used this view to obtain the first finite-sample bounds for Gradient-TD and accelerated variants (projected GTD2 and the mirror-prox GTD2-MP).Bo Liu, Ji Liu, Mohammad Ghavamzadeh, Sridhar Mahadevan, and Marek Petrik. Finite-Sample Analysis of Proximal Gradient TD Algorithms. In Proceedings of the Thirty-First Conference on Uncertainty in Artificial Intelligence (UAI 2015), pages 504–513, Amsterdam, 2015 For nonlinear approximators, Maei et al. had already shown that GTD2 and TDC, with the projection taken onto the tangent space of the value manifold and an extra Hessian-vector term, converge almost surely to local optima of a projected Bellman objective, at cost linear in the number of parameters.Hamid R. Maei, Csaba Szepesvári, Shalabh Bhatnagar, Doina Precup, David Silver, and Richard S. Sutton. Convergent Temporal-Difference Learning with Arbitrary Smooth Function Approximation. In Advances in Neural Information Processing Systems 22 (NIPS 2009), pages 1204–1212, 2009

Offline RL: the triad with no exploration. In offline reinforcement learning the agent must learn from a fixed dataset and can never correct its mistakes by acting. The danger moves from the state distribution to the action distribution: the Bellman target max⁡a′q^(s′,a′)\max_{a'}\hat q(s', a') queries actions the data never contain, their values are pure extrapolation, and maximization selects exactly the overestimated ones. Conservative Q-learning (CQL) adds to the squared Bellman error a penalty that pushes values down under a policy that maximizes them and up under the data’s actions,Aviral Kumar, Aurick Zhou, George Tucker, and Sergey Levine. Conservative Q-Learning for Offline Reinforcement Learning. In Advances in Neural Information Processing Systems, volume 33, pages 1179–1191. Curran Associates, Inc., 2020

min⁡q^  α 𝔼s∼𝒟[log⁡∑aeq^(s,a)−𝔼a∼π^β(⋅∣s)q^(s,a)]+12 𝔼(s,a,s′)∼𝒟[(q^(s,a)−ℬ^πkq^k(s,a))2], \begin{aligned} \min_{\hat q}\;&\alpha\,\mathbb{E}_{s \sim \mathcal{D}}\Bigl[\log\sum_a e^{\hat q(s, a)} - \mathbb{E}_{a \sim \hat\pi_\beta(\cdot \mid s)}\hat q(s, a)\Bigr] \\ &+ \tfrac{1}{2}\,\mathbb{E}_{(s, a, s') \sim \mathcal{D}}\Bigl[\bigl(\hat q(s, a) - \hat{\mathcal{B}}^{\pi_k}\hat q_k(s, a)\bigr)^2\Bigr], \end{aligned}

with π^β\hat\pi_\beta the empirical behavior policy, and proves that the resulting policy value is a lower bound on the true one. Conservatism replaces importance correction: rather than reweight toward π\pi, it refuses to trust what bb never tried.

All the methods of this chapter were one-step methods. Chapter 12 unifies one-step and Monte Carlo updates through eligibility traces, and returns at its end to the off-policy question with traces: importance-sampled and tree-backup traces, and the Gradient-TD and Emphatic-TD methods of this chapter extended to λ>0\lambda > 0.

11.11Exercises

Exercise 11.11. In the chain of Section 11.2.2, compute the episodic on-policy distribution (start in the first state; the second is visited 1/ε1/\varepsilon times on average) and verify the key-matrix test (11.3) directly: show that both column sums of diag⁡(𝐦)(𝐈−γ𝐏π)\operatorname{diag}(\mathbf{m})(\mathbf{I} - \gamma\mathbf{P}_\pi) are nonnegative, and find for which γ\gamma the uniform weighting fails it.

Solution

With 𝐏π=(0101−ε)\mathbf{P}_\pi = \begin{pmatrix}0 & 1 \\ 0 & 1 - \varepsilon\end{pmatrix} (the missing mass is termination) and η=(1,1/ε)\eta = (1, 1/\varepsilon), normalized to 𝐦=(ε,1)/(1+ε)\mathbf{m} = (\varepsilon, 1)/(1 + \varepsilon), the column sums are 𝐦⊤(𝐈−γ𝐏π)\mathbf{m}^\top(\mathbf{I} - \gamma\mathbf{P}_\pi).

Column sums of the key matrix for the on-policy weighting and for the uniform weighting.
const Pchain = await wl`{{0, 1}, {0, 1 - \[Epsilon]}}`
await show`Simplify[{({\[Epsilon], 1}/(1 + \[Epsilon])) . (IdentityMatrix[2] - \[Gamma] ${Pchain}), {1/2, 1/2} . (IdentityMatrix[2] - \[Gamma] ${Pchain})}]`
(ϵ1+ϵ1−γ1+ϵ1212(1+γ(−2+ϵ))) \left( \begin{array}{cc} \frac{\epsilon }{1+\epsilon } & \frac{1-\gamma }{1+\epsilon } \\ \frac{1}{2} & \frac{1}{2} (1+\gamma (-2+\epsilon )) \\ \end{array} \right)

The on-policy sums are ε/(1+ε)\varepsilon/(1 + \varepsilon) and (1−γ)/(1+ε)(1 - \gamma)/(1 + \varepsilon), both nonnegative, as the episodic identity (1−γ)η⊤+γ𝐡⊤(1 - \gamma)\eta^\top + \gamma\mathbf{h}^\top predicts after normalization. The uniform weighting has second column sum 12(1−γ(2−ε))\frac{1}{2}(1 - \gamma(2 - \varepsilon)), negative for γ>1/(2−ε)\gamma > 1/(2 - \varepsilon); the test then gives no guarantee, and indeed the scalar AA turns negative at the larger threshold γ>5/(6−4ε)\gamma > 5/(6 - 4\varepsilon), where the features enter.

Exercise 11.12. Show that for fixed 𝐰\mathbf{w} the function L(𝐰,𝐯)=𝐯⊤(𝐛−𝐀𝐰)−12𝐯⊤𝐂𝐯L(\mathbf{w}, \mathbf{v}) = \mathbf{v}^\top(\mathbf{b} - \mathbf{A}\mathbf{w}) - \frac{1}{2}\mathbf{v}^\top\mathbf{C}\mathbf{v} is maximized at 𝐯∗=𝐂−1(𝐛−𝐀𝐰)\mathbf{v}^* = \mathbf{C}^{-1}(\mathbf{b} - \mathbf{A}\mathbf{w}) with value 12PBE̅(𝐰)\frac{1}{2}\overline{\mathrm{PBE}}(\mathbf{w}). Then show that the expected GTD2 update is α(∇𝐯L for 𝐯; −∇𝐰L for 𝐰)\alpha(\nabla_\mathbf{v}L \text{ for } \mathbf{v};\ -\nabla_\mathbf{w}L \text{ for } \mathbf{w}) up to the step-size ratio, so GTD2 is gradient descent–ascent.

Solution

LL is strictly concave in 𝐯\mathbf{v} because 𝐂\mathbf{C} is positive definite; ∇𝐯L=𝐛−𝐀𝐰−𝐂𝐯\nabla_\mathbf{v}L = \mathbf{b} - \mathbf{A}\mathbf{w} - \mathbf{C}\mathbf{v} vanishes at 𝐯∗\mathbf{v}^*. In two dimensions with symbolic entries:

Maximize LL over 𝐯∈ℝ2\mathbf{v} \in \mathbb{R}^2 for a symmetric positive definite 𝐂\mathbf{C} and compare with half the PBE̅\overline{\mathrm{PBE}} written with 𝐠=𝐛−𝐀𝐰\mathbf{g} = \mathbf{b} - \mathbf{A}\mathbf{w}.
const gS = await wl`{Subscript[g, 1], Subscript[g, 2]}`
const cS = await wl`{{Subscript[c, 11], Subscript[c, 12]}, {Subscript[c, 12], Subscript[c, 22]}}`
await show`Module[{v1, v2, sol}, sol = Solve[Thread[D[{v1, v2} . ${gS} - (1/2) {v1, v2} . ${cS} . {v1, v2}, {{v1, v2}}] == 0], {v1, v2}]; Simplify[({v1, v2} . ${gS} - (1/2) {v1, v2} . ${cS} . {v1, v2} /. First[sol]) - (1/2) ${gS} . Inverse[${cS}] . ${gS}]]`
0 0

The gradients are ∇𝐯L=𝐛−𝐀𝐰−𝐂𝐯=𝔼[ρt(δt−𝐯⊤𝐱t)𝐱t]\nabla_\mathbf{v}L = \mathbf{b} - \mathbf{A}\mathbf{w} - \mathbf{C}\mathbf{v} = \mathbb{E}[\rho_t(\delta_t - \mathbf{v}^\top\mathbf{x}_t)\mathbf{x}_t], the expected 𝐯\mathbf{v}-update divided by β\beta, and −∇𝐰L=𝐀⊤𝐯=𝔼[ρt(𝐱t−γ𝐱t+1)𝐱t⊤]𝐯-\nabla_\mathbf{w}L = \mathbf{A}^\top\mathbf{v} = \mathbb{E}[\rho_t(\mathbf{x}_t - \gamma\mathbf{x}_{t+1})\mathbf{x}_t^\top]\mathbf{v}, the expected 𝐰\mathbf{w}-update divided by α\alpha.

Exercise 11.13. Show that on Baird’s counterexample the expected TDC dynamics are not stable for every step-size ratio: find, for γ=0.99\gamma = 0.99, an η=β/α\eta = \beta/\alpha for which 𝐆TDC\mathbf{G}_{\mathrm{TDC}} of (11.10) has an eigenvalue with positive real part, and compare with GTD2 at the same η\eta.

Solution

By the scalar computation of Section 11.7, TDC needs the secondary process to be fast where 𝐀\mathbf{A} has an unstable direction; so try a small η\eta.

Largest real part of the spectra of 𝐆TDC\mathbf{G}_{\mathrm{TDC}} and 𝐆GTD2\mathbf{G}_{\mathrm{GTD2}} on Baird’s problem at η=1/10\eta = 1/10.
const tdcSlow = await wl`ArrayFlatten[{{-${Ab}, -(${Cb} - Transpose[${Ab}])}, {-\[Eta] ${Ab}, -\[Eta] ${Cb}}}] /. {\[Gamma] -> 99/100, \[Eta] -> 1/10}`
const gtdSlow = await wl`ArrayFlatten[{{0 ${Ab}, Transpose[${Ab}]}, {-\[Eta] ${Ab}, -\[Eta] ${Cb}}}] /. {\[Gamma] -> 99/100, \[Eta] -> 1/10}`
await show`Chop[{Max[Re[N[Eigenvalues[${tdcSlow}]]]], Max[Re[N[Eigenvalues[${gtdSlow}]]]]}, 10^-10]`
{0.0388161,0} \{0.0388161,0\}

With η=0.1\eta = 0.1 TDC has an unstable mode, while GTD2’s largest real part is 00 (only the neutral directions of the singular 𝐀\mathbf{A} and 𝐂\mathbf{C}), as Theorem 11.8 predicts on the complement of the null space. This is why TDC is run with β\beta larger than α\alpha (the experiments in Figure 11.1 use η=10\eta = 10).

Exercise 11.14. In the first, single-state MRP of Section 11.6 (rewards 00 and 22 with probability 12\frac{1}{2}, γ=0\gamma = 0), and in the second, two-state one, compute VE̅(w)\overline{\mathrm{VE}}(w) and RE̅(w)\overline{\mathrm{RE}}(w) as functions of ww. Check that the VE̅\overline{\mathrm{VE}}s differ, the RE̅\overline{\mathrm{RE}}s coincide, and all four are minimized at w=1w = 1, as Proposition 11.5 requires.

Solution

With γ=0\gamma = 0 the return is the next reward. In the first MRP vπ=1v_\pi = 1; in the second, the state emitting 00 has vπ=0v_\pi = 0 and the other vπ=2v_\pi = 2, each with μ=12\mu = \frac{1}{2}. In both, GtG_t is 00 or 22 with probability 12\frac{1}{2}.

(VE̅1,VE̅2,RE̅1,RE̅2)(\overline{\mathrm{VE}}_1, \overline{\mathrm{VE}}_2, \overline{\mathrm{RE}}_1, \overline{\mathrm{RE}}_2) for the two indistinguishable MRPs.
await show`Expand[{(w - 1)^2, (1/2) (w - 0)^2 + (1/2) (w - 2)^2, (1/2) (0 - w)^2 + (1/2) (2 - w)^2, (1/2) (0 - w)^2 + (1/2) (2 - w)^2}]`
{1−2w+w2,2−2w+w2,2−2w+w2,2−2w+w2} \left\{1-2 w+w^2,2-2 w+w^2,2-2 w+w^2,2-2 w+w^2\right\}

VE̅2−VE̅1=1\overline{\mathrm{VE}}_2 - \overline{\mathrm{VE}}_1 = 1 for all ww, the variance 𝔼[(Gt−vπ(St))2]\mathbb{E}[(G_t - v_\pi(S_t))^2] is 11 in the first and 00 in the second, and RE̅=w2−2w+2\overline{\mathrm{RE}} = w^2 - 2w + 2 in both: every objective is minimized at w=1w = 1.