Search

Search papers and lessons

Temporal-Difference Learning

Level 3 · 10 min read

Temporal-difference learning updates a value estimate using the next step's estimate instead of waiting for the final outcome, so an agent can learn while it is still running.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

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 α\alpha, the target from kk updates ago carries weight α(1−α)k\alpha(1 - \alpha)^k.

00.20.40.60.8100.20.40.60.81step size αweight on the target
  • newest target
  • one update ago
  • two updates ago
How much each past target counts in the current estimate depends on the step size. A large step forgets old targets quickly; a small step averages over many of them.

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 π\pi, and we want the state-value function vπ(s)v_\pi(s) from the value function lesson, which is the expected discounted return from state ss. The discount factor is γ\gamma with 0≤γ≤10 \le \gamma \le 1. We write V(s)V(s) for the agent's current estimate of vπ(s)v_\pi(s).

The TD(0) update

After the agent moves from state StS_t to St+1S_{t+1} and receives reward Rt+1R_{t+1}, the TD error is

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

and the update is

V(St)←V(St)+α δt.V(S_t) \leftarrow V(S_t) + \alpha\, \delta_t.

Here α∈(0,1]\alpha \in (0, 1] is the step size. The quantity Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1}) 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 V(St)V(S_t).

Why it converges to the right answer

Compare with the Bellman expectation equation from the Value Function lesson:

vπ(s)=Eπ[Rt+1+γvπ(St+1)∣St=s].v_\pi(s) = \mathbb{E}_\pi\left[ R_{t+1} + \gamma v_\pi(S_{t+1}) \mid S_t = s \right].

The TD target is a sample of the right-hand side, with VV standing in for vπv_\pi. 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 ∑α=∞\sum \alpha = \infty and ∑α2<∞\sum \alpha^2 \lt \infty, the updates converge to vπv_\pi. 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 GtG_t and updates V(St)←V(St)+α(Gt−V(St))V(S_t) \leftarrow V(S_t) + \alpha(G_t - V(S_t)). 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 VV is wrong, because it leans on V(St+1)V(S_{t+1}). 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 γ=0.9\gamma = 0.9 and α=0.5\alpha = 0.5. Start with V(A)=V(B)=0V(A) = V(B) = 0.

The true values under this fixed policy are v(B)=1v(B) = 1 and v(A)=0.9×1=0.9v(A) = 0.9 \times 1 = 0.9, because A's only reward is paid through B.

Example
Three episodes of TD(0)

Episode 1.

  1. At A, the next state is B and the reward is 0. Then δ=0+0.9⋅V(B)−V(A)=0+0−0=0\delta = 0 + 0.9 \cdot V(B) - V(A) = 0 + 0 - 0 = 0. So V(A)V(A) stays 0.
  2. At B, the episode ends with reward 1. Then δ=1+0−V(B)=1\delta = 1 + 0 - V(B) = 1, and V(B)←0+0.5⋅1=0.5V(B) \leftarrow 0 + 0.5 \cdot 1 = 0.5.

Episode 2.

  1. At A: δ=0+0.9⋅0.5−0=0.45\delta = 0 + 0.9 \cdot 0.5 - 0 = 0.45, so V(A)←0+0.5⋅0.45=0.225V(A) \leftarrow 0 + 0.5 \cdot 0.45 = 0.225.
  2. At B: δ=1−0.5=0.5\delta = 1 - 0.5 = 0.5, so V(B)←0.5+0.5⋅0.5=0.75V(B) \leftarrow 0.5 + 0.5 \cdot 0.5 = 0.75.

Episode 3.

  1. At A: δ=0+0.9⋅0.75−0.225=0.45\delta = 0 + 0.9 \cdot 0.75 - 0.225 = 0.45, so V(A)←0.225+0.5⋅0.45=0.45V(A) \leftarrow 0.225 + 0.5 \cdot 0.45 = 0.45.
  2. At B: δ=1−0.75=0.25\delta = 1 - 0.75 = 0.25, so V(B)←0.75+0.5⋅0.25=0.875V(B) \leftarrow 0.75 + 0.5 \cdot 0.25 = 0.875.

Interpretation: after three episodes the estimates are V(A)=0.45V(A) = 0.45 and V(B)=0.875V(B) = 0.875, moving toward the true values 0.9 and 1. Notice that V(A)V(A) stayed at 0 in episode 1, because V(B)V(B) was still 0 when A was updated. From episode 2 on, V(A)V(A) rises using only the current estimate V(B)V(B) 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 α=0.1\alpha = 0.1, and no discounting (γ=1\gamma = 1), with the cost of each step (minutes spent) playing the role of the reward. At the office the estimate is V(office)=30V(\text{office}) = 30. The drive to the car takes 5 minutes, and the estimate at the car is V(car)=28V(\text{car}) = 28.

Example
Reading a TD error
  1. The target is the time spent on this step plus the estimate for the next state: 5+28=335 + 28 = 33.
  2. The current estimate for the office is 30.
  3. The TD error is δ=33−30=3\delta = 33 - 30 = 3.
  4. The update is V(office)←30+0.1⋅3=30.3V(\text{office}) \leftarrow 30 + 0.1 \cdot 3 = 30.3.

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.

SituationWhat goes wrongBetter choice
Episodes are very long or never endMonte Carlo must wait for the whole return, so learning is slow or impossibleTD(0), which updates every step
Value estimates are poor early onBootstrapping spreads the error from wrong estimates into their neighboursMulti-step returns, or TD(λ), which blend short and long targets
Function approximation with off-policy dataThe updates can diverge, a failure known as the deadly triadGradient-based TD methods, or target networks as in Deep Q-Network
Rewards are very noisyTD targets still vary, so estimates jitterSmaller step sizes, or averaging with Experience Replay
Step size is too largeEstimates chase the latest target and oscillateDecrease α\alpha over time, or tune it on a validation task
Only the final result matters and the episode is shortTD adds bias for little gainMonte 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 γ=0.9\gamma = 0.9, α=0.5\alpha = 0.5, V(A)=0V(A) = 0, V(B)=0.5V(B) = 0.5, a transition A to B gives reward 0. What is the TD update for V(A)V(A)?Show answer

The TD error is δ=0+0.9⋅0.5−0=0.45\delta = 0 + 0.9 \cdot 0.5 - 0 = 0.45. The update is V(A)←0+0.5⋅0.45=0.225V(A) \leftarrow 0 + 0.5 \cdot 0.45 = 0.225.

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 GtG_t, 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 kk updates ago is α(1−α)k\alpha(1-\alpha)^k, which is almost zero for old targets when α\alpha is that large.

Sources