Simple Algorithm
Artificial Intelligence · Deep Layer · 26 min

Deep Q-Networks

Replay buffers, target networks and Atari.

In 2015 one network architecture, with one set of hyperparameters, learned to play 49 Atari games from raw pixels and the score. Its algorithm was Q-learning, run inside the deadly triad and kept stable by two engineering ideas. The decade of fixes that followed, from Double DQN to distributional RL, each repaired a flaw that theory can name.

What you’ll learn

  • Write the DQN loss and algorithm, and explain how experience replay and a target network turn Q-learning into a sequence of regression problems.
  • Prove E[max⁡iXi]≥max⁡iE[Xi]\E[\max_iX_i]\ge\max_i\E[X_i] and van Hasselt's lower bound on overestimation, and show why Double DQN's decoupled estimator removes the upward bias.
  • Explain dueling networks, prioritized replay with importance weights, multi-step targets and noisy nets, and what Rainbow's ablations say about them.
  • State the distributional Bellman equation, prove that its evaluation operator contracts in the Wasserstein metric, and describe C51's categorical projection.

DQN: Q-learning as repeated regression

DQN (Mnih et al., 2015) represents Q(s,a;θ)Q(s,a;\theta) by a convolutional network that reads the last four 84 × 84 grayscale frames and outputs one value per action. It minimizes a squared TD error against a target network with frozen parameters θ−\theta^-, on minibatches drawn uniformly from a replay buffer D\data of recent transitions:

L(θ)=E(s,a,r,s′)∼U(D)[(r+γmax⁡a′Q(s′,a′;θ−)⏟y (held fixed)−Q(s,a;θ))2]\loss(\theta)=\E_{(s,a,r,s')\sim U(\data)}\Big[\big(\underbrace{r+\gamma\max_{a'}Q(s',a';\theta^-)}_{y\ \text{(held fixed)}}-Q(s,a;\theta)\big)^2\Big]
The gradient flows only through Q(s,a;θ)Q(s,a;\theta), a semi-gradient as in tabular Q-learning. Every CC updates, θ−←θ\theta^-\leftarrow\theta.
text
initialize Q_θ;  target θ⁻ ← θ;  replay buffer D with capacity N
for each environment step t:
    a_t ← random action with probability ε, else argmax_a Q_θ(s_t, a)
    execute a_t; observe r_t, s_{t+1}, done_t; store the transition in D
    every 4 steps:
        sample a minibatch of B transitions (s, a, r, s', d) uniformly from D
        y ← r + γ·(1 − d)·max_a' Q_θ⁻(s', a')          # no gradient through y
        θ ← θ − η·∇_θ mean Huber(y − Q_θ(s, a))
    every C updates:  θ⁻ ← θ

The Nature DQN used N=106N=10^6 transitions, B=32B=32, C=10,000C=10{,}000 updates, RMSProp, ε\varepsilon annealed from 1 to 0.1 over the first million frames, rewards clipped to [−1,1][-1,1], and 50 million frames of play per game.

Each ingredient answers a failure of naive online Q-learning with a network. Consecutive frames are highly correlated, so a network trained on them in order chases the recent past; uniform replay over a million transitions makes minibatches closer to i.i.d. and reuses each transition many times (experience replay, Lin, 1992). A bootstrapped target that moves with every gradient step chases itself; freezing it for CC updates makes each phase an ordinary supervised regression. DQN is thus close to fitted Q-iteration (Ernst, Geurts & Wehenkel, 2005; Riedmiller, 2005):

Qk+1≈arg min⁡Q E(s,a,r,s′)∼D[(r+γmax⁡a′Qk(s′,a′)−Q(s,a))2]Q_{k+1}\approx\argmin_Q\ \E_{(s,a,r,s')\sim\data}\Big[\big(r+\gamma\max_{a'}Q_k(s',a')-Q(s,a)\big)^2\Big]
Approximate value iteration with a regression step. Its errors can compound by up to a factor 1/(1−γ)21/(1-\gamma)^2 (MDPs: Value & Policy Iteration), which is one reason the target is refreshed only occasionally.

Across the 49 games, DQN outperformed the best earlier reinforcement-learning methods on 43 and reached at least 75% of a professional human tester's score on 29, with the same architecture and hyperparameters throughout. Scores in this literature are usually human-normalized, 100×(agent−random)/(human−random)100\times(\text{agent}-\text{random})/(\text{human}-\text{random}). Five years later Agent57 (Badia et al., 2020) became the first agent to exceed the human benchmark on all 57 games of the standard suite.

Overestimation and Double DQN

DerivationProofs
  1. E[max⁡iXi]≥max⁡jE[Xj]\E\big[\max_iX_i\big]\ge\max_j\E[X_j]

    (a) For each jj, max⁡iXi≥Xj\max_iX_i\ge X_j pointwise, so E[max⁡iXi]≥E[Xj]\E[\max_iX_i]\ge\E[X_j]; take the maximum over jj. Equivalently, Jensen's inequality for the convex function max⁡\max.

  2. mC=∑aϵa2≤δ ⁣∑ϵa>0 ⁣ϵa+(∑ϵa<0∣ϵa∣)2=δS+S2≤m(m−1) δ2mC=\sum_a\epsilon_a^2\le\delta\!\sum_{\epsilon_a>0}\!\epsilon_a+\Big(\sum_{\epsilon_a<0}\abs{\epsilon_a}\Big)^2=\delta S+S^2\le m(m-1)\,\delta^2

    (b) Let ϵa=Q(s,a)−V∗(s)\epsilon_a=Q(s,a)-V^*(s) and δ=max⁡aϵa\delta=\max_a\epsilon_a. The errors sum to zero, so the positive ones and the negative ones have the same total size SS; at most m−1m-1 are positive, so S≤(m−1)δS\le(m-1)\delta. Then:

  3. max⁡aQ(s,a)−V∗(s)≥Cm−1\max_aQ(s,a)-V^*(s)\ge\sqrt{\frac{C}{m-1}}

    Hence δ≥C/(m−1)\delta\ge\sqrt{C/(m-1)}, with equality when m−1m-1 errors equal +δ+\delta and one equals −(m−1)δ-(m-1)\delta. Errors that are unbiased on average still produce a biased maximum.

Predict before you look

Two actions both have true value 0, and their estimates carry independent standard normal errors. What is the expected value of the larger estimate?

Show the answer

0.56

max⁡(X1,X2)=12(X1+X2)+12∣X1−X2∣\max(X_1,X_2)=\tfrac12(X_1+X_2)+\tfrac12\abs{X_1-X_2}, and X1−X2∼N(0,2)X_1-X_2\sim\mathcal N(0,2) has E∣X1−X2∣=2⋅2/π\E\abs{X_1-X_2}=\sqrt2\cdot\sqrt{2/\pi}. So E[max⁡]=1/π≈0.56\E[\max]=1/\sqrt\pi\approx0.56: more than half a standard deviation of bias, from noise alone. With 10 actions it is about 1.54.

Q-learning bootstraps from exactly this maximum, so the bias feeds the next targets. Double Q-learning (van Hasselt, 2010) keeps two estimates and lets one choose while the other evaluates. If a^=arg max⁡aQA(s′,a)\hat a=\argmax_aQ_A(s',a) and QBQ_B is unbiased and independent of QAQ_A, then E[QB(s′,a^)]=E[Q(s′,a^)]≤max⁡aQ(s′,a)\E[Q_B(s',\hat a)]=\E[Q(s',\hat a)]\le\max_aQ(s',a): in general an underestimate rather than an overestimate. Double DQN (van Hasselt, Guez & Silver, 2016) reuses the two networks DQN already has: the online network selects, the target network evaluates.

yDQN=r+γ Q(s′,arg max⁡a′Q(s′,a′;θ−); θ−)yDouble=r+γ Q(s′,arg max⁡a′Q(s′,a′;θ); θ−)y^{\text{DQN}}=r+\gamma\,Q\big(s',\argmax_{a'}Q(s',a';\theta^-);\,\theta^-\big)\qquad y^{\text{Double}}=r+\gamma\,Q\big(s',\argmax_{a'}Q(s',a';\theta);\,\theta^-\big)
A one-line change. On Atari, DQN's value estimates had been far above the discounted returns its own policies achieved; Double DQN brought them much closer and improved scores on many games.

Dueling heads, prioritized replay and Rainbow

Many states are good or bad whatever you do there. A dueling network (Wang et al., 2016) splits its head into a state value and action advantages, recombined with a centering term that makes the split identifiable:

Q(s,a)=V(s)+(A(s,a)−1∣A∣∑a′A(s,a′))Q(s,a)=V(s)+\Big(A(s,a)-\frac1{\abs{\mathcal A}}\sum_{a'}A(s,a')\Big)
Without the centering, adding a constant to VV and subtracting it from every AA would leave QQ unchanged. With it, the value stream learns from every transition through ss, whichever action was taken.

Uniform replay spends most updates on transitions the network already predicts well. Prioritized replay (Schaul et al., 2016) samples transition ii with probability P(i)=piα/∑kpkαP(i)=p_i^\alpha/\sum_kp_k^\alpha, where the priority pi=∣δi∣+ϵp_i=\abs{\delta_i}+\epsilon comes from its last TD error. That changes the distribution the expected update averages over, so each update is weighted by wi=(N P(i))−β/max⁡jwjw_i=(N\,P(i))^{-\beta}/\max_jw_j, with β\beta annealed to 1 so that the correction is exact by the end of training.

ExtensionFlaw it addressesChange
Double Q-learning (van Hasselt et al., 2016)maximization biasonline network selects, target network evaluates
Prioritized replay (Schaul et al., 2016)uniform replay wastes updatessample by TD error; correct with importance weights
Dueling heads (Wang et al., 2016)values learned one action at a timeseparate V(s)V(s) and centered A(s,a)A(s,a)
Multi-step targetsslow propagation of rewardnn-step returns (n=3n=3 in Rainbow)
Distributional RL (Bellemare et al., 2017)only the mean return is learnedlearn the whole return distribution (C51)
Noisy nets (Fortunato et al., 2018)ε-greedy explores blindlylearned parameter noise in the linear layers
Rainbow (Hessel et al., 2018) combined all six. In its ablations, removing prioritized replay or multi-step targets hurt most, followed by distributional learning; the combination set a new state of the art on the 57-game Atari suite at the time.
Check your understanding

Why does prioritized replay weight each update by wi∝(N P(i))−βw_i\propto(N\,P(i))^{-\beta}?

  1. To make high-error transitions count even more
  2. To keep the replay buffer from overflowing
  3. To decorrelate consecutive transitions
  4. Prioritized sampling changes the distribution the expected update averages over, which would shift the fixed point unless corrected
Show the answer

D. Prioritized sampling changes the distribution the expected update averages over, which would shift the fixed point unless corrected

The expected update under uniform replay is an average over the buffer with weights 1/N1/N. Sampling with probability P(i)P(i) reweights transition ii by N P(i)N\,P(i); multiplying by (N P(i))−1(N\,P(i))^{-1}, i.e. β=1\beta=1, undoes this exactly. Annealing β\beta from about 0.4 accepts some bias early, while the targets are moving anyway, in exchange for lower variance.

Distributional reinforcement learning

Q-learning learns the mean of the return. Distributional RL learns its whole distribution Z(s,a)Z(s,a), a random variable whose mean is Q(s,a)Q(s,a), through the distributional Bellman equation, an equality in distribution:

Zπ(s,a)=DR(s,a)+γ Zπ(S′,A′),S′∼P(⋅∣s,a),A′∼π(⋅∣S′)Z^\pi(s,a)\overset{D}{=}R(s,a)+\gamma\,Z^\pi(S',A'),\qquad S'\sim P(\cdot\mid s,a),\quad A'\sim\pi(\cdot\mid S')
DerivationProof sketch (also in Bellman Equations & Contraction)
  1. Wp(cU,cV)=∣c∣ Wp(U,V),Wp(A+U,A+V)≤Wp(U,V)W_p(cU,cV)=\abs c\,W_p(U,V),\qquad W_p(A+U,A+V)\le W_p(U,V)

    Wp(U,V)W_p(U,V) is the smallest value of (E∣U−V∣p)1/p\big(\E\abs{U-V}^p\big)^{1/p} over couplings of UU and VV. Two properties follow directly, for a scalar cc and a random variable AA independent of UU and VV:

  2. (R+γZ1(S′,A′))−(R+γZ2(S′,A′))=γ (Z1(S′,A′)−Z2(S′,A′))\big(R+\gamma Z_1(S',A')\big)-\big(R+\gamma Z_2(S',A')\big)=\gamma\,\big(Z_1(S',A')-Z_2(S',A')\big)

    Couple Z1(s′,a′)Z_1(s',a') and Z2(s′,a′)Z_2(s',a') optimally for each next pair, and let both targets share the reward and the transition. Under this coupling the two targets differ by γ\gamma times the coupled difference:

  3. Wˉp(TπZ1,TπZ2)≤γ Wˉp(Z1,Z2)\bar W_p\big(\mathcal T^\pi Z_1,\mathcal T^\pi Z_2\big)\le\gamma\,\bar W_p(Z_1,Z_2)

    Averaging over (S′,A′)(S',A') cannot exceed the worst pair: Wp(TπZ1(s,a),TπZ2(s,a))p≤γp ES′,A′[Wp(Z1(S′,A′),Z2(S′,A′))p]≤γp Wˉp(Z1,Z2)pW_p\big(\mathcal T^\pi Z_1(s,a),\mathcal T^\pi Z_2(s,a)\big)^p\le\gamma^p\,\E_{S',A'}\big[W_p\big(Z_1(S',A'),Z_2(S',A')\big)^p\big]\le\gamma^p\,\bar W_p(Z_1,Z_2)^p. Take the supremum over (s,a)(s,a):

C51 represents each Z(s,a)Z(s,a) as a categorical distribution on 51 fixed atoms evenly spaced on [−10,10][-10,10]. The shifted atoms r+γzjr+\gamma z_j of the target fall between grid points, so the target is projected back onto the support, each shifted atom's mass split between its two neighbors in proportion to proximity, and the network minimizes the cross-entropy to the projected target. This trains well but does not match the theory, because KL is not a metric in which Tπ\mathcal T^\pi contracts. The projected categorical operator turns out to be a contraction in the Cramér distance (Rowland et al., 2018), and QR-DQN (Dabney et al., 2018) learns quantiles with a quantile-regression loss, whose projected operator contracts in the maximal ∞\infty-Wasserstein metric.

Why should learning a distribution help a greedy policy that uses only its mean? In tabular and linear settings, distributional and expected methods produce the same means (Lyle, Bellemare & Castro, 2019), so the gains with deep networks are usually attributed to a richer training signal for the shared representation, an explanation that is plausible but not settled. Distributions are directly useful for risk-sensitive control, where the policy cares about more than the mean.

Key takeaways

  • DQN is Q-learning with a convolutional network, uniform replay and a periodically frozen target network; each target period is a regression, so DQN approximates fitted Q-iteration.
  • E[max⁡]≥max⁡E\E[\max]\ge\max\E; with errors unbiased on average and mean square CC, the maximum overestimates by at least C/(m−1)\sqrt{C/(m-1)}. Double DQN selects with the online network and evaluates with the target network.
  • Dueling heads, prioritized replay with importance weights annealed to β=1\beta=1, multi-step targets, distributional learning and noisy nets each fix a named flaw; Rainbow's ablations ranked prioritization and multi-step targets highest.
  • The distributional evaluation operator is a γ\gamma-contraction in the maximal Wasserstein metric, while the control operator is not; C51 projects onto 51 atoms and minimizes cross-entropy, and QR-DQN learns quantiles.
  • Open problems include data efficiency, loss of plasticity, scaling value-based RL to large networks, and a theory of why the stabilizers work.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What does holding the target network θ−\theta^- fixed for CC updates achieve?

  1. It decorrelates consecutive training samples
  2. It makes each phase an ordinary regression toward fixed targets, so the network is not chasing a target that moves with every step
  3. It removes the overestimation bias of the max
  4. It turns the semi-gradient into the true gradient of the squared Bellman error
Show the answer

B. It makes each phase an ordinary regression toward fixed targets, so the network is not chasing a target that moves with every step

With θ−\theta^- frozen, y=r+γmax⁡a′Q(s′,a′;θ−)y=r+\gamma\max_{a'}Q(s',a';\theta^-) is a fixed label for each transition, and minimizing the loss is supervised regression: one step of approximate fitted Q-iteration per target period.

Question 2

Which target does Double DQN use?

  1. r+γmax⁡a′Q(s′,a′;θ−)r+\gamma\max_{a'}Q(s',a';\theta^-)
  2. r+γmax⁡a′Q(s′,a′;θ)r+\gamma\max_{a'}Q(s',a';\theta)
  3. r+γ Q(s′,arg max⁡a′Q(s′,a′;θ);θ−)r+\gamma\,Q\big(s',\argmax_{a'}Q(s',a';\theta);\theta^-\big)
  4. r+γmin⁡i=1,2Q(s′,a′;θi)r+\gamma\min_{i=1,2}Q(s',a';\theta_i)
Show the answer

C. r+γ Q(s′,arg max⁡a′Q(s′,a′;θ);θ−)r+\gamma\,Q\big(s',\argmax_{a'}Q(s',a';\theta);\theta^-\big)

The online network θ\theta selects the action and the target network θ−\theta^- evaluates it. Selection and evaluation use different noise, so the selected action's estimate is no longer systematically the luckiest one.

Question 3

Why does a dueling head subtract the mean advantage, Q(s,a)=V(s)+A(s,a)−1∣A∣∑a′A(s,a′)Q(s,a)=V(s)+A(s,a)-\frac1{\abs{\mathcal A}}\sum_{a'}A(s,a')?

  1. To make the network's output nonnegative
  2. To reduce overestimation
  3. To normalize the rewards
  4. Without it, a constant could be shifted between VV and AA without changing QQ, so the split would be arbitrary
Show the answer

D. Without it, a constant could be shifted between VV and AA without changing QQ, so the split would be arbitrary

V(s)+cV(s)+c and A(s,a)−cA(s,a)-c give the same QQ for every cc. Centering the advantages pins the decomposition down, so that VV really estimates the state's value, which every transition through the state then helps to learn.

Question 4

In which metric is the distributional Bellman evaluation operator Tπ\mathcal T^\pi a γ\gamma-contraction?

  1. The maximal Wasserstein metric Wˉp\bar W_p
  2. The Kullback–Leibler divergence
  3. The total variation distance
  4. It contracts only in expectation, not in distribution
Show the answer

A. The maximal Wasserstein metric Wˉp\bar W_p

Scaling a random return by γ\gamma scales Wasserstein distances by γ\gamma, and adding a shared reward or mixing over next states does not increase them. KL and total variation do not shrink under scaling: two distinct point masses stay at total-variation distance 1, and infinite KL divergence, however close together they move. It is the control operator that fails to contract.

Further reading

  • V. Mnih et al., "Human-level control through deep reinforcement learning," Nature 518, 529–533 (2015).
  • H. van Hasselt, A. Guez & D. Silver, "Deep reinforcement learning with double Q-learning," AAAI (2016).
  • M. Hessel et al., "Rainbow: Combining improvements in deep reinforcement learning," AAAI (2018).
  • M. G. Bellemare, W. Dabney & R. Munos, "A distributional perspective on reinforcement learning," ICML (2017).
  • M. G. Bellemare, W. Dabney & M. Rowland, Distributional Reinforcement Learning (MIT Press, 2023).