TL;DR
A deep Q-network, or DQN, is Q-learning where the table of action values is replaced by a neural network. The network takes a state and outputs one value per action, so it can handle states too numerous to tabulate, such as raw screen pixels. Naive training of such a network is unstable, so DQN adds two things: a replay buffer that breaks up the correlation between consecutive samples, and a target network that holds the regression target still for many updates.
Where you'll see it: the 2015 Atari agent that learned dozens of games from pixels, and the template for later value-based agents in games, robot control and recommendation.
Intuition
Q-learning keeps one number per state-action pair. That works for a maze with 25 cells. It does not work for a game whose screen has more possible images than there are atoms in the room you are in. The fix is to stop storing the numbers and instead learn a function that produces them. A neural network can take the screen as input and output a score for each joystick action. Nearby screens get similar scores, so what it learns about one situation carries over to similar ones.
The trouble is that the thing being learned keeps moving. In supervised learning the targets are fixed labels. Here, the target for a state depends on the network's own estimate of the next state, and that estimate changes after every update. The network is chasing a target that shifts whenever it moves. Two consecutive frames of a game are also nearly identical, so the training data is highly correlated, which makes gradient steps noisy and biased in one direction.
DQN handles both problems with simple machinery:
- A replay buffer stores recent transitions and trains on random samples from it, so each minibatch mixes different moments of play. See experience replay.
- A target network is a copy of the Q-network whose weights are held fixed and refreshed only every few thousand steps. Targets come from this copy, so they stop moving with every step.
flowchart LR A["Agent acts with epsilon-greedy policy"] --> B["Store transition in replay buffer"] B --> C["Sample random minibatch"] C --> D["Compute targets with frozen target network"] D --> E["Gradient step on online network"] E --> F["Copy weights to target network every C steps"] F --> D E --> A
The math
Setup
The agent sees a state and picks an action from a finite set . It receives reward and next state , and the goal is to maximise the discounted return with discount factor . The optimal action-value function is the expected return from taking in and then acting optimally. Q-learning estimates it with an update rule. DQN estimates it with a function approximator , where is the vector of network weights.
The loss
For a stored transition , the target is
where are the weights of the target network and is 1 if the episode ended. The loss is the squared gap between the prediction and the target, averaged over a minibatch :
Only the online weights receive gradients. The target is treated as a constant. The 2015 paper also clipped the error term to the interval inside the update, so that a single huge error cannot dominate a step. (This is equivalent to using a Huber loss, which is quadratic for small errors and linear for large ones.) The gradient with respect to a weight, per transition and before clipping, is
and in practice the in this expression is the clipped one.
which is backpropagated through the network, the same way as in any feedforward network trained with stochastic gradient descent.
Exploration and the update schedule
The agent acts -greedily: with probability it picks a random action, otherwise the action with the largest . The value of is annealed from near 1 down to a small value over the first part of training. The online network is updated every few agent steps, and every steps the target weights are overwritten: .
Input for images
For Atari, the raw frame is converted to grayscale and downsampled to . The network input stacks the last four frames, so a single input carries motion. The first layers are convolutional, and the final layer is linear with one output per action.
Worked examples
To keep the arithmetic visible, use a linear approximator with one weight per action: , where is a scalar feature of the state. Take , learning rate , and the transition: state feature , action left, reward , next feature , not terminal. The weights are and online, and and in the target network.
- Prediction: .
- Target uses the target network at : the values are for left and for right. Maximum is , so .
- Error: . It is within , so no clipping.
- Gradient for : .
- Step: .
- New prediction: .
The estimate moved from 1.0 to 1.28, toward the target 1.35. It covered 0.28 of the 0.35 gap. The step size here is of the error, because this linear model is very sensitive to . With a deep network the same logic holds, but the step is spread across millions of weights.
Use . Three transitions come out of the replay buffer, each with the target network's maximum next-state value already computed.
| Transition | Reward | Done | Target max next value | Target | Prediction | Error | Clipped |
|---|---|---|---|---|---|---|---|
| 1 | 1 | no | 10 | 9.5 | 1.4 | 1.0 | |
| 2 | 0 | yes | 7 | 0.5 | |||
| 3 | 1 | no | 4 | 4.0 | 0.96 | 0.96 |
Transition 2 is terminal, so the next-state value is dropped and the target is just the reward. Forgetting this flag is a common bug, because the agent then learns that the value of a finished episode is the value of a non-existent future.
Transition 1 has an error of 1.4 and is clipped to 1.0. Without clipping it would dominate the minibatch gradient. Capping the error limits how much any single transition can move the weights. The 2015 agent also clipped every reward to , and together these let it use one learning rate across games with very different reward scales.
Tradeoffs and failure modes
DQN works for discrete actions and high-dimensional observations. It is a poor fit for continuous action spaces, where you would use an actor-critic method. Its main weaknesses are the following.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Continuous control (joint torques) | Taking a max over actions is intractable | Actor-critic methods such as DDPG or SAC |
| Rare, delayed rewards | Replay is full of uninformative transitions | Prioritised replay, reward shaping with care |
| Maximum over noisy estimates | Q-values are systematically overestimated | Double DQN, which selects the action with the online network and evaluates it with the target network |
| Long training on one environment | Little generalisation beyond the training setup | Model-based methods, or training across varied environments |
| Huge replay memory of old data | Stale transitions from an old policy | Smaller buffer, or on-policy methods |
| Epsilon anneals too fast | Agent locks onto a poor action early | Slower schedule, or a count-based exploration bonus |
The overestimation row is worth a moment. The max over noisy estimates is biased upward, and the target network does not remove that bias. Double DQN is a one-line change that reduces it.
History
Q-learning was introduced by Chris Watkins in his 1989 PhD thesis, with convergence proved for the tabular case by Watkins and Dayan in 1992. Experience replay was proposed by Long-Ji Lin in 1992 as a way to reuse past transitions in neural-network reinforcement learning. Gerald Tesauro's TD-Gammon, in the early 1990s, used a neural network with temporal-difference learning to reach expert backgammon play, but it did not use a replay buffer or a target network, and it did not scale to pixels.
Attempts to combine neural networks with Q-learning often diverged. The breakthrough came from Volodymyr Mnih and colleagues at DeepMind. Their 2013 workshop paper trained one convolutional network on Atari screens and reached strong play on several games. Their 2015 Nature paper extended the approach to 49 games with one set of hyperparameters and reported more than 75 percent of a professional human tester's score on 29 of them. The two stabilisers, replay and the target network, were what made the training reliable across games. Later work refined the method. Van Hasselt's Double DQN reduced overestimation, and Hessel and colleagues combined several such improvements in Rainbow in 2018.
Check yourself
QuestionName the two additions that DQN makes to plain Q-learning with a neural network, and say what problem each one solves.Show answer
The first is a replay buffer. It stores transitions and trains on random minibatches, which breaks the correlation between consecutive frames and reuses old data. The second is a target network, a copy of the Q-network with weights held fixed for many steps. It stops the regression target from moving every time the online weights change.
QuestionIn a DQN update, , , the target network's largest next-state value is 6, the episode does not end, and the prediction is . What is the clipped error used in the update?Show answer
The target is . The raw error is . Clipping to gives . The clipped error is 1.
QuestionWhy are the targets computed with the frozen target network rather than the online network being trained?Show answer
If the same weights produced both the prediction and the target, each gradient step would move the target as well, and the network would chase its own moving estimate. This tends to make training diverge. Freezing the target weights for steps gives the online network a fixed regression problem for that stretch, and the target is refreshed only occasionally.
QuestionA team trains a DQN on a robot task. After two days the agent repeats one action forever, and its Q-values grow steadily without bound. Name two likely causes and a change for each.Show answer
One likely cause is exploration that stopped too early, so the agent never sampled the alternatives, and the fix is a slower epsilon schedule or a warm-up period with more random actions. Another is overestimation from the max over noisy values, which can make Q-values drift upward, and the fix is Double DQN or reward scaling so that targets stay in a sensible range. A third cause, worth checking, is a replay buffer dominated by the repeated action, which calls for a larger buffer or a warm-up period before updates begin.
Sources
- Mnih et al., Playing Atari with Deep Reinforcement Learning (2013): https://arxiv.org/abs/1312.5602
- Mnih et al., Human-level control through deep reinforcement learning, Nature (2015): https://www.nature.com/articles/nature14236
- Sutton and Barto, Reinforcement Learning: An Introduction, 2nd ed.: http://incompleteideas.net/book/the-book-2nd.html
- Q-learning (Wikipedia): https://en.wikipedia.org/wiki/Q-learning