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.
Monte Carlo
It observes every actual reward through the terminal state.
Must we choose only one of them?
We could observe two rewards before bootstrapping, or observe five. Once 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(), which blends returns of several lengths, and eligibility traces, which calculate the same idea online.
The n-Step Return
The -step return observes the next rewards and then bootstraps the rest of the future from the value of .
We can read the terms in temporal order.
- The first reward is used as-is.
- The second reward is multiplied by once.
- The th reward is multiplied by .
- The rest of the future is folded into .
Only the target in the update changes to .
n=1 and n=∞
Substituting gives
which is the TD(0) target.
When extends all the way to the end of an episode, no value remains from which to bootstrap.
This is the Monte Carlo return.
is a control that balances how much we trust actual rewards against estimates.
Calculating a 3-Step Return
Suppose the next three rewards are
and
The 3-step return is
The first three rewards were observed directly. For the future after the fourth step, we borrowed the current estimate .
If the third reward were followed immediately by a terminal state, the final bootstrap term would be zero.
Is a Larger n Always More Accurate?
As 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 length | Actual rewards | Bootstrap | Typical tendency |
|---|---|---|---|
| Small | Uses few | Uses early | Potentially greater bias, lower variance |
| Large | Uses many | Uses late | Less bias, higher variance |
| End of episode | Uses all | Does not use | Monte Carlo |
The best 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 may fluctuate severely.
Can We Blend Several n-Step Returns?
Instead of choosing one , we can average returns of different lengths.
For example, an equal blend of the 2-step and 4-step returns is
TD() takes a geometrically weighted average of every n-step return.
The weight of the th return is
For , a small concentrates weight on short returns. As approaches , 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 .
The Two Ends of λ
When , only the first return remains.
This is TD(0).
As approaches , most of the weight moves to returns that extend to the end of the episode, making the result resemble Monte Carlo.
determines the temporal importance of future rewards. determines how much the learning target blends returns of different lengths. Their roles are distinct.
- : How important are future rewards in the objective of the problem?
- : How much should value learning use long backups?
The Forward View and Its Computational Problem
The formulation of TD() above is called the forward view because it looks from the present into several n-step returns unfolding in the future.
This is conceptually easy to understand, but calculating 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 , we maintain a trace . Initially, every trace is zero.
When state is visited at time , we add to that state's trace and decay every earlier trace by .
is an indicator function.
- It is when the current state is .
- It is 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.
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).
Instead of updating only the current state, however, we update every state in proportion to its trace.
For example, suppose the current error is and three states have traces
With , the update sizes are
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 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 is another hyperparameter that balances bias, variance, and the speed of credit propagation.
Accumulating and Replacing Traces
The equation above adds whenever a state is visited. This is an accumulating trace.
If the same state is visited repeatedly within a short period, its trace can exceed .
A replacing trace sets the visited state's trace to .
The traces of every other state continue to decay by .
The two methods differ in how strongly they represent repeated visits. It is enough to understand the main structure of TD() 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.
| View | Direction | Role |
|---|---|---|
| Forward view | Present to future | Explains the meaning of the TD() target |
| Backward view | Present to past | Computes 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().
The SARSA TD error is
and every state-action value is updated in proportion to its trace.
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() blends returns of several lengths, and eligibility traces calculate the same effect online in the backward direction.
More specifically:
- The -step return observes rewards and then bootstraps from .
- gives TD(0), while extending to the end of an episode gives Monte Carlo.
- A small tends to have lower variance, while a large tends to have less bootstrapping bias.
- The -return is a weighted average of all n-step returns.
- An eligibility trace gives more credit to states visited recently and frequently.
- 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 or 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.