Value Methods against Policy Methods
Q-learning, SARSA, DQN, REINFORCE, A2C and PPO side by side. Targets, defaults, diagnostics and fixes.
- Concepts and terms
- Tasks and commands
- Annotated code
- Diagrams
- Comparisons
- Defaults and numbers
- Troubleshooting
- Questions and tips
Four methods side by side#
| Factor | Q-learning, SARSA | DQN | REINFORCE | A2C, PPO |
|---|---|---|---|---|
| Learns | Q table | Neural Q(s, a; \theta) | Policy \pi_\theta(a \mid s) | Actor \pi_\theta plus critic V_\phi(s) |
| Picks actions | ε-greedy on Q | ε-greedy on Q | Sample from \pi_\theta | Sample from \pi_\theta |
| Data | Q-learning off-policy, SARSA on-policy | Off-policy, replay buffer | On-policy, one update | A2C one update, PPO K epochs |
| Target | r + \gamma \max_{a'} Q(s', a') | Same, from target net \theta^- | Return G_t | GAE advantage |
| Bias and variance | Bootstrap bias, low variance | Max bias upward | Unbiased, high variance | Critic bias, far lower variance |
| Continuous actions | No | No, route to SAC or TD3 | Yes, Gaussian policy | Yes, Gaussian policy |
| Toy GridWorld | ~2,000 episodes | ~500 episodes | ~500 episodes with a baseline | PPO with ε 0.2, K 4 |
From Lesson 09.04, Lesson 09.05, Lesson 09.06, Lesson 09.07, Lesson 09.08
Value side: TD targets#
a = epsilon_greedy(Q, s, rng, epsilon)behaviour actions_next, r, done = step(s, a)best_next = max(Q[s_next].values())Q-learning: greedy maxtarget = r + gamma * best_nexta_next = epsilon_greedy(Q, s_next, rng, epsilon)target = r + gamma * Q[s_next][a_next]SARSA: the next action takenQ[s][a] += alpha * (target - Q[s][a])same TD update for bothQ[s][a] += alpha * (r - Q[s][a])terminal step, no bootstrapFrom 09.04 code/main.py
Policy side: gradient, GAE, clip#
G = r + gamma * Greward-to-go, walked backwardsgrad = (1.0 if i == a else 0.0) - probs[i]onehot minus probstheta[i][j] += lr * adv * grad * x[j]REINFORCE ascentdelta = traj[t]["r"] + gamma * next_v - traj[t]["v"]TD residualgae = delta + gamma * lam * gaeGAE with lam=0.95ratio = math.exp(logp - rec["log_pi_old"])PPO importance ratioclipped = (adv > 0 and ratio > 1 + eps) or (adv < 0 and ratio < 1 - eps)pg_scale = 0.0clipped sample, no gradientFrom 09.06 code/main.py, 09.07 code/main.py, 09.08 code/main.py
Defaults#
| Setting | Default | Note |
|---|---|---|
| Tabular α and ε | 0.1 | Pin α in [0.05, 0.3] |
| Discount γ | 0.99 | Default in the lesson code |
| DQN target sync | 10,000 | Atari env steps |
| GAE λ | 0.95 | The 2026 default |
Value weight c_v | 0.5 | A2C and PPO |
Entropy weight c_e | 0.01 | A2C and PPO |
| PPO clip ε | 0.2 | 0.1 is timid, 0.3+ is unstable |
| PPO epochs K | 10 | Minibatches of 64 |
Troubleshooting#
| Symptom | Cause | Fix |
|---|---|---|
| Q values overestimate | max over noisy Q is biased up | Double DQN or Double Q-learning |
| DQN diverges | Deadly triad | Keep the target net and the replay buffer |
| DQN overfits early | Gradients on ~20 samples | Wait for a few thousand transitions |
| Policy stops exploring | Entropy collapse | Entropy bonus, c_e above 0 |
| Policy gradient explodes | Huge returns | Normalize G to ~N(0, 1) per batch |
| Actor learns noise | Critic is still random | Warm up the critic, slow actor LR |
| PPO mean KL past 0.1 | Too many epochs or high LR | Reduce K_EPOCHS or LR |
| Clip fraction near 0 | Clip never triggers | Raise LR or K_EPOCHS |
From Lesson 09.04, Lesson 09.05, Lesson 09.06, Lesson 09.07, Lesson 09.08
Use X when#
- Q-learning: small tabular environments. It learns the optimal policy directly.
- SARSA or Expected SARSA: on-policy safety-critical tasks. They stay conservative during exploration.
- DQN: high-dimensional state with discrete actions. It is the reference off-policy algorithm.
- REINFORCE: read it, rarely run it.
loss = -advantage * log_probis REINFORCE with a baseline. - A2C: batched parallel envs on a GPU. GPU-based A2C dominates A3C in 2026.
- PPO: the default for MuJoCo, Atari and RLHF for LLMs.
- SAC or TD3: continuous control. DQN has no policy network.
- GRPO: reasoning LLMs. It is a PPO variant without a critic.
From Lesson 09.04, Lesson 09.05, Lesson 09.06, Lesson 09.07, Lesson 09.08
DQN training loop#
PPO training loop#
Tasks in code#
| Task | Code |
|---|---|
| Tabular SARSA | sarsa(episodes, |
| Tabular Q-learning | q_learning(episodes, |
| One DQN gradient step | train_step(online, |
| Sync the target net | target = |
| REINFORCE with baseline | reinforce(episodes, |
| GAE advantages | gae_advantages(traj, |
| Train A2C | actor_critic(episodes, |
| PPO update with diagnostics | mean_kl, |
From 09.04 code/main.py, 09.05 code/main.py, 09.06 code/main.py, 09.07 code/main.py, 09.08 code/main.py
Key formulas#
- TD error
- \delta = r + \gamma V(s') - V(s)
- SARSA target
- r + \gamma Q(s', a') with the action taken next.
- DQN loss
- (r + \gamma \max_{a'} Q(s', a'; \theta^-) - Q(s, a; \theta))^2
- Double DQN
- r + \gamma Q(s', \arg\max_{a'} Q(s', a'; \theta); \theta^-)
- Policy gradient
- \nabla J = \mathbb{E}[G_t \nabla_\theta \log \pi_\theta(a_t \mid s_t)]
- Advantage
- A = G - \hat{V}(s). Same expectation, lower variance.
- GAE
- A_t = \sum_l (\gamma \lambda)^l \delta_{t+l}. λ 0 is TD, λ 1 is MC.
- PPO clip
- \min(r_t A_t, \mathrm{clip}(r_t, 1-\epsilon, 1+\epsilon) A_t)
From Lesson 09.04, Lesson 09.05, Lesson 09.06, Lesson 09.07, Lesson 09.08
Abbreviations#
- TD
- temporal difference. Update after every step from a bootstrapped target.
- SARSA
- the tuple (s, a, r, s', a'). On-policy TD.
- GLIE
- Greedy in the Limit with Infinite Exploration. The ε condition.
- DQN
- Q-learning with a neural Q, replay buffer and target network.
- GAE
- Generalized Advantage Estimation. Weighted sum of n-step advantages.
- A2C, A3C
- synchronous batched, or asynchronous threaded, actor-critic.
- TRPO
- Trust Region Policy Optimization. KL constraint, conjugate gradients.
- PPO
- Proximal Policy Optimization. Clipped importance ratio.
Questions#
- Why is Q-learning off-policy?
- The
maxassumes the greedy policy, whatever action the agent takes. - Why is SARSA safer on the cliff?
- It puts exploration noise into its Q values.
- Why are value methods weak on continuous actions?
argmaxover a 10-dimensional torque is hard.- Why is a baseline unbiased?
- The expected baseline times the log-policy gradient is 0.
- Why can PPO reuse a rollout?
- The clip stops the update once the policy drifts too far.
- Most common PPO bug?
- A flipped sign. Minimize the negative of the clipped surrogate.