Foundations to Frontier
Tests
L. Reinforcement Learning and Autonomy › L3 Deep RL: Value-Based

DQN: experience replay and target networks

Deep Q-networks (DQN) replace the Q-table by a neural network, so Q-learning can handle large or continuous state spaces. Two additions make this work: experience replay, which trains on random minibatches of stored transitions, and a target network, a slowly updated copy that provides the bootstrap targets. This page builds DQN from scratch in NumPy on cart-pole and measures what each addition contributes, to the policy and to the value estimates.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learning from a diary

A table with one entry per situation cannot cope when situations never repeat exactly, as with a pole balanced on a moving cart. A neural network can generalise from similar situations instead. But learning only from the very last moment is like revising your opinion after every sentence you hear: unstable. DQN keeps a diary of past moments and learns from random pages of it, and it compares its guesses against a slightly older copy of itself that changes slowly.

Deep reinforcement learning lab
DQN on cart-pole with one hidden layer of 48 units, ε falling from 1 to 0.05 over 3,000 steps, learning from step 500. Top: length of the 147 training episodes (10-episode average; 200 is the limit, dashed). Bottom: the network's value max_a Q(s, a) at the upright start state; dashed: 100, the largest possible value at γ = 0.99. The final greedy policy balances for 174.3 of 200 steps on average, and its start value is 22.1. With both, learning is steadier and the values stay below 100.
Why engineers care

In 2015 DQN learned to play 49 Atari games from the screen pixels alone, many at human level, with one set of settings.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A neural network is used instead of a table because…
  2. 2. Experience replay learns from…
  3. 3. The target network is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Q-network: Q(s, a; θ) is a neural network with one output per action; acting greedily needs one forward pass.
  • Loss: minimise (y − Q(s, a; θ))² with target y = r + γ(1 − d) max_a’ Q(s’, a’; θ⁻), where d = 1 only for a true termination.
  • Experience replay: store transitions (s, a, r, s’, d) in a buffer; train on random minibatches. This breaks the correlation between consecutive samples and reuses each transition many times.
  • Target network θ⁻: a copy of θ updated every C steps (or slowly, θ⁻ ← τθ + (1 − τ)θ⁻), so the target does not move with every update.
  • Exploration: ε-greedy with ε decaying from 1 to a small value.
  • Practical details: Huber loss or gradient clipping, Adam or RMSProp, a warm-up period before learning starts.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The DQN target uses…
  2. 2. Experience replay breaks…
  3. 3. A time-limit truncation should…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

DQN on cart-pole

Exercise 1.

  • Cart-pole: keep a pole upright on a cart by pushing left or right; +1 per step, failure when the pole tilts past 12° or the cart leaves the track, at most 200 steps.
  • A Q-network with two hidden layers of 32 ReLU units, Adam with learning rate 1e-3, γ = 0.99.
  • A replay buffer of 10,000 transitions, minibatches of 32, a target network copied every 250 steps, ε from 1 to 0.05 over 3,000 steps, learning from step 500.
  • Three runs of 8,000 steps, trained side by side; evaluation of the greedy policy over 10 episodes.

Practice.

  • Always use experience replay with a large buffer; it was the single most important ingredient here.
  • Use a target network or Polyak averaging, and check that the value estimates stay in a plausible range, below r_max/(1 − γ).
  • Separate termination from truncation: bootstrap through time limits.
  • Monitor several seeds: single DQN runs vary a lot.
  • Tune the exploration schedule and the learning rate first; they matter more than the network size.
  • Tools: Stable-Baselines3 DQN, CleanRL’s single-file DQN, Gymnasium CartPole-v1; PyTorch or JAX for the network.
q2, _ = forward(target_net, s2)
y = r + gamma * (1 - terminated) * q2.max(-1)
q, acts = forward(net, s)
qa = np.take_along_axis(q, a[..., None], -1)[..., 0]
g = np.zeros_like(q)
np.put_along_axis(g, a[..., None], ((qa - y) / len(y))[..., None], -1)
adam.step(net, backward(net, acts, g))
if t % 250 == 0: target_net = copy(net)
🐍 Exercise 1: DQN from scratch
Implement cart-pole, a Q-network with backpropagation and Adam, a replay buffer and a target network, and train DQN to balance the pole.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The most important DQN ingredient in these runs was…
  2. 2. A sign of trouble in DQN is…
  3. 3. Single DQN runs…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why DQN can diverge. Combining function approximation, bootstrapping and off-policy learning (the deadly triad) can make semi-gradient Q-learning diverge, even in small linear examples (Baird 1995; Tsitsiklis and Van Roy 1997). Each update changes Q at many states at once, including the states used in the target.

What the two additions do. Experience replay makes the minibatch distribution change slowly and approximately i.i.d., so stochastic gradient descent behaves like regression on a stable data set. A frozen target network turns each period of C steps into an ordinary supervised regression toward fixed targets, a form of fitted Q-iteration (Ernst, Geurts and Wehenkel 2005; Riedmiller 2005): Q_(k+1) ≈ T* Q_k.

Value scale. With reward 1 per step and γ = 0.99, no value can exceed 1/(1 − γ) = 100. Values above this bound reveal the feedback loop of bootstrapping from a network that is itself being changed. With a frozen target, each period can raise the values by at most one application of T*, so values grow toward their true level from below.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The deadly triad is…
  2. 2. With a frozen target, each period is like…
  3. 3. With reward 1 per step and γ = 0.99, values cannot exceed…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much do experience replay and the target network each contribute to DQN, in the policy it learns and in its value estimates?

History.

  • L.-J. Lin (1992) introduced experience replay.
  • M. Riedmiller (2005) proposed neural fitted Q-iteration.
  • V. Mnih and colleagues (2013, 2015) introduced DQN and its target network, reaching human level on many Atari games.
  • H. van Hasselt, A. Guez and D. Silver (2016) showed DQN’s overestimation and introduced Double DQN (next topic).

Exercise 2.

  • Cart-pole as in Exercise 1, 6,000 steps, three runs for each of the four combinations of replay (on or off) and target network (on or off), all trained side by side.
  • Without replay, each update uses only the latest transition; without a target network, the online network computes the targets.
  • Measured: the greedy policy’s mean episode length (out of 200) over 10 episodes, the mean length of the last 10 training episodes, and the mean value estimate max_a Q(s0, a) at 100 random start states. No value can exceed 100.

Seed 0:

variant greedy length (per run) last 10 training episodes value at the start
replay + target 182.9 (198.2, 151.6, 198.8) 192.5 23.6
replay only 137.2 (133.7, 152.2, 125.6) 95.9 68.8
target only 9.5 (9.6, 9.4, 9.6) 9.6 24.7
neither 23.5 (20.1, 25.7, 24.8) 43.6 30.4
  • Experience replay made the difference between learning and not learning: both variants without it stayed below 25 steps, against 137 to 183 with it.
  • The target network kept the values in a plausible range: with it, the start value was 23.6. Without it, it was 68.8 in seed 0 and 148.7 and 109.2 in seeds 1 and 2, above the largest possible value of 100.
  • The values with a target network are far below the true value (a perfect 200-step episode is worth 86.6, and the policies were close to perfect): a frozen target lets values grow only one Bellman step per period. A good policy does not require accurate values, only correctly ordered ones.
  • Its effect on the policy was mixed at this budget: replay + target beat replay only in seed 0 (182.9 against 137.2), but not in seeds 1 and 2 (187.1 against 195.4, and 191.8 against 169.1).
  • Seeds 1 and 2: without replay, the greedy lengths were 82.7 and 24.2 (target only) and 36.0 and 82.2 (neither).
🐍 Exercise 2 (research): how much do experience replay and the target network each contribute to DQN, in the policy it learns and in its value estimates?
Train DQN on cart-pole with and without experience replay and a target network, and compare the policies and the value estimates.

DQN in practice

  • Improvements (next topics): Double DQN against overestimation, dueling networks, prioritised replay, distributional value estimates; Rainbow (Hessel et al. 2018) combines them.
  • Discrete actions only: for continuous actions, use actor-critic methods such as DDPG, TD3 or SAC (topic L4-5).
  • Sample efficiency: DQN needs many interactions; model-based methods (topic L5-1) reduce this.
  • Reproducibility: report several seeds and confidence intervals; results vary widely between runs.

References

  1. V. Mnih et al. (2015). “Human-level control through deep reinforcement learning.” Nature.
  2. L.-J. Lin (1992). “Self-improving reactive agents based on reinforcement learning, planning and teaching.” Machine Learning.
  3. M. Riedmiller (2005). “Neural fitted Q iteration.” ECML.
  4. J. N. Tsitsiklis and B. Van Roy (1997). “An analysis of temporal-difference learning with function approximation.” IEEE Transactions on Automatic Control.
  5. A. G. Barto, R. S. Sutton and C. W. Anderson (1983). “Neuronlike adaptive elements that can solve difficult learning control problems.” IEEE Transactions on Systems, Man, and Cybernetics.