Skip to the lesson
FixTheRobot Explore Robotics / Robot Learning / M8 Offline and Data-Efficient Learning / M8-T5

Robot Learning · Theory · M8

M8-T5 — Replay Buffers and Data Efficiency

A replay buffer turns each real step into many learning steps, and decides which old steps the learner can still remember.

  • Lesson 5 of 5
  • Module 8: Offline and Data-Efficient Learning
  • Theory
  • About 17 minutes to read

Every robot transition costs time on a real machine. A replay buffer stores recent transitions and lets the learner sample them again, so one step of experience can drive several updates and the correlation between consecutive steps is broken. It also decides what is forgotten, which matters when the important events, such as collisions, are rare. This lesson defines the ring buffer and uniform sampling, shows with a calculation why a small buffer forgets rare events, derives prioritised replay and works a four-transition example by hand, describes hindsight relabelling and data augmentation at the level of the idea, and discusses the update-to-data ratio. Then it measures, over 20 seeds on the cliff and the ward of Module 4, what replay buffers of 50, 500 and 5,000 transitions and 1 or 8 updates per step do to the number of episodes needed. The result is less clean than the folklore: replay with one update per step was slower than plain online Q-learning, and the gain appears only with more updates. It builds on lessons M4-T6, M6-T6, M8-T1 to M8-T3 and M8-T4, and is practised in labs M8-L1 and M8-L3. Lesson M7-T6 reaches the same goal, fewer real steps, with a model instead of a buffer.

Learning objectives

After this lesson, you should be able to:

  • Implement a ring buffer with uniform sampling, and say what replay buys (decorrelation, reuse) and what it risks (staleness, forgetting).
  • Compute prioritised-replay probabilities pα / Σpα and importance weights by hand, and explain what each is for.
  • Describe hindsight relabelling, augmentation and the update-to-data ratio, and the way each reuses data.
  • Read a replay experiment over 20 seeds, including its instabilities, and cost a buffer in robot-hours and storage.

The intuition

A nurse robot bumps into a trolley once in two thousand steps. The learner trains from a buffer of recent steps. If the buffer holds the last 50 steps, then at any moment it contains a collision only about 2.5% of the time, and when it does, the collision is sampled for a few updates and then gone. The learner is trained mostly on corridors, and what it believes about trolleys is whatever it remembers from the last time one appeared. The figure shows the probability that a first-in-first-out buffer holds at least one collision, as a function of its capacity: 0.025 at 50, 0.221 at 500, 0.918 at 5,000.

A small buffer forgets rare eventsThree bars: the probability that a first-in-first-out buffer contains at least one rare collision, when a collision occurs with probability 1 in 2000 per step. Capacity 50 gives 0.025, capacity 500 gives 0.221, capacity 5000 gives 0.918.0.025capacity 500.221capacity 5000.918capacity 5000probability that the buffer holds at least one collision (rate 1 in 2000 steps)
The probability that a FIFO buffer holds at least one rare event with probability 1/2000 per step is 1 − (1 − 1/2000)capacity. The calculation assumes independent steps; a real log has bursts.

Replay is therefore two things at once. It is a way to reuse data (cheap computation in place of expensive robot time) and a memory policy (what the learner is allowed to remember). Getting the first right and ignoring the second is a common cause of learners that look fine on average and fail on the rare event that matters.

The mathematics

The ring buffer and uniform sampling

A buffer of capacity C holds transitions (s, a, r, s', done). A ring buffer appends while it is not full; when full, it overwrites the oldest entry at the position ptr, then moves ptr to (ptr + 1) mod C. Sampling a minibatch of B items draws B indices uniformly from the stored entries: index = floor(u × size) for a uniform u in [0, 1), with the guard that an empty buffer cannot be sampled. Lab M8-L1 asks you to write these two functions, and the usual bugs are an off-by-one at the top index (the newest entry is never drawn) and an overwritten pointer that is not advanced.

What replay does

  • Breaks correlation. Consecutive steps are almost the same state. A minibatch of consecutive steps pushes a network the same way B times; a uniform sample from the buffer mixes many situations.
  • Reuses data. Each transition can feed many updates, which matters when a step costs robot time (lesson M6-T6).
  • Needs an off-policy learner. A transition in the buffer was made by an older policy. Q-learning and SAC (lessons M4-T6 and M6-T6) allow this; plain policy gradients (lessons M6-T2 and M6-T5) do not.
  • Introduces staleness and forgetting. An old transition may be unrepresentative of the current policy, and a transition older than C steps is gone.

Prioritised replay

Schaul et al. (2016) sample transitions in proportion to a priority pi, typically the magnitude of the temporal-difference error |δi| plus a small constant so that no transition has zero probability. The sampling probability and the correction weight are:

P(i)=piα∑kpkα,wi=(N P(i))−βmax⁡j(N P(j))−βP(i) = \frac{p_i^{\alpha}}{\sum_k p_k^{\alpha}}, \qquad w_i = \frac{(N \, P(i))^{-\beta}}{\max_j (N \, P(j))^{-\beta}}

With α = 0 sampling is uniform; α = 1 is proportional to the priority. Because high-priority items are over-sampled, the gradient is biased, and the importance weight wi multiplies the loss to correct it: β = 1 corrects fully, and the original paper increases β toward 1 during training. The denominator normalises the weights so that the largest is 1 and the update never gets larger than uniform replay would. This is the same importance-sampling idea as in off-policy evaluation, applied to the sampling of the buffer.

Hindsight relabelling

In goal-conditioned tasks with sparse rewards (lesson M5-T3), most episodes end without reaching the goal and carry no reward signal. Hindsight experience replay (Andrychowicz et al., 2017) stores each transition a second time with the goal replaced by a state that was actually reached later in the same episode; with that goal, the episode succeeded, and the reward is recomputed accordingly. The learner receives a reward signal from failures. It applies when the goal is a part of the state that can be substituted and when the reward is a function of the goal, which is true for reach-and-place tasks and not for every task.

Data augmentation

If the observation is an image, a small random shift or crop of each sampled image gives the learner many slightly different views of the same transition. Laskin et al. (2020) and Kostrikov, Yarats and Fergus (2021) showed that this simple change improves the sample efficiency of pixel-based RL substantially on the benchmarks they used. The principle is the one of lesson M2-T5: an augmentation is a claim that the label does not change under the transformation, so it should be chosen only for transformations of which that is true (a horizontal flip of a camera image swaps left and right, and is wrong for a steering task).

The update-to-data ratio

The update-to-data (UTD) ratio is the number of gradient updates per environment step. A UTD of 1 is the usual default; a larger one extracts more learning from each transition at the price of computation, and of the risk of overfitting a network to the contents of the buffer, since the same transitions are fitted again and again. Chen et al. (2021) report that high UTD ratios can be made to work with ensembles of Q-functions that reduce the bias, which indicates that the naive version is fragile; I describe their result at that level.

C
Buffer capacity in transitions.
K
Updates per environment step (the UTD ratio) in the experiments below.
p, α, β
Priority, how strongly priority sets the sampling probability, and how strongly the weights correct it.
N
The number of transitions currently in the buffer.

A worked example

Every number below was computed with Node. The first two parts are hand-checkable; the third is a 20-seed experiment on the grids of Module 4.

1. A rare event in a FIFO buffer

A collision has probability 1/2000 per step. The probability that a buffer of capacity C holds at least one is 1 − (1 − 1/2000)C = 0.0247 for C = 50, 0.2212 for C = 500 and 0.9180 for C = 5,000 (the figure). If collisions were exactly 2,000 steps apart, a buffer of 500 would contain one for 500 / 2000 = 25% of the time and a buffer of 50 for 2.5%. A collision enters, is drawn about B × (updates / size) times while it stays, and leaves. Both calculations assume the buffer is filled with steps in order and that the events are independent; a real log has bursts, and the calculation shows the order of magnitude and not the exact retention.

2. Prioritised replay by hand

Four transitions have priorities p = 0.1, 0.5, 1 and 4, with α = 0.6 and β = 0.4.

Prioritised replay, four transitions (N = 4, sum of p^0.6 = 4.2083)
ipp0.6P(i)(N P)−0.4w (normalised)
10.10.25120.05971.77351.000
20.50.65980.15681.20520.680
311.00000.23761.02050.575
442.29740.54590.73170.413

The transition with priority 4 is drawn with probability 0.546, compared with 0.25 for uniform sampling, and 9.1 times as often as the one with priority 0.1 (0.546 / 0.060). With α = 1 it would be 0.714, which is 40 times (4 / 0.1) as often as the low-priority one. The weights go the other way: the over-sampled transition is down-weighted to 0.413 and the rarely drawn one has weight 1.000. A rarely drawn transition is in this way not neglected by the update.

3. Replay on the cliff and the ward

The setting: Q-learning of lesson M4-T6 on the cliff (deterministic, step reward −1, goal +10, stairwell −100 with reset, discount 0.99) and the ward (slip 0.2, step reward −0.1, goal +10, stairwell −10 terminal, discount 0.95), step size 0.5, epsilon 0.1, at most 200 steps per episode, 300 episodes. I wrote the replay loop myself in Node: after each environment step, the transition is written into a ring buffer of capacity C and then K updates are made, each on a transition drawn uniformly from the buffer. The baseline without replay does K updates on the newest transition only (K = 1 is ordinary Q-learning). After every episode I compute the exact value at the start of the greedy policy (RLCore.evaluatePolicyDP). "Good" is a value of at least −3.5 on the cliff (the safe path that avoids the stairwell is worth −3.38; the optimum is 0.58) and at least 80% of the optimum, 2.65, on the ward (optimum 3.31). Twenty seeds, SLAMCore.rng(1..20). First = the first episode at which the greedy policy is good, mean over the seeds that reached it. Steps = environment steps at that moment (the robot’s cost).

Cliff: episodes to a good policy (mean of 20 seeds, 95% interval)
CliffReachedFirst good episodeEnv. steps thenStays good fromUpdates then
no replay, K = 1 (Q-learning)20 / 2011.9 (10.6 to 13.2)59616.0596
no replay, K = 820 / 2010.3 (8.9 to 11.7)49113.53,930
replay C = 50, K = 120 / 208.1 (6.5 to 9.6)89815.7898
replay C = 500, K = 120 / 209.9 (7.4 to 12.4)1,39720.61,397
replay C = 5,000, K = 120 / 209.8 (7.2 to 12.4)1,42919.91,429
replay C = 50, K = 820 / 204.3 (3.9 to 4.7)2644.32,114
replay C = 500, K = 820 / 203.8 (3.2 to 4.3)3614.32,884
replay C = 5,000, K = 820 / 203.8 (3.2 to 4.3)3644.02,916
Ward: episodes to a good policy (mean of 20 seeds)
WardReachedFirst good episodeMedianEnv. steps thenGood at episode 300
no replay, K = 1 (Q-learning)20 / 2030.9 (18.4 to 43.5)25.01,69810 / 20
no replay, K = 820 / 2024.1 (4.3 to 44.0)15.01,6455 / 20
replay C = 50, K = 119 / 2038.5 (23.7 to 53.2)25.02,90810 / 20
replay C = 500, K = 118 / 2043.7 (33.6 to 53.9)39.53,3659 / 20
replay C = 5,000, K = 120 / 2046.5 (33.4 to 59.7)42.53,1655 / 20
replay C = 50, K = 820 / 2039.5 (6.5 to 72.6)13.02,4698 / 20
replay C = 500, K = 819 / 2018.7 (13.1 to 24.4)15.01,0217 / 20
replay C = 5,000, K = 817 / 2012.4 (9.9 to 14.9)10.07408 / 20

What the numbers say.

  • Replay with one update per step did not help. On the cliff, C = 50 was a little faster in episodes (8.1 against 11.9) but took more environment steps (898 against 596), because its episodes were longer. Larger buffers were no better than 11.9 and used 1,397 to 1,429 steps. On the ward, replay with K = 1 was slower (38.5 to 46.5 episodes against 30.9). The reason I believe, not tested separately: with K = 1 a sample from a big buffer is mostly an old transition, so the newest information, which is the most informative for a learner that is still changing, is applied rarely.
  • Replay with more updates did help, in this tabular setting. Cliff with C = 500 or 5,000 and K = 8: 3.8 episodes and 361 to 364 steps, about a third of Q-learning’s episodes and 61% of its steps. Ward with C = 5,000 and K = 8: 12.4 episodes and 740 steps against 30.9 and 1,698, a factor of 2.3 in real steps. Updates cost computation: 2,916 on the cliff and 5,918 on the ward at the moment of success against 596 and 1,698.
  • Repeating the same newest transition 8 times (no replay, K = 8) gained little on the cliff (10.3 against 11.9, intervals overlap) and nothing reliable on the ward. The gain with replay comes from the variety of transitions in the 8 updates.
  • The benefit was not free of cost. On the ward, C = 5,000 with K = 8 failed to reach a good policy in 300 episodes in 3 of 20 seeds, and C = 500 in 1 of 20 (and C = 500, K = 1, in 2 of 20). I did not diagnose the failed seeds. A candidate (untested) is that a big buffer keeps replaying early, mis-valued transitions.
  • "Good" is not the same as "stays good". With step size 0.5 in a slippery world the greedy policy keeps flipping between good and bad: only 5 to 10 of 20 ward runs were good at episode 300 whatever the buffer, so the first-good episode overstates how soon a learner is dependable. On the deterministic cliff, replay with K = 8 became good at about episode 4 and stayed good (4.0 to 4.3); Q-learning needed 16.0 to stay good, and replay with K = 1 needed 15.7 to 20.6. The step size is large for noisy problems, and I did not tune it.

The cost of a buffer in robot time

At a control rate of 10 Hz, 5,000 transitions are 500 s, or 8.3 minutes, of robot time; one million transitions are 100,000 s = 27.8 hours, about three working days of continuous operation, before any resets, charging or failures. The 364 real steps in the best cliff run correspond to 36 seconds at 10 Hz; the tabular tasks of this lesson are far smaller than any real task, and these are costs of the toy, not of a robot.

What this simulation simplifies. Tabular, tiny state spaces, uniform sampling only, a constant step size, no function approximation (so no overfitting to the buffer) and no prioritised-replay experiment: the prioritisation part of the lesson is a hand calculation and was not run on the grids. Conclusions about the update-to-data ratio with neural networks should not be drawn from these tables.

Engineering implications and limitations

Design checklist for a replay buffer

  1. Choose the capacity from the time scale of what must be remembered, not from the memory available: how many steps between rare but important events, and how much the policy changes over that time.
  2. Store everything needed to reconstruct the update: state, action, reward, next state, a terminal flag that is separate from a time-limit truncation (lesson M4-T6), and the policy or version that produced it.
  3. Decide what is never overwritten: a reserved region for rare events (collisions, recoveries, successes) or a second buffer, so that the corridor does not push them out. A reservoir sample is a simple alternative to FIFO when old data must stay representative; lab M8-L1 draws the three side by side.
  4. Sample uniformly first. Add prioritisation only if the learner is limited by rare informative events, and then correct with importance weights.
  5. Raise the UTD ratio step by step and watch the value-loss and the evaluation return (lesson M6-T7); stop when evaluation stops improving or the Q-values grow without the returns.
  6. Evaluate on fresh episodes in the real or simulated environment, never on buffer data.

Reuse versus overfitting to the buffer

With a network, many updates on a small buffer can fit the buffer’s contents (including its noise) instead of the task; the evaluation return then stops following the training loss. The remedies are the same family as in lesson M2-T5: more diverse data, augmentation, regularisation, and a smaller UTD ratio. Another failure is feedback: the policy collects data, the data train the policy, and the buffer holds a narrow slice of experience. That is the online counterpart of the coverage problem of lessons M8-T2 and M8-T3.

Real robot costs

  • Robot-hours are the scarce quantity, and a buffer is the record of how they were spent. Count the hours behind each dataset: setup, resets, failures, supervision, and the hours that produced nothing usable.
  • Storage: images dominate. A 64 x 64 RGB frame is 12,288 bytes uncompressed, so a million transitions with two frames each is about 25 GB before compression; plan for it.
  • Safety of collecting the data is part of the cost (lesson M5-T5); randomness used for exploration must itself be safe.
  • Reuse across experiments: a buffer from one policy is an offline dataset for the next, which is exactly the setting of module 8’s first lessons, so treat it with the same coverage checks (lesson M8-T2).

Logging and versioning

Treat the buffer as a dataset with a version. Record the code revision, the policy checkpoint and exploration noise that wrote each part, the robot and calibration, the observation and action definitions, and the reward function (a reward change makes old rewards wrong; relabel or discard). Keep an immutable copy of the data used by each reported result and the random seeds (lessons M11-T3 and M2-T6). Hindsight relabelling and augmentation should be applied at sampling time, never written over the stored data, so the original is recoverable. If a log is later used for offline learning, the conservative methods of lesson M8-T4 and the checks of lesson M8-T3 apply.

Common misconceptions

“A replay buffer always speeds up learning.”

Not by itself. On the ward, replay with one update per step needed 38.5 to 46.5 episodes against 30.9 for plain Q-learning; the speed-up appeared with 8 updates per step (12.4 episodes with a 5,000 buffer), and brought 3 failed seeds of 20.

“A bigger buffer is better.”

On the cliff, buffers of 500 and 5,000 behaved identically with 8 updates (3.8 episodes). Bigger holds staler data, and it costs memory and (with images) storage. Size it by how long rare events must be remembered.

“Prioritised replay is just sampling the hard examples, with no cost.”

Over-sampling biases the gradient, which the importance weights (β) correct; with α = 1 the example above samples the top transition 40 times as often as the bottom one. The paper’s priorities are computed at the time a transition was last used and become stale.

“More updates per step is free learning.”

Updates cost computation, and with networks a high update-to-data ratio can overfit the buffer; reported fixes use ensembles to control the bias. Repeating the same newest transition 8 times gained little in the cliff experiment.

Engineering takeaways

  1. A ring buffer overwrites the oldest entry; uniform sampling breaks correlation and allows reuse, and it needs an off-policy learner.
  2. A FIFO buffer forgets rare events: with a collision rate of 1 in 2000, capacity 50, 500 and 5,000 hold one 2.5%, 22.1% and 91.8% of the time.
  3. Prioritised replay samples P(i) = pα / Σpα and corrects with wi normalised by the largest: for p = 0.1, 0.5, 1, 4 at α = 0.6 and β = 0.4, P = 0.060, 0.157, 0.238, 0.546 and w = 1.000, 0.680, 0.575, 0.413.
  4. Measured over 20 seeds: replay with 1 update per step was not faster than Q-learning on either grid; with 8 updates per step the cliff needed 3.8 episodes against 11.9 and the ward 12.4 against 30.9 (with 3 of 20 failed seeds), and the ward policy kept flipping.
  5. Hindsight relabelling and augmentation reuse data by changing how it is read, the UTD ratio reuses it by computation; all are paid for in assumptions, in overfitting risk, and in the robot-hours that produced the buffer, which need logging and versioning.

References and further reading

Lin introduced experience replay for RL and Mnih et al. made it standard for deep Q-learning; the other entries are the primary sources of the techniques named above. The numbers are my own measurements, not from these papers.

  • L.-J. Lin, “Self-Improving Reactive Agents Based on Reinforcement Learning, Planning and Teaching”, Machine Learning 8, 1992.
  • V. Mnih et al., “Human-Level Control through Deep Reinforcement Learning”, Nature 518, 2015.
  • T. Schaul, J. Quan, I. Antonoglou and D. Silver, “Prioritized Experience Replay”, International Conference on Learning Representations (ICLR), 2016.
  • M. Andrychowicz et al., “Hindsight Experience Replay”, Advances in Neural Information Processing Systems (NeurIPS), 2017.
  • M. Laskin, K. Lee, A. Stooke, L. Pinto, P. Abbeel and A. Srinivas, “Reinforcement Learning with Augmented Data”, Advances in Neural Information Processing Systems (NeurIPS), 2020.
  • I. Kostrikov, D. Yarats and R. Fergus, “Image Augmentation Is All You Need: Regularizing Deep Reinforcement Learning from Pixels”, International Conference on Learning Representations (ICLR), 2021.
  • X. Chen, C. Wang, Z. Zhou and K. Ross, “Randomized Ensembled Double Q-Learning: Learning Fast Without a Model”, International Conference on Learning Representations (ICLR), 2021.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

Priorities 0.1, 0.5, 1 and 4 with alpha 0.6: which transition has the highest sampling probability and which the smallest importance weight (beta 0.4)? Give the probability of the highest.

Explanation

The transition with priority 4 has both: its probability is 0.546 and its normalised weight 0.413 (the over-sampled item is down-weighted). The priority-0.1 item has probability 0.060 and weight 1.000.

Question 2

A collision occurs about once per 2000 steps and the buffer holds the last 500 steps. How often does the buffer contain a collision, and what does that do to a learner that must avoid collisions?

Explanation

About 22% of the time (1 − (1 − 1/2000)^500 = 0.221). For the rest of the time the learner cannot learn from collisions at all and may forget them; keep rare events in a reserved region, use a larger buffer, or prioritise them.

Question 3

On the ward, replay with C = 5,000 and one update per step took 46.5 episodes against 30.9 for Q-learning. Why, and what changed it?

Explanation

With one update per step the sampled transition is mostly old, so the newest information is applied rarely and the learner is slower. Making 8 updates per step reduced the mean to 12.4 episodes, but 3 of 20 seeds did not reach a good policy in 300 episodes.

What’s next

That completes the theory of Module 8: fixed datasets, coverage, extrapolation error, conservative methods and buffers. Labs M8-L1 to M8-L3 build a replay buffer, find a coverage gap and compare an online and an offline policy. Module 9 turns to where learning meets contact: grasping, manipulation and locomotion.

Practice this lesson

Build a Replay Buffer

The buffer forgets the rare collisions and over-samples the corridor: fix sampling and capacity.

Next lesson

M9-T1 — Learning to Grasp

Describe grasp success prediction and grasp policies, and what the data and the sensor must provide.

Nothing below is required to finish this lesson.