Deep Q-Learning

A neural network learns a game from pixels while you watch. Experience replay and target networks keep it stable, and Double Q-learning stops it overrating itself.

Deep Q-Learning

Put a neural network where the Q-table was, feed it the screen, and train it on its own predictions. Done naively, this tends to fall apart. Two tricks — replaying old experience and chasing a slowly moving target — were enough for one network to learn dozens of Atari games from pixels.

Learning to Catch

Press Train and watch a network that sees only pixels learn to catch the ball in about twenty seconds. Try Step by step to see single training moves and the minibatches being replayed, then play it yourself.

Speed:
0
episodes (0 steps)
0
gradient updates (32 frames each)
—
catch rate, last 100 episodes
1.00
ε (chance of a random move)
—
your catch rate (Play it yourself)
The untrained network’s Q-values are near zero and its moves are arbitrary. Press Train: the agent starts by moving at random (ε = 1), stores every transition in the replay buffer, and after each step takes one gradient step on 32 frames drawn from it.

Network: 100 → 64 ReLU → 3 (6,659 parameters), Adam (learning rate 0.001), Huber loss, γ = 0.95; ε falls from 1 to 0.05 over 3,000 steps; replay buffer of 5,000, minibatch 32, target network refreshed every 200 updates. Normal trains 200 steps a second while the board replays the current network at a watchable pace. In the bars, orange is the chosen move; in the weight images, a green pixel excites that unit and a rose one inhibits it, and the arrow underneath is the move the unit most favours.

Go deeper:DQN: Q-Learning with a Network

Tabular Q-learning keeps one number per state–action pair. With pixels as the state that quickly becomes hopeless — this 10 × 10 screen has only hundreds of reachable frames, but an Atari screen has astronomically many, and a table cannot generalise from one frame to a similar one — so DQN replaces the table with a network Q(s, a; θ) that outputs one value per action, and turns the Q-learning update into a regression: move Q(s, a; θ) towards the target y = r + γ maxₐ′ Q(s′, a′; θ⁻) by a gradient step on the loss (y − Q(s, a; θ))². Here θ⁻ are the weights of a separate target network, a periodic copy of θ. The demo uses the Huber loss, which is quadratic for small errors and linear for large ones, so a single surprising transition cannot produce an enormous gradient; the 2015 paper achieved the same effect by clipping the error term to [−1, 1].

Mnih and colleagues introduced the method in a 2013 arXiv paper on seven Atari 2600 games, and in “Human-level control through deep reinforcement learning” (Nature, 2015) trained one architecture, with the same hyperparameters, on 49 games, from nothing but the screen and the score. Two ingredients made it work. Experience replay (Lin, 1992) stores transitions in a large buffer and trains on random minibatches drawn from it, which breaks the strong correlation between consecutive frames and lets each transition be reused many times. The target network, added in the 2015 paper, holds the regression target fixed for thousands of updates at a time.

This demo is a deliberately tiny stand-in. The Atari network was convolutional and read stacks of four 84 × 84 grey-scale frames; ours is fully connected, reads one 10 × 10 frame, and has a few thousand parameters instead of over a million. The game is a toy too: a falling ball never needs more than nine moves of foresight. But the algorithm — ε-greedy exploration, replay, target network, bootstrapped regression — is the same one. In our tests the default settings pass a 90% catch rate after roughly 3,000 to 4,000 environment steps, about 15 to 20 seconds at Normal speed.

Overestimation

Press Sample continuously and watch the maximum of noisy estimates settle above the true value, then raise N or σ. Below, slide the number of actions at B and watch Q-learning prefer the worse path while Double Q-learning does not.

0
draws
—
mean of max (single estimator)
+1.424
theory: (σ/√k) · E[max of N standard normals]
—
mean of double estimator

Every action is truly worth 0, but each estimate is noisy (standard deviation σ/√k = 1.00). The blue histogram is the largest estimate in set 1; the green one takes the action set 1 picked and reads its value from an independent set 2. The dotted blue line is the exact expected maximum.

0
independent runs averaged
—
Q-learning: left, episodes 5–50
—
Double Q: left, episodes 5–50
B has 10 actions, all equally bad. Q-learning’s target for going left is maxₐ Q(B, a): after a few visits some action at B has been lucky, so the max is positive and left looks better than right’s 0. Double Q-learning picks B’s best action with one table and values it with the other, so the luck does not carry over.

Sutton and Barto’s Example 6.7: ε-greedy with ε = 0.1, step size α = 0.1, γ = 1, all estimates starting at 0; each curve is the average of 2,000 independent runs of 300 episodes. Blue is Q-learning, green is Double Q-learning, and the dashed line is the best an ε-greedy agent can do.

Go deeper:Maximisation Bias and Double Q-Learning

If Q̂₁, …, Q̂ₙ are unbiased but noisy estimates of values q₁, …, qₙ, then maxᵢ Q̂ᵢ is not an unbiased estimate of maxᵢ qᵢ. Because the maximum is a convex function, Jensen’s inequality gives E[maxᵢ Q̂ᵢ] ≥ maxᵢ E[Q̂ᵢ] = maxᵢ qᵢ, and the gap grows with the noise and with the number of actions: for n equal values with independent Gaussian noise of standard deviation σ/√k, the bias is exactly (σ/√k) · E[max of n standard normals], about 1.54 σ/√k for n = 10. Q-learning’s target r + γ maxₐ′ Q(s′, a′) takes exactly such a maximum, at every update, and the bias propagates backwards from state to state.

The fix is to decouple choosing an action from evaluating it. The double estimator keeps two independent sets of estimates, picks i* = argmaxᵢ Q̂ᵢ⁽¹⁾ with one and reports Q̂⁽²⁾ᵢ* from the other. Since the second set’s noise is independent of the choice, the result is never biased upwards; it can underestimate when the true values differ. Double Q-learning (van Hasselt, 2010) applies this to Q-learning with two tables, updating one at random each step: Q₁(s, a) ← Q₁(s, a) + α[r + γ Q₂(s′, argmaxₐ′ Q₁(s′, a′)) − Q₁(s, a)].

The small MDP is Example 6.7 of Sutton and Barto’s Reinforcement Learning: An Introduction. Going left from A is worse in expectation (−0.1 against 0), but every action at B has a noisy reward, so after a few visits the largest of B’s estimates is usually positive and Q-learning concludes that left is better. With more actions at B the effect is stronger and lasts longer.

DQN inherits the same bias through the max in its target, and with function approximation the noise comes not just from rewards but from the network’s own errors. Double DQN (van Hasselt, Guez and Silver, 2016) uses the online network to choose a′ and the target network to evaluate it, y = r + γ Q(s′, argmaxₐ′ Q(s′, a′; θ); θ⁻), a one-line change that reduced overestimation and improved scores on many Atari games.

Why the Tricks Matter

Press Run to train twelve networks side by side — full DQN, and versions without replay, without a target network, or without either — and compare their catch rates and value estimates. Then switch to One ball, or change the sync period, and rerun.

0% of 240,000 steps
Task:
Target sync every:
Buffer:
Seeds:
—
Full DQN: average catch rate so far · max-Q now
—
No replay: average catch rate so far · max-Q now
—
No target network: average catch rate so far · max-Q now
—
Neither: average catch rate so far · max-Q now
Press Run to train 12 networks at once — each of the four variants from 3 different seeds. Lines are the mean over seeds; shaded bands span the best and worst seed.

Each network is the Learning to Catch DQN (100 → 64 → 3) trained for 20,000 steps. In Endless balls, a new ball appears as soon as one lands and the agent bootstraps across balls with γ = 0.99, so a state is worth up to 1/(1 − γ⁹) ≈ 11.6 rather than 1. “No replay” learns from each transition once, as it happens; “no target network” computes r + γ maxₐ′ Q(s′, a′) with the network being trained. The true value line is the exact optimum on the same 64 frames, by value iteration. Blue full DQN, purple no replay, amber no target network, pink neither.

Go deeper:Moving Targets, Correlated Samples and the Deadly Triad

Supervised learning assumes a fixed target and independent, identically distributed samples. Naive deep Q-learning breaks both. The regression target r + γ maxₐ′ Q(s′, a′; θ) depends on the very weights being trained, so each step moves the target it is aiming at — and because a network generalises, an update at one state also shifts the values of similar states, including s′. The target network freezes θ⁻ between syncs, turning each stretch into an ordinary regression problem. Meanwhile consecutive frames of a game are nearly identical and the data distribution follows the current policy; replay mixes old and new experience so that minibatches look much more like independent samples.

Bootstrapping, function approximation and off-policy learning together are what Sutton and Barto call the deadly triad: any two are safe, but all three can diverge, and DQN has all three. The tricks do not remove the triad; they make divergence much less likely in practice. See also the Function Approximation lesson, where Baird’s counterexample diverges with no noise at all.

What this small experiment shows: on the one-ball game, where no value is built from more than nine steps of the network’s own predictions, the target network makes little difference, while removing replay clearly slows and destabilises learning. In the endless version (γ = 0.99, values up to about 11.6), the network without a target network still catches well but its value estimates climb past the largest value any state can have, while full DQN approaches the true value from below. These are measurements on a toy problem over a few seeds, not general laws; with other settings the naive variants sometimes learn perfectly well, and sometimes much worse.

Many improvements followed. Prioritised experience replay (Schaul et al., 2016) replays surprising transitions more often; dueling networks (Wang et al., 2016) split Q into a state value plus action advantages; distributional RL (Bellemare, Dabney and Munos, 2017) learns the whole distribution of returns rather than its mean. Rainbow (Hessel et al., 2018) combined six such extensions to DQN and outperformed each of them alone on the Atari benchmark.

Key Takeaways

  • DQN replaces the Q-table with a network trained to regress onto r + γ maxₐ′ Q̂(s′, a′).
  • Experience replay breaks the correlation between consecutive frames and reuses every transition.
  • A target network holds the regression target still, so updates do not chase their own tails.
  • The max over noisy estimates is biased upwards; Double Q-learning separates choosing from evaluating.

Spot a mistake or have an idea?

Search