5.2.2Deep & Advanced RL

Experience replay

2,111 words10 min readdifficulty · medium5 backlinks

WHAT is experience replay?

The typical home of this trick is Deep Q-Networks (DQN), so we ground everything there.


WHY do we need it? (the two diseases it cures)

Naive "online" deep RL updates the network from each transition as it arrives. Two problems arise:

Disease 1 — Correlated samples. Consecutive states st,st+1,st+2s_t, s_{t+1}, s_{t+2} are almost identical (the car moved 3 pixels). Stochastic gradient descent assumes your samples are roughly i.i.d. Feeding it a stream of near-duplicate, highly-correlated samples gives high-variance, biased gradient estimates — the net over-fits to whatever it's seeing right now.

Disease 2 — Data inefficiency. A rare, informative experience (you finally scored a point!) is seen once and discarded. With a buffer it can be replayed dozens of times.

Disease 3 — Non-stationary target chasing. The policy changes → the distribution of incoming data changes → the network chases a moving target and can oscillate/diverge. Averaging over a large buffer smooths the training distribution.


HOW does it work? (algorithm, derived step by step)

We want to fit Qθ(s,a)Q_\theta(s,a) to satisfy the Bellman optimality equation: Q(s,a)=Es[r+γmaxaQ(s,a)].Q^*(s,a) = \mathbb{E}_{s'}\Big[\, r + \gamma \max_{a'} Q^*(s',a') \,\Big].

Since we don't know QQ^*, we bootstrap: build a target using the current network, y=r+γmaxaQθ(s,a),y = r + \gamma \max_{a'} Q_{\theta^-}(s', a'), where θ\theta^- is a (slowly updated) target-network copy. The loss for one transition is the squared TD error: (e;θ)=(yQθ(s,a))2.\ell(e;\theta) = \big(y - Q_\theta(s,a)\big)^2.

Why sample from a buffer here? The true objective is an expectation over the state–action distribution ρ\rho: L(θ)=Eeρ[(e;θ)].L(\theta) = \mathbb{E}_{e\sim\rho}\big[\ell(e;\theta)\big]. An unbiased Monte-Carlo estimate of θL\nabla_\theta L needs samples drawn independently from ρ\rho. Uniform sampling from a large buffer approximates i.i.d. draws far better than the correlated live stream. So: θL(θ)    1BeBθ(e;θ),BU(D).\nabla_\theta L(\theta) \;\approx\; \frac{1}{|B|}\sum_{e\in B} \nabla_\theta \ell(e;\theta), \qquad B \sim \mathcal{U}(\mathcal{D}).

Figure — Experience replay

Prioritized Experience Replay (the 80/20 upgrade)

Uniform sampling wastes effort on transitions the net already predicts well. Prioritized Experience Replay (PER) samples in proportion to the magnitude of the TD error δi=yiQθ(si,ai)\delta_i = y_i - Q_\theta(s_i,a_i) — "the transitions I'm most wrong about are the ones I should study."


Worked examples


Common mistakes (steel-manned)


Forecast-then-Verify

Recall Forecast before you read the answer

Q: If you set α=0\alpha=0 in PER, what algorithm do you recover, and why? A: Plain (uniform) experience replay — every pi0=1p_i^0=1, so P(i)=1/NP(i)=1/N for all ii. Q: You increase buffer size 100×; does the correlation between two randomly sampled transitions go up or down? A: Down — larger, more diverse pool ⇒ two random picks are less likely temporally adjacent ⇒ more i.i.d.-like.


Recall Feynman: explain to a 12-year-old

Imagine you're learning a video game. A silly way is to only think about the move you just made and then forget it forever. A smart way: keep a notebook of past moves and what happened. Every practice round you flip to a few random pages and review them. Random pages stop you from getting stuck thinking only about the last thing, and reviewing old pages means a lucky trick you found once isn't forgotten. If some pages are marked "I got this really wrong," you flip to those more often — but you remind yourself they're rare so you don't get an unrealistic idea of how good you are.


Flashcards

What problem in online deep RL does experience replay solve first?
It breaks the correlation between consecutive (near-identical) samples, restoring the ~i.i.d. assumption SGD needs.
What is stored in a replay buffer?
Transitions (st,at,rt,st+1)(s_t,a_t,r_t,s_{t+1}) (plus a terminal flag), in a fixed-capacity FIFO.
How are mini-batches drawn in plain experience replay?
Uniformly at random from the buffer.
Why does replay improve data efficiency?
Each transition (especially rare informative ones) can be reused in many gradient updates instead of once.
Why is replay only valid for off-policy algorithms?
The buffer contains data from old policies; on-policy methods require samples from the current policy.
What is the TD target in DQN with replay?
y=r+γ(1d)maxaQθ(s,a)y=r+\gamma(1-d)\max_{a'}Q_{\theta^-}(s',a').
In PER, what determines a transition's priority?
The magnitude of its TD error, pi=δi+ϵp_i=|\delta_i|+\epsilon.
Give the PER sampling probability.
P(i)=piα/kpkαP(i)=p_i^{\alpha}/\sum_k p_k^{\alpha}.
Why does PER need importance-sampling weights?
Non-uniform sampling biases the gradient estimate; wi=(NP(i))βw_i=(N\,P(i))^{-\beta} corrects that bias.
What does α=0\alpha=0 give in PER?
Uniform experience replay (all priorities equal).
What role does the target network θ\theta^- play with replay?
Provides a slowly-changing target so the net isn't chasing a moving objective, adding stability.

Connections

  • Deep Q-Networks (DQN) — the algorithm replay was designed for
  • Bellman optimality equation — source of the TD target
  • Temporal-Difference Learning — the update replay feeds
  • Off-policy vs On-policy — why replay needs off-policy learning
  • Importance Sampling — the bias correction used in PER
  • Target Networks — companion stabiliser
  • DDPG / Soft Actor-Critic — off-policy actor-critics that also use replay

Concept Map

suffers

suffers

suffers

stored in

sample uniform mini-batch

breaks correlation, approx i.i.d.

reuses rare data

smooths distribution

enables stable

builds TD target

minimise squared TD error

Naive online deep RL

Correlated samples

Data inefficiency

Non-stationary target

Transition s a r s prime

Replay buffer D FIFO

Experience replay

Deep Q-Networks

y equals r plus gamma max Q theta-minus

Loss L theta

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, deep RL me agar hum agent ke har experience ko turant, sequence me, ek baar use karke phenk dein, to do problem aati hain. Pehli: lagataar aane wale states almost same hote hain (car sirf 3 pixel hili) — ye samples aapas me bahut correlated hote hain, aur SGD ko chahiye hote hain roughly i.i.d. samples. Correlated data se gradient noisy aur biased ho jaata hai. Doosri: koi rare achha experience (aakhirkaar point mil gaya!) sirf ek baar dikhta hai aur khatam. Experience replay in dono ka ilaaj hai — hum har transition (s,a,r,s)(s,a,r,s') ko ek replay buffer me store karte hain, aur training ke waqt buffer se random mini-batch utha kar seekhte hain. Random uthane se correlation toot jaata hai, aur purane useful experiences baar-baar replay hote rehte hain.

Buffer FIFO hota hai — jab full ho jaaye to sabse purana transition nikal jaata hai. Har step pe target banate hain y=r+γmaxaQθ(s,a)y = r + \gamma \max_{a'} Q_{\theta^-}(s',a') (agar terminal hai to future term zero), aur loss (yQθ(s,a))2(y - Q_\theta(s,a))^2 pe gradient descent karte hain. Ek important baat: replay sirf off-policy methods (DQN, DDPG, SAC) ke liye valid hai, kyunki buffer me purani policy ka data hota hai; on-policy methods (jaise vanilla policy gradient) ke liye ye seedha kaam nahi karta.

Ek smart upgrade hai Prioritized Experience Replay (PER): jo transitions pe network sabse zyada galat hai (bada TD error δ|\delta|), unhe zyada baar sample karo — "jahan sabse zyada galti, wahan zyada practice" (yehi 80/20 hai). Par ek jaal hai: agar aap deliberately hard examples zyada uthaoge to gradient biased ho jaayega. Isliye har sample ko importance-sampling weight wi=(NP(i))βw_i=(N\,P(i))^{-\beta} se multiply karte hain, taaki bias theek ho jaaye. Yaad rakho: Store karo, Off-policy Shuffle karke Sample karo — S.O.S.!

Go deeper — visual, from zero

Test yourself — Deep & Advanced RL

Connections