Deep Q-Networks (DQN)
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: possible images — astronomically many states).
WHY not plain Q-learning? Tabular Q-learning stores one number 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 — a neural net with weights . Nearby states share weights, so learning about one state generalises to similar ones.
Recap: what is ? (derivation from first principles)
Deriving the Bellman optimality equation. Split the sum into "first reward" + "rest":
The "rest" term, if we act optimally from onward, is exactly . So:
Turning Bellman into a loss (the DQN objective)
We want 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 and not the true ? We don't know ! 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 (treating as a constant, i.e. don't backprop through the target):
The two tricks (this is the "Deep" secret sauce)

The full DQN algorithm
- Initialise net , target net , empty replay buffer .
- For each step:
- Pick action -greedy: random with prob , else .
- Execute , observe ; store in .
- Sample a minibatch from .
- Compute targets (use if terminal).
- Gradient step on .
- Every steps: .
Why -greedy? Pure greedy never explores unknown actions → gets stuck. forces exploration; we anneal from ~1 down to ~0.1 over time (explore early, exploit late).
Worked Example 1 — one gradient target by hand
State , took action , got reward , landed in (not terminal), . Target net says , . Current net: .
- Step: . Why? Bellman uses the best next action.
- Step: . Why? This is our bootstrapped target.
- Step: TD error . Why? Net underestimates; push up toward .
Worked Example 2 — terminal state
Same but is terminal (episode ended). Then .
- Why? No future rewards after termination, so the term is dropped.
- If old , TD error → nudge 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?
Write the DQN target for a non-terminal transition.
What is for a terminal transition and why?
Why do we use a separate target network ?
Why experience replay?
Should you backprop through the target term?
Can vanilla DQN handle continuous action spaces?
What is the TD error in DQN?
Why -greedy exploration?
Is DQN on-policy or off-policy?
State the Bellman optimality equation DQN approximates.
Connections
- Q-Learning — tabular ancestor DQN generalises.
- Bellman Equation — the fixed point DQN's loss targets.
- Experience Replay — the buffer trick, also basis of prioritized replay.
- Double DQN — fixes DQN's overestimation bias.
- Dueling DQN — splits value & advantage streams.
- Temporal Difference Learning — bootstrapping source.
- Policy Gradient Methods — alternative family for continuous actions.
- Epsilon-Greedy Exploration — action selection strategy.
Concept Map
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 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: . Matlab abhi ka reward plus next state se milne wali best future value. Hum nahi jaante true , isliye hum apne hi network ke estimate se target bana lete hain — isko bootstrapping kehte hain. Phir loss 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 jise har 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 rakhna (max wala term drop), aur yaad rakhna DQN sirf discrete actions ke liye
hai kyunki 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!