5.2.2 · D4Deep & Advanced RL

Exercises — Experience replay

2,526 words11 min readBack to topic

This page assumes only what the parent note built: a transition , the replay buffer , the TD target , and the TD error . Every symbol below is used exactly as defined there.


Level 1 — Recognition

Exercise 1.1

Match each symbol to its plain-English meaning: , , , .

Recall Solution
  • = the discount factor, a number in that says how much a reward one step in the future is worth now. means "future rewards count 90% as much per step".
  • = the terminal flag: if ended the episode (game over), else . It switches off the future-value term.
  • = the weights of the target network, a slow-changing frozen copy of (see Target Networks).
  • = the replay buffer, the FIFO memory of past transitions.

Exercise 1.2

The parent note lists three diseases naive online deep RL suffers. Name them in one phrase each.

Recall Solution
  1. Correlated samples — consecutive states are near-duplicates, violating the i.i.d. assumption of SGD.
  2. Data inefficiency — a rare informative transition is seen once and thrown away.
  3. Non-stationary target chasing — the incoming data distribution shifts as the policy changes.

Exercise 1.3

True or false: "Experience replay requires the transitions to be replayed in the order they occurred." Explain.

Recall Solution

False. The whole point is to sample uniformly at random, (the operator you met above). In-order replay preserves the temporal correlation you were trying to destroy.


Level 2 — Application

Exercise 2.1

A non-terminal transition has , , and . The current estimate is . Compute the target and the TD error .

Recall Solution

What/Why: the target injects the discounted best future value — the one-step bootstrap of the Bellman optimality equation , with replaced by our frozen copy . The figure below reads this off a number line: the current estimate sits at , the target at , and is the gap the gradient step closes.

Figure — Experience replay

Exercise 2.2

A terminal transition has , , , and . Compute and . Which factor makes the term vanish?

Recall Solution

Terminal , so the bootstrap term is killed: The == factor== zeroes the future value: a terminal state has no future.

Exercise 2.3

A mini-batch has TD errors and . Compute the mean-squared batch loss .

Recall Solution


Level 3 — Analysis

Exercise 3.1

Four transitions have TD errors of magnitude . Using PER with and , compute the sampling probabilities . (Priority .)

Recall Solution

Step 1 — priorities: . Step 2 — raise to (square root): Step 3 — normalise. Sum . Interpretation: the highest-error transition is sampled about more often than a low-error one. Here is the same TD error you meet in Temporal-Difference Learning — big means "my prediction is far from the target," so PER studies it more. Figure s03 shows how the bar heights sharpen as grows.

Figure — Experience replay

Exercise 3.2

For the same four transitions, take the buffer size and . Compute the importance-sampling weight for the highest-priority transition () and the lowest (), then normalise by .

Recall Solution

What/Why: biased sampling ⇒ biased gradient; the IS weight from Importance Sampling down-weights the frequently-picked (high-priority) transitions to restore an unbiased average. High-priority: . Low-priority: . Normalise by the max (, the low-priority one — largest weight goes to the rarest-sampled): The often-sampled hard transition is scaled down to ; the rarely-sampled easy one keeps weight . That is exactly the correction.

Exercise 3.3

A student runs DQN with a buffer of 1 million transitions on a task where the good policy is found in the first 50k steps but the optimal policy needs fine-tuning near the end. They notice adaptation is slow late in training. Diagnose using the parent's bias–freshness idea.

Recall Solution

A 1M buffer keeps stale transitions from a much worse early policy. Late in training, most sampled data reflects behaviour the agent has already outgrown, so gradients pull toward outdated targets and fine-tuning stalls. Fix: shrink the buffer (or bias sampling toward recent data) so fresh, near-optimal transitions dominate — the bias–freshness trade-off. Bigger is not always better.


Level 4 — Synthesis

Exercise 4.1

Show that setting in PER exactly recovers uniform replay. Then show that as , the IS weights all become equal (before normalisation) so the correction becomes a no-op. Assume fixed.

Recall Solution

Uniform recovery: with , every , so That is exactly . IS weights collapse: substitute : All weights equal ⇒ after normalising by the max, each is ⇒ multiplying every by changes nothing. So plain replay needs no IS correction — consistent with the fact that uniform sampling is already unbiased.

Exercise 4.2

You have TD errors magnitude , . Compute the ratio of to for , , and . What does the trend tell you about 's role?

Recall Solution

Priorities ; highest , lowest . The normalisation constant cancels in a ratio, so .

  • : (uniform — no preference).
  • : .
  • : (fully greedy). Reading: is a sharpness knob. At all transitions are equal; as the highest-error transition is preferred ever more aggressively. It smoothly interpolates between uniform and greedy prioritisation.

Exercise 4.3

Explain why raw experience replay is valid for Deep Q-Networks (DQN), DDPG, and Soft Actor-Critic but breaks vanilla policy-gradient (A2C). Tie it to Off-policy vs On-policy.

Recall Solution

Replay stores transitions generated by old policies. DQN/DDPG/SAC are off-policy: their learning target (the Bellman backup) is defined for any behaviour data — the (DQN) or a separately-trained critic (DDPG/SAC) evaluates the current greedy/actor policy regardless of who collected the sample. Vanilla policy gradient is on-policy: its gradient is an expectation under the current policy . Feeding it stale data estimates the gradient of the wrong distribution. Remedy if you must: apply Importance Sampling ratios to reweight — the same correction principle PER uses.


Level 5 — Mastery

Exercise 5.1

Prove that uniform sampling from gives an unbiased estimate of when 's empirical distribution equals , and explain in one sentence why PER breaks this and needs .

Recall Solution

Let with empirical distribution matching . Draw one index uniformly (): . The expected gradient of a single uniform draw is which is precisely the empirical mean = Monte-Carlo estimate of . So the estimate is unbiased. Why PER breaks it: PER draws with probability , so — a biased mean; multiplying by rescales each term back so the weighted expectation again equals the uniform mean.

Exercise 5.2

Design. Under PER with , a transition with a large one-off (caused by a noisy reward, not genuine learning value) gets sampled repeatedly and dominates the buffer's compute. Propose two concrete mechanisms from the parent's toolbox that mitigate this, and justify each.

Recall Solution

Mechanism 1 — lower (e.g. ). Since , reducing flattens the distribution, capping how much any single noisy transition can dominate (Ex 4.2 showed the ratio is ). It trades focus for robustness. Mechanism 2 — the additive and periodic priority refresh. The floor keeps every transition sample-able so a spuriously-high one is eventually re-evaluated; when it is resampled its priority is recomputed with the current , and if the large error was noise, drops and its priority collapses back to normal. Together they prevent permanent lock-on to one noisy sample.

Exercise 5.3

Edge case — new transitions. A brand-new transition has just been inserted into a PER buffer; the network has never computed a TD error for it, so it has no . (a) What initial priority does standard PER assign it, and why? (b) A buffer holds transitions with priorities (the is such a freshly-inserted transition given max priority; already included). With compute its sampling probability. (c) With compute the raw IS weights of the high- and a low-priority transition and show that is equal across them — the hallmark of a fully-corrected () estimate.

Recall Solution

(a) Initial priority = the current maximum priority in the buffer, . Why: with no we cannot rank it, and we want to guarantee it is sampled at least once soon (so its real gets measured). Giving it the maximum priority makes it maximally likely to be picked next; after that first visit its priority is overwritten with the true . Initialising to or the mean would risk a genuinely important transition never being seen. (b) Sampling probability. Sum of priorities . With , , so (c) Raw IS weights (): . Normalising by the max (): , . The check: compute for each: They are equal (). At the sampling frequency times the weight is constant across transitions, i.e. the weighted estimate reproduces the uniform (unbiased) mean exactly. That is the whole purpose of the IS weight.


Recall One-line summaries to self-quiz

means what? ::: The uniform sampling operator — pick each stored transition with equal probability . controls what? ::: The sharpness of prioritisation — uniform, fully greedy. controls what? ::: How fully the importance-sampling bias is corrected — anneal to . Initial priority of a brand-new transition? ::: The current maximum priority, so it is sampled at least once soon. Why normalise by ? ::: To keep every weight in so updates only shrink, preserving stability. Terminal-state target formula? ::: (the factor zeroes the bootstrap). Replay is valid for which method class? ::: Off-policy (DQN, DDPG, SAC).