Temporal-difference learning
Temporal-difference (TD) learning combines the two previous ideas. Like Monte Carlo it learns from raw experience with no model. Like dynamic programming it bootstraps, updating a guess from other guesses, so it can learn after every single step. This page covers TD prediction, then the two most widely used control algorithms, Sarsa and Q-learning, and runs them side by side.
01TD prediction
Monte Carlo moves V(St) toward the full return Gt, which is known only at the end of the episode. TD(0) uses the recursive form of the return, Gt = Rt+1 + γGt+1, and substitutes the current estimate V(St+1) for the unknown Gt+1:
The bracket is the TD error, δt = Rt+1 + γV(St+1) − V(St): the difference between what one step of experience suggests and what was predicted. If V does not change during an episode, the Monte Carlo error is exactly a discounted sum of TD errors, Gt − V(St) = Σk≥t γk−t δk, so TD spreads Monte Carlo's single end-of-episode correction across the individual steps.
TD(0) combines sampling, like Monte Carlo (it uses one sampled next state, not an expectation over all of them), with bootstrapping, like DP (its target contains an estimate). For a fixed policy it converges to vπ in the tabular case: in the mean for a small enough constant α, and with probability 1 if α decreases appropriately. The TD vs MC page compares it with Monte Carlo update by update on the random walk.
02Why TD is useful
- No model needed, unlike DP.
- Fully online: it updates after each step, so it works on long episodes and on continuing tasks that never end, where Monte Carlo has nothing to wait for.
- Usually faster than constant-α Monte Carlo in practice, because its targets have lower variance. There is no general proof of this, but it holds on most stochastic tasks studied.
- Exploits the Markov property: in batch training it converges to the certainty-equivalence estimate, the exact values of the maximum-likelihood model of the data.
03Sarsa: on-policy TD control
For control, learn action values and apply the same TD update to state–action pairs. After each transition from a non-terminal state, use the next action At+1 that the current policy actually chooses:
The name comes from the quintuple each update uses: (St, At, Rt+1, St+1, At+1). The policy is ε-greedy with respect to Q, so evaluation and improvement happen together at every step: GPI at the finest grain. Sarsa learns the value of the policy it follows, including its exploration. Q(terminal, ·) = 0.
04Q-learning: off-policy TD control
Q-learning replaces the next action's value with the best next action's value:
Its target is the Bellman optimality backup, sampled. So Q approximates q* directly, whatever exploratory policy is used to choose actions, as long as every pair keeps being visited. It is off-policy, but needs no importance sampling, because the target doesn't depend on which action the behaviour policy takes next.
05Expected Sarsa
A middle ground: instead of the sampled next action, use the expected value of the next action under the current policy. This removes the variance from the random choice of At+1. With a greedy target policy it is exactly Q-learning.
06Worked example: cliff walking
This is Sutton & Barto's Example 6.6. On a 4×12 grid the agent starts at S in the bottom-left corner and must reach G in the bottom-right. Every move costs −1. The cells between them along the bottom edge are a cliff: stepping in gives −100 and sends the agent back to S. The shortest path runs right along the cliff edge (13 steps, return −13). Both agents below use ε-greedy with the same ε and α and γ = 1, and step in lockstep. They don't share experience, since each follows its own policy.
Sarsa and Q-learning on the cliff
Sarsa
Q-learning
Arrows: the greedy action in each cell. Shading: maxa Q(s, a), darker is worse. Amber box: the state–action value just updated. White outline: the path the greedy policy would take from S. With a constant α the values keep fluctuating, so the greedy path occasionally loops back on itself; the outline then stops at the loop.
Sum of rewards per episode (this run, smoothed over 10 episodes)
What to try
- Step through the first few moves. Every value starts at 0, so the first updates are −1 × α, and the arrows point wherever ties were broken.
- Watch the update panels when the next action is exploratory. Sarsa's target uses the action it will actually take, even a bad one; Q-learning's target uses the best one, whatever happens next.
- Run +500 episodes. Q-learning's greedy path hugs the cliff, the optimal route. Sarsa's takes a safer route, a row or two further from the edge. Sarsa learns that with ε = 0.1 it will sometimes stumble, so it stays away from the edge.
- Compare the per-episode returns: Sarsa earns more while learning even though Q-learning has the better greedy policy. Q-learning walking the edge with ε-greedy regularly falls off. Set ε to 0 after training and both would walk their greedy paths with no falls.
07Averaged over runs
One run is noisy. The chart below averages 50 independent runs of 500 episodes each, with ε = 0.1 and the α from the slider above, and adds Expected Sarsa.
Online performance, 50 runs
Sum of rewards per episode, averaged over 50 runs and smoothed over 10 episodes
Q-learning learns the values of the optimal policy, but because it acts ε-greedily it pays for occasional falls, so its online return is lower. Sarsa optimises the policy it actually runs, exploration included. If ε is reduced over time, both converge to the optimal path. Expected Sarsa learns the same safe path as Sarsa but its targets are less noisy, so it tolerates larger step sizes and usually does at least as well.
08Maximisation bias and Double Q-learning
Q-learning's target takes a maximum over noisy estimates. The max of noisy estimates is biased upward, even when every estimate is individually unbiased, so Q-learning can overrate actions whose rewards are merely noisy. Double Q-learning keeps two independent estimates, Q1 and Q2. One chooses the best action and the other evaluates it, which removes the bias. On each step one of the two is picked at random to update:
The same idea underlies Double DQN in deep reinforcement learning.
09Where TD sits
TD methods are the core of modern reinforcement learning: Q-learning and its deep variant DQN, actor-critic methods that use the TD error as a learning signal, and the n-step and TD(λ) methods that interpolate between TD(0) and Monte Carlo. For a side-by-side comparison of TD and Monte Carlo prediction on the same episodes, including bias, variance and the n-step bridge, see the next page.