Intuition The one core idea
Experience replay is a notebook of past moments the agent keeps, and every time it studies it flips to a few random pages instead of only re-reading the very last thing that happened. To read the parent note you must first learn what one "moment" is, what it means to predict how good a moment is , and why random review beats in-order review — this page builds all of that from nothing.
This is the ground-floor page for Experience replay . Nothing here assumes you have seen reinforcement learning before. Every letter and squiggle the parent note uses is defined below, in an order where each idea rests on the one above it.
Picture a little robot playing a game. At every tick of the clock, four things happen. Let us name them.
s — "where I am right now"
The state s is a snapshot of the situation the agent sees: the picture on the screen, the positions of everything. In the figure it is the whole board at time t .
Picture: the frozen screenshot.
Why the topic needs it: the buffer stores states so it can re-ask "what should I have done here ?" later.
a — "the button I press"
The action a is the choice the agent makes from that state: move left, jump, fire.
Picture: the arrow leaving the current screenshot in the figure.
Why the topic needs it: an experience is worthless unless we remember what we chose , because that choice caused the outcome.
r — "the score change I just got"
The reward r is a single number the world hands back: + 1 for a point, 0 for nothing, − 1 for dying.
Picture: the amber number popping up after the action.
Why the topic needs it: reward is the feedback signal . Learning means turning past rewards into better future choices.
s ′ — "where I ended up"
After acting, the world moves on to a new snapshot, written s ′ (read "s -prime", the tick mark just means "the next one"). At the next clock tick it becomes the new s .
Picture: the second screenshot the arrow points to.
Why the topic needs it: to judge an action we need to know what came after it .
Bundle those four into one packet:
e t = ( s t , a t , r t , s t + 1 )
A transition (also called an experience ) is the tuple — an ordered bundle written inside round brackets ( ) — of the four items above: state, action, reward, next-state.
The subscript t (read "at time t ") is just a clock stamp : e 1 is the first moment, e 2 the second.
s t + 1 is the same thing as s ′ — the "next state" of moment t is the "current state" of moment t + 1 .
Picture: the whole boxed row in the figure.
Why the topic needs it: this packet is exactly one page of the notebook . The buffer is a stack of these.
s ′ is a totally different symbol from s t + 1 ."
Why it feels right: they look different. Why it's wrong: they are two spellings of the same idea — "the state that comes next". The parent note uses both. Fix: read every ′ as "+1 on the clock".
N and D — the notebook
N is just a count : how many transitions we are holding. The fancy letter D (a curly "D", for Data ) is the name of the whole buffer : D = { e 1 , e 2 , … , e N } . The curly braces { } mean "a collection of these items".
Picture: a filing cabinet with N drawers, each holding one transition.
Why the topic needs it: "FIFO, capacity N " means when drawer N + 1 arrives, the oldest drawer is thrown out (F irst I n F irst O ut).
Why does "almost identical neighbours" matter? Because 3 pixels of movement between s t and s t + 1 barely changes the screen.
Definition i.i.d. — "independent and identically distributed"
A pile of samples is i.i.d. when (a) picking one tells you nothing about the next (independent), and (b) they all come from the same source (identically distributed).
Picture (left panel): the live stream is a chain of near-copies — knowing s t tells you s t + 1 almost exactly, so not independent. Right panel: random draws from a full cabinet are scattered and unrelated — close to i.i.d.
Why the topic needs it: the maths that trains neural nets assumes i.i.d. samples. Correlated (chained) samples break that assumption, so we shuffle with U ( D ) to restore it.
∣ B ∣ 1 ∑
The symbol ∑ (capital sigma) means "add all of these up" . The bars ∣ B ∣ mean "how many things are in B " (its size). So ∣ B ∣ 1 ∑ e ∈ B ( ⋯ ) is literally "add up one number per transition, then divide by how many — the plain average" .
Picture: stacking the batch's numbers and sharing them equally.
Why the topic needs it: we don't update from one page; we average the lesson over the whole handful B to get a steadier signal.
Now the heart of it: what is the network actually learning ?
Definition The value function
Q ( s , a )
Q ( s , a ) is a predicted score : "if I am in state s and press button a , how much total future reward do I expect?" One number, for each (situation, choice) pair.
Picture: a scorecard that, shown a screenshot and an arrow, prints a number.
Why the topic needs it: the agent picks the action with the biggest Q . Better Q ⇒ better play.
θ in Q θ
θ (Greek "theta") is a stand-in for all the tuning knobs inside the neural network — its weights. Writing Q θ says "this scorecard's answers depend on the current knob settings θ ."
Picture: a machine with thousands of little dials; θ is the whole set of dial positions .
Why the topic needs it: "learning" = nudging θ so Q θ predicts better. See Deep Q-Networks (DQN) .
γ (discount) and max a ′
γ (Greek "gamma", between 0 and 1 ) is the discount factor : future reward counts a little less than reward now. γ = 0.9 means "each step into the future is worth 90% of the last."
max a ′ Q ( s ′ , a ′ ) means: look at the next state s ′ , try every possible next action a ′ , and keep the biggest score. The prime on a ′ again just means "the next action".
Picture: at the next screen, fan out all buttons, read each score, circle the highest (amber ring in the figure).
Why the topic needs it: this "best future value, shrunk by γ " is how we estimate what a good move is worth without playing to the end of the game.
Now we can state the equation the whole target is built on.
Intuition Why this equation lets us
bootstrap
We don't know Q ∗ (that's the whole thing we're solving for). But the equation says Q ∗ at one moment is built from Q ∗ at the next moment. So we can cheat : plug in our current estimate for the future term and treat the whole right-hand side as a temporary answer key. That reused-your-own-guess move is called bootstrapping , and it turns the equation above into the target below.
y = r + γ max a ′ Q θ − ( s ′ , a ′ )
y is the "should-have-been" answer — the score we wish Q θ ( s , a ) had printed. It is the right-hand side of Bellman's equation with our current network standing in for the unknown Q ∗ .
θ − (theta-minus) is a frozen, slightly-old copy of the knobs, used only to compute y so it doesn't wobble. See Target Networks .
Picture: the answer key we compare our guess against.
Why the topic needs it: this bootstrapped y is what every replayed transition is trained toward.
Definition Terminal states and the
( 1 − d ) switch
Some next states s ′ are terminal — the episode is over (you died, or the level ended). There is no next move , so there is no future value to add: max a ′ Q ( s ′ , a ′ ) is defined to be 0 at a terminal s ′ .
We record a done flag d : d = 1 if s ′ is terminal, d = 0 otherwise.
The target becomes y = r + γ ( 1 − d ) max a ′ Q θ − ( s ′ , a ′ ) . When d = 1 the factor ( 1 − d ) = 0 erases the bootstrap term, leaving just y = r .
Picture: a light-switch on the future term — terminal flips it off, so only the final reward remains.
Why the topic needs it: forget this and the network hallucinates future reward after the game ended, poisoning every replay that touches a terminal transition.
δ = y − Q θ ( s , a )
δ (Greek "delta") is simply guess-versus-answer-key gap : the target minus what we predicted. Big ∣ δ ∣ (bars mean size, ignore the sign) = "I was very wrong here."
Picture: the length of the red mismatch bar between the two numbers.
Why the topic needs it: δ drives both how much we correct the network and (in the upgrade) how interesting a transition is to replay.
ℓ = ( y − Q θ ( s , a ) ) 2 = δ 2
Loss is the gap, squared . Squaring does two jobs: it makes every gap positive (a miss is a miss whether too high or too low), and it punishes big misses much harder than small ones.
Picture: the area of a square whose side is the mismatch bar — doubling the error quadruples the pain.
Why the topic needs it: training = turning knobs θ to shrink this squared gap, averaged over the batch.
transition e = s a r s prime
buffer D of N transitions
Bellman optimality equation
discount gamma and max over a prime
bootstrapped target y with done flag d
Read it top-down: the four raw items form a transition, transitions fill the buffer, random sampling makes an almost-i.i.d. batch; meanwhile the value network and the discounted Bellman rule build a target (with the terminal ( 1 − d ) switch), the gap becomes a squared loss — and averaging that loss over the random batch is experience replay.
Definition Off-policy vs on-policy
On-policy learning insists you only study data made by your current way of playing. Off-policy learning is happy to study data made by an older, different way of playing.
Picture: on-policy = only trust today's diary; off-policy = old diary pages still teach you.
Why the topic needs it: the buffer is full of old-policy pages , so replay only works cleanly for off-policy methods. This is the Off-policy vs On-policy distinction, used in DDPG and Soft Actor-Critic .
Definition Importance sampling (preview)
If you deliberately pick some pages more often than others, a plain average is tilted . Importance sampling re-weights each picked item by its pick-probability 1 to undo the tilt.
Picture: a see-saw rebalanced by putting lighter weights on the over-picked side.
Why the topic needs it: the Prioritized replay upgrade picks high-δ pages more, so it must re-weight. Full story in Importance Sampling .
Cover the right side; can you say each before revealing?
What the four items of a transition are state s , action a , reward r , next state s ′ (= s t + 1 )
What D and N mean D is the whole FIFO buffer; N is how many transitions it holds
What B ∼ U ( D ) says out loud "take a mini-batch B by picking transitions from D each with equal probability"
Why we want i.i.d. samples neural-net training assumes samples are independent and from one source; correlated live streams break that
What ∑ and ∣ B ∣ 1 ∑ do ∑ adds up; the ∣ B ∣ 1 version is the plain average over the batch
What Q θ ( s , a ) predicts the expected total (discounted) future reward for doing a in state s , given knob settings θ
What the Bellman optimality equation says Q ∗ ( s , a ) = E s ′ [ r + γ max a ′ Q ∗ ( s ′ , a ′ ) ] — true value = reward + discounted best future value
What γ and max a ′ do in the target γ shrinks future reward; max a ′ picks the best possible next action's value
What y and θ − are y is the bootstrapped target ("should-have-been" score); θ − is a frozen old copy of the network used to build it
How a terminal next state changes y with done flag d = 1 the factor ( 1 − d ) = 0 kills the bootstrap, so y = r (no future value)
What δ and the squared loss are δ = y − Q θ ( s , a ) is the gap; the loss is δ 2 , punishing big misses hardest
Why replay suits off-policy but not on-policy the buffer holds old-policy data; only off-policy methods can learn from data made by a different/older policy