Notes / Reinforcement Learning / Note 5 of 9

Leer en español

n-step bootstrapping

Using n real rewards before trusting an estimate: the range of methods between one-step TD and Monte Carlo.

An agent that learns from experience has to decide how long to wait before it learns from what happened. One-step methods, such as Sarsa and Q-learning, learn after every move: they take the reward they just received and add their own estimate of what the next state is worth, which stands in for everything that follows. Monte Carlo methods do the opposite. They wait until the episode ends and learn from all the rewards that actually came.

Neither is always best:

  • trusting an estimate after one step is quick, but early on the estimates are poor, and so are the targets built on them;
  • waiting for the end uses only real rewards, but those vary from episode to episode, and nothing is learned until the end.

The methods in this note sit in between: they use n real rewards, then trust the estimate of the state they reach. With n = 1 they are the one-step methods, and with an n as long as the episode, Monte Carlo.

⋯ St St+1 St+2 St+3 ST 1 step 3 steps to the end
What the target for St uses: the rewards along the blue line, then, in red, the estimated value of the state where the line stops. Running to the end of the episode, as Monte Carlo does, needs no estimate.

Throughout this page, the steps of an episode are drawn in blue ink, and the estimate a target relies on in red ink.

1The n-step return

Start with prediction: estimating vπ(s), the expected return from each state s when following a fixed policy π, from episodes that follow π. V is the table of estimates. The target of a Monte Carlo update is the full return, every reward until the episode ends at time T:

Gt = Rt+1 + γRt+2 + γ2Rt+3 + ⋯ + γT−t−1RT

The target of a one-step TD update keeps the first reward and replaces the rest by the discounted estimate of the next state. It is the one-step return, Gt:t+1 = Rt+1 + γVt(St+1). Keeping n rewards instead of one gives the n-step return:

Gt:t+n = Rt+1 + γRt+2 + ⋯ + γn−1Rt+n + γnVt+n−1(St+n)

In this return:

  • the subscript t:t+n says that it starts at time t and uses rewards up to time t + n;
  • the last term is the estimate of the state reached after n steps, standing in for all the rewards after it, and Vt+n−1 is the table as it stands when that return can first be computed;
  • if the episode ends before n steps, when t + n ≥ T, there is nothing left to estimate, and the n-step return is the full return Gt.

An example. A robot drives home to its charger from the kitchen, through the hallway and the study. Reaching the charger gives +10, every other step 0, and γ = 0.9, so the true values are 8.1 for the kitchen, 9 for the hallway and 10 for the study. The robot's table is still poor: V(kitchen) = 2, V(hallway) = 4 and V(study) = 6. The returns from the kitchen, at t = 0:

nn-step return from the kitchen
10 + 0.9 · 4 = 3.6
20 + 0.9 · 0 + 0.81 · 6 = 4.86
3 or more0 + 0.9 · 0 + 0.81 · 10 = 8.1

Each extra step replaces part of an estimate with a real reward. With three steps the robot reaches the charger, so no estimate is left, and the return is the full return, 8.1.

Why a longer return is a better target

In the n-step return, the rewards are real, and the only estimate is multiplied by γn. So its expectation is closer to the truth than the table, in the worst case by a factor γn. This is the error reduction property:

maxs |Eπ[Gt:t+n | St = s] − vπ(s)| ≤ γn maxs |Vt+n−1(s) − vπ(s)|

In the example, the table's worst error is 6.1, at the kitchen:

nWorst errorBound, γn · 6.1
14.50.9 · 6.1 = 5.49
23.240.81 · 6.1 = 4.94

This property is why every n-step TD method converges to the right values under the appropriate technical conditions: the one-step methods and Monte Carlo are the two ends of one family.

2n-step TD

Vt+n(St) ≐ Vt+n−1(St) + α[Gt:t+n − Vt+n−1(St)], 0 ≤ t < T

The update moves the estimate of St toward its n-step return, and leaves every other state as it was. The indices record timing:

  1. Gt:t+n needs Rt+n and St+n, which arrive at time t + n, so the state visited at time t is updated n steps later.
  2. Nothing is updated during the first n − 1 steps of an episode.
  3. To make up for it, the last n − 1 states are updated after the episode ends, with returns cut short by the end.

Here is the hallway episode with n = 2 and α = 0.5. At each time t, the state updated is the one from time τ = t − n + 1:

tThe robot seesUpdate
00, arrives in the hallwaynone yet
10, arrives in the studyV(kitchen): 2 → 3.43, toward 0.81 · 6 = 4.86
2+10, reaches the chargerV(hallway): 4 → 6.5, toward 0 + 0.9 · 10 = 9
3the episode is overV(study): 6 → 8, toward 10

One-step TD on the same episode moves the kitchen only from 2 to 2.8 and the hallway from 4 to 4.7, since their targets, 3.6 and 5.4, rest on the poor estimates of the next state. With two steps, the reward of +10 reaches two states back instead of one, so a single episode carries it twice as far.

The algorithm

For a policy π to evaluate, a step size α in (0, 1] and a positive integer n, and for each episode:

  1. Observe the first state S0, and set T ← ∞, since the end is not known yet.
  2. At each time t = 0, 1, 2, …, while t < T: take an action from π, and store the reward Rt+1 and the next state St+1. If St+1 is terminal, set T ← t + 1.
  3. Set τ ← t − n + 1, the time whose state is updated now. If τ ≥ 0, set G ← Σmin(τ+n, T)i=τ+1γi−τ−1Ri, the discounted rewards; if τ + n < T, add γnV(Sτ+n); then update V(Sτ) ← V(Sτ) + α[G − V(Sτ)].
  4. Stop when τ = T − 1, once the last state of the episode has been updated.

Only the last n + 1 states and rewards are ever needed, so they can be kept in a buffer indexed modulo n + 1.

The n-step error as a sum of TD errors

The one-step TD error measures how much one step changes an estimate:

δt = Rt+1 + γV(St+1) − V(St)

If the table does not change along the way, the error of the n-step update is the discounted sum of the one-step errors of its n steps, because the estimates in between cancel:

Gt:t+n − V(St) = δt + γδt+1 + ⋯ + γn−1δt+n−1

In the example, δ0 = 0 + 0.9 · 4 − 2 = 1.6 and δ1 = 0 + 0.9 · 6 − 4 = 1.4, and 1.6 + 0.9 · 1.4 = 2.86, which is 4.86 − 2. With the whole episode this becomes the Monte Carlo error, and eligibility traces are built on the same sum.

Choosing n

Which n learns fastest depends on the task. Short returns lean on estimates, which are poor early on; long ones add up many random rewards, which vary; an intermediate n often does better than either end. A larger n also separates two time scales that one-step methods tie together: an agent may need to choose an action every fraction of a second, while a meaningful change of state takes many such steps, and bootstrapping works best across a change that matters.

3n-step Sarsa

For control, the same idea works with action values: replace states by state–action pairs, and act ε-greedily with respect to Q. The n-step return ends in the estimate of the pair reached after n steps,

Gt:t+n = Rt+1 + γRt+2 + ⋯ + γn−1Rt+n + γnQt+n−1(St+n, At+n)

for n ≥ 1 and 0 ≤ t < T − n, and it is the full return Gt when t + n ≥ T. The update is the same as for states:

Qt+n(St, At) ≐ Qt+n−1(St, At) + α[Gt:t+n − Qt+n−1(St, At)]

With n = 1 this is one-step Sarsa, also called Sarsa(0). The gain shows most when rewards are rare. Take a robot crossing an open room to its charger, with reward 0 on every move except +1 on arrival, all action values 0 at first, γ = 1 and α = 0.5. Its first episode wanders for nine moves.

One-step Sarsa 4-step Sarsa +1 +1
The same first episode, from the start (circle) to the charger (+1). The thick arrows are the moves whose values this episode raises: the last one with one-step Sarsa, the last four with 4-step Sarsa.

During the episode every target is 0, since the rewards are 0 and the table is still all zeros, until the returns start to reach the charger. One-step Sarsa's only such return is that of the last move, so one value rises, to 0.5; with n = 4, the returns of the last four moves reach the charger, and four values rise to 0.5. When rewards are rare, one episode of n-step Sarsa leaves a trail of up to n moves behind a reward, instead of one. Here the next episode that passes through those cells finds a trail four moves long.

The figure runs 4-step Sarsa on the cliff of the TD note, with the same settings, α = 0.5, γ = 1 and ε = 0.1. Its marks:

  • a target's window of four moves is outlined in blue, from Sτ on;
  • the move picked after it, whose estimate the target relies on, is outlined in red.

The first moves show how the updates wait for four rewards.

Next goes forward, Back goes back. Keys: → and ←.

n-step Expected Sarsa

Expected Sarsa ends its return, instead, in the average value of the actions in St+n under the target policy π:

Gt:t+n = Rt+1 + ⋯ + γn−1Rt+n + γnVt+n−1(St+n), Vt(s) ≐ Σa π(a|s) Qt(s, a)

for t + n < T, and the full return otherwise. Vt(s), the expected approximate value of s, is 0 for a terminal state. The actions along the way are the ones actually taken; only the last step averages over all of them.

4Off-policy n-step learning

Off-policy learning learns the values of a target policy π from experience that follows a behavior policy b: often π is greedy with respect to Q and b is ε-greedy. One-step Q-learning needs no correction for the difference, but an n-step return does. Its rewards depend on the actions taken along the way, and b chose them, not π.

Importance sampling ratio: the relative probability of the actions taken under the two policies, π and b.

The correction weights the update by it:

ρt:h ≐ ∏min(h, T−1)k=tπ(Ak|Sk)b(Ak|Sk)
  • If π would never take one of those actions, the ratio is 0 and the return is ignored.
  • If π takes them more often than b does, the ratio is above 1 and the return counts more, which makes up for how rarely b produces what π would do.
  • When the two policies are the same, every factor is 1, and the updates below become the ones of sections 2 and 3.

For state values, the update of St is weighted by the ratio of the n actions At to At+n−1:

Vt+n(St) ≐ Vt+n−1(St) + αρt:t+n−1[Gt:t+n − Vt+n−1(St)]

For action values, the ratio starts and ends one step later:

Qt+n(St, At) ≐ Qt+n−1(St, At) + αρt+1:t+n[Gt:t+n − Qt+n−1(St, At)]

The pair being updated already contains its action At, so how likely b was to choose it does not matter: the value of (St, At) is about what happens after taking At. The actions after it, At+1 to At+n, are weighted, the last one included, since the return ends in its value Q(St+n, At+n).

For example, let π be greedy and b ε-greedy with ε = 0.2 and two actions, so that b takes the greedy action with probability 0.9. Each action contributes a factor, and the ratio is their product. In 3-step Sarsa:

GreedyExploring
Factor of one action1/0.9 ≈ 1.110/0.1 = 0
Ratio of the next three, ρt+1:t+31/0.93 ≈ 1.370 if any explores

Only stretches of greedy behavior teach about the greedy policy. A product of many ratios can swing widely, so these updates are noisier than on-policy ones.

5Summary

The key ideas next to their NumPy equivalents. The arrays s, a and r hold one episode, with r[i] for Ri, and v and q are the tables.

n-step return
Σmin(t+n, T)i=t+1γi−t−1Ri, plus γnV(St+n) if t + n < T
h = min(t + n, T)
w = gamma ** np.arange(h - t)
g = w @ r[t+1:h+1]
if h < T:
    g += gamma**n * v[s[h]]
The time being updated
τ = t − n + 1
tau = t - n + 1
n-step TD update
V(Sτ) ← V(Sτ) + α[G − V(Sτ)]
s_tau = s[tau]
v[s_tau] += alpha * (g - v[s_tau])
TD error
δt = Rt+1 + γV(St+1) − V(St)
delta = r[t+1] + gamma * v[s[t+1]]
delta -= v[s[t]]
n-step Sarsa return
the same rewards, + γnQ(St+n, At+n)
g += gamma**n * q[s[h], a[h]]
n-step Expected Sarsa return
the same rewards, + γnV(St+n)
g += gamma**n * pi[s[h]] @ q[s[h]]
Importance sampling ratio
ρt:h = ∏min(h, T−1)k=tπ(Ak|Sk) / b(Ak|Sk)
k = np.arange(t, min(h, T-1) + 1)
rho = np.prod(pi[s[k], a[k]]
              / b[s[k], a[k]])
Off-policy n-step Sarsa
Q(St, At) ← Q(St, At) + αρt+1:t+n[Gt:t+n − Q(St, At)]
sa = s[t], a[t]
q[sa] += alpha * rho * (g - q[sa])