ai theory
Reinforcement Learning Basics (7) — TD Learning Without Waiting for the End
Junyoung Park · 2024-04-26 · 8 min
Introduction
Monte Carlo used a fairly trustworthy target. It ran an episode to completion and then calculated the return that was actually received.
But this creates a problem if one game takes an hour or if the environment never ends. To learn whether the first action was good, the agent must wait an entire hour. A continuously operating environment, such as a temperature-control system, may have no terminal state at all.
Temporal-difference learning, or TD, learns immediately after every step.
Because the return for the entire episode is not yet known, TD borrows the current estimate for the future after the next state.
It combines one actual reward with one estimate to correct the current estimate. In the wording of David Silver's lecture, it “updates a guess toward another guess.”
The Targets of Monte Carlo and TD
Placing the two methods side by side makes the difference clear.
Monte Carlo
The target is the actual return, which becomes available after the episode ends.
TD(0)
The target combines the reward just received with the current value estimate of the next state.
The after TD distinguishes this method from n-step TD and TD(), which we will see later. For now, think of TD(0) as the shortest TD method: it observes one actual step and immediately bootstraps the rest.
Bootstrapping Corrects One Estimate with Another
The TD target contains . This value is not yet a correct answer; it is another estimate that is still being learned.
Replacing the unobserved future after the next state with an existing estimate is called bootstrapping.
The name may be unfamiliar, but the same pattern appears in everyday reasoning. Suppose the first part of a commute actually took 20 minutes, and the remaining trip from that point usually takes 15 minutes. We can revise the estimated total commute to 35 minutes even before arriving at work. We combined an intermediate observation with an estimate of what remains.
Borrowing the next state's estimate can seem unreliable when the current estimates are wrong. Indeed, a TD target can be biased. But as experience accumulates, the next state's estimate is also corrected, and that information propagates to earlier states.
TD Error Measures How Surprising the Outcome Was
The difference between the TD target and the current value is called the TD error and is written as .
The update becomes much shorter.
Let us substitute some numbers.
- Current estimate:
- Reward received:
- Next-state estimate:
- Discount factor:
- Learning rate:
The TD target is
Its difference from the current estimate of is
The final update is
If , the target is smaller than the current estimate and the value decreases. We can interpret the TD error as “how surprisingly the expectation of the future changed after the most recent step.”
How Can One Step Teach Us About the Distant Future?
At first, TD can look too hasty. If the next state's value is only an estimate, how does information from a terminal reward ever reach a distant state?
Consider a short chain.
Suppose every initial value is zero.
- Moving from to the terminal state reveals , so increases.
- In the next episode, moving from to uses the now-higher as a target, so increases.
- Later, moving from to borrows , so increases.
Reward information propagates backward one step at a time along actual experience. This resembles the repeated backups in dynamic programming, but rather than averaging every possible next state according to its probability, TD uses one transition that was actually observed.
The Bias–Variance Difference Between Monte Carlo and TD
Neither Monte Carlo nor TD is always superior.
Monte Carlo Target
Because it uses the actual return, it has no bias from the current value estimate. But it is affected by the randomness of many actions, transitions, and rewards, so its variance is high.
TD Target
Because it uses the current estimate , it can be biased. It passes through only one step of random outcomes, however, so its variance is generally lower.
| Property | Monte Carlo | TD(0) |
|---|---|---|
| Update time | After the episode | Every step |
| Target | Actual return | |
| Bootstrapping | No | Yes |
| Episodic task | Supported | Supported |
| Continuing task | Difficult as-is | Supported |
| Typical tendency | Low bias, high variance | Possible bias, low variance |
In practice, this trade-off affects the speed and stability of learning.
Viewing DP, Monte Carlo, and TD Through Sampling and Bootstrapping
Two questions organize the relationship between the three methods.
- Does the method average over every possible next state, or use one actual transition as a sample?
- Does it use the actual return through the end, or bootstrap from an estimate?
| Method | Sampling | Bootstrapping | Requires a model |
|---|---|---|---|
| Dynamic programming | No | Yes | Yes |
| Monte Carlo | Yes | No | No |
| Temporal difference | Yes | Yes | No |
Dynamic programming uses a model to calculate the expectation over every outcome. Monte Carlo samples an entire episode and observes it to the end. TD samples one actual transition and folds everything after the next state into an estimate.
Remembering this table makes it easier to identify what later algorithms approximate.
Why Online Learning Is Possible
TD can update after every step, before the episode ends.
These three pieces of information and the current value table are enough. There is no need to keep the entire episode in memory.
This is particularly useful in
- games with extremely long episodes,
- process-control systems that never end,
- systems that must revise state evaluations in real time, and
- environments where episodes are cut off midway or only partial experience is collected.
If Monte Carlo watches an entire film through the ending before revising its rating, TD watches one scene and makes a small revision by comparing it with the previous expectation of the next scene.
From TD Prediction to TD Control
So far, we have estimated the state value of a fixed policy .
To find a good policy, we need to learn action values and improve the policy.
Replacing in the state-value TD update with gives
The central question in the next article is which Q-value should enter the target.
- SARSA: for the next action actually selected
- Q-Learning: the largest value among the possible next actions,
The updates look the same, but they borrow different futures.
What to Remember
The central idea of this article is
TD learns after every step with a target that combines one actual reward and the value estimate of the next state.
More specifically:
- The TD target is .
- Correcting one estimate with another is called bootstrapping.
- The TD error is the difference between the target and the current prediction.
- TD can update online without waiting for the end of an episode.
- It can be more biased than Monte Carlo, but generally has lower variance.
- TD uses both sampling and bootstrapping.
The next article extends TD to a control problem. We will compare the equations for SARSA and Q-Learning and see how the next action whose value enters the target determines whether learning is on-policy or off-policy.