5.2.5 · D3Deep & Advanced RL

Worked examples — Policy gradient methods

2,173 words10 min readBack to topic

This page is the practice arena for Policy gradient methods. The parent note built the theory; here we grind through every kind of situation the policy gradient can throw at you: positive and negative returns, the do-nothing (zero-gradient) case, degenerate deterministic policies, continuous actions, a full multi-step trajectory, a baseline rescue, and an exam twist.

Before a single number: recall the one object we keep computing, the score — literally "how would the log-probability of the action I took change if I wiggled ?" Every example is that score times a weight (return, reward-to-go, or advantage). If any symbol here feels unearned, re-read the parent's derivation and the Log-derivative trick.


The scenario matrix

Every policy-gradient exercise lands in one of these cells. Each worked example below is tagged with the cell(s) it covers.

# Cell class What's special Covered by
C1 Positive return, discrete good action, push up Ex 1
C2 Negative return, discrete bad action, push down Ex 2
C3 Zero / degenerate weight return or baseline cancels → no update Ex 3
C4 Deterministic-limit policy or : score blows up / vanishes Ex 4
C5 Multi-action softmax full gradient over actions, reward-to-go Ex 5
C6 Continuous action (Gaussian) impossible; score of a normal density Ex 6
C7 Baseline rescue large-offset returns, variance killed by Ex 7
C8 Full trajectory, causality discounting + reward-to-go across time Ex 8
C9 Exam twist "gradient is zero because reward has no " trap, sign of -weighting Ex 9

Building blocks we reuse

Two policies appear everywhere below. Let's earn their scores once.

Figure — Policy gradient methods

The figure shows why the score is for : when is already near 1, there's little room to grow, so the nudge is small; when is near 0, the score is near 1 — a huge push. Look at the orange curve's slope.


The worked examples

Figure — Policy gradient methods

The figure shows the Gaussian centred at ; the sampled sits on the right tail; the arrow shows the mean sliding right because . If had been negative, the arrow would flip left — the score's sign is fixed, the reward decides the direction.


Recall Rapid self-check

Which sub-quantity carries the sign of "good vs bad"? ::: The weight (return / advantage), not the score. Near a deterministic policy, whose score dominates the gradient? ::: The rare, surprising action's score (); the confident action's score . Do softmax scores over all actions sum to? ::: Zero — probability mass is only redistributed. Why does baseline subtraction leave the gradient unbiased? ::: . For continuous actions, what replaces the impossible ? ::: The score of the action's density, e.g. for a Gaussian.


Connections

  • Policy gradient methods (index 5.2.5) — the parent derivation these examples exercise.
  • REINFORCE algorithm — the Monte-Carlo estimator used in Ex 1–2, 5, 8–9.
  • Score function estimator and Log-derivative trick — the machinery behind every score.
  • Advantage function · Variance reduction in Monte Carlo — Ex 7.
  • Actor-Critic methods · Generalized Advantage Estimation — where the baseline becomes learned.
  • Value-based methods (Q-learning) — the Ex 6 sidesteps.