Search

Search papers and lessons

Prioritized Experience Replay

Level 4 · 12 min read

Prioritized experience replay samples the stored transitions an agent learned most from more often, and corrects the resulting bias with importance weights.

On this page

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

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 α=1\alpha = 1 and β=1\beta = 1, and we ignore the small constant that practical implementations add to every priority.

  1. Store each transition with its priority. Each transition gets priority pi=∣δi∣p_i = |\delta_i|, the size of its TD error. So p=(1,4,2,1)p = (1, 4, 2, 1). Check: a transition with error 0 would get priority 0 and never be sampled, which is why practical code adds a small constant.

  2. Turn priorities into probabilities. Divide each priority by the total. The total is 1+4+2+1=81 + 4 + 2 + 1 = 8, so P=(1/8,4/8,2/8,1/8)P = (1/8, 4/8, 2/8, 1/8). Check: the four probabilities add up to 1.

  3. 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.

  4. Compute the importance weight. Each sample gets a weight wi=(NPi)−βw_i = (N P_i)^{-\beta}, where N=4N = 4 is the number of stored transitions. The values NPiN P_i are (0.5,2,1,0.5)(0.5, 2, 1, 0.5), so the raw weights are (2,0.5,1,2)(2, 0.5, 1, 2). Check: the transitions that are sampled most often get the smallest weight.

  5. Normalise the weights. Divide by the largest weight, 2, so that every weight is at most 1. The weights become (1,0.25,0.5,1)(1, 0.25, 0.5, 1). Check: the second transition, which was sampled half the time, now counts for a quarter of a normal update.

  6. Update, then reprioritise. The network learns from the sample by stochastic gradient descent, with the loss multiplied by wiw_i. 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

Definition: Prioritized experience replay

Each stored transition ii has a priority pi=∣δi∣+εp_i = |\delta_i| + \varepsilon, where δi\delta_i is its TD error and ε>0\varepsilon > 0 is a small constant. The sampling probability is

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

where α≥0\alpha \ge 0 controls how strongly priority matters. With α=0\alpha = 0 every transition has the same probability, which is ordinary uniform replay.

The symbol δi\delta_i is the TD error of transition ii, the gap between the agent's current estimate and the target it learns toward. The symbol ε\varepsilon keeps every transition sampleable, and α\alpha 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 α\alpha. This makes the probabilities add to 1 for any buffer. In the walkthrough, α=1\alpha = 1, so P(i)=pi/∑kpkP(i) = p_i / \sum_k p_k.

Step 4 in symbols: the importance weight

wi=(1N P(i))β,w~i=wimax⁡jwj.w_i = \left(\frac{1}{N\,P(i)}\right)^{\beta}, \qquad \tilde{w}_i = \frac{w_i}{\max_j w_j}.

Here NN is the number of stored transitions and β\beta 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 β\beta 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 gig_i be the gradient from transition ii. Under uniform sampling, the average update is 1N∑igi\frac{1}{N}\sum_i g_i. Under prioritized sampling with weights 1NP(i)\frac{1}{N P(i)}, the expected update is

∑iP(i)⋅1N P(i)⋅gi=1N∑igi,\sum_i P(i)\cdot\frac{1}{N\,P(i)}\cdot g_i = \frac{1}{N}\sum_i g_i,

because P(i)P(i) cancels. The expectation is the uniform one again. Normalising by the maximum weight multiplies this by a constant, so with β=1\beta = 1 the bias is removed up to that scale.

The TD error

For a deep Q-network, the TD error of a transition (s,a,r,s′)(s, a, r, s') is

δ=r+γmax⁡a′Q(s′,a′)−Q(s,a),\delta = r + \gamma \max_{a'} Q(s', a') - Q(s, a),

where rr is the reward, γ\gamma is the discount factor, and QQ 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 log⁡2N\log_2 N steps.

Worked examples

Example
1. Two transitions

A buffer holds two transitions with TD errors 1 and 3. Use α=1\alpha = 1, β=1\beta = 1 and ignore ε\varepsilon.

  1. Priorities are p=(1,3)p = (1, 3). Check: they are the absolute errors.
  2. The total is 44, so P=(1/4,3/4)P = (1/4, 3/4). Check: 1/4+3/4=11/4 + 3/4 = 1.
  3. N=2N = 2, so NP=(1/2,3/2)N P = (1/2, 3/2) and the raw weights are (2,2/3)(2, 2/3).
  4. The maximum weight is 2, so the normalised weights are (1,1/3)(1, 1/3). Check: the transition sampled most often, with P=3/4P = 3/4, gets the smaller weight.
  5. Check unbiasedness: ∑iP(i)⋅1NP(i) gi=∑igiN=g1+g22\sum_i P(i)\cdot\frac{1}{N P(i)}\,g_i = \sum_i \frac{g_i}{N} = \frac{g_1 + g_2}{2}, which is the uniform average.
Example
2. A standard case with four transitions

Take the four transitions from the walkthrough, with priorities p=(1,4,2,1)p = (1, 4, 2, 1), α=1\alpha = 1 and β=1\beta = 1. The gradients are g=(0.2,0.8,0.4,0.2)g = (0.2, 0.8, 0.4, 0.2) (in arbitrary units).

  1. Probabilities are P=(1/8,1/2,1/4,1/8)P = (1/8, 1/2, 1/4, 1/8). Check: they sum to 1.
  2. Under uniform sampling the average gradient is 14(0.2+0.8+0.4+0.2)=1.64=0.4\frac{1}{4}(0.2 + 0.8 + 0.4 + 0.2) = \frac{1.6}{4} = 0.4.
  3. The expected prioritized gradient without weights is ∑iP(i) gi=0.025+0.4+0.1+0.025=0.55\sum_i P(i)\,g_i = 0.025 + 0.4 + 0.1 + 0.025 = 0.55. Check: this is larger than 0.4, so the agent would overweight the high-error transitions.
  4. The weights, from the walkthrough, are (1,0.25,0.5,1)(1, 0.25, 0.5, 1) after normalisation. The expected weighted gradient is ∑iP(i) w~i gi=0.025+0.1+0.05+0.025=0.2\sum_i P(i)\,\tilde w_i\, g_i = 0.025 + 0.1 + 0.05 + 0.025 = 0.2.
  5. The unnormalised weights 1/(NP(i))1/(N P(i)) are (2,0.5,1,2)(2, 0.5, 1, 2), and the expected weighted gradient is ∑iP(i)⋅1NP(i) gi=1.64=0.4\sum_i P(i)\cdot\frac{1}{N P(i)}\,g_i = \frac{1.6}{4} = 0.4. Check: this is the uniform average from step 2, so the weights remove the bias exactly.
  6. 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.
Example
3. A sparse reward in a long episode, with a try-it at the end

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 α=1\alpha = 1, β=1\beta = 1, and ε=0\varepsilon = 0.

  1. Total priority is 999⋅0.01+10=9.99+10=19.99999 \cdot 0.01 + 10 = 9.99 + 10 = 19.99.
  2. Under prioritized sampling, the probability of the reward step is 10/19.99≈0.5010 / 19.99 \approx 0.50. Check: about half of the samples include the reward.
  3. Under uniform sampling, the probability is 1/1000=0.0011/1000 = 0.001. Check: that is 500 times lower.
  4. The importance weight for the reward step is (NP)−1=(1000⋅0.50)−1=0.002(N P)^{-1} = (1000 \cdot 0.50)^{-1} = 0.002. For a typical low-error step it is (1000⋅0.01/19.99)−1=(0.5)−1=2(1000 \cdot 0.01/19.99)^{-1} = (0.5)^{-1} = 2. Check: the weights are tiny for the reward step and large for the others, so the normalised weights are 0.002/2=0.0010.002/2 = 0.001 for the reward step and 11 for the others.
  5. 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 9.99+1=10.999.99 + 1 = 10.99, so the probability is 1/10.99≈0.0911/10.99 \approx 0.091. The raw weight is (1000⋅0.091)−1=1/91≈0.011(1000 \cdot 0.091)^{-1} = 1/91 \approx 0.011. A typical low-error step now has probability 0.01/10.99≈0.000910.01/10.99 \approx 0.00091, so its raw weight is (0.91)−1≈1.1(0.91)^{-1} \approx 1.1. 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 α\alpha and the constant ε\varepsilon 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 β\beta reaches 1. If β\beta 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 ∣δ∣α|\delta|^{\alpha}, so surprising steps are replayed more often.
  • Importance weights (NP(i))−β(N P(i))^{-\beta}, normalised by the maximum, cancel the bias this causes, and β\beta is annealed to 1.
  • A sum tree makes each sample and each priority update take about log⁡2N\log_2 N 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.

Check yourself

QuestionWhat does prioritized replay change compared with uniform experience replay, and what does it keep the same?Show answer
It changes the sampling probabilities: each stored transition is drawn with probability proportional to a power of its priority, instead of equally. It keeps the same buffer, the same learning update and the same stored transitions. It then adds importance weights to correct the bias the new sampling introduces.
QuestionA buffer holds three transitions with TD errors 2, 1 and 1, with α=1\alpha = 1 and ε=0\varepsilon = 0. What is the sampling probability of the first transition?Show answer
The priorities are (2,1,1)(2, 1, 1) and their total is 4. So the probability of the first transition is 2/4=0.52/4 = 0.5. Check: the other two each have probability 0.25, and together the three add up to 1.
QuestionWhy does the importance weight use 1/(NP(i))1/(N P(i)) rather than just 1/P(i)1/P(i)?Show answer
The factor NN turns the probability into a fraction of the buffer. With NP(i)N P(i), a transition sampled at its uniform rate 1/N1/N gets weight 1, so the weights measure how much more or less often a transition is sampled than under uniform replay. Without NN, every weight would be scaled by the buffer size, which the normalisation would then cancel anyway, but the meaning would be less clear.
QuestionIn the walkthrough, the second transition has priority 4 out of a total of 8. Its raw weight is 0.5. Why is its normalised weight 0.25, and what does that do to its update?Show answer
The raw weights are (2,0.5,1,2)(2, 0.5, 1, 2), and the largest is 2. Dividing 0.5 by 2 gives 0.25. The transition is sampled half the time, so the weight reduces its update to a quarter of the size a uniform sample would have had. This offsets the extra frequency with which it is drawn.

Sources