Q-learning with a neural network
Tabular Q-learning (the TD vs MC tab) keeps one number per state and action. CartPole's state is four real numbers (cart position and velocity, pole angle and angular velocity), so there is no table to fill; instead a neural network maps a state to one value per action, and the agent pushes whichever way has the larger value. Each step of the pole staying up pays 1, and an episode ends when the pole tips past 12°, the cart leaves the track, or after 500 steps. A perfect policy is worth with .
The update is Q-learning's, as a regression: move towards the TD target
and when the pole fell. The loss is the squared error , averaged over a minibatch, and Adam takes the step. The One update table shows eight samples of a minibatch recorded at step 100,000 and the loss it produced.
Why it is unstable
This combination of bootstrapping, function approximation and off-policy data is what Sutton and Barto call the deadly triad: nothing guarantees it converges. Two problems are specific to learning from a stream of experience. First, consecutive states are nearly identical, so a minibatch of recent steps is effectively one example repeated, and the network overfits to whatever it is doing right now. Second, the target depends on the same network that is being trained: every step that raises also raises for similar states, and so the target it is chasing. DQN (Mnih et al., 2013 and 2015) added one fix for each.
The replay buffer
Every transition goes into a buffer of the last 50,000 steps, and each update draws its 64 samples uniformly from all of it. The What it learns from panel shows a sample of the buffer at four points of training, as pole angle against angular velocity, darker for newer. A uniform draw mixes many episodes and every part of the state space; each transition is reused many times. The ablation without replay learns from the 64 newest transitions only, in order: in the panel's second chart they are one thin curve through state space, about a second of play from one or two episodes.
The target network
The target uses a second network , a copy of refreshed every 500 steps. Between copies the targets stay put, so each update is ordinary regression towards fixed numbers. The cost is speed: values can only propagate one copy at a time. In a first run with a copy every 1,000 steps for 100,000 steps, the value of the upright state reached about 66, close to for 100 copies, and the policy never held the pole for 500 steps; the recorded runs copy every 500 steps for 200,000.
Without a target network, . The Q at four fixed states charts show what happens: in the full runs the values climb towards about 100 and overshoot slightly (to 109–116 at the upright state), since the max in the target picks up noise as value. Without the target network they keep climbing: to about 198 in seed 0, and in seed 1 to over 10,000 at the upright state and 253,000 at another, with a TD loss of 220,000. Remarkably the greedy policy can still balance: only the difference between the two actions' values decides the push, and that difference can stay right while both run away.
Reading the runs
Each version was trained with three seeds; the Greedy return chart shows all nine, evaluated every 5,000 steps on ten fixed episodes with no exploration, and the Every run table the mean of the last ten evaluations. CartPole is forgiving, so every version learns to balance some of the time, and the differences show in how reliably. Of the 40 evaluations of each run, full DQN balanced for all 500 steps in 20, 20 and 25. Without the target network the values diverged in all three seeds (to 2,967, 253,162 and 65,435 at some probe state) and the runs held 500 in 15, 15 and 13. Without replay the values stayed near 100, but the runs held 500 in only 9, 7 and 11: each batch is the last 64 steps of play, so what the network learned about other states fades. Even full DQN is not monotone; a run that balances for 500 steps can drop to 150 at the next evaluation and recover. That noise is normal for DQN, and one reason later work compared methods over many seeds.
What followed
Replay goes back to Lin (1992); DQN made it work with deep networks on Atari from pixels. Double DQN (van Hasselt et al.) chooses the next action with the online network and evaluates it with the target network, which removes most of the overestimation seen here; prioritised replay samples surprising transitions more often. Policy-gradient methods such as PPO avoid the max over actions altogether.
Papers
- Human-level control through deep reinforcement learning DQN with a replay buffer and a periodically copied target network, the two pieces this page removes one at a time.
- Playing Atari with Deep Reinforcement Learning The first DQN: Q-learning with a convolutional network and experience replay, before the target network was added.
- Self-improving reactive agents based on reinforcement learning, planning and teaching Introduces experience replay for reinforcement learning with neural networks.
- Neuronlike adaptive elements that can solve difficult learning control problems The cart-pole balancing task, in the form Gymnasium's CartPole implements.
- Deep Reinforcement Learning with Double Q-learning Shows DQN's max overestimates values and fixes it by decoupling action choice from evaluation.
- Deep Reinforcement Learning and the Deadly Triad Measures when bootstrapping, function approximation and off-policy learning make values diverge, and how target networks help.