TL;DR
Prioritized experience replay is a change to the replay buffer of a reinforcement learning agent. The buffer stores past steps, and the agent trains on random samples from it. Instead of sampling every stored step equally, this method samples steps in proportion to how surprising they were, measured by the size of their TD error. Because this changes which steps the agent sees, each sample is given an importance weight that corrects the bias it introduces.
Builds on: Experience Replay, Importance Sampling, Temporal-Difference Learning · Leads to: Rainbow-style agents that combine several replay and learning improvements
Where it's used
Atari game agents. The 2015 deep Q-network learned to play 49 Atari games from raw pixels using a uniform replay buffer. Prioritized replay was tested on the same games and improved results on most of them, in the paper by Schaul and colleagues that introduced the method.
Combined agents. The Rainbow agent of 2017 put six improvements to deep Q-learning into one system, and prioritized replay was one of them. Its ablation studies found that removing the prioritization lowered performance, which is why the component is now standard in value-based agents.
Sparse-reward tasks. In a game or robot task where a reward appears only once in thousands of steps, the transitions around that reward are rare but very informative. Uniform sampling sees them only occasionally. Prioritization makes sure they are replayed often enough to be learned from.
How it works
Here is the method as a process, for one training step.
flowchart LR A["Store each transition with a priority"] --> B["Sample a transition with probability proportional to its priority"] B --> C["Compute the TD error and the importance weight"] C --> D["Update the network, scaled by the weight"] D --> E["Write the new TD error back as the new priority"] E --> B
Let us trace one tiny buffer of four stored transitions. Their TD errors have magnitudes 1, 4, 2 and 1. We use and , and we ignore the small constant that practical implementations add to every priority.
-
Store each transition with its priority. Each transition gets priority , the size of its TD error. So . Check: a transition with error 0 would get priority 0 and never be sampled, which is why practical code adds a small constant.
-
Turn priorities into probabilities. Divide each priority by the total. The total is , so . Check: the four probabilities add up to 1.
-
Sample. Draw one transition using these probabilities. The second transition is drawn half the time, and the first and fourth one eighth of the time each. Check: over 8 draws, the second transition is drawn about 4 times on average.
-
Compute the importance weight. Each sample gets a weight , where is the number of stored transitions. The values are , so the raw weights are . Check: the transitions that are sampled most often get the smallest weight.
-
Normalise the weights. Divide by the largest weight, 2, so that every weight is at most 1. The weights become . Check: the second transition, which was sampled half the time, now counts for a quarter of a normal update.
-
Update, then reprioritise. The network learns from the sample by stochastic gradient descent, with the loss multiplied by . After the update, the new TD error for that transition replaces its old priority. Check: the transition's priority now reflects what the network has learned, so it will be sampled less often if it has become easy.
Why does step 5 matter? Without the weights, the agent would learn most from the transitions it samples most often, and its estimate of the value would be skewed toward them. With the weights, the expected effect of the update matches what uniform sampling would give.
The math
Definition
Each stored transition has a priority , where is its TD error and is a small constant. The sampling probability is
where controls how strongly priority matters. With every transition has the same probability, which is ordinary uniform replay.
The symbol is the TD error of transition , the gap between the agent's current estimate and the target it learns toward. The symbol keeps every transition sampleable, and is an exponent that softens or sharpens the priorities.
Step 2 in symbols: the sampling probability
The denominator is the sum of all priorities raised to the power . This makes the probabilities add to 1 for any buffer. In the walkthrough, , so .
Step 4 in symbols: the importance weight
Here is the number of stored transitions and controls how fully the bias is corrected. Dividing by the maximum keeps every weight at most 1, which keeps the update size stable. The paper anneals from a small starting value up to 1 over training, so the correction is weak at first and complete at the end.
Why the weights remove the bias
Let be the gradient from transition . Under uniform sampling, the average update is . Under prioritized sampling with weights , the expected update is
because cancels. The expectation is the uniform one again. Normalising by the maximum weight multiplies this by a constant, so with the bias is removed up to that scale.
The TD error
For a deep Q-network, the TD error of a transition is
where is the reward, is the discount factor, and is the value estimate. This is the same error used by Q-learning, so the priority tells the agent how wrong its estimate was the last time it saw this transition.
Efficient sampling with a sum tree
Drawing from a distribution over a million transitions by scanning all priorities would be too slow. The usual implementation is a sum tree, a Binary Tree whose leaves hold the priorities and whose internal nodes hold the sum of their two children. To sample, draw a random number between 0 and the total at the root, then walk down: go left if the number is less than the left child's sum, otherwise subtract that sum and go right. Each sample and each priority update takes about steps.
Worked examples
A buffer holds two transitions with TD errors 1 and 3. Use , and ignore .
- Priorities are . Check: they are the absolute errors.
- The total is , so . Check: .
- , so and the raw weights are .
- The maximum weight is 2, so the normalised weights are . Check: the transition sampled most often, with , gets the smaller weight.
- Check unbiasedness: , which is the uniform average.
Take the four transitions from the walkthrough, with priorities , and . The gradients are (in arbitrary units).
- Probabilities are . Check: they sum to 1.
- Under uniform sampling the average gradient is .
- The expected prioritized gradient without weights is . Check: this is larger than 0.4, so the agent would overweight the high-error transitions.
- The weights, from the walkthrough, are after normalisation. The expected weighted gradient is .
- The unnormalised weights are , and the expected weighted gradient is . Check: this is the uniform average from step 2, so the weights remove the bias exactly.
- Normalising divides every weight by the largest one, 2. That is why the expected update in step 4 is 0.2 and not 0.4: the normalisation shrinks every update by the same factor of 2, which keeps the step size stable but leaves the direction unchanged.
An agent plays an episode of 1,000 steps. Only one step, step 900, gives a reward of 10. The other 999 steps have TD error about 0.01, and the reward step has TD error 10. Use , , and .
- Total priority is .
- Under prioritized sampling, the probability of the reward step is . Check: about half of the samples include the reward.
- Under uniform sampling, the probability is . Check: that is 500 times lower.
- The importance weight for the reward step is . For a typical low-error step it is . Check: the weights are tiny for the reward step and large for the others, so the normalised weights are for the reward step and for the others.
- Try it: if the reward step's TD error instead were 1, what would its sampling probability be, and what would its raw weight be, given that the other steps still total 9.99? Answer: the total becomes , so the probability is . The raw weight is . A typical low-error step now has probability , so its raw weight is . The reward step is still weighted far below the others.
Common mistakes and tradeoffs
It is tempting to think high-error transitions are always the most useful ones. In fact a large TD error can come from noise, such as a random reward or a value estimate that has not yet been learned, and replaying it over and over can waste updates. The exponent and the constant control how far this goes.
It is tempting to think prioritization on its own removes the bias it causes. In fact the bias is corrected only by the importance weights, and only when reaches 1. If is left small, the agent keeps a skewed estimate of the value for the whole run.
It is tempting to think prioritization is free. In fact it costs memory for the tree and time for every priority update, and a priority is only as good as the last time its transition was used. Transitions that have not been sampled for a long time carry stale priorities.
When to use it. Use it when the useful transitions are rare, as in sparse-reward tasks or when a few events matter much more than the rest, and when the buffer is large enough for the sampling bias to matter. It works best on top of a value method that already uses replay, such as DQN.
When not to use it. Skip it when the buffer is small and every transition is about equally informative, when the environment is simple enough for a table, or when the extra bookkeeping costs more than the faster learning it gives. Alternatives include uniform replay with more updates, and methods that do not use replay at all.
History
Replay of past experience in learning systems goes back to Long-Ji Lin's work in 1992. Deep reinforcement learning made it central in 2013, when the first deep Q-network paper used a replay buffer to stabilise training on Atari games. The 2015 Nature paper, Human-level control through deep reinforcement learning, used a uniform buffer across 49 games.
In 2015 Tom Schaul and colleagues at DeepMind proposed sampling the buffer by TD error, with the importance weights to correct for it. The paper was published at ICLR in 2016. It caught on because it improved results on the same benchmark with a change that was small to implement, and because the sum tree made it fast enough to use on large buffers.
Recap
- Experience replay stores past steps and trains on random samples from them.
- Prioritized replay samples each step with probability proportional to , so surprising steps are replayed more often.
- Importance weights , normalised by the maximum, cancel the bias this causes, and is annealed to 1.
- A sum tree makes each sample and each priority update take about steps.
Read next: Rainbow shows how prioritized replay combines with other improvements to deep Q-learning. The Q-Learning and Deep Q-Network lessons explain the TD error that sets each priority.