ai theory

Reinforcement Learning Basics (10) — Connecting One Step to an Entire Episode

Junyoung Park · 2024-05-17 · 10 min

Introduction

Monte Carlo and TD(0) appeared to be two endpoints for learning a value.

TD(0)

It observes only one actual reward and immediately bootstraps.

Gt(1)=Rt+1+γV(St+1)G_t^{(1)} = R_{t+1} + \gamma V(S_{t+1})

Monte Carlo

It observes every actual reward through the terminal state.

Gt=Rt+1+γRt+2++γTt1RTG_t = R_{t+1} + \gamma R_{t+2} + \cdots + \gamma^{T-t-1}R_T

Must we choose only one of them?

We could observe two rewards before bootstrapping, or observe five. Once nn determines how much real experience to use, we can construct many targets between TD and Monte Carlo.

This article connects the two methods with the n-step return. It then introduces TD(λ\lambda), which blends returns of several lengths, and eligibility traces, which calculate the same idea online.

The n-Step Return

The nn-step return observes the next nn rewards and then bootstraps the rest of the future from the value of St+nS_{t+n}.

Gt(n)=Rt+1+γRt+2++γn1Rt+n+γnV(St+n)\begin{aligned} G_t^{(n)} =& R_{t+1} + \gamma R_{t+2} + \cdots \\ & + \gamma^{n-1}R_{t+n} + \gamma^n V(S_{t+n}) \end{aligned}

We can read the terms in temporal order.

  • The first reward is used as-is.
  • The second reward is multiplied by γ\gamma once.
  • The nnth reward is multiplied by γn1\gamma^{n-1}.
  • The rest of the future is folded into γnV(St+n)\gamma^n V(S_{t+n}).

Only the target in the update changes to Gt(n)G_t^{(n)}.

V(St)V(St)+α[Gt(n)V(St)]V(S_t) \leftarrow V(S_t) + \alpha \left[ G_t^{(n)} - V(S_t) \right]
As n grows, the target looks farther ahead at actual rewards and moves the bootstrap point later in time.

n=1 and n=∞

Substituting n=1n=1 gives

Gt(1)=Rt+1+γV(St+1),G_t^{(1)} = R_{t+1} + \gamma V(S_{t+1}),

which is the TD(0) target.

When nn extends all the way to the end of an episode, no value remains from which to bootstrap.

Gt()=GtG_t^{(\infty)} = G_t

This is the Monte Carlo return.

TD(0)n-step TDMonte Carlo\text{TD(0)} \longleftrightarrow \text{n-step TD} \longleftrightarrow \text{Monte Carlo}

nn is a control that balances how much we trust actual rewards against estimates.

Calculating a 3-Step Return

Suppose the next three rewards are

Rt+1=1,Rt+2=2,Rt+3=3,R_{t+1}=1, \quad R_{t+2}=2, \quad R_{t+3}=3,

and

V(St+3)=5,γ=0.9.V(S_{t+3})=5, \qquad \gamma=0.9.

The 3-step return is

Gt(3)=1+0.9×2+0.92×3+0.93×5=1+1.8+2.43+3.645=8.875.\begin{aligned} G_t^{(3)} &= 1 + 0.9\times2 + 0.9^2\times3 + 0.9^3\times5 \\ &= 1+1.8+2.43+3.645 \\ &= 8.875. \end{aligned}

The first three rewards were observed directly. For the future after the fourth step, we borrowed the current estimate V(St+3)=5V(S_{t+3})=5.

If the third reward were followed immediately by a terminal state, the final bootstrap term would be zero.

Gt(3)=1+1.8+2.43=5.23G_t^{(3)} = 1+1.8+2.43 = 5.23

Is a Larger n Always More Accurate?

As nn grows, the target uses more actual rewards, so bootstrapping bias tends to decrease. At the same time, it becomes exposed to the randomness in more actions, transitions, and rewards, so its variance may increase.

Target lengthActual rewardsBootstrapTypical tendency
Small nnUses fewUses earlyPotentially greater bias, lower variance
Large nnUses manyUses lateLess bias, higher variance
End of episodeUses allDoes not useMonte Carlo

The best nn depends on the environment and the accuracy of the current value estimate.

If the initial values are very inaccurate, TD(0) can quickly spread bad information by borrowing the estimate only one step ahead. Conversely, if episodes are long and rewards are noisy, a return with a large nn may fluctuate severely.

Can We Blend Several n-Step Returns?

Instead of choosing one nn, we can average returns of different lengths.

For example, an equal blend of the 2-step and 4-step returns is

12Gt(2)+12Gt(4).\frac{1}{2}G_t^{(2)} + \frac{1}{2}G_t^{(4)}.

TD(λ\lambda) takes a geometrically weighted average of every n-step return.

Gtλ=(1λ)n=1λn1Gt(n)G_t^\lambda = (1-\lambda) \sum_{n=1}^{\infty} \lambda^{n-1} G_t^{(n)}

The weight of the nnth return is

(1λ)λn1.(1-\lambda)\lambda^{n-1}.

For 0λ10\leq\lambda\leq1, a small λ\lambda concentrates weight on short returns. As λ\lambda approaches 11, longer returns receive more weight.

In an episodic task, the remaining weight is assigned to the final Monte Carlo return through the terminal state so that all weights sum to 11.

The Two Ends of λ

When λ=0\lambda=0, only the first return remains.

Gt0=Gt(1)G_t^0 = G_t^{(1)}

This is TD(0).

As λ\lambda approaches 11, most of the weight moves to returns that extend to the end of the episode, making the result resemble Monte Carlo.

λ=0short backup\lambda=0 \quad\Rightarrow\quad \text{short backup} λ1long backup\lambda\rightarrow1 \quad\Rightarrow\quad \text{long backup}

γ\gamma determines the temporal importance of future rewards. λ\lambda determines how much the learning target blends returns of different lengths. Their roles are distinct.

  • γ\gamma: How important are future rewards in the objective of the problem?
  • λ\lambda: How much should value learning use long backups?

The Forward View and Its Computational Problem

The formulation of TD(λ\lambda) above is called the forward view because it looks from the present into several n-step returns unfolding in the future.

Gt(1),Gt(2),Gt(3),G_t^{(1)}, G_t^{(2)}, G_t^{(3)}, \ldots

This is conceptually easy to understand, but calculating GtλG_t^\lambda exactly requires future rewards. We seem to lose the online advantage of TD because we must wait until the episode ends.

The backward view calculates the same effect in the opposite temporal direction. It distributes the TD error that occurs now among recently visited states.

This is where eligibility traces enter the picture.

An Eligibility Trace Is a Mark Left on Past States

For every state ss, we maintain a trace Et(s)E_t(s). Initially, every trace is zero.

E0(s)=0E_0(s)=0

When state StS_t is visited at time tt, we add 11 to that state's trace and decay every earlier trace by γλ\gamma\lambda.

Et(s)=γλEt1(s)+1(St=s)E_t(s) = \gamma\lambda E_{t-1}(s) + \mathbf{1}(S_t=s)

1(St=s)\mathbf{1}(S_t=s) is an indicator function.

  • It is 11 when the current state is ss.
  • It is 00 otherwise.

A state visited just now has a large trace, while a state visited long ago gradually receives a smaller one. Repeated visits to the same state accumulate in its trace.

Eligibility traces assign more responsibility for the current TD error to states visited recently and frequently.

David Silver's lecture describes this as a combination of recency and frequency heuristics.

  • A state visited more recently has a larger trace.
  • A state visited more frequently has its trace increased several times.

Sending the TD Error to Every Trace

The TD error for the current step is the same as in TD(0).

δt=Rt+1+γV(St+1)V(St)\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t)

Instead of updating only the current state, however, we update every state in proportion to its trace.

V(s)V(s)+αδtEt(s)sV(s) \leftarrow V(s) + \alpha \delta_t E_t(s) \qquad \forall s

For example, suppose the current error is δt=2\delta_t=2 and three states have traces

Et(A)=0.1,Et(B)=0.4,Et(C)=1.0.E_t(A)=0.1, \quad E_t(B)=0.4, \quad E_t(C)=1.0.

With α=0.1\alpha=0.1, the update sizes are

ΔV(A)=0.1×2×0.1=0.02,\Delta V(A)=0.1\times2\times0.1=0.02, ΔV(B)=0.1×2×0.4=0.08,\Delta V(B)=0.1\times2\times0.4=0.08, ΔV(C)=0.1×2×1.0=0.2.\Delta V(C)=0.1\times2\times1.0=0.2.

State C, which is closest to the current state, changes the most. The older state A changes only a little.

The Credit-Assignment Problem

When a large reward appears at the end of an episode, which past states and actions deserve credit for it?

TD(0) propagates reward information backward one step at a time. Reaching a distant state may take several episodes.

With eligibility traces, the current TD error is delivered to several recently visited states at once. Credit can spread more quickly in an environment where rewards arrive late.

If λ\lambda is too large, however, many old states are affected by one update and variance may increase. If it is too small, credit moves slowly, as in TD(0).

Thus λ\lambda is another hyperparameter that balances bias, variance, and the speed of credit propagation.

Accumulating and Replacing Traces

The equation above adds 11 whenever a state is visited. This is an accumulating trace.

Et(s)=γλEt1(s)+1if St=sE_t(s) = \gamma\lambda E_{t-1}(s) + 1 \qquad \text{if }S_t=s

If the same state is visited repeatedly within a short period, its trace can exceed 11.

A replacing trace sets the visited state's trace to 11.

Et(s)=1if St=sE_t(s)=1 \qquad \text{if }S_t=s

The traces of every other state continue to decay by γλ\gamma\lambda.

The two methods differ in how strongly they represent repeated visits. It is enough to understand the main structure of TD(λ\lambda) first and distinguish these variants later, during implementation.

Why the Forward and Backward Views Agree

The forward view updates the current state with a weighted average of several n-step returns.

The backward view distributes the TD error from every step to past states through eligibility traces.

They appear to point in opposite directions, but under the appropriate conditions, the updates accumulated over an entire episode produce the same result.

ViewDirectionRole
Forward viewPresent to futureExplains the meaning of the TD(λ\lambda) target
Backward viewPresent to pastComputes it online with eligibility traces

In the wording of David Silver's lecture, the forward view provides the theory and the backward view provides the mechanism.

Extending the Idea to SARSA(λ)

Extending a state-value trace to a state-action pair gives SARSA(λ\lambda).

Et(s,a)=γλEt1(s,a)+1(St=s,At=a)E_t(s,a) = \gamma\lambda E_{t-1}(s,a) + \mathbf{1}(S_t=s,A_t=a)

The SARSA TD error is

δt=Rt+1+γQ(St+1,At+1)Q(St,At),\delta_t = R_{t+1} + \gamma Q(S_{t+1},A_{t+1}) - Q(S_t,A_t),

and every state-action value is updated in proportion to its trace.

Q(s,a)Q(s,a)+αδtEt(s,a)Q(s,a) \leftarrow Q(s,a) + \alpha\delta_tE_t(s,a)

The TD error from the current experience is delivered to several state-action pairs visited recently.

What to Remember

The central idea of this article is

n-step TD controls how many actual rewards to observe, TD(λ\lambda) blends returns of several lengths, and eligibility traces calculate the same effect online in the backward direction.

More specifically:

  1. The nn-step return observes nn rewards and then bootstraps from V(St+n)V(S_{t+n}).
  2. n=1n=1 gives TD(0), while extending to the end of an episode gives Monte Carlo.
  3. A small nn tends to have lower variance, while a large nn tends to have less bootstrapping bias.
  4. The λ\lambda-return is a weighted average of all n-step returns.
  5. An eligibility trace gives more credit to states visited recently and frequently.
  6. The forward view explains the meaning; the backward view shows how to compute it online.

This completes the basic flow of model-free reinforcement learning when values are stored in a table. If a state is an image or a continuous robot pose, however, there are too many states to store every V(s)V(s) or Q(s,a)Q(s,a) in its own entry.

The next article replaces the value table with a parameterized function and introduces value function approximation. This is where linear functions and neural networks enter reinforcement learning.

References