Deep Q-networks (DQN)
Q-learning with neural networks: experience replay, target networks and the deadly triad.
Animated explanation
The idea
Tabular Q-learning needs one number per state–action pair. An Atari screen has more possible states than atoms in the universe. DQN replaces the table with a neural network that takes the state and outputs a value for every action, and trains it with the same Q-learning target.
Done naively, this is unstable: consecutive experiences are highly correlated, and the target shifts every time the network updates, like chasing your own tail. DQN's two fixes made deep RL work in 2015: an experience replay buffer (learn from random past transitions) and a target network (compute targets with a frozen copy updated only occasionally).
The deadly triad
Function approximation, bootstrapping and off-policy learning each work fine alone, but together they can make value estimates diverge. DQN's replay buffer and target network don't remove the triad; they damp its feedback loops enough for training to succeed. Gradient clipping or the Huber loss also helps with occasional huge TD errors.
Practical knobs
Buffer size (10⁵–10⁶), mini-batch 32–256, target sync every 1k–10k steps (or soft updates ), ε decaying from 1 to 0.01–0.1, Adam with a small learning rate, frame stacking for Markov states. Rainbow combines Double, Dueling, Prioritised replay, n-step returns, distributional values and noisy nets. DQN needs discrete actions; continuous control uses actor-critic methods (DDPG, TD3, SAC).
The algorithm, step by step
- 1Initialise , target , and an empty replay buffer.
- 2Each step: act ε-greedily on ; store .
- 3Sample a random mini-batch; compute .
- 4Take a gradient step on ; every C steps set ; decay ε.
The maths
Target
Loss
Soft target update
Gradient for a linear Q (one-hot features)
Strengths and weaknesses
Strengths
- Scales Q-learning to images and huge state spaces
- Off-policy: reuses past experience efficiently
- Simple, well-understood baseline for discrete actions
Weaknesses
- Discrete actions only
- Sample-hungry and sensitive to hyperparameters
- Overestimation and instability without the standard fixes
Reach for it when…
Check your understanding
Lambdian Lab
DQN machinery on a corridor
A linear Q-function (a table W of shape N × 2 over one-hot states) trained the DQN way.
- ReplayBuffer(capacity): push(transition) keeps only the newest capacity items; sample(batch, rng) draws without replacement; __len__.
- td_targets(batch, W_target, gamma): array of .
- train_dqn(steps, batch, lr, sync_every, seed): ε decays linearly 1 → 0.05 over the first half; each step push, then (once the buffer holds ≥ batch) one gradient step on the mean squared TD error for the sampled batch; sync W⁻ ← W every sync_every steps. Return W.