Lesson 3.111,209 words

Least-Squares TD

Semi-gradient TD spends one cheap step per example and needs many examples; this lesson makes the opposite tradeoff. Least-Squares TD (LSTD) accumulates the matrices A\mathbf{A} and b\mathbf{b} and solves the TD fixed point w=A1b\mathbf{w} = \mathbf{A}^{-1}\mathbf{b} directly, using the Sherman-Morrison identity to maintain the inverse in O(d2)O(d^2) — the most data-efficient linear TD method, at a quadratic cost.

╌╌╌╌

The previous lessons settled on semi-gradient TD(0) as the default linear learner: one gradient step per transition, memory and compute per step for weights. That frugality is also a weakness. Each example moves the weights only a little, and the example is then discarded, so the method needs a great many transitions to converge. This lesson makes the opposite tradeoff: spend more compute per step to extract more from each example.

Least-Squares TD stays parametric and linear but stops iterating: it accumulates the same matrices and that defined the TD fixed point and solves for in closed form, the most data-efficient linear TD method there is. The complementary route to sample efficiency — abandoning the parametric form and storing the examples themselves — follows in the next lesson.

Least-Squares TD

Every method in the previous lesson spent compute per step proportional to the number of parameters. Recall the endpoint that linear semi-gradient TD(0) converges to. Under the on-policy distribution it settles at the TD fixed point, the weight vector where the expected update vanishes:1

writing for the feature vector at time . TD(0) reaches only in the limit, by taking many small steps that gradually average toward the expectations defining and . This is wasteful of data. If the fixed point is a solved linear system, why iterate at all? Estimate and from the data directly, then solve. Least-Squares TD (LSTD) takes this route. It forms the running sums

where is the identity and , for a small , guarantees the sum is always invertible. These look like they ought to be divided by to be estimates of and ; as written they estimate and . But the extra cancels in the ratio, so LSTD estimates the fixed point as

LSTD versus semi-gradient TD(0). TD(0) takes one small step per example and drifts toward the fixed point over many episodes; LSTD accumulates and and lands on the exact fixed point of the data seen so far every step.

A worked LSTD solve

For example, take a chain with three states carrying one-hot features , , and , discount , and two transitions in the data: with reward , and with reward . Take for a clean solve.

Each transition contributes the rank-one term to and to . For the first transition, , so

For the second transition , so

Summing gives and . Its inverse is , and the fixed point is

So (its return is the single reward ) and — the reward plus the discounted value of , exactly the Bellman-consistent values. Semi-gradient TD(0) would have reached the same only after many passes; LSTD read it off the two transitions directly.

The two-transition chain of the worked LSTD solve. State (feature ) rewards into (feature ), which rewards into the terminal (feature ). With LSTD returns and , the exact Bellman-consistent values.

The Sherman-Morrison incremental inverse

Written naively, LSTD looks like it grows more expensive as grows, and forming from scratch is — the cost of inverting a general matrix. An incremental update resolves both problems. The accumulator is a running sum of outer products (each term is a column vector times a row vector, a rank-one matrix). A matrix of that special form — a previous matrix plus a rank-one update — has an inverse that can be updated directly, without ever inverting anything, by the Sherman-Morrison formula:1

for , initialized with . The formula is simpler than it looks: the numerator is an outer product of two -vectors, and the denominator is a scalar. Every operation is a matrix-vector or vector-vector product — , never . So LSTD stores the inverse matrix (itself memory), maintains it with one Sherman-Morrison update per step, and reads off — all in time and memory per step.

One LSTD step. The new transition contributes a rank-one outer product to ; Sherman-Morrison folds that update into the stored inverse using only matrix-vector products (never a fresh inversion), and is read off directly.

The full procedure keeps and and updates both every step:

Algorithm:LSTD\textsc{LSTD} — batch linear prediction of v^=wx()vπ\hat v = \mathbf{w}^\top\mathbf{x}(\cdot) \approx v_\pi
  1. 1
    input: feature map x\mathbf{x} with x(terminal)=0\mathbf{x}(\text{terminal}) = \mathbf{0}, small ε>0\varepsilon > 0
  2. 2
    A^1ε1I\widehat{\mathbf{A}}^{-1} \gets \varepsilon^{-1}\mathbf{I}
    a d×dd \times d matrix
  3. 3
    b^0\widehat{\mathbf{b}} \gets \mathbf{0}
    a dd-vector
  4. 4
    for each episode do
  5. 5
    initialize SS
  6. 6
    xx(S)\mathbf{x} \gets \mathbf{x}(S)
  7. 7
    repeat
  8. 8
    choose Aπ(S)A \sim \pi(\cdot \mid S), take it, observe RR, SS'
  9. 9
    xx(S)\mathbf{x}' \gets \mathbf{x}(S')
  10. 10
    v(A^1)(xγx)\mathbf{v} \gets (\widehat{\mathbf{A}}^{-1})^\top(\mathbf{x} - \gamma\mathbf{x}')
  11. 11
    A^1A^1(A^1x)v/(1+vx)\widehat{\mathbf{A}}^{-1} \gets \widehat{\mathbf{A}}^{-1} - (\widehat{\mathbf{A}}^{-1}\mathbf{x})\,\mathbf{v}^\top / (1 + \mathbf{v}^\top\mathbf{x})
  12. 12
    b^b^+Rx\widehat{\mathbf{b}} \gets \widehat{\mathbf{b}} + R\,\mathbf{x}
  13. 13
    wA^1b^\mathbf{w} \gets \widehat{\mathbf{A}}^{-1}\widehat{\mathbf{b}}
  14. 14
    SSS \gets S'
  15. 15
    xx\mathbf{x} \gets \mathbf{x}'
  16. 16
    until SS is terminal

What the quadratic cost buys, and what it costs

LSTD is the most data-efficient linear TD(0) method — it extracts the fixed point from the data as fast as the data can define it — but is still substantially more than the of semi-gradient TD. Whether the greater data efficiency is worth the expense depends on how large is, how costly data is relative to compute, and the cost of the rest of the system. With in the thousands, can be prohibitive, and semi-gradient TD with a good step size wins on wall-clock; with small and data expensive to gather, LSTD's sample efficiency justifies the cost.

LSTD also has no step-size parameter, which is sometimes cited as an advantage but is overstated. It does still require : set too small, the sequence of inverses can vary wildly; set too large, learning is slowed. More consequentially, lacking a step size means LSTD never forgets. That is fine for a fixed policy , but in control — where generalized policy iteration keeps changing the target policy — an approximation that weights all past data equally is a liability. The data from an abandoned policy should fade, and LSTD has no mechanism to fade it. In control applications LSTD must be combined with some means of inducing forgetting, which moots the supposed advantage of needing no step size.1

Semi-gradient TD(0)LSTD
Updateone small SGD step per examplerecompute exact fixed point of data so far
Compute / step
Memory (holds )
Data efficiencyneeds many examplesmost efficient linear TD method
Step sizeneeds (must be tuned)none, but needs
Forgettingbuilt in (step size)none — bad for changing

Least-squares control

LSTD as presented evaluates a fixed policy. The natural next question — how to do control with the same data efficiency — was answered by least-squares policy iteration (Lagoudakis and Parr 2003, JMLR), which Sutton and Barto cite as the extension of least-squares methods to control. LSPI runs policy iteration on a batch of transitions: an inner routine, LSTDQ, solves the LSTD fixed point for the action-value function of the current policy directly from the stored data, then the policy is made greedy with respect to that , and the two steps repeat over the same fixed batch. Because every iteration reuses all the data and never touches the environment again, LSPI is fully off-line and among the most sample-efficient linear control methods — the control counterpart of LSTD's prediction efficiency, at the same cost.2

Least-squares policy iteration. LSTDQ solves the action-value fixed point from a fixed batch of transitions; the policy is made greedy in the resulting ; the two steps alternate over the same batch until the policy stops changing, never re-sampling the environment.

Footnotes

  1. Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), §9.8 — Least-Squares TD: the TD fixed point with and ; the LSTD estimates (9.20)–(9.21) with the regularizer; the Sherman-Morrison incremental inverse (9.22); the LSTD algorithm box; and the discussion of the versus tradeoff, the parameter, and the lack of forgetting in control. LSTD is due to Bradtke and Barto (1996); the incremental inverse to Sherman and Morrison (1949). 2 3
  2. Lagoudakis and Parr (2003), Least-Squares Policy Iteration, Journal of Machine Learning Research 4: extends least-squares TD to control by running policy iteration over a fixed batch of transitions, with an inner LSTDQ step that solves the LSTD fixed point for the action-value function of the current policy. Cited by Sutton & Barto (§9.8, Bibliographical Remarks) as the extension of least-squares methods to control.

╌╌ END ╌╌