TL;DR
Temporal-difference (TD) learning estimates how good each state is by updating the estimate after every step. The update uses the reward just received plus the current estimate of the next state, instead of waiting until the episode ends to see the total. Because the estimate is built on other estimates, the method is called bootstrapping. The payoff is that an agent can learn while it is still acting, in tasks that never end or take a long time to finish.
Where you'll see it: in the value updates inside Q-Learning, in the critics of actor-critic methods, and in the advantage estimates used by GAE when training game-playing and robotics agents.
Intuition
Predicting the trip while you drive
Suppose you want to predict how long your drive home takes. You leave the office and guess 30 minutes. Five minutes later you reach your car and notice the traffic is worse than usual. You now guess the remaining trip takes 28 minutes, not the 25 you assumed. Should you wait until you are home to correct your first guess? That is the Monte Carlo approach: remember the whole trip and fix every earlier guess at the end.
Temporal-difference learning does something more immediate. The moment you reach the car, you compare the guess you made at the office with what the car now tells you, and you nudge the office guess toward the new information. The car's guess stands in for the rest of the trip, and the car's guess is itself a rough estimate that will improve later.
That is the core idea: learn from the difference between successive predictions. The prediction at one time is adjusted using the prediction at the next time.
An average that moves
Each update has the form new estimate equals old estimate plus a step toward a target. This is the same shape as the exponential moving average. If the target were fixed, repeated updates would make the estimate an average of past targets with the most recent ones weighted most. With step size , the target from updates ago carries weight .
- newest target
- one update ago
- two updates ago
A small step size gives a steady, slow average. A large step size chases the latest target and is noisy. Most of the practical work in TD learning is choosing that step size and keeping the targets from changing too much at once.
The math
Setting
The environment is a Markov decision process. The agent follows a fixed policy , and we want the state-value function from the value function lesson, which is the expected discounted return from state . The discount factor is with . We write for the agent's current estimate of .
The TD(0) update
After the agent moves from state to and receives reward , the TD error is
and the update is
Here is the step size. The quantity is the target. It is a one-step estimate of the return: the reward seen now, plus the discounted estimate of everything after. The TD error is the gap between that target and the current estimate .
Why it converges to the right answer
Compare with the Bellman expectation equation from the Value Function lesson:
The TD target is a sample of the right-hand side, with standing in for . If the expected TD error is zero for every state, the estimates satisfy the Bellman equation and are correct. Under the standard conditions for stochastic approximation, namely that every state is visited infinitely often and the step sizes shrink with and , the updates converge to . Those conditions are sufficient, not something to check by hand in practice.
Bootstrapping versus Monte Carlo
The Monte Carlo alternative waits for the full return and updates . Both methods move the estimate toward a target. The difference is the target. The Monte Carlo target is unbiased but has high variance, because it sums many random rewards. The TD target has low variance, since it uses one reward, but it is biased whenever is wrong, because it leans on . This tradeoff between bias and variance is the central design choice in reinforcement learning, and it sits in the same family of tradeoffs as the Bias-Variance Tradeoff in supervised learning.
Worked examples
Example 1: a two-state chain by hand
Two states, A then B, then the episode ends. The transition A to B gives reward 0. The transition B to the end gives reward 1. Use and . Start with .
The true values under this fixed policy are and , because A's only reward is paid through B.
Episode 1.
- At A, the next state is B and the reward is 0. Then . So stays 0.
- At B, the episode ends with reward 1. Then , and .
Episode 2.
- At A: , so .
- At B: , so .
Episode 3.
- At A: , so .
- At B: , so .
Interpretation: after three episodes the estimates are and , moving toward the true values 0.9 and 1. Notice that stayed at 0 in episode 1, because was still 0 when A was updated. From episode 2 on, rises using only the current estimate and no sampled return for A. Each A update uses a single transition, and the information about the final reward travels backwards one state per episode.
Example 2: a TD error as a surprise
Now use the drive home, with values measured as expected remaining minutes, step size , and no discounting (), with the cost of each step (minutes spent) playing the role of the reward. At the office the estimate is . The drive to the car takes 5 minutes, and the estimate at the car is .
- The target is the time spent on this step plus the estimate for the next state: .
- The current estimate for the office is 30.
- The TD error is .
- The update is .
Interpretation: the step was three minutes worse than the office estimate expected. The office estimate moves up by only 0.3 minutes, because the step size is small. A single surprise should not rewrite the whole picture. Over many trips the estimate settles on the average.
Tradeoffs and failure modes
TD learning is the default for reinforcement learning, but its behaviour depends on the setting.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Episodes are very long or never end | Monte Carlo must wait for the whole return, so learning is slow or impossible | TD(0), which updates every step |
| Value estimates are poor early on | Bootstrapping spreads the error from wrong estimates into their neighbours | Multi-step returns, or TD(λ), which blend short and long targets |
| Function approximation with off-policy data | The updates can diverge, a failure known as the deadly triad | Gradient-based TD methods, or target networks as in Deep Q-Network |
| Rewards are very noisy | TD targets still vary, so estimates jitter | Smaller step sizes, or averaging with Experience Replay |
| Step size is too large | Estimates chase the latest target and oscillate | Decrease over time, or tune it on a validation task |
| Only the final result matters and the episode is short | TD adds bias for little gain | Monte Carlo is simpler and unbiased |
The deadly triad is the one to remember. TD learning, function approximation, and off-policy updates together can make values diverge. Each of the three is fine alone. Combining them needs care, and most of the modern techniques in deep reinforcement learning exist to manage that combination.
History
The idea of learning a prediction from the difference between successive predictions goes back to the 1950s. Arthur Samuel's checkers program in 1959 used a form of it to adjust evaluations of board positions from later positions, long before the modern framework existed.
Richard Sutton formalised the method in 1988 in the paper "Learning to predict by the methods of temporal differences", which defined the TD family of prediction methods. Sutton and Andrew Barto then built the field around it in their textbook, which made the TD error and the bootstrapping picture standard. Gerald Tesauro's TD-Gammon in the early 1990s showed that a neural network trained with TD updates from self-play could reach strong backgammon play. That result is part of why the method caught on, since it worked on a hard problem with a learned evaluation function.
TD learning spread because it is simple, uses one step of data at a time, and works without a model of the environment. Those properties still make it the first thing to try when an agent has to learn from experience.
Check yourself
QuestionIn one sentence, what is the difference between a TD target and a Monte Carlo target?Show answer
The TD target is the one-step reward plus the discounted estimate of the next state, so it uses a single real reward and the current estimate. The Monte Carlo target is the full sampled return to the end of the episode. TD targets have lower variance but are biased when the estimates are wrong; Monte Carlo targets are unbiased but noisier.
QuestionWith , , , , a transition A to B gives reward 0. What is the TD update for ?Show answer
The TD error is . The update is .
QuestionWhy does TD(0) work when episodes never end, but Monte Carlo does not?Show answer
TD(0) updates after every step using the reward just received and the current estimate of the next state, so it never needs the episode to finish. Monte Carlo needs the full return , which is only defined once the episode reaches an end. In a task with no end, Monte Carlo has no complete return to learn from.
QuestionAn agent with a fixed step size of 0.9 learns values in a noisy environment and its estimates keep swinging. Why, and what would you change?Show answer
A step size of 0.9 means each update moves 90 percent of the way toward the newest target, so the estimate mostly reflects the last noisy sample. Lower the step size, so the update averages over many targets, or decrease it over time. The weight on a target from updates ago is , which is almost zero for old targets when is that large.
Sources
- Sutton and Barto, Reinforcement Learning: An Introduction, 2nd ed., Chapter 6. incompleteideas.net
- Temporal difference learning, Wikipedia. en.wikipedia.org/wiki/Temporal_difference_learning