Skip to content
Lambdian

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 Qθ(s,⋅)Q_\theta(s,\cdot) 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 r+γmax⁡Qθ(s′,⋅)r + \gamma\max Q_\theta(s',\cdot) 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 θ−←τθ+(1−τ)θ−\theta^- \leftarrow \tau\theta + (1-\tau)\theta^-), ε 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

  1. 1
    Initialise QθQ_\theta, target θ−=θ\theta^- = \theta, and an empty replay buffer.
  2. 2
    Each step: act ε-greedily on Qθ(s,⋅)Q_\theta(s,\cdot); store (s,a,r,s′,d)(s,a,r,s',d).
  3. 3
    Sample a random mini-batch; compute y=r+γ(1−d)max⁡a′Qθ−(s′,a′)y = r + \gamma(1-d)\max_{a'}Q_{\theta^-}(s',a').
  4. 4
    Take a gradient step on (y−Qθ(s,a))2(y - Q_\theta(s,a))^2; every C steps set θ−←θ\theta^- \leftarrow \theta; decay ε.

The maths

Target

y=r+γ(1−d)max⁡a′Qθ−(s′,a′)y = r + \gamma(1-d)\max_{a'}Q_{\theta^-}(s',a')

Loss

L(θ)=E(s,a,r,s′,d)∼D[(y−Qθ(s,a))2]L(\theta) = \mathbb{E}_{(s,a,r,s',d)\sim\mathcal{D}}\big[(y - Q_\theta(s,a))^2\big]

Soft target update

θ−←τθ+(1−τ)θ−\theta^- \leftarrow \tau\theta + (1-\tau)\theta^-

Gradient for a linear Q (one-hot features)

∇Ws,aL=−2(y−Ws,a)\nabla_{W_{s,a}} L = -2\big(y - W_{s,a}\big)

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…

Discrete-action problems with high-dimensional observations and a simulator: games, discrete control, scheduling.

Check your understanding

Question 1

Q1What problem does the replay buffer address?

Question 2

Q2Why keep a separate target network?

Question 3

Q3Can vanilla DQN control a robot arm with continuous torques?

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 r+γ(1−d)max⁡aW−[s′,a]r + \gamma(1-d)\max_a W^-[s', a].
  • 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.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.