5.2.1Deep & Advanced RL

Deep Q-Networks (DQN)

1,836 words8 min readdifficulty · medium6 backlinks

WHY do we need DQN?

WHAT problem are we solving? We want an agent that picks actions to maximise long-term reward in an environment with a huge or continuous state space (e.g. raw Atari pixels: 210×160×3210\times160\times3 possible images — astronomically many states).

WHY not plain Q-learning? Tabular Q-learning stores one number Q(s,a)Q(s,a) per (state,action). For pixels you'd need a table larger than the number of atoms in the universe, and you'd never visit the same state twice — no generalisation.

HOW does DQN fix it? Approximate the whole table with a parametric function Q(s,a;θ)Q(s,a;\theta) — a neural net with weights θ\theta. Nearby states share weights, so learning about one state generalises to similar ones.


Recap: what is QQ? (derivation from first principles)

Deriving the Bellman optimality equation. Split the sum into "first reward" + "rest":

Q(s,a)=E[rt+γk=0γkrt+1+k]Q^*(s,a)=\mathbb{E}\big[r_t + \gamma \sum_{k=0}^\infty \gamma^k r_{t+1+k}\big]

The "rest" term, if we act optimally from st+1s_{t+1} onward, is exactly maxaQ(st+1,a)\max_{a'} Q^*(s_{t+1},a'). So:


Turning Bellman into a loss (the DQN objective)

We want Q(s,a;θ)Q(s,a;\theta) to satisfy Bellman. If it did, the two sides would be equal, so their difference (the TD error) would be zero. Minimise the squared TD error:

Why is the target y=r+γmaxaQ(s,a;θ)y = r+\gamma\max_{a'}Q(s',a';\theta^-) and not the true QQ^*? We don't know QQ^*! So we bootstrap: use our current best estimate of the next state's value as a stand-in. This is a moving target — which is exactly why we need the two tricks below.

HOW do we update? Gradient descent on L\mathcal{L} (treating yy as a constant, i.e. don't backprop through the target): θθ+α(yQ(s,a;θ))θQ(s,a;θ)\theta \leftarrow \theta + \alpha\big(y - Q(s,a;\theta)\big)\nabla_\theta Q(s,a;\theta)


The two tricks (this is the "Deep" secret sauce)

Figure — Deep Q-Networks (DQN)

The full DQN algorithm

  1. Initialise net θ\theta, target net θ ⁣= ⁣θ\theta^-\!=\!\theta, empty replay buffer DD.
  2. For each step:
    • Pick action ϵ\epsilon-greedy: random with prob ϵ\epsilon, else argmaxaQ(s,a;θ)\arg\max_a Q(s,a;\theta).
    • Execute aa, observe r,sr, s'; store (s,a,r,s)(s,a,r,s') in DD.
    • Sample a minibatch from DD.
    • Compute targets y=r+γmaxaQ(s,a;θ)y = r + \gamma\max_{a'}Q(s',a';\theta^-) (use y=ry=r if ss' terminal).
    • Gradient step on (yQ(s,a;θ))2(y - Q(s,a;\theta))^2.
    • Every CC steps: θθ\theta^- \leftarrow \theta.

Why ϵ\epsilon-greedy? Pure greedy never explores unknown actions → gets stuck. ϵ\epsilon forces exploration; we anneal ϵ\epsilon from ~1 down to ~0.1 over time (explore early, exploit late).


Worked Example 1 — one gradient target by hand

State ss, took action aa, got reward r=1r=1, landed in ss' (not terminal), γ=0.9\gamma=0.9. Target net says Q(s,left)=2Q(s',\text{left})=2, Q(s,right)=5Q(s',\text{right})=5. Current net: Q(s,a)=3Q(s,a)=3.

  • Step: maxaQ(s,a)=max(2,5)=5\max_{a'}Q(s',a')=\max(2,5)=5. Why? Bellman uses the best next action.
  • Step: y=r+γ5=1+0.9(5)=5.5y = r+\gamma\cdot 5 = 1 + 0.9(5) = 5.5. Why? This is our bootstrapped target.
  • Step: TD error =yQ(s,a)=5.53=2.5=y-Q(s,a)=5.5-3=2.5. Why? Net underestimates; push Q(s,a)Q(s,a) up toward 5.55.5.

Worked Example 2 — terminal state

Same but ss' is terminal (episode ended). Then y=r=1y=r=1.

  • Why? No future rewards after termination, so the γmaxQ\gamma\max Q term is dropped.
  • If old Q(s,a)=1.2Q(s,a)=1.2, TD error =11.2=0.2=1-1.2=-0.2 → nudge Q(s,a)Q(s,a) down.


Recall Feynman: explain to a 12-year-old

Imagine a video game. You want a robot that learns which button to press in each screen. Writing down "best button for every possible screen" is impossible — there are too many screens. So instead we teach a smart guesser (a brain-like network) that looks at the screen and guesses how good each button is. It learns from a notebook of past plays (replay buffer), and it compares its guess to a slightly-older, steadier version of itself (target network) so it doesn't get confused chasing a wobbling answer. Over many games it gets better and better.


Flashcards

What does DQN replace the Q-table with?
A neural network Q(s,a;θ)Q(s,a;\theta) that generalises across similar states.
Write the DQN target yy for a non-terminal transition.
y=r+γmaxaQ(s,a;θ)y = r + \gamma\max_{a'}Q(s',a';\theta^-).
What is yy for a terminal transition and why?
y=ry=r; there is no future reward after the episode ends, so drop γmaxQ\gamma\max Q.
Why do we use a separate target network θ\theta^-?
To keep the regression target fixed for CC steps, avoiding chasing a moving target → stability.
Why experience replay?
Breaks correlation between consecutive samples (restores i.i.d.) and reuses data for efficiency.
Should you backprop through the target term?
No — detach it; it's treated as a fixed label.
Can vanilla DQN handle continuous action spaces?
No; maxa\max_{a'} needs enumerable (discrete) actions.
What is the TD error in DQN?
δ=yQ(s,a;θ)\delta = y - Q(s,a;\theta), the difference between bootstrapped target and current estimate.
Why ϵ\epsilon-greedy exploration?
Ensures unvisited actions still get tried, preventing premature convergence to a bad policy.
Is DQN on-policy or off-policy?
Off-policy — it learns the greedy QQ^* while acting ϵ\epsilon-greedily, which is why replay is valid.
State the Bellman optimality equation DQN approximates.
Q(s,a)=Es[r+γmaxaQ(s,a)]Q^*(s,a)=\mathbb{E}_{s'}[r+\gamma\max_{a'}Q^*(s',a')].

Connections

Concept Map

breaks

motivates

approximates Q with

enables

recursion gives

defines target y

minimised by

frozen copy stabilises

random minibatches

creates moving target

uses trick 1

uses trick 2

Huge state space e.g. Atari pixels

Tabular Q-learning

Deep Q-Network

Neural net Q s,a,theta

Generalisation across states

Action-value Q s,a

Bellman optimality

DQN loss squared TD error

Gradient descent on theta

Target network theta-minus

Experience replay buffer

Bootstrapping

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, DQN ka core idea bahut simple hai: normal Q-learning mein hum ek table banate hain jisme har state-action pair ke liye ek Q-value store hoti hai. Lekin jab states bahut zyada ho (jaise Atari game ke raw pixels), to table impossible ho jaati hai. Isliye hum table ki jagah ek neural network laga dete hain jo Q(s,a;θ)Q(s,a;\theta) predict karta hai. Network similar states ke liye similar values seekh leta hai — yehi generalisation ka fayda hai.

Target kahaan se aata hai? Bellman equation se: y=r+γmaxaQ(s,a)y = r + \gamma\max_{a'}Q(s',a'). Matlab abhi ka reward plus next state se milne wali best future value. Hum nahi jaante true QQ^*, isliye hum apne hi network ke estimate se target bana lete hain — isko bootstrapping kehte hain. Phir loss (yQ(s,a;θ))2(y - Q(s,a;\theta))^2 ko minimise karte hain gradient descent se.

Do tricks yaad rakhna zaroori hai. Pehla experience replay — purane transitions ek buffer mein store karke random minibatch nikalte hain, taaki consecutive frames ka correlation toot jaaye aur training stable rahe. Doosra target network — ek alag frozen copy θ\theta^- jise har CC steps baad update karte hain, warna target khud hilti rahegi aur network apni hi poonchh ke peeche bhaagta rahega (diverge ho jaayega).

Kuch cheezein galat mat karna: target ko detach karna (uske through backprop nahi), terminal state par y=ry=r rakhna (max wala term drop), aur yaad rakhna DQN sirf discrete actions ke liye hai kyunki maxa\max_{a'} ke liye actions count karne padte hain. Continuous actions ke liye DDPG/SAC use hote hain. Bas itna clear ho gaya to DQN aapka pakka hai!

Go deeper — visual, from zero

Test yourself — Deep & Advanced RL

Connections