Tabular Solution Methods/n-Step Bootstrapping

Lesson 2.71,875 words

n-Step Bootstrapping

Monte Carlo waits for the full return; one-step TD bootstraps after a single reward. Between them lies a whole spectrum, indexed by one integer n: look ahead n real rewards, then bootstrap from the value n steps out.

╌╌╌╌

The last two lessons drew the same estimation problem from opposite ends. Monte Carlo waits until an episode terminates and updates each visited state toward the full observed return — no bootstrapping, but no learning until the very end. Temporal-difference learning updates after a single step, using one real reward plus the current estimate as a stand-in for everything after — all bootstrapping, and online. The two are the endpoints of one family, indexed by a single integer .

An -step method looks ahead exactly real rewards and then bootstraps from the estimated value of the state reached steps later. Set and you recover one-step TD; let run to the end of the episode and you recover Monte Carlo. Every value of in between is a genuine method, and an intermediate often learns faster than either extreme.1

The n-step return

Fix the state–reward sequence an agent generates while following ,

where is the terminal step (actions omitted for now). Whatever method we use to estimate , it updates toward some target. The different members of the family are just different choices of target.

Monte Carlo aims at the complete return, every reward until termination:

One-step TD truncates after a single reward and patches the missing tail with a bootstrapped estimate — the one-step return:

where is the estimate of at time . The subscript notation reads a return for time using rewards up through time : the discounted estimate takes the place of the discarded tail . The same idea extends past one step. A two-step return keeps two real rewards and bootstraps from the value two steps out,

where now corrects for the absence of . In general, the target for an arbitrary -step update is the -step return:

for all and . Every -step return is an approximation to the full return , truncated after rewards and then corrected for the remaining missing terms by . If the horizon runs to or past termination — that is, if — all the missing terms are already zero, and the -step return equals the ordinary full return: whenever .

One subtlety: the -step return for step uses future rewards and the future state — none of which exist at time . No real algorithm can use until it has seen and computed ; the first moment all of that is available is time . This lag is unavoidable when looking ahead, and it shapes the pseudocode below.

The n-step TD update

With the target in hand, the update follows directly. At time (the first moment is computable) we move a fraction of the way toward it:

while every other state's estimate is left unchanged, for . This is -step TD. The bracketed quantity is the familiar TD error, but with an -step target in place of the one-step one. Because the update for fires only at , the first states of each episode get no update as the episode begins; to compensate, an equal number of updates are made at the end, after termination, before the next episode starts.

Algorithm:n-Step-TD\textsc{n-Step-TD} — estimate VvπV \approx v_\pi
  1. 1
    input: a policy π\pi; step size α(0,1]\alpha \in (0,1]; a positive integer nn
  2. 2
    initialize V(s)V(s) arbitrarily for all ss
  3. 3
    for each episode do
  4. 4
    initialize and store S0S_0 \ne terminal
  5. 5
    TT \gets \infty
  6. 6
    for t=0,1,2,t = 0, 1, 2, \ldots do
  7. 7
    if t<Tt < T then
  8. 8
    take an action according to π(St)\pi(\cdot \mid S_t)
  9. 9
    observe and store Rt+1R_{t+1} and St+1S_{t+1}
  10. 10
    if St+1S_{t+1} is terminal then
  11. 11
    Tt+1T \gets t + 1
  12. 12
    τtn+1\tau \gets t - n + 1
    τ\tau = time whose estimate is updated now
  13. 13
    if τ0\tau \ge 0 then
  14. 14
    Gi=τ+1min(τ+n,T)γiτ1RiG \gets \sum_{i=\tau+1}^{\min(\tau+n,\,T)} \gamma^{\,i-\tau-1} R_i
  15. 15
    if τ+n<T\tau + n < T then
  16. 16
    GG+γnV(Sτ+n)G \gets G + \gamma^{n} V(S_{\tau+n})
    bootstrap
  17. 17
    V(Sτ)V(Sτ)+α[GV(Sτ)]V(S_\tau) \gets V(S_\tau) + \alpha\,[\,G - V(S_\tau)\,]
  18. 18
    until τ=T1\tau = T - 1

The index is the state currently being updated: at wall-clock time we have just enough lookahead to finish the update for the state steps in the past. All storage can be kept modulo , since only the last states and rewards ever matter at once — the method's memory is bounded by , not by the episode length.

Why an intermediate n is sound

That -step returns interpolate between two known-good methods does not by itself prove they converge. The guarantee comes from a worst-case bound. The intuition: each real reward folded into the target is observed rather than estimated, so it reduces the target's dependence on the error in the bootstrapped estimate; after steps only a fraction of that error remains. Formally, the expectation of the -step return is a better estimate of than is, in the following sense: its worst error over states is at most times the worst error of the current estimate,

for all . This is the error-reduction property. Because , each expected -step backup contracts the worst-case error, and one can show from this that -step TD converges to the correct under appropriate conditions. Every method in the family is sound; one-step TD and Monte Carlo are simply its two extreme members.

A worked n-step target

For example, take , and suppose an agent following produces the reward stream

with current value estimates , , , and . The one-step return keeps one real reward and bootstraps from the very next state:

The two-step return keeps two rewards and bootstraps two states out:

The three-step return keeps three:

Each target is the same shape — accumulated discounted reward, then one bootstrap term standing in for everything past the horizon. The three targets differ in the depth at which they rely on the estimate: depends almost entirely on , while has replaced two layers of that estimate with real rewards and now depends on . If the early estimates were biased, the deeper target has removed more of that bias — the error-reduction property in a single trajectory. The bootstrap weight itself shrinks with depth: , , , so the further out the bootstrap, the less it can distort the target.

The backup-diagram spectrum

The family is easiest to see through its backup diagrams. Each diagram traces the states and rewards whose values are backed up into the root. One-step TD backs up from a single reward and one bootstrapped state; -step TD strings rewards down a spine before bootstrapping; Monte Carlo extends the spine all the way to the terminal square. The whole family is one picture at increasing depth.

The backup-diagram spectrum of -step TD. Left to right, the spine deepens: one-step TD () backs up from a single reward and one bootstrapped state, -step TD from rewards, and -step TD from the whole episode down to the terminal square — Monte Carlo. The final open circle of each column (or the terminal square) is where the estimate is bootstrapped.

Depth controls a bias–variance tradeoff: shallow backups (small ) depend heavily on the bootstrapped estimate, so they are low-variance but inherit whatever bias sits in ; deep backups (large ) use more real reward, shedding bias but taking on the variance of long reward sequences. The best depth is intermediate and task-dependent.

An intermediate n beats both extremes

The random walk quantifies the tradeoff. Take a chain of states with a terminal on each end, an outcome of on the left and on the right, all values initialized to , and equiprobable left/right moves. Run -step TD for a sweep of and , and measure the RMS error of the predictions against the true values, averaged over the first episodes and runs.

Two things stand out. Each curve is a U in — too small a step learns too slowly, too large a step overshoots — and the bottom of the U, the best achievable error, is lowest for an intermediate , neither nor the very largest. A one-step method after this walk would change only the value of the last state visited; a two-step method would nudge the last two; an -step method credits the last states of the run, all by the same reward. Spreading credit further than one step, but not all the way to the episode's start, gives the lowest error.

Performance of -step TD on the 19-state random walk: average RMS error (first 10 episodes, 100 runs) versus step size , one curve per . Each curve is U-shaped in ; the lowest minimum belongs to an intermediate (here around ), beating both one-step TD () and the deep, Monte-Carlo-like backups (). Curves are labelled at their right ends.

Sutton and Barto's full figure sweeps more values of and finds the whole family of U-curves nested this way, with the minimum-of-minima at a small-but-not-one . As the backup-diagram spectrum suggests, bootstrapping too aggressively (small ) ties the estimate to a possibly poor initial value function, while not bootstrapping at all (Monte Carlo) takes on the full variance of the sampled return. An intermediate holds both errors down at once.

n-step Sarsa: control

Prediction estimates for a fixed policy; control improves the policy. The move from prediction to control is the one from the previous lesson: switch from states to state–action pairs and act -greedily with respect to the action values. The -step version of Sarsa — the original one-step Sarsa is now Sarsa(0) — redefines the return over action values instead of state values:

with when . The bootstrap now uses the estimated value of a state–action pair, , rather than a state. The update mirrors -step TD:

leaving all other action values unchanged. Its backup diagrams are the same spectrum as before, but every column now begins and ends on an action (a solid dot) rather than a state, with the sample actions and states alternating down the spine.

Backup diagrams for the -step Sarsa spectrum on state-action values. Each column begins and ends on an action (solid dot); the spine alternates sample actions and states. Left to right: one-step Sarsa (Sarsa(0)), -step Sarsa, and -step Sarsa (Monte Carlo, down to the terminal square). Far right is -step Expected Sarsa, whose last step branches over all actions and takes an expectation under .

The far-right diagram is -step Expected Sarsa: identical to -step Sarsa except the last element is a full branch over all actions, weighted by their probability under . Its return replaces the final action-value with an expected value,

where the expected approximate value of a state is the policy-weighted average of its action values,

Bootstrapping from instead of a single sampled action removes the variance of that last action choice, exactly as one-step Expected Sarsa removed it in the previous lesson.

Why n-step control speeds learning

The reason to reach past one step in control is that a single delayed reward should teach more than one action. Consider an agent wandering a gridworld, receiving zero reward until it stumbles onto a goal cell. When the episode ends, one-step Sarsa strengthens exactly one state–action value: the last action, the one that stepped onto the goal. Every earlier action on that successful path learns nothing from this episode. An -step method strengthens the last actions of the path in one sweep, so a single lucky trajectory propagates credit back along its own tail.

The -step speedup in a gridworld. All rewards are zero except at the goal G. After one successful episode, one-step Sarsa (middle) strengthens only the single action that entered G; -step Sarsa (right) strengthens the last actions of the path, so far more is learned from the one episode.

This continues in n-Step Bootstrapping: Off-Policy Methods, which reweights n-step returns by the importance-sampling ratio, builds the tree-backup algorithm that goes off-policy with no ratios at all, and unifies the whole family under n-step Q(sigma).

Footnotes

  1. Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), §7.1 — n-step TD Prediction: the -step return (7.1) as a truncated, bootstrap-corrected return, the -step TD update (7.2), and the error-reduction property (7.3) that makes every member of the family sound. §7.2 — n-step Sarsa: the action-value return (7.4), the control update (7.5), and -step Expected Sarsa (7.7)–(7.8).

╌╌ END ╌╌