TL;DR
Experience replay is a memory for a learning agent. Each step, the agent stores the transition it just saw, a tuple of state, action, reward and next state, in a buffer. Training then draws random transitions from that buffer rather than learning only from the latest step. Random draws break the strong correlation between consecutive steps, and each transition can be used many times instead of once.
It is what made deep Q-learning stable enough to play Atari games from pixels, and it is still a standard part of off-policy value learning.
Where you'll see it: in deep Q-networks and their descendants, in robot learning from logged trials, and in any off-policy method that learns from data collected by an earlier version of the agent.
Intuition
A diary and a shuffled deck
Picture a student who studies only what happened in the last five minutes. Every lesson is a continuation of the one before, so the student keeps learning the same local pattern and forgets the rest. A better student keeps a notebook of everything they have seen and, each evening, reviews a random selection of pages. Today's events are mixed with last week's, and a rare but important page can be reviewed again and again.
Experience replay is that notebook. The agent writes every transition into a fixed-size buffer. The oldest entries are dropped when the buffer is full. Training picks a random minibatch from the buffer at each update and learns from it.
The reason this matters is that the stream of experience is badly ordered for learning. Consecutive states in a game look almost identical, and the next reward depends on the last one. A learning rule that assumes its data are independent samples, as Stochastic Gradient Descent does, is misled by a stream of near-duplicates. Shuffling the stream through a buffer is the simplest fix.
flowchart LR E[Environment] -->|transition| B[Replay buffer] B -->|random minibatch| T[Training step] T -->|update Q| P[Action choice] P -->|action| E
The agent acts in the environment, writes each transition to the buffer, and trains on random samples from the buffer. The action choice uses the latest network, so the data in the buffer come from a mix of past policies.
The math
Setting
Suppose the agent has produced transitions at each step . The replay buffer holds the most recent of them. Its capacity is a fixed number, such as one million. The agent uses an action-value function , where is the vector of network weights, and it learns with the Q-learning target
Here is the discount factor from the Markov decision process, and and come from the stored transition.
The replay loss
At each update, a minibatch of transitions is drawn uniformly at random from the buffer. The loss is the mean squared error between the prediction and the target over that minibatch:
The notation means uniform sampling from the buffer. The expectation is an average over the minibatch. Gradient descent on this loss updates , and the new transitions keep arriving and replacing the oldest ones.
In practice the target is often computed with a second, slowly updated copy of the weights. That refinement belongs to the deep Q-network, and it is separate from replay itself.
Why uniform sampling helps
Two effects matter.
- Decorrelation. Consecutive transitions share states, so a minibatch of consecutive transitions is nearly one sample repeated. A random minibatch from a large buffer contains transitions from many different times, so its samples are much closer to the independent draws that Stochastic Gradient Descent assumes. Independence in this sense is the idea in Statistical Independence.
- Reuse. Each transition can be drawn many times. Without replay, a rare reward is seen once and then discarded.
Off-policy learning makes replay legitimate
Replay is only sound because Q-learning is off-policy. The target uses the best action in the next state, not the action the agent actually took. The transitions in the buffer were produced by older versions of the policy, and that does not change the target: the rule still estimates from any data that cover the state-action pairs. An on-policy method such as SARSA would need the stored actions to come from the current policy, which replay breaks.
Worked examples
A tiny world has states . The buffer holds four transitions:
- : , not terminal.
- : , terminal.
- : , not terminal.
- : , not terminal.
Take . The current estimates are , , and . The other action in has .
Draw a minibatch of two transitions uniformly at random. Suppose the draw gives and .
- For , the transition is terminal, so the target is . The prediction is , so the error is .
- For , the target is . The best action in is "right" with value , larger than , so . The prediction is , so the error is .
- The minibatch loss is the mean squared error: , about .
Both errors push in the right direction. The important transition , the only one that carries a reward, was pulled into this update even though the agent may have been far from it in time. An online learner would have had to step onto again to train on it.
Suppose the buffer holds transitions, the agent takes one step per update, and each minibatch has transitions. A transition stays in the buffer for about steps before it is overwritten. At each step, each of the stored transitions is included with probability . Over the transition's lifetime, the expected number of times it is used is
- The buffer capacity cancels out: the expected reuse is set by the minibatch size, not by the buffer size.
- In practice the buffer is not full at the start, and the update schedule differs, so the exact count varies. The point is the order of magnitude: each transition contributes to about 32 gradient steps.
- Without replay, a transition contributes to one update and is discarded. Replay multiplies the data use by about 32 for the same environment steps.
Each environment step is expensive, often a game frame or a robot action. Reuse is the reason replay makes learning sample-efficient.
Tradeoffs and failure modes
Replay is a good default for off-policy value learning when collecting experience is slow or costly. It is a poor fit when the algorithm needs fresh on-policy data, and it brings its own tuning problems.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Buffer too small | Consecutive transitions dominate minibatches and the correlation returns | A larger buffer, within memory limits |
| Buffer very large | Old transitions come from policies far from the current one, so they describe a world the agent no longer visits | A moderate buffer, or prioritised replay that favours informative samples |
| Policy-gradient or on-policy method | Stored actions come from an old policy, so the gradient estimate is biased | Discard old data, or use [[proximal-policy-optimization |
| Rare states with high reward | Uniform sampling sees them rarely, even with replay | Prioritised sampling by temporal-difference error |
| Memory limits | Storing raw frames, such as images, is expensive | Store compressed observations, or fewer frames per entry |
The main hidden cost is staleness. The data in the buffer reflect the agent's past behaviour, so the value estimates are a blend of many past policies. Off-policy learning corrects this in the limit, but in finite time the mismatch can slow learning or make it unstable. Prioritised replay, which samples transitions with large temporal-difference errors more often, is one response. It changes the sampling distribution, so it needs importance weights to remain unbiased; the importance sampling idea is the same correction.
History
Long-Ji Lin proposed replaying stored experience to speed up reinforcement learning in the early 1990s, in work that combined learning from experience with planning. The idea was known, but it was not central until deep networks made it necessary. Function approximation with neural networks is unstable when training data are highly correlated, and that was the problem the buffer solved.
In 2013 Mnih and colleagues applied a deep Q-network with replay to Atari games from raw pixels, and it learned several games with one architecture and one set of hyperparameters. Their 2015 Nature paper refined the method with a fixed-size buffer of about one million transitions and minibatches of 32, which became a common reference setting. Replay has been used in most deep off-policy agents since, and it is one of the key ingredients that made value-based deep reinforcement learning practical.
Check yourself
QuestionRecall: what are the four components of a stored transition, and why is the buffer described as a fixed-size memory?Show answer
A transition is the state, the action taken, the reward received, and the next state. The buffer has a fixed capacity so that memory use stays bounded; when it is full, the oldest transitions are overwritten by new ones.
QuestionComputation: with and , what is the Q-learning target for a non-terminal transition with reward 0?Show answer
The target is .
QuestionWhy: why does sampling uniformly from a buffer help learning more than training on the latest transition alone?Show answer
Consecutive transitions are strongly correlated, so training on them one after another gives updates that point in similar directions and repeat the same local pattern. A random minibatch from a large buffer mixes transitions from many different times, so the samples look more like independent draws, which is what stochastic gradient methods assume. Each transition is also reused several times, so rare rewards are learned from instead of being discarded.
QuestionApplied: a team trains an off-policy agent with a buffer of 200,000 transitions, but the agent's behaviour changed a lot during the first million steps. Its value estimates now look unstable. What would you check and change?Show answer
First check the age of the transitions in the buffer, since most of them come from a policy the agent no longer uses. Reducing the buffer size or discarding the oldest data would bring the sample distribution closer to the current policy, at the cost of more correlation between samples. Prioritised replay or a larger minibatch could also help. The right choice depends on how fast the policy changes, which can be measured by comparing the current action distribution with the one that produced the older transitions.
Sources
- Mnih et al. (2013), Playing Atari with Deep Reinforcement Learning: arxiv.org/abs/1312.5602
- Mnih et al. (2015), Human-level control through deep reinforcement learning, Nature: doi.org/10.1038/nature14236
- Reinforcement learning (Wikipedia): en.wikipedia.org/wiki/Reinforcement_learning