Simple Algorithm
Reinforcement Learning · Fixed Point · 30 min

Stable Off-Policy Learning: Gradient TD & Emphatic TD

True gradient methods on the projected Bellman error, and reweighting by emphasis.

Off-policy linear TD can diverge because its expected update is not the gradient of anything. Two repairs make it provably stable at O(d)\order{d} cost per step. Gradient-TD methods do true stochastic gradient descent on the projected Bellman error, using a second weight vector to break a product of expectations; emphatic TD keeps the TD update but reweights it so that the key matrix becomes positive definite. This lesson derives both from scratch, through the saddle-point view that unifies the gradient methods, and weighs what each costs: slow progress, step-size sensitivity, or variance.

What you’ll learn

  • Derive the mean squared projected Bellman error E[ρδx]⊤E[xx⊤]−1E[ρδx]\E[\rho\delta\mathbf x]^\top\E[\mathbf x\mathbf x^\top]^{-1}\E[\rho\delta\mathbf x], and rewrite it as a convex–concave saddle point with unbiased single-sample gradients.
  • Derive GTD2 and TDC, state their convergence conditions, and explain why TDC is fast on the projected Bellman error but slow on the value error in Baird's problem.
  • Derive the followon trace and emphasis of emphatic TD, prove that the emphatic key matrix is positive definite, and explain the variance problem.
  • Compare GTD2, TDC, emphatic TD and TDRC, and describe where these methods stand in practice.

An objective whose minimum is the TD fixed point

Fix linear features, a target policy π\pi with transition matrix PπP_\pi and reward vector rπr_\pi, and a behavior policy bb whose state distribution dbd_b sits on the diagonal of D\mathbf D. Transitions arrive as (x,R,x′)(\mathbf x,R,\mathbf x') with S∼dbS\sim d_b, A∼bA\sim b, and importance ratio ρ=π(A∣S)/b(A∣S)\rho=\pi(A\mid S)/b(A\mid S), so that E[ρ f(A)∣S]=Eπ[f(A)∣S]\E[\rho\,f(A)\mid S]=\E_\pi[f(A)\mid S]. Three expectations summarize everything linear TD needs:

g(w)=E[ρ δ x]=b−Aw,A=X⊤D(I−γPπ)X,C=E[xx⊤]=X⊤DX.\mathbf g(\mathbf w)=\E[\rho\,\delta\,\mathbf x]=\mathbf b-\mathbf A\mathbf w,\qquad\mathbf A=\mathbf X^\top\mathbf D(\mathbf I-\gamma P_\pi)\mathbf X,\qquad\mathbf C=\E[\mathbf x\mathbf x^\top]=\mathbf X^\top\mathbf D\mathbf X.

Semi-gradient TD follows g\mathbf g, which is not a gradient field, and off-policy A\mathbf A can have eigenvalues with negative real part. The fix is to pick an objective that vanishes exactly at the TD fixed point g(w)=0\mathbf g(\mathbf w)=\mathbf 0 and descend it.

DerivationThe projected Bellman error and its saddle-point form
  1. PBE‾(w)=∥Πδw∥D2=δw⊤DX C−1X⊤D δw=E[ρδx]⊤ E[xx⊤]−1 E[ρδx]\overline{\mathrm{PBE}}(\mathbf w)=\norm{\Pi\boldsymbol\delta_{\mathbf w}}_{\mathbf D}^2=\boldsymbol\delta_{\mathbf w}^\top\mathbf D\mathbf X\,\mathbf C^{-1}\mathbf X^\top\mathbf D\,\boldsymbol\delta_{\mathbf w}=\E[\rho\delta\mathbf x]^\top\,\E[\mathbf x\mathbf x^\top]^{-1}\,\E[\rho\delta\mathbf x]

    With Π=XC−1X⊤D\Pi=\mathbf X\mathbf C^{-1}\mathbf X^\top\mathbf D the dbd_b-weighted projection and δw=TπXw−Xw\boldsymbol\delta_{\mathbf w}=\mathcal T^\pi\mathbf X\mathbf w-\mathbf X\mathbf w the Bellman residual, X⊤Dδw=g(w)\mathbf X^\top\mathbf D\boldsymbol\delta_{\mathbf w}=\mathbf g(\mathbf w), so the projected Bellman error collapses to three expectations:

  2. −12∇wPBE‾(w)=A⊤C−1 g(w)-\tfrac12\nabla_{\mathbf w}\overline{\mathrm{PBE}}(\mathbf w)=\mathbf A^\top\mathbf C^{-1}\,\mathbf g(\mathbf w)

    It is a convex quadratic in w\mathbf w, zero exactly at the TD fixed point (when A\mathbf A is invertible). But its gradient is a product of expectations, and multiplying independent-looking single-sample estimates of each factor would be biased, the double-sampling problem again:

  3. 12PBE‾(w)=max⁡h E[ρ δw (x⊤h)−12(x⊤h)2]=max⁡hL(w,h)\tfrac12\overline{\mathrm{PBE}}(\mathbf w)=\max_{\mathbf h}\ \E\Big[\rho\,\delta_{\mathbf w}\,(\mathbf x^\top\mathbf h)-\tfrac12(\mathbf x^\top\mathbf h)^2\Big]=\max_{\mathbf h}L(\mathbf w,\mathbf h)

    The Fenchel identity 12g⊤C−1g=max⁡h(h⊤g−12h⊤Ch)\tfrac12\mathbf g^\top\mathbf C^{-1}\mathbf g=\max_{\mathbf h}\big(\mathbf h^\top\mathbf g-\tfrac12\mathbf h^\top\mathbf C\mathbf h\big), attained at h⋆=C−1g\mathbf h^\star=\mathbf C^{-1}\mathbf g, moves the inverse into an optimization. Every term inside is now the expectation of a single-transition quantity:

  4. ∇hL=E[ρδx]−Ch,∇wL=E[ρ (γx′−x)(x⊤h)]\nabla_{\mathbf h}L=\E[\rho\delta\mathbf x]-\mathbf C\mathbf h,\qquad\nabla_{\mathbf w}L=\E\big[\rho\,(\gamma\mathbf x'-\mathbf x)(\mathbf x^\top\mathbf h)\big]

    LL is linear in w\mathbf w and concave in h\mathbf h, so minimizing PBE‾\overline{\mathrm{PBE}} is the convex–concave saddle-point problem min⁡wmax⁡hL\min_{\mathbf w}\max_{\mathbf h}L, and one transition gives unbiased stochastic gradients of both arguments (Liu, Liu, Ghavamzadeh, Mahadevan & Petrik, 2015). The optimal h⋆=C−1E[ρδx]\mathbf h^\star=\mathbf C^{-1}\E[\rho\delta\mathbf x] is the least-squares regression of ρδ\rho\delta on the features: the expected TD error, as the features can see it.

The first gradient-TD method (GTD; Sutton, Szepesvári & Maei, 2008) minimized a different objective with the same zero, the norm of the expected update ∥E[ρδx]∥2\norm{\E[\rho\delta\mathbf x]}^2. The projected Bellman error weights that vector by C−1\mathbf C^{-1}, which corresponds to the geometry of the feature space and gave faster algorithms (Sutton et al., 2009).

GTD2 and TDC

GTD2 is stochastic gradient descent in w\mathbf w and ascent in h\mathbf h on LL:

h←h+β ρ (δ−h⊤x) x,w←w+α ρ (x−γx′)(x⊤h).\mathbf h\leftarrow\mathbf h+\beta\,\rho\,(\delta-\mathbf h^\top\mathbf x)\,\mathbf x,\qquad\mathbf w\leftarrow\mathbf w+\alpha\,\rho\,(\mathbf x-\gamma\mathbf x')(\mathbf x^\top\mathbf h).

TDC (TD with gradient correction) uses one more identity before sampling. Since E[ρxx⊤]=C\E[\rho\mathbf x\mathbf x^\top]=\mathbf C, the gradient direction A⊤C−1g=E[ρ(x−γx′)x⊤] C−1g\mathbf A^\top\mathbf C^{-1}\mathbf g=\E[\rho(\mathbf x-\gamma\mathbf x')\mathbf x^\top]\,\mathbf C^{-1}\mathbf g equals g−γ E[ρ x′x⊤] C−1g\mathbf g-\gamma\,\E[\rho\,\mathbf x'\mathbf x^\top]\,\mathbf C^{-1}\mathbf g: the ordinary TD update minus a correction. Replacing C−1g\mathbf C^{-1}\mathbf g by h\mathbf h gives

w←w+α ρ (δ x−γ x′ (x⊤h)).\mathbf w\leftarrow\mathbf w+\alpha\,\rho\,\big(\delta\,\mathbf x-\gamma\,\mathbf x'\,(\mathbf x^\top\mathbf h)\big).

When h=0\mathbf h=\mathbf 0, TDC is off-policy semi-gradient TD; the correction term is what removes the instability.

python
import numpy as np

def tdc_step(w, h, x, r, x_next, rho, gamma, alpha, beta):
    """One TDC update from an off-policy transition; rho = pi(a|s) / b(a|s)."""
    delta = r + gamma * (w @ x_next) - w @ x
    hx = h @ x                                               # features' estimate of E[rho * delta | s]
    w = w + alpha * rho * (delta * x - gamma * x_next * hx)  # TD step minus gradient correction
    h = h + beta * rho * (delta - hx) * x                    # least-mean-squares regression
    return w, h

Both vectors cost O(d)\order{d} per step. At termination pass x_next as zeros.

Think first TDC on Baird's problem makes the projected Bellman error tiny while the value error stays near 2. Since the seven feature vectors are independent, the projection is the identity and PBE‾\overline{\mathrm{PBE}} equals the full Bellman error, which is zero only at the true values. Why is progress on the value error so slow?

Because a value function that is nearly constant is nearly a fixed point of the target policy's backup. In Baird's problem Tπv=γ v(s7)1\mathcal T^\pi v=\gamma\,v(s_7)\mathbf 1, so a constant function c1c\mathbf 1 has Bellman error (1−γ)∣c∣=0.01∣c∣(1-\gamma)\abs c=0.01\abs c. An expected-update computation (step sizes α=0.005\alpha=0.005, β=0.05\beta=0.05) gives PBE<0.01\sqrt{\mathrm{PBE}}<0.01 after 500 sweeps with VE≈1.94\sqrt{\mathrm{VE}}\approx1.94, and thousands of sweeps later barely anything has changed: the gradient of an objective that is already almost zero is almost zero. The method does converge to the true values, but at a rate governed by (1−γ)2(1-\gamma)^2-small curvature. Minimizing the projected Bellman error is the right limit and a weak signal when γ\gamma is near 1.

The same construction extends to action values and control. GQ(λ\lambda) (Maei & Sutton, 2010) learns action values with eligibility traces; Greedy-GQ (Maei, Szepesvári, Bhatnagar & Sutton, 2010) targets the greedy policy of a linear QQ from a fixed behavior policy, at O(d)\order{d} per step, with convergence to a stationary point of its objective rather than to Q⋆Q^\star. For smooth nonlinear approximators, Maei et al. (2009) proved convergence of gradient-TD methods to local optima of a projected objective.

Emphatic TD: reweight until the key matrix is positive definite

Emphatic TD (Sutton, Mahmood & White, 2016) keeps the semi-gradient update and changes how much each state's update counts. Recall the column-sum criterion: a key matrix M(I−γPπ)\mathbf M(\mathbf I-\gamma P_\pi), with nonnegative state weights mm on the diagonal of M\mathbf M, is positive definite if its row sums (1−γ)m(1-\gamma)m and column sums m⊤(I−γPπ)m^\top(\mathbf I-\gamma P_\pi) are positive. Under the behavior's own weighting, the column sums db⊤−γdb⊤Pπd_b^\top-\gamma d_b^\top P_\pi can be negative. So choose the weights to make them positive by construction: give each state an interest i(s)≥0i(s)\ge0 and solve

m⊤(I−γPπ)=(db∘i)⊤⟺m(s)=db(s) i(s)+γ∑s′m(s′) Pπ(s′,s).m^\top(\mathbf I-\gamma P_\pi)=(d_b\circ i)^\top\quad\Longleftrightarrow\quad m(s)=d_b(s)\,i(s)+\gamma\sum_{s'}m(s')\,P_\pi(s',s).

The emphasis m(s)m(s) is the discounted flow of interest into ss under the target policy: the weighting an on-policy learner would have used, rebuilt from off-policy data.

DerivationThe followon trace estimates the emphasis online
  1. Ft=γ ρt−1 Ft−1+It,w←w+α ρt Ft δt xtF_t=\gamma\,\rho_{t-1}\,F_{t-1}+I_t,\qquad\mathbf w\leftarrow\mathbf w+\alpha\,\rho_t\,F_t\,\delta_t\,\mathbf x_t

    ETD(0) multiplies each update by a scalar followon trace FtF_t, with F−1=0F_{-1}=0:

  2. f(s)=db(s) i(s)+γ∑s′f(s′) Pπ(s′,s)⟹f=mf(s)=d_b(s)\,i(s)+\gamma\sum_{s'}f(s')\,P_\pi(s',s)\quad\Longrightarrow\quad f=m

    In steady state, f(s)=db(s) E[Ft∣St=s]f(s)=d_b(s)\,\E[F_t\mid S_t=s] satisfies the same recursion as mm: reaching ss means arriving from some s′s' through an action whose ratio ρ\rho converts the behavior's transition probabilities into the target policy's:

  3. 1⊤M(I−γPπ)=(db∘i)⊤ ≥ 0\mathbf 1^\top\mathbf M(\mathbf I-\gamma P_\pi)=(d_b\circ i)^\top\ \ge\ 0

    So the expected update is bm−Amw\mathbf b_m-\mathbf A_m\mathbf w with Am=X⊤M(I−γPπ)X\mathbf A_m=\mathbf X^\top\mathbf M(\mathbf I-\gamma P_\pi)\mathbf X, whose state-space key matrix has positive row sums and column sums db∘i≥0d_b\circ i\ge0. With i>0i>0 the column-sum criterion applies and Am\mathbf A_m is positive definite: stable for any behavior policy that covers the target's actions. With λ>0\lambda>0 the emphasis becomes Mt=λIt+(1−λ)FtM_t=\lambda I_t+(1-\lambda)F_t and the same argument goes through.

In Baird's problem with interest 1 everywhere, db=171d_b=\tfrac17\mathbf 1 and the target policy funnels everything into s7s_7, so m=171+γ1−γ e7m=\tfrac17\mathbf 1+\frac{\gamma}{1-\gamma}\,\mathbf e_7: about 0.143 for each upper state and 0.143+99=99.140.143+99=99.14 for s7s_7. The emphatic key matrix then has eigenvalues 00 (the null direction n\mathbf n), 47\tfrac47 five times, about 1.001.00 and 3.693.69, all nonnegative, and the expected iteration converges to an exact solution. Yu (2015) proved that ETD(λ\lambda) converges with probability one under general off-policy conditions.

Predict before you look

In Baird's problem with γ=0.99\gamma=0.99 and interest 1 in every state, how many times more emphasis does s7s_7 receive than an upper state? Use m(s7)/m(si)=(17+γ1−γ)/17m(s_7)/m(s_i)=\big(\tfrac17+\tfrac{\gamma}{1-\gamma}\big)\big/\tfrac17.

Show the answer

694

1+7γ/(1−γ)=1+7⋅99=6941+7\gamma/(1-\gamma)=1+7\cdot99=694. Emphatic TD concentrates almost all of its weight where the target policy actually goes, as an on-policy learner would. The catch is how it estimates that weight: FtF_t accumulates products of importance ratios, and to represent a factor of about 700 from data in which solid actions carry ρ=7\rho=7 and dashed ones ρ=0\rho=0, the trace must occasionally become very large. Its variance is so high on this problem that Sutton and Barto report it "nigh impossible" to get consistent results from sampled runs; they show only the expected trajectory.

Saddle points, regularized corrections, and practice

The saddle-point view does more than tidy the derivation. Because LL is convex–concave, primal–dual stochastic gradient methods come with finite-sample guarantees, and accelerated or proximal variants apply (Liu et al., 2015). The same Fenchel trick handles control: SBEED (Dai et al., 2018) applies it to a smoothed, entropy-regularized Bellman optimality error, obtaining a primal–dual algorithm for general nonlinear function classes, with a guarantee of convergence to a stationary point.

The most practical descendant so far is TDRC (Ghiassian, Patterson, Garg, Gupta, White & White, 2020). It fits the secondary weights by ridge regression, solving (C+βI)h=E[δx](\mathbf C+\beta\mathbf I)\mathbf h=\E[\delta\mathbf x], and keeps TDC's update for w\mathbf w. With β=0\beta=0 it is TDC; as β→∞\beta\to\infty, h→0\mathbf h\to\mathbf 0 and it becomes plain TD. With the default β=1\beta=1 and a single step size it behaved as well as TD where TD works, remained sound where TD diverges, and outperformed other gradient-TD methods across their benchmarks; its control version, QRC, extends the idea to deep networks.

MethodCost per stepOff-policy guarantee (linear)Main weakness
Semi-gradient TDO(d)\order{d}None; can divergeInstability (Baird)
LSTDO(d2)\order{d^2}Solves A^w=b^\hat{\mathbf A}\mathbf w=\hat{\mathbf b}; off-policy fixed point may be poorQuadratic cost
GTD2, TDCO(d)\order{d}, two vectorsConverge to the TD fixed point (GTD2 with proportional step sizes, TDC on two timescales)Second step size; slow where the objective is flat
Emphatic TDO(d)\order{d}, one scalar tracePositive definite key matrix; converges w.p. 1 (Yu, 2015)High variance of the followon trace
TDRCO(d)\order{d}, two vectorsSound where TD divergesRegularization biases h\mathbf h; mostly empirical support beyond the linear theory
Check your understanding

TDRC fits its secondary weights by ridge regression with penalty β\beta. What does TDRC reduce to as β→∞\beta\to\infty?

  1. TDC
  2. GTD2
  3. Ordinary semi-gradient TD
  4. Emphatic TD
Show the answer

C. Ordinary semi-gradient TD

An infinite ridge penalty forces h=0\mathbf h=\mathbf 0, and with h=0\mathbf h=\mathbf 0 TDC's correction term γx′(x⊤h)\gamma\mathbf x'(\mathbf x^\top\mathbf h) vanishes, leaving semi-gradient TD. At β=0\beta=0 the secondary weights are an unregularized regression and TDRC is TDC. The default β=1\beta=1 sits between.

Key takeaways

  • The projected Bellman error is E[ρδx]⊤E[xx⊤]−1E[ρδx]\E[\rho\delta\mathbf x]^\top\E[\mathbf x\mathbf x^\top]^{-1}\E[\rho\delta\mathbf x], zero at the TD fixed point; a Fenchel identity turns it into a convex–concave saddle point with single-sample gradients.
  • GTD2 and TDC learn a second vector h≈E[xx⊤]−1E[ρδx]\mathbf h\approx\E[\mathbf x\mathbf x^\top]^{-1}\E[\rho\delta\mathbf x] and converge off-policy at O(d)\order{d} per step (TDC's proof uses two timescales); on Baird's problem TDC zeroes the projected Bellman error quickly but approaches the true values very slowly.
  • Emphatic TD reweights each update by a followon trace whose expectation is the discounted flow of interest under the target policy; this makes the key matrix positive definite, at the cost of high variance.
  • TDRC interpolates between TD and TDC with a ridge penalty on h\mathbf h; in deep RL, target networks and replay remain the common stabilizers as of the mid-2020s.

Check yourself

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

Question 1

Why can't plain stochastic gradient descent on PBE‾(w)=E[ρδx]⊤E[xx⊤]−1E[ρδx]\overline{\mathrm{PBE}}(\mathbf w)=\E[\rho\delta\mathbf x]^\top\E[\mathbf x\mathbf x^\top]^{-1}\E[\rho\delta\mathbf x] use one transition per update?

  1. Because the objective is not convex
  2. Because its gradient is a product of expectations, and multiplying single-sample estimates of the factors gives a biased estimate
  3. Because the importance ratios have mean zero
  4. Because the projected Bellman error cannot be computed for linear features
Show the answer

B. Because its gradient is a product of expectations, and multiplying single-sample estimates of the factors gives a biased estimate

−12∇PBE‾=A⊤C−1g(w)-\tfrac12\nabla\overline{\mathrm{PBE}}=\mathbf A^\top\mathbf C^{-1}\mathbf g(\mathbf w) multiplies three expectations. Gradient-TD methods learn h≈C−1g(w)\mathbf h\approx\mathbf C^{-1}\mathbf g(\mathbf w) separately, or equivalently solve the saddle point min⁡wmax⁡hL\min_{\mathbf w}\max_{\mathbf h}L, whose gradients are expectations of single-transition quantities.

Question 2

What does TDC's secondary weight vector h\mathbf h converge to, on its faster timescale?

  1. The TD fixed point
  2. The true value function's weights
  3. E[xx⊤]−1E[ρδx]\E[\mathbf x\mathbf x^\top]^{-1}\E[\rho\delta\mathbf x], the least-squares regression of the TD error on the features
  4. The emphasis weights
Show the answer

C. E[xx⊤]−1E[ρδx]\E[\mathbf x\mathbf x^\top]^{-1}\E[\rho\delta\mathbf x], the least-squares regression of the TD error on the features

Its update is a least-mean-squares regression of δ\delta on x\mathbf x (importance-weighted). It estimates where the features say the TD errors are systematically positive or negative, which is exactly the factor C−1g\mathbf C^{-1}\mathbf g needed to complete the gradient.

Question 3

Why is emphatic TD's expected update stable for any behavior policy that covers the target's actions?

  1. Because the followon trace has low variance
  2. Because it is a true gradient method on the projected Bellman error
  3. Because it updates only on-policy transitions
  4. Because its emphasis makes the key matrix's column sums equal to db∘i≥0d_b\circ i\ge0, with positive row sums, so the key matrix is positive definite
Show the answer

D. Because its emphasis makes the key matrix's column sums equal to db∘i≥0d_b\circ i\ge0, with positive row sums, so the key matrix is positive definite

The emphasis mm solves m⊤(I−γPπ)=(db∘i)⊤m^\top(\mathbf I-\gamma P_\pi)=(d_b\circ i)^\top by construction, so the column-sum criterion holds whatever the mismatch between bb and π\pi. The expected update is then a stable linear system, which Yu (2015) turned into an almost-sure convergence result for ETD(λ\lambda).

Further reading

  • R. S. Sutton, H. R. Maei, D. Precup, S. Bhatnagar, D. Silver, C. Szepesvári & E. Wiewiora, "Fast gradient-descent methods for temporal-difference learning with linear function approximation", ICML (2009).
  • R. S. Sutton, A. R. Mahmood & M. White, "An emphatic approach to the problem of off-policy temporal-difference learning", Journal of Machine Learning Research 17 (2016).
  • H. Yu, "On convergence of emphatic temporal-difference learning", COLT (2015).
  • B. Liu, J. Liu, M. Ghavamzadeh, S. Mahadevan & M. Petrik, "Finite-sample analysis of proximal gradient TD algorithms", UAI (2015).
  • S. Ghiassian, A. Patterson, S. Garg, D. Gupta, A. White & M. White, "Gradient temporal-difference learning with regularized corrections", ICML (2020).