Deep Q-Networks & Replay Buffers

How a neural network learned to play Atari straight from the screen.

🔴 advanced5 min readrl
Deep Q-networks, from DeepMind, combined Q-learning with a neural network and learned to play Atari games straight from screen pixels. Simply plugging a neural network into Q-learning tends to blow up during training, so DQN added two fixes. Experience replay stores past moves and trains on random samples of them, so the network is not fed a stream of near-identical moments. A target network, a frozen copy updated only occasionally, gives learning a stable goal instead of one that shifts every step.

The DQN Architecture

  Raw Input Screen (84x84x4 pixels) ──► [ CNN Layers + FFN ] ──► Vector of Q-Values [ Q(s, a1), Q(s, a2), ..., Q(s, aK) ]

A single forward pass computes QQ-values for all discrete actions simultaneously!

The Two Core Stabilizing Innovations

When combining Q-Learning with Deep Neural Networks, 3 sources of instability arise:

  1. Correlations between sequential observations (st,st+1s_t, s_{t+1}).
  2. Non-stationary data distribution (Policy changes as QQ updates).
  3. Chasing a Moving Target: Updating θ\theta changes both current Q(s,a;θ)Q(s, a; \theta) AND target r+γmax⁡a′Q(s′,a′;θ)r + \gamma \max_{a'} Q(s', a'; \theta).
                            DQN STABILIZATION TRIAD
┌───────────────────────────────────────┬───────────────────────────────────────┐
│ 1. EXPERIENCE REPLAY BUFFER (D)       │ 2. SEPARATE TARGET NETWORK (θ^-)      │
├───────────────────────────────────────┼───────────────────────────────────────┤
│ Store 1M transitions (s, a, r, s').   │ Maintain periodic copy of weights θ^-.│
│ Sample random mini-batches for SGD.   │ Freeze θ^- for C steps (e.g. C=10k).  │
│ Breaks temporal correlation -> i.i.d.! │ Eliminates moving target feedback!    │
└───────────────────────────────────────┴───────────────────────────────────────┘

Loss Function & Target Update

DQN minimizes Loss Li(θi)\mathcal{L}_i(\theta_i) over random mini-batch U(D)U(D):

Li(θi)=E(s,a,r,s′)∼U(D)[(r+γmax⁡a′Q(s′,a′;θi−)⏟Target (Frozen Weights θ−)−Q(s,a;θi)⏟Prediction (Active Weights θ))2]\mathcal{L}_i(\theta_i) = \mathbb{E}_{(s, a, r, s') \sim U(D)} \left[ \left( \underbrace{r + \gamma \max_{a'} Q(s', a'; \theta_i^-)}_{\text{Target (Frozen Weights } \theta^-)} - \underbrace{Q(s, a; \theta_i)}_{\text{Prediction (Active Weights } \theta)} \right)^2 \right]

Every CC steps, copy active weights to target network: θ−←θ\theta^- \leftarrow \theta.

DQN Extension Family

Say this out loud

"DQN scales Q-learning to high-dimensional pixel inputs using deep CNNs. Training is stabilized using Experience Replay to break sample autocorrelation and a frozen Target Network Q(s,a; θ^-) to eliminate the moving target problem. Double DQN further decouples action selection from evaluation to eliminate overestimation bias."

Follow-ups to expect

Check yourself

Question 1 of 3

Why does training a Deep Q-Network directly on sequential environment steps (s_t, a_t, r_t, s_{t+1}) without a Replay Buffer cause training instability and divergence?

More in Reinforcement Learning

See all →
Value-Based vs Policy-Based Methods5 minMulti-Armed Bandits4 minProximal Policy Optimization5 min