Tabular Solution Methods/Temporal-Difference Learning

Lesson 2.52,451 words

Temporal-Difference Learning

Temporal-difference learning is the one idea most central to reinforcement learning: learn a value directly from experience, like Monte Carlo, but update each guess toward the next guess before the episode ends, like dynamic programming. We derive the TD(0) prediction rule and its reward-prediction error, contrast its one-step backup with MC and DP, work the driving-home and random-walk examples, and show the batch-updating optimality that makes TD approximate the certainty-equivalence estimate.

╌╌╌╌

If one idea had to be singled out as central and novel to reinforcement learning, it would be temporal-difference (TD) learning. It is a blend of the two families of solution methods we have already seen.1 Like Monte Carlo methods, TD learns directly from raw experience with no model of the environment's dynamics — no need for the transition probabilities . Like dynamic programming, TD updates its estimates in part from other learned estimates, without waiting for a final outcome. It bootstraps: it updates a guess from a later guess rather than from an actual return. The rest of this lesson works out the consequences of that one change.

As usual we begin with the prediction problem: estimating the value function for a fixed policy . Control (finding a good policy) comes after, and follows the same generalized-policy-iteration pattern as everything else; the methods differ chiefly in how they solve prediction.

From Monte Carlo to TD

Both TD and Monte Carlo use experience to solve prediction. Following , each generates a stream of states, actions, and rewards, and each updates its estimate of at the nonterminal states it meets. The difference is when, and toward what, they update.

A Monte Carlo method waits until the return following a visit is known, then uses that return as the target. A simple every-visit constant- MC update is

where is the actual return following time and is a constant step size. Call this method constant- MC. Its target is not known until the episode terminates, so MC must wait for the end of the episode before it can change .

TD does not wait. At time it immediately forms a target from the one reward it just observed, , and its existing estimate of the next state, . The simplest TD method makes the update

on the transition to and receiving . Where MC's target is the full return , TD's target is — one real reward plus the discounted estimate of the rest. This method is called TD(0), or one-step TD, because it is the base case of the -step and TD() methods that look further ahead.2

Constant- MC waits for the whole episode, then corrects toward the actual return ; TD(0) corrects after a single step, toward — one real reward plus the estimate of the rest. The target is the only thing that changes.
Algorithm:Tabular-TD(0)\textsc{Tabular-TD}(0) — estimate VvπV \approx v_\pi for a given policy π\pi
  1. 1
    input: policy π\pi, step size α(0,1]\alpha \in (0,1]
  2. 2
    V(s)V(s) \gets arbitrary, with V(terminal)=0V(\text{terminal}) = 0, for all sSs \in \mathcal{S}
  3. 3
    for each episode do
  4. 4
    initialize SS
  5. 5
    repeat
  6. 6
    AA \gets action given by π\pi for SS
  7. 7
    take action AA, observe RR, SS'
  8. 8
    V(S)V(S)+α[R+γV(S)V(S)]V(S) \gets V(S) + \alpha[R + \gamma V(S') - V(S)]
  9. 9
    SSS \gets S'
  10. 10
    until SS is terminal

The whole method is one line inside two loops. No return has to be stored, no episode has to finish; the estimate at moves the instant the next state and reward arrive.

Why bootstrapping is legitimate

It is not obvious that updating a guess from another guess can converge to the true value function. The justification runs through the same recursive identity that produced the Bellman equation. Write the value three ways:

Each line is a target, and each method aims at a different one. Monte Carlo aims at the first line: because the expected return is unknown, it uses a sample return in its place. Dynamic programming aims at the third line: it can take the expectation exactly because it is handed a complete model, but it must substitute the current estimate for the true , which is unknown. TD aims at that same third line and makes both substitutions at once — it samples the expectation (using the single observed and rather than averaging over all successors) and it bootstraps (using rather than ). Making both substitutions at once is what lets TD combine the sampling of Monte Carlo with the bootstrapping of DP.2

Backup diagrams. DP (left) backs up from all successors using a model but bootstraps from estimates; MC (right) samples one full trajectory to the terminal state but does not bootstrap; TD(0) (center) does both — it samples a single successor and backs up from its estimate.

The backup diagrams show the contrast directly. We call TD and MC updates sample updates: they look ahead to a sample successor, use its value and the reward along the way to compute a backed-up value, and then adjust the value of the original state toward it. Sample updates differ from the expected updates of DP in resting on a single sample successor rather than the complete distribution of all possible successors. TD's diagram is the shortest of the three — a single link from a state to its one sampled successor — because TD backs up from the next state only.

The TD error

The bracketed quantity in the TD(0) update is a kind of error. It measures the difference between the estimated value of and the better estimate . This is the TD error, and it recurs throughout reinforcement learning:

is the prediction made at time ; is a corrected prediction after one observed step. Their difference is the one-step prediction error, and TD(0) moves by to reduce it.

Notice a subtlety in the timing. The TD error at each step is the error in the estimate made at that time. Because depends on the next state and the next reward , it is not available until one step later: is the error in but can only be computed at time . This one-step delay is unavoidable when learning without a model.

If does not change during an episode — as it does in Monte Carlo, but not quite in TD(0) — the Monte Carlo error decomposes exactly into a sum of TD errors:

The full-episode error is the discounted sum of the one-step errors. The identity is only approximate in TD(0) itself, since shifts as the episode runs, but it holds tightly for small and underlies the eligibility traces introduced later.2

The TD error is the gap between the old estimate at and the one-step-corrected target; TD(0) moves a fraction of the way to close it.

Driving home: TD versus MC intuition

For example: each day you drive home from work and try to predict how long it will take. Leaving the office on a Friday at 6:00 you estimate 30 minutes. Reaching the car at 6:05 it has begun to rain, so you revise the total to 40. Off the highway at 6:20 you cut the total to 35; then you get stuck behind a slow truck and revise up to 43; you turn onto your street at 6:40 and reach home at 6:43. The sequence of states carries a running predicted total travel time — 30, 40, 35, 43, 43 — and the reward on each leg is the elapsed time, with so the return from any state is the actual remaining time to go.3

A Monte Carlo method plots those predictions and, at the end of the trip, when the true 43-minute total is finally known, corrects every earlier prediction toward it. When you left the highway you thought 15 minutes remained; it took 23; so MC pushes that prediction up — but only after you are already home. TD instead shifts each estimate toward the estimate that immediately follows it. The moment you revise from 35 to 43 behind the truck, TD updates the exiting-highway prediction toward 43, on the spot, without waiting to see the actual outcome. Each error is proportional to the change in prediction over time — the temporal difference in predictions — which is where the method gets its name.

Driving home. Monte Carlo (left) waits for the actual 43-minute outcome and corrects every prediction toward it (red arrows all point to the final level); TD (right) corrects each prediction toward the very next prediction, as soon as it is made.

The example also shows a practical advantage of TD. Suppose you move to a new office but still merge onto the same highway. After one bad day in traffic you already know your initial estimate was too low. Monte Carlo cannot raise it until you get home, because the true return is not yet known. TD shifts the estimate the moment the next, better-informed estimate arrives. Learning during the episode, not only at its end, is what makes TD suited to long episodes, to continuing tasks with no episodes at all, and to situations where waiting to the end is simply too slow.4

Advantages of TD prediction

TD methods bootstrap: they learn a guess from a guess. Three advantages recommend them.4

  • No model. Unlike DP, TD needs no model of the environment: no reward distribution, no next-state probabilities. It learns straight from experience.
  • Online and incremental. Unlike MC, TD updates after every step, needing no final return. Some episodes are very long, so waiting until the end makes all learning slow; some tasks are continuing and have no episode boundary; and some MC variants must discard episodes on which experimental actions are taken. TD is far less exposed to any of these, because it learns from each transition regardless of what follows.
  • Sound. For any fixed policy , TD(0) has been proved to converge to : in the mean for a small enough constant step size, and with probability 1 if decreases according to the usual stochastic- approximation conditions.

Which learns faster, TD or constant- MC? No one has proved either uniformly faster, and it is not even settled what the right formal question is. In practice, though, TD methods have usually been found to converge faster than constant- MC on stochastic tasks. Consider the random-walk example.

The random-walk Markov reward process. Episodes start in the center state C and step left or right with equal probability; termination on the far right pays reward 1, all other rewards are 0. The true value of each state is its probability of ending on the right — 1/6, 2/6, 3/6, 4/6, 5/6 for A..E.

Run TD(0) on this chain from an initial guess of everywhere and the estimates march toward the true values ; averaged over many runs, the root-mean-square error falls faster and settles lower than constant- MC across a range of step sizes. Sampling and bootstrapping beats sampling alone here.

One episode, two update rules

Consider a single episode updating under each method, with real numbers. Take the random walk, , , and the standard start for all five states . Suppose the episode runs , so it visits , , and terminates on the right with reward ; every earlier reward is .

Constant- MC waits for the return. The episode's return from every visited state is (undiscounted, the only reward was the terminal ). So each visited state moves toward :

All three visited states rise by the same , because MC hands each the same target regardless of position in the episode.

TD(0) updates on each transition, using the next state's current estimate as the target. Process the transitions in order. On the reward is and , so the target is , equal to ; the TD error is zero and does not move. Same for : target , no change to . Only the last transition carries information: terminal pays reward , and the terminal value is , so the target is and

One random-walk episode C-D-E-right-terminal, alpha = 0.1. Monte Carlo (top) moves all three visited states up by 0.05 toward the shared return G = 1. TD(0) (bottom) moves only V(E), because C and D see a zero reward and an unchanged next-state estimate, so their TD errors are zero on this episode.

After one episode MC has raised all three visited estimates; TD has raised only . TD credits a state only when the step out of it was informative, and it propagates that credit backward on later episodes: once is above , a future visit to that steps to will find a target above and raise in turn. TD diffuses value backward one link per episode; MC injects the outcome everywhere at once but with the full variance of a single sampled return.

Optimality of TD(0): batch updating

TD's advantage is clearest when data is scarce. Suppose only a finite batch of experience is available — say ten episodes. A natural tactic is to present the batch repeatedly until the estimate converges: compute the increments (6.1) or (6.2) for every step at every visited state, but change only once, by the sum of all the increments, then reprocess the whole batch, and repeat. This is batch updating, because updates are made only after processing each complete batch of training data.

Under batch updating, TD(0) converges deterministically to a single answer, independent of the step size as long as is small. Constant- MC also converges deterministically under the same conditions, but to a different answer — and comparing the two answers is what reveals the difference between the methods.

Consider a tiny example. You observe eight episodes of an unknown Markov reward process:

The first episode went from to with reward , then terminated from with reward ; six other episodes went directly from and terminated with reward ; one terminated from with reward . Clearly : six of eight visits to ended in . But what is ? There are two defensible answers, one per method: MC uses only the one return observed from , while TD infers that always leads to , so should inherit 's value.

The two answers for . Batch Monte Carlo estimates 0 — the one observed return from A was 0, which minimizes error on the training set. Batch TD(0) estimates 3/4 by first building the maximum-likelihood Markov model (A always goes to B, and ) — the certainty-equivalence answer.

Batch Monte Carlo gives : was visited once, and the return that followed was . This answer gives zero squared error on the training set — it is optimal in that limited sense. Batch TD(0) gives , by a different route. It first builds the maximum-likelihood model of the Markov process: the estimated transition probability from to is the fraction of observed transitions from that went to , and each reward is the average observed on that transition. Here went to every time, and is worth , so must be worth too. The value function that would be exactly correct if this model were the true model is the certainty-equivalence estimate — it acts as if the estimated process were certainly correct.

This is why we expect to predict future data better even though fits the past data perfectly. If the process is Markov, the certainty-equivalence estimate captures its structure, and structure generalizes. It also explains why TD tends to converge faster: nonbatch TD(0) does not reach the certainty-equivalence estimate, but it moves roughly toward it, whereas MC moves toward the minimum-training-error solution. TD approximates the certainty-equivalence answer with memory only in the number of states, where forming the model outright would take memory and computation — so on large problems TD may be the only feasible way to approach it.5

This continues in TD Control: Sarsa, Q-learning, and Double Learning, which turns TD prediction into control — Sarsa on-policy, Q-learning off-policy, Expected Sarsa spanning the two — and fixes the maximization bias with Double Q-learning.

Footnotes

  1. Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), Ch. 6 — introduction: TD learning as the combination of Monte Carlo ideas (learn from raw experience, no model) and dynamic-programming ideas (bootstrap from an existing estimate without waiting for a final outcome).
  2. Sutton & Barto, §6.1 — TD Prediction: the constant- MC update (6.1), the TD(0) update (6.2), the recursive identity (6.3)–(6.4) that TD samples and bootstraps at once, the tabular TD(0) box, the sample-versus-expected-update contrast, the TD error (6.5), and its decomposition of the Monte Carlo error (6.6). 2 3
  3. Sutton & Barto, §6.1, Example 6.1 (Driving Home) and Figure 6.1: the predicted-total-travel-time sequence with , and the contrast between Monte Carlo corrections (toward the actual outcome) and TD corrections (toward the next prediction).
  4. Sutton & Barto, §6.2 — Advantages of TD Prediction Methods: no model required (unlike DP), online and incremental learning (unlike MC), convergence of TD(0) to , and Example 6.2 (Random Walk) with Figure 6.2 showing TD beating constant- MC. 2
  5. Sutton & Barto, §6.3 — Optimality of TD(0): batch updating, Example 6.4 (You are the Predictor), the certainty-equivalence estimate as the maximum-likelihood-model value function that batch TD(0) computes, and the memory/computation argument ( versus /).

╌╌ END ╌╌