Search

Search papers and lessons

Prioritized Experience Replay

Tom Schaul, John Quan, Ioannis Antonoglou et al. · 2015

Prioritized experience replay samples stored transitions in proportion to how surprising they were, with a correction for the bias this adds. Added to DQN, it beat uniform replay on 41 of 49 Atari games.

On this page

  • Colour = topic mix
  • Probability
  • Reinforcement learning
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Agents that learn from a replay memory usually sample old experience uniformly at random, so a rare, informative transition is replayed no more often than a boring one. Schaul and colleagues sample transitions with probability that grows with their temporal-difference error, the size of the surprise between the agent's prediction and its updated estimate. They keep the sampling unbiased in the long run with importance-sampling weights that are annealed to full strength by the end of training. Added to DQN and Double DQN, the method improved the median score across the 49 original Atari games from 48 percent to 106 percent of human level, and it beat uniform replay on 41 of those 49 games.

Builds on: Experience Replay, Deep Q-Network, Temporal-Difference Learning, Importance Sampling · Leads to: later replay methods and the Rainbow combination of value-learning improvements (see Rainbow: Combining Improvements in Deep Reinforcement Learning)

Why it mattered

Most deep reinforcement learning agents trained on images or simulators keep a replay memory, because reusing old experience is cheap and stabilises learning. This paper changed one line of that pipeline, the rule for choosing which stored experience to replay, and got a large gain on the standard Atari suite. The authors report that the boost is complementary to Double Q-learning, so the two can be combined. The prioritized Double DQN agent set a new state of the art on Atari when it appeared, and the paper reports that, on aggregate, learning is about twice as fast.

The idea spread because it costs little. The paper reports a 2 to 4 percent increase in running time for the rank-based variant, and the same sampling idea reappears in many later agents.

History

Experience replay itself is older than deep learning. The paper credits Lin (1992) for the idea of reusing stored transitions. The Deep Q-Network (Mnih and colleagues, 2013 and 2015) made it central to deep reinforcement learning. DQN sampled its memory uniformly and revisited each transition about eight times on average. Earlier work had already used prediction error to decide where to focus effort, notably prioritized sweeping in planning, and this paper adapts that idea to model-free learning with sampled transitions.

The problem

A DQN agent stores its experience in a sliding window of recent transitions and replays them uniformly at random. Two things go wrong. First, a transition the agent already predicts well is replayed as often as a transition that is badly wrong, so most updates teach little. Second, rare, valuable experience, such as the single success in a long chain of failures, is replayed only a few times before it leaves the window.

The paper's motivating example makes the cost concrete. In the Blind Cliffwalk task with nn states, a random sequence of actions reaches the reward with probability 2−n2^{-n}, and the one useful transition is hidden among highly redundant failures. Replaying the transitions in a good order can learn it in exponentially fewer updates than uniform replay needs.

How it works

We trace a tiny replay memory with three transitions, each with a stored error. The transition's priority is the size of its last TD error, ∣δ∣|\delta|. For this trace we use the simplest choice: priority equals ∣δ∣|\delta|, and the exponent α\alpha is 1.

  1. Store with a priority. Each new transition enters the memory with the largest priority seen so far. This makes sure every transition is replayed at least once before its error is known. Check: if the largest priority so far is 2.0, a new transition gets 2.0.
  2. Compute each priority. Suppose three stored transitions have last errors ∣δ∣=0.1|\delta| = 0.1, 0.50.5 and 2.02.0. Their priorities are these values.
  3. Turn priorities into probabilities. Divide each priority by the total, 0.1+0.5+2.0=2.60.1 + 0.5 + 2.0 = 2.6. The sampling probabilities are about 0.038, 0.192 and 0.769. Check: they sum to 1.
  4. Sample a minibatch. Draw transitions from these probabilities. The high-error transition is drawn about 77 percent of the time, and the low-error one about 4 percent.
  5. Update and refresh the priority. After a learning step on a sampled transition, its priority is set to its new TD error. Transitions that become well predicted drop in priority and are replayed less often.
  6. Correct the bias. Replaying surprising transitions more often changes the distribution the updates come from, so each update is scaled by an importance-sampling weight. The weight is largest for rarely drawn transitions, and the scaling is reduced to keep updates stable (the math section gives the formula).

Here is the loop in the full agent, from Algorithm 1 in the paper:

flowchart LR
  A["Act and store transition with max priority"] --> B["Every K steps: sample minibatch by priority"]
  B --> C["Compute TD error and IS weight"]
  C --> D["Update network with weighted TD error"]
  D --> E["Set each sampled priority to its new |TD error|"]
  E --> A

Two implementation points make this practical with a million stored transitions. The rank-based variant keeps the memory in an approximately sorted binary heap, which is a tree-shaped array where the largest value sits at the top, and samples from fixed segments. The proportional variant keeps a sum-tree, where each parent stores the sum of its children, so a sampling value can be found by walking down the tree in logarithmic time.

The math

Let the memory hold NN transitions ii with priorities pi>0p_i \gt 0. The two variants are defined as follows.

Step 2 in symbols: the priorities

The proportional variant sets pi=∣δi∣+ϵp_i = |\delta_i| + \epsilon, where ϵ\epsilon is a small positive constant so that a zero error still gives a non-zero chance. The rank-based variant sets pi=1rank⁡(i)p_i = \frac{1}{\operatorname{rank}(i)}, where rank⁡(i)\operatorname{rank}(i) is the position of transition ii when the memory is sorted by ∣δ∣|\delta| from largest to smallest.

Step 3 in symbols: the sampling probability

The probability of sampling transition ii is

P(i)=piα∑kpkα.P(i) = \frac{p_i^{\alpha}}{\sum_k p_k^{\alpha}}.

The exponent α\alpha sets how much prioritization is used. With α=0\alpha = 0 every transition has the same probability, which is uniform replay. With α=1\alpha = 1 the probability is proportional to the priority. The paper's tuned values are α=0.7\alpha = 0.7 for rank-based and α=0.6\alpha = 0.6 for proportional.

Step 5 in symbols: the priority update

After a transition is replayed and its new TD error is computed, its priority is set to pi←∣δi∣p_i \leftarrow |\delta_i| (plus ϵ\epsilon in the proportional variant). The TD error is

δi=ri+γi Qtarget ⁣(si′,arg⁡max⁡aQ(si′,a))−Q(si−1,ai−1),\delta_i = r_i + \gamma_i \, Q_{\text{target}}\!\left(s_i', \arg\max_a Q(s_i', a)\right) - Q(s_{i-1}, a_{i-1}),

which is the Double DQN form: the online network chooses the next action and the target network scores it.

Step 6 in symbols: the importance-sampling correction

Each sampled transition gets the weight

wi=(1N⋅1P(i))β,w_i = \left( \frac{1}{N} \cdot \frac{1}{P(i)} \right)^{\beta},

which fully undoes the non-uniform sampling when β=1\beta = 1. The paper's Algorithm 1 uses the equivalent form (NP(j))−β(N P(j))^{-\beta} and divides by the largest weight in the minibatch, so the weights only scale updates downward. The weighted TD error wiδiw_i \delta_i replaces δi\delta_i in the update.

The schedule for β\beta starts at a value β0\beta_0 and rises linearly to 1 at the end of training:

β(t)=β0+(1−β0)⋅tT,\beta(t) = \beta_0 + (1 - \beta_0) \cdot \frac{t}{T},

where tt is the current step and TT is the total number of steps.

Worked examples

Example
Example 1: priorities, probabilities and weights by hand

Use three transitions with priorities 0.10.1, 0.50.5 and 2.02.0, so N=3N = 3, α=1\alpha = 1, ϵ=0\epsilon = 0, and β=1\beta = 1.

  1. The total is 0.1+0.5+2.0=2.60.1 + 0.5 + 2.0 = 2.6.
  2. The probabilities are P(1)=0.1/2.6≈0.0385P(1) = 0.1 / 2.6 \approx 0.0385, P(2)=0.5/2.6≈0.1923P(2) = 0.5 / 2.6 \approx 0.1923, and P(3)=2.0/2.6≈0.7692P(3) = 2.0 / 2.6 \approx 0.7692.
  3. Raw weights are 1/(NP(i))1 / (N P(i)). For transition 1: 1/(3⋅0.0385)≈8.6671 / (3 \cdot 0.0385) \approx 8.667. For transition 2: 1/(3⋅0.1923)≈1.7331 / (3 \cdot 0.1923) \approx 1.733. For transition 3: 1/(3⋅0.7692)≈0.4331 / (3 \cdot 0.7692) \approx 0.433.
  4. The largest raw weight is 8.6678.667, so divide every weight by it.
  5. The normalised weights are 11, 0.20.2 and 0.050.05.

Interpretation: the rarely drawn transition 1 keeps full weight, and the frequently drawn transition 3 is scaled down to a twentieth. Its update is still applied, but its share of the total change is smaller.

Example
Example 2: the paper's replay ratio and the annealing schedule

The Atari setup in the paper keeps a sliding window of 10610^6 transitions and performs one minibatch update of 32 transitions for each 4 new transitions.

  1. Each update samples 32 transitions.
  2. There is one update per 4 new transitions, so there are 32/4=832 / 4 = 8 samples per new transition on average.
  3. This matches the paper's statement that every transition is replayed about 8 times in the uniform baseline.
  4. The proportional variant uses β0=0.4\beta_0 = 0.4. At 25 percent of training, t/T=0.25t / T = 0.25, so β=0.4+(1−0.4)⋅0.25=0.4+0.15=0.55\beta = 0.4 + (1 - 0.4) \cdot 0.25 = 0.4 + 0.15 = 0.55.
  5. At the midpoint, β=0.4+0.6⋅0.5=0.7\beta = 0.4 + 0.6 \cdot 0.5 = 0.7. At the end, β=1\beta = 1.

Interpretation: early in training the correction is weak, so learning is faster. By the end the correction is complete, so the final solution is unbiased.

Example
Example 3: reading the paper's Atari results

The text reports median normalised scores across the 49 original games (random play is 0 percent, human is 100 percent). Plain DQN has a median of 48 percent, and DQN with rank-based prioritized replay has a median of 106 percent.

  1. The gain in median is 106−48=58106 - 48 = 58 percentage points.
  2. For Double DQN over 57 games, the median rises from 111 percent to 128 percent.
  3. The gain is 128−111=17128 - 111 = 17 percentage points.
  4. The mean over 57 games rises from 418 percent to 551 percent, a gain of 133 points. The paper warns that one game, Video Pinball, dominates the mean, so the median is the safer number.
  5. The paper's learning-speed summary says rank-based prioritization reaches Double DQN's median-of-maximum score at 47 percent of total training time, and proportional at 38 percent.

Try it: a run uses 200 million frames of training, as in the paper's learning curves. At 47 percent of training, how many frames have been seen, and why is the median gain over plain DQN larger than the gain over Double DQN? Answer: 0.47⋅200=940.47 \cdot 200 = 94 million frames. The gain over plain DQN is larger because plain DQN starts from a weaker baseline (48 percent) than Double DQN (111 percent), so there is more room to improve.

Results

All numbers come from the paper's text and captions. The Atari learning curves are single training runs, and the headline comparisons use human-starts evaluation.

  • Atari, DQN baseline (49 games). Prioritized replay improves the score on 41 of 49 games. The median normalised score rises from 48 to 106 percent.
  • Atari, Double DQN baseline (57 games). The median rises from 111 to 128 percent and the mean from 418 to 551 percent. The paper names games such as River Raid, Seaquest and Surround that reach human level for the first time.
  • Learning speed. The paper summarises learning speed as equivalence points, the fraction of training at which the prioritized variant matches the Double DQN baseline. For the maximum-score curve these are 47 percent (rank-based) and 38 percent (proportional). For the mean-score curve they are 41 and 43 percent. Section 4 states that learning is about twice as fast on aggregate.
  • Cliffwalk (Figures 1 and 2). The oracle and the prioritized variants learn with far fewer updates than uniform replay, with differences of several orders of magnitude on a log scale. This is the paper's evidence that order matters.
  • Class-imbalanced MNIST (Section 6). With 99 percent of the examples for digits 0 to 4 removed, uninformed prioritized sampling beats uniform sampling on the test set and approaches the uniform baseline that was told the imbalance. The paper presents this in its Section 6 extensions, alongside other ideas.

Common mistakes and tradeoffs. It is tempting to think that replaying high-error transitions more often always helps. The paper's finding is that the bias it creates must be corrected near convergence, and that it matters most at the end of training. It is tempting to think that the TD error is the true measure of what an agent can learn. In fact the paper says the TD error is only a proxy for expected learning progress, and it is unreliable when rewards are noisy; its Appendix A discusses alternatives, such as priorities based on how much the weights change and asymmetric treatment of positive and negative errors, as possible improvements. It is tempting to think that the two variants differ a lot. The paper finds they usually perform similarly, and it suspects the reward and error clipping used by DQN is why.

When to use prioritized replay: a learner that reuses a large stored memory and can afford the extra bookkeeping, with a training signal whose error is informative. When not to use it: when the memory is small enough that uniform sampling already covers everything, when rewards are very noisy so that errors mostly measure noise, or when the extra storage and tree updates are too costly. Alternatives include the staleness bonus and the hybrid sampling that the paper discusses in its appendix, and the off-policy correction it describes in Section 6.

Reading guide

  1. Abstract and Section 1. The two problems with uniform replay, and the idea of replaying important transitions more often. Needs Experience Replay and Deep Q-Network.
  2. Section 2 (Background). Read it for context on prioritized sweeping and TD error as a priority. Needs Temporal-Difference Learning.
  3. Section 3.1 (Motivating example). Figure 1 and Figure 2 show the Blind Cliffwalk. Needs Q-Learning for the update used there.
  4. Section 3.2 (Prioritizing with TD error). The greedy priority and its failure modes. Needs Bellman Equation for the TD error.
  5. Section 3.3 (Stochastic prioritization). Equation 1 and the two variants. Needs Priority Queue and Binary Tree for the implementation paragraphs. Work Example 1 alongside.
  6. Section 3.4 (Annealing the bias). The importance-sampling weights. Needs Importance Sampling and the schedule for β\beta.
  7. Section 4 (Atari experiments) and Algorithm 1. The results. Needs Double Q-Learning and Deep Reinforcement Learning with Double Q-Learning for the baseline.
  8. Sections 5 to 7 and the appendices. Discussion, extensions, and implementation details. Read Appendix B.2.1 for the heap and sum-tree.

Check yourself

QuestionIn one or two sentences, why does the paper need importance-sampling weights if it wants prioritized replay to learn the right values?Show answer

Prioritized sampling replays some transitions more often than their natural frequency, so the average update no longer matches the true expectation under the environment. The importance-sampling weights scale each update by the inverse of how much it was over-sampled, which removes that bias when β=1\beta = 1. The paper anneals β\beta toward 1 so that early learning is faster and the final solution is unbiased.

QuestionThree transitions have priorities 1, 2 and 5 under proportional prioritization with α=1\alpha = 1 and ϵ=0\epsilon = 0. What is the probability of sampling the transition with priority 5?Show answer

The total is 1+2+5=81 + 2 + 5 = 8, so the probability is 5/8=0.6255 / 8 = 0.625, or 62.5 percent.

QuestionA new transition enters the memory before its TD error is known. Why does the paper give it the largest priority seen so far?Show answer

A transition that has never been replayed has no measured error, so it could be wrongly ignored if it started with a low priority. Giving it the maximum priority guarantees that every transition is replayed at least once before the agent relies on its error.

QuestionA Double DQN run uses 200 million frames, and the rank-based variant reaches the equivalence point at 47 percent of training on the maximum-score curve. Roughly how many frames is that, and what does the number mean?Show answer

0.47⋅200=940.47 \cdot 200 = 94 million frames. It means the prioritized agent reaches the median score that the Double DQN baseline reaches over its whole run, about halfway through training, so prioritized replay learns faster in frames.

QuestionA game designer uses prioritized replay for a bandit-like problem where each episode lasts one step and rewards are very noisy. Name one reason the paper's method may fit poorly, using the paper's own discussion.Show answer

The paper says the TD error can be a poor estimate when rewards are noisy, because high error may just reflect noise. Its own appendix notes the TD error ignores inherent stochasticity, which is the situation here. The designer should consider the alternatives the paper discusses, or check whether uniform replay already works.

Sources

Read the full paper

Tom Schaul, John Quan, Ioannis Antonoglou et al., 2015. arXiv:1511.05952