Worked examples — Deep Q-Networks (DQN)
Before anything, let us re-earn the symbols we will use on every line.
See Bellman Equation, Q-Learning and Temporal Difference Learning for where these come from.
The scenario matrix
Every DQN target-computation you will ever do lives in one of these cells. We will hit all of them.
| Cell | Case class | What is special | Example |
|---|---|---|---|
| A | Normal non-terminal, positive reward | the "textbook" case | Ex 1 |
| B | Terminal state | drop the term, | Ex 2 |
| C | Negative reward | sign flips the target | Ex 3 |
| D | (myopic) vs (far-sighted) | limiting values of discount | Ex 4 |
| E | Tie in the | two actions equally best | Ex 5 |
| F | Minibatch (several transitions at once) | vector of targets & mean loss | Ex 6 |
| G | Real-world word problem | translate a story into | Ex 7 |
| H | Exam twist — backprop-through-target trap | conceptual gradient check | Ex 8 |
| I | Overestimation & the Double-DQN fix | why is biased | Ex 9 |
A small map of how a single transition flows into a number. Watch which network each box uses:
Example 1 — Cell A: normal non-terminal, positive reward
Forecast: Guess now — will be positive or negative? Is our guess of too low?
- Take the max over next actions: . Why this step? Bellman assumes we will act optimally from onward, so we credit the best available next value, not the average.
- Build the target: . Why this step? Split reward into "now" () plus "discounted best future" — this is the bootstrapped stand-in for the true we cannot see.
- TD error: . Why this step? tells the gradient step which way and how hard to nudge the guess.
Verify: , matching the forecast that was too low; the update raises toward . Units: everything is in "reward" units, consistent.
Example 2 — Cell B: terminal state
Forecast: The future is gone. Will be bigger or smaller than in Example 1?
- Recognise termination: there is no to evaluate, so the term vanishes. Why this step? No actions happen after the episode ends, so there is no future reward to discount.
- Target: . Why this step? The definition of is the total future reward, which here is just .
- TD error: . Why this step? Now the guess is too high, so we push it down.
Verify: Compare to Example 1 (): dropping the future term shrinks the target from to , exactly as expected. Sign of flipped from to : same state-guess, but the correct value collapsed because nothing follows.
Example 3 — Cell C: negative reward
Forecast: A big negative reward — will end up negative?
- Max over next actions: . Why this step? Even after a penalty, the best future option still governs the bootstrap.
- Target: . Why this step? Reward is negative but the future is mildly positive; they partly cancel.
- TD error: . Why this step? Guess of was optimistic; push it down toward .
Verify: is negative but not as negative as , because a positive future softened it — sanity-consistent with "penalty now, some hope later". correctly says "lower it".
Example 4 — Cell D: the two extremes of
The discount is a dial. To feel it, we hold everything else fixed and turn the dial to both ends. This is the "limiting behaviour" row of the matrix.

What to see in the figure: the red line is the target , with the horizontal axis labelled discount factor gamma (from to ) and the vertical axis labelled target y. Three black dots sit exactly on the line at , and — each dot is annotated with its own pair so you can read the answer straight off the picture. The red line starts at the immediate reward (where kills the future) and would reach at (full future value added).
Forecast: Which makes the far-future value of matter most?
- (myopic): . Why this step? zeroes the future entirely — the agent only ever sees the next reward.
- : . Why this step? Future is worth half; the big contributes .
- (far-sighted): . Why this step? Nearly all of the future value flows through; the target is dominated by it.
Verify: As rises , rises — monotone, matching the rising red line and the three annotated black dots in the figure (heights , , ).
Example 5 — Cell E: a tie in the
Forecast: Does a tie break the formula? What number does return?
- Max ignores which action wins: . Why this step? returns the value, not the identity of the action. Ties are fine for ; the tie only matters if you also need (for acting), where you'd pick either.
- Target: . Why this step? Undiscounted (), zero immediate reward, so is purely the future.
Verify: Swapping which tied action we "prefer" leaves unchanged — the target is well-defined even under ties. (Contrast: -greedy acting would break the tie randomly; see Epsilon-Greedy Exploration.)
Example 6 — Cell F: a full minibatch
Real DQN trains on a minibatch: several transitions sampled from Experience Replay at once. The loss is the mean squared TD error over the batch.
Forecast: Which transition contributes the biggest squared error?
- ; . Why? Cell-A style (non-terminal).
- ; . Why? Terminal — drop the future term (Cell B). This sample is already perfect, contributes nothing.
- ; . Why? Non-terminal, zero immediate reward.
- Mean loss: . Why this step? Averaging (not summing) keeps the gradient scale independent of batch size.
Verify: Transition 3 has the largest , so it dominates the loss — matches forecast. Terminal transition 2 correctly adds . , as any squared error must be.
Example 7 — Cell G: real-world word problem
Forecast: Immediate reward is negative, but the dock has a great "scan" option. Net effect?
- Translate the story: aisle , forward, , dock (non-terminal), next values . Why this step? DQN only ever sees the tuple ; the story must become numbers.
- Max over next actions: . Why? The dock's best follow-up is "scan".
- Target: . Why? Time cost now, big scan value soon.
- TD error: . Why? The move led somewhere valuable; upgrade the guess.
Verify: Despite the negative reward, moving forward is judged better than the old guess of because it unlocked a high-value next state — a nice reminder that a penalising step can still be optimal. Units: all in reward units; positive ⇒ "raise ".
Example 8 — Cell H: exam twist, the backprop-through-target trap
Forecast: If we do let gradients flow into , is the target a fixed post or a moving one?
- Correct target is detached: in real DQN uses (frozen), so . Why this step? We are solving a fixed-point equation ; the right side is treated as a label, like -values in supervised learning.
- Correct per-sample gradient: . With , : . Why this step? Only carries the gradient; is a constant.
- Update size: with step , , raising toward the fixed . Why? Chases a still target ⇒ stable descent.
- The trap: if instead , the "target" shifts every step (dog-chasing-tail), converting a stable regression into an unstable dynamical system that can diverge — the very reason Double DQN and the target-network trick exist.
Verify: Correct gradient ; the update raises the guess from toward , and the
sign ( times = up) matches the positive TD error . The exam-correct
answer is "detach " — code word stop_gradient / .detach().
Example 9 — Cell I: overestimation and the Double-DQN fix
DQN's is biased upward whenever the value estimates are noisy: taking the max of noisy numbers tends to pick a positive error. This is why Double DQN splits choosing from evaluating.

What to see in the figure: the true next-state value is (the black dotted line). The red bar is DQN's estimate — it sits above the truth at because the max latched onto the luckiest positive noise. The right-hand black bar is Double-DQN's estimate at , which hugs the dotted truth line far more closely. The picture is the whole lesson: red overshoots, Double-DQN corrects.
Forecast: The truth is . Which of the two targets do you expect to land closer to ?
- Vanilla DQN — max of noisy values: , so . Why this step? selects whichever action got the luckiest positive noise, so it systematically overshoots the true — this is the overestimation bias.
- Double-DQN — step 1, choose: the online net's over is action (value is largest). Why this step? One network decides which action looks best; call it .
- Double-DQN — step 2, evaluate with a different net: the evaluator net scores that same action as , so . Why this step? Because a separate network supplies the value, the positive noise that inflated the pick does not also inflate the score — the two noises are independent, so the upward bias cancels on average.
- Compare both to truth : versus . Why this step? Smaller absolute error ⇒ less bias; Double-DQN is the better target here.
Verify: is closer to the true than (), illustrating the overestimation fix, and matching the figure where the red bar overshoots to while the Double-DQN bar sits near the dotted truth line at .
Recall Quick self-test across the matrix
Non-terminal, , , : what is ? ::: . Same but terminal: what is ? ::: . Why does a tie in not break the target? ::: returns the value, not the action identity. In a minibatch, do we sum or average the squared TD errors? ::: Average, to keep gradient scale batch-size-independent. Why detach the target ? ::: It is a fixed label; differentiating through it chases a moving target and can diverge. Why is plain DQN's overoptimistic? ::: Taking the max of noisy estimates systematically selects positive errors. How does Double-DQN reduce that bias? ::: One net chooses the action, a separate net scores it, so the selection noise does not also inflate the value.
Connections
- Deep Q-Networks (DQN) — the parent note these examples drill.
- Bellman Equation — every target here is one Bellman backup.
- Temporal Difference Learning — is the TD error.
- Experience Replay — where Example 6's minibatch comes from.
- Double DQN — the fix demonstrated in Example 9.
- Epsilon-Greedy Exploration — how ties (Example 5) are broken while acting.