Unit 8 of 10 · about 4 hours · measure
Reward clipping, observation scaling and Q, TD-error and gradient diagnostics
Scale decides what the optimizer sees. Clipped rewards stabilize gradients but blind the agent to magnitudes, and unscaled inputs slow or break learning.
You'll be able to
- Explain what reward clipping changes about the optimal policy and the optimizer.
- Read Q scale, TD-error spread and gradient norms as run-health signals.
- Diagnose a run from curves alone: stale, overestimating or clipped-blind.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Reward clipping and the evaluation protocolPlaying Atari with Deep Reinforcement Learning
Section 5 in arXiv:1312.5602v1 (training-only clipping of positive rewards to +1 and negative rewards to -1, with the stated cost that the agent cannot tell magnitudes apart) and Section 5.1 (noisy episode return versus smooth average predicted Q on a fixed random-policy state set)
- Practical stabilization tipsBerkeley CS 185/285 Deep Reinforcement Learning (Spring 2026) - Value-Based RL and Q-Learning in Practice
Lecture 8, slides 21 and 22: test on easy tasks first, large replay buffers, schedules, gradient clipping or Huber loss, Double Q-learning, and multiple seeds
- Loss, clipping and evaluation in codeHands-on Modern RL - From Q-Learning to DQN
The
updateloss lines (mean squared error withclip_grad_norm_atmax_norm=10) and the greedy evaluation withepsilon=0.0. Note the file stores terminated only, which is the correct mask.
Focus on
- Clipping maps every positive reward to +1, so one learning rate works across games. The price is explicit in the paper: magnitudes above 1 become indistinguishable.
- The paper's remedy for noisy evaluation is a fixed state set from a random policy with average predicted max-Q. Return is noisy because small weight changes swing which states the policy visits, so smooth Q with noisy return means the values are improving while behavior still shifts.
- Early runs can look random for a while. Impatient restarts destroy the multi-seed evidence the comparison needs.
- CartPole needs no clipping or scaling. The diagnostic habit (Q mean, TD loss, gradient norm) transfers to tasks that do.
Common mistakes · 3
- Calling clipping a pure rescale. It requantizes: a bonus of 10 and a bonus of 1000 train identically, which can change the optimal policy.
- Reading raw episode return as the learning signal during heavy exploration. The policy visits new states as it learns, so return confounds behavior with values.
- Copying Atari preprocessing onto CartPole and expecting paper curves. Scale choices are task-specific, not portable defaults.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.A game pays 10 or 1000 for two bonuses. What does the clipped agent learn to prefer?Answer
Neither systematically. Both train as +1, so the agent cannot learn the magnitude difference. That is the stated cost of clipping.
2.Average Q rises smoothly while episode return thrashes. Verdict?Answer
Keep training. Values improve on the fixed set while small weight changes still swing which states the policy visits. The paper's Section 5.1 names exactly this split.
3.Gradients explode after removing gradient clipping. What do you check before restoring it?Answer
Target and reward scale first. A huge TD error from a wrong mask or unclipped magnitudes is the cause; clipping would only hide it.
4.Two identical-code CartPole runs differ by 200 return. Next step?Answer
Run more seeds before changing code. Runs are very inconsistent between seeds, so one pair proves nothing.
Practice
- Hands-on Modern RL: CartPole DQN diagnostics run
Extend the file's training loop to log Q-value mean, TD loss and gradient norm each update, and plot episode return against average predicted Q on a fixed state set
Keep both curves. The section mini-project reuses them as stability evidence alongside the fixed-batch tests.
You're done when
Your run reports the clipping policy, fixed-set average Q and episode return side by side, and you can name which curve moved first and why.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 9 more in the section
- Berkeley CS 185/285 Deep Reinforcement Learning (Spring 2026) - Value-Based RL and Q-Learning in PracticeUC Berkeley · primary
Slides are public at https://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-N.pdf; current videos are not, and the public Fall 2023 playlist numbers lectures differently. Study Lecture 7 (value-based RL) and Lecture 8 (Q-learning in practice: replay, target networks, double Q-learning). Work the DQN part of Homework 3 (
src/agents/dqn_agent.py, CartPole then LunarLander with double DQN) on CPU or one small GPU; the Atari run is optional. The homework pinsgym==0.25.2, which fails on NumPy 2, so port it to Gymnasium (terminatedandtruncated) or pinnumpy<2in its own environment, and turn off its forced WandB logging. - Playing Atari with Deep Reinforcement LearningDeepMind · required
Read the algorithm, replay and target-network design plus experimental protocol. Reproduce DQN on a small discrete environment, then ablate replay and target networks rather than attempting an Atari score reproduction.
- Hands-on Modern RL - From Q-Learning to DQNWalking Labs · support
Use Sections 5.1 to 5.4, with the masked target on the components page (the formula on the first page omits the terminal mask).
code/chapter04_dqn/dqn_cartpole.pytrains CartPole on CPU in about two minutes. Treat score claims as hypotheses requiring local seed and version evidence. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; itscode/chapterNNfolders do not follow the site chapter numbers.
Also in this section
- Reinforcement Learning: An Introduction (2nd ed.) - Chapters 9 and 11Richard Sutton and Andrew Barto · required
The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Sections 9.1 to 9.4 (value error, semi-gradient TD, linear methods) and Sections 11.1 to 11.3 and 11.6 (off-policy divergence, the deadly triad, why the Bellman error is not learnable). Reproduce Baird's counterexample (Section 11.2, Figure 11.1) and show the weights diverge under off-policy semi-gradient TD.
- Stanford CS224R Spring 2026 - Q-Learning and Off-Policy MethodsStanford University · support
Use Lecture 06 (Q-learning) and the TA Q-learning tutorial (
material/CS224R_Tutotial_Max.pdf) as a second derivation of fitted Q-iteration, targets and overestimation. Its homeworks run on cloud GPUs; follow the Stanford honor code for course materials. - Deep Reinforcement Learning with Double Q-LearningDeepMind · required
Derive the action-selection and action-evaluation split, construct a noisy maximization example and compare DQN with Double DQN using identical samples and tuning budgets.
- Dueling Network Architectures for Deep Reinforcement LearningWang et al. (ICML 2016) · support
Read the dueling architecture and its aggregation equations (the mean-subtracted form). Implement it as an optional head and test that the value and advantage streams are identifiable only up to the stated constraint.
- CleanRL DQNCleanRL · support
Read the single-file DQN implementation and documented benchmark details after the blank-file update passes. Trace replay, target, seeding, termination and evaluation semantics. The script expects the
final_infoandfinal_observationkeys of older Gymnasium releases; on Gymnasium 1.x (next-step autoreset) it logs no episode returns and raisesKeyErrorat truncation, so run it in an environment withgymnasium==0.29.1or patch those lines and say so. - Deep-ML - Function Approximation and DQN ProblemsDeep-ML · support
Use named problems as closed-book checks after your own implementation passes: 530 (semi-gradient TD(0)), 556 (deadly-triad instability), 557 (Baird's counterexample), 537 (experience replay) and 477 (double Q-learning). Read each problem's tests before trusting its statement.
- Reinforcement Learning Control Laboratory - Deep Value CoreRoadmap-Final · required
Implement DQN and Double DQN with replay and target state, fixed-batch target tests, checkpoint and resume, overestimation evidence and injected online-target, detach, action-gather and replay defects.
- Gymnasium - Time Limits and Vector AutoresetFarama Foundation · reference
Read Termination, Truncation, Importance in learning code and Solution in the time-limits tutorial, plus the vector autoreset note at https://farama.org/Vector-Autoreset-Mode. Next-step autoreset is the default for Gymnasium 1.x vector environments; on Gymnasium 1.4.0 the step after an episode ends returns reward 0 with both flags false and is not a transition.
- Gymnasium - Env.reset and seedingFarama Foundation · reference
Read the
Env.resetentry for the seeding contract: an integer seed resets the environment PRNGnp_random, and a laterresetwithseed=Nonecontinues it, so seed once right after creation.