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+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.
We want to fit Qθ(s,a) to satisfy the Bellman optimality equation:
Q∗(s,a)=Es′[r+γmaxa′Q∗(s′,a′)].
Since we don't know Q∗, we bootstrap: build a target using the current network,
y=r+γmaxa′Qθ−(s′,a′),
where θ− is a (slowly updated) target-network copy. The loss for one transition is the squared TD error:
ℓ(e;θ)=(y−Qθ(s,a))2.
Why sample from a buffer here? The true objective is an expectation over the state–action distribution ρ:
L(θ)=Ee∼ρ[ℓ(e;θ)].
An unbiased Monte-Carlo estimate of ∇θL needs samples drawn independently from ρ. Uniform sampling from a large buffer approximates i.i.d. draws far better than the correlated live stream. So:
∇θL(θ)≈∣B∣1∑e∈B∇θℓ(e;θ),B∼U(D).
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=yi−Qθ(si,ai) — "the transitions I'm most wrong about are the ones I should study."
Q: If you set α=0 in PER, what algorithm do you recover, and why?
A: Plain (uniform) experience replay — every pi0=1, so P(i)=1/N for all i.
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.
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′) 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+γmaxa′Qθ−(s′,a′) (agar terminal hai to future term zero), aur loss (y−Qθ(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 ∣δ∣), 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 weightwi=(NP(i))−β se multiply karte hain, taaki bias theek ho jaaye. Yaad rakho: Store karo, Off-policy Shuffle karke Sample karo — S.O.S.!