Home/Reinforcement Learning

Temporal Difference Learning

Bootstrap value estimates from partial trajectories — no model, no completed episodes required. Q-Learning and SARSA are TD at heart, and DQN is TD with a neural network.

Q-Learning Controls

0.10
0.90
0.10

4×4 Grid World — Q-Value Heatmap

Colour = max Q per state (green = high). White arrows show greedy policy. Agent (circle) learns to reach G.

Metrics

Episodes0
Steps0
Total Reward0.0
Max Q0.00

Temporal Difference Learning: The Engine of Modern RL

Learn online, one step at a time — no model, no complete episodes.

The TD prediction problem

In dynamic programming (DP), we update values using the Bellman equation and a known model. In Monte Carlo (MC), we wait until an episode ends, then update using the actual return Gt. Temporal Difference combines both ideas: update at every step using a bootstrapped estimate of the return.

The key insight: we don’t need to wait until the end of an episode. We can use the value estimate of the next state as a proxy for the future return — just like the Bellman equation, but learned online from experience.

TD(0) update rule

The simplest TD algorithm updates the value function after each (s, a, r, s′) transition:

V(s) ← V(s) + α [r + γ V(s′) − V(s)]

The bracketed term is called the TD error δt: it measures how much the current estimate V(s) differs from the bootstrapped target r + γV(s′). The learning rate α controls how fast we move toward the target. When δt = 0, V(s) is already consistent with the observed transition — no update needed.

TD vs Monte Carlo vs DP

MethodNeeds model?Needs full episode?Bootstraps?
Dynamic ProgrammingYesNoYes
Monte CarloNoYesNo
Temporal DifferenceNoNoYes

TD combines the best of both: it is model-free like MC, and it can learn online (step-by-step) like DP. This makes it practical for continuing tasks with no natural episode boundary.

SARSA — on-policy TD

SARSA updates the action-value function Q(s,a) using the actual next action taken under the current policy π:

Q(s,a) ← Q(s,a) + α [r + γ Q(s′,a′) − Q(s,a)]

The name comes from the 5-tuple (S, A, R, S′, A′) used in the update. SARSA is on-policy: it evaluates the same policy it improves. If you’re using ε-greedy exploration, SARSA accounts for the exploration noise — safer in environments where unsafe exploratory actions have irreversible consequences.

Q-Learning — off-policy TD

Q-Learning uses the best possible next action instead of the actual one:

Q(s,a) ← Q(s,a) + α [r + γ maxa′ Q(s′,a′) − Q(s,a)]

This is off-policy: the update target is the optimal policy, regardless of which action was actually taken. Q-Learning converges to the optimal Q* under mild conditions (every state-action pair visited infinitely often, decaying α). The interactive demo above uses Q-Learning on a 4×4 grid world.

Eligibility traces — TD(λ)

TD(0) only uses the immediate next state; Monte Carlo uses all future states. TD(λ) interpolates with eligibility traces:

Gλt = (1−λ) Σn=1∞ λn−1 G(n)t

An eligibility trace e(s) records how recently and frequently a state was visited. When a TD error occurs, all states with high eligibility get updated. λ=0 gives TD(0); λ=1 gives Monte Carlo. Intermediate values (e.g. λ=0.9) often learn faster by propagating errors backward through time.

Function approximation

Tabular methods store Q(s,a) as a table — infeasible for large or continuous state spaces. Function approximation replaces the table with a parameterised function Q(s,a;θ) and updates θ by gradient descent on the TD error:

θ ← θ + α δt ∇θ Q(s,a;θ)

This generalises across similar states, enabling RL to scale to high-dimensional inputs. Linear function approximation with tile coding or radial basis features is still competitive in many settings.

From Q-Learning to DQN

Deep Q-Network (DQN) applies Q-Learning with a deep neural network as Q(s,a;θ). Two key tricks make it stable:

  • Experience replay: store transitions (s,a,r,s′) in a buffer and sample mini-batches at random, breaking temporal correlations.
  • Target network: use a periodically-copied “frozen” copy of Q to compute the TD target r + γ max Q(s′,a′;θ−), stabilising the moving target problem.

DQN achieved superhuman performance on dozens of Atari games using raw pixels as input — the first clear demonstration that TD + neural networks can match human-level play.

Recap

  • TD learning updates values after each step using a bootstrapped target r + γV(s′) — no model, no full episode.
  • TD error δt = r + γV(s′) − V(s) drives all updates; when δ=0, the estimate is self-consistent.
  • SARSA: on-policy (uses actual next action). Q-Learning: off-policy (uses best next action).
  • TD(λ) bridges TD(0) and Monte Carlo via eligibility traces.
  • DQN = Q-Learning + neural network + experience replay + target network.
Q-Learning on a 4×4 Grid World

Recall check

0 of …

Say the answer out loud before you reveal it — recalling it is what makes it stick.

  1. What is the TD error and what does it mean when it equals zero?

  2. What is the difference between SARSA and Q-Learning?

  3. How does TD(λ) relate to both TD(0) and Monte Carlo?

  4. What two tricks does DQN add to Q-Learning to make it stable with neural networks?

Cheat sheet

Temporal Difference Learning

TD error: δ=r+γV(s')−V(s). TD(0): V(s)+=αδ. SARSA (on-policy): Q(s,a)+=α[r+γQ(s',a')−Q(s,a)]. Q-Learning (off-policy): Q(s,a)+=α[r+γmax Q(s',·)−Q(s,a)]. DQN adds experience replay + target network.

REINFORCEMENT LEARNING · AI.Cruz