Pearl
Lab

Chapter 06 · Policy optimization

Policy Gradients

Abstract. Policy gradient methods parameterize the policy \(\pi_\theta(a\mid s)\) directly and ascend the expected return \(J(\theta)\) in parameter space. The policy gradient theorem — via the log-derivative identity — yields a gradient that does not require differentiating through the environment. This chapter derives REINFORCE, shows why baselines (advantages) cut variance without bias, introduces actor–critic as TD bootstrapping inside the policy gradient, previews GAE, and motivates natural gradients / trust regions as the bridge to TRPO and PPO. The laboratory trains a tabular softmax policy with REINFORCE, baseline, and one-step actor–critic on a small grid.

1. Overview

Chapters 04–05 optimized behavior by learning \(Q\) and reading \(\pi(s)=\arg\max_a Q(s,a)\). That recipe breaks for continuous actions and struggles when the optimal policy is inherently stochastic. Policy gradients flip the approach: parameterize \(\pi_\theta\) and push \(\theta\) uphill on expected return.

Everything from here through PPO and (with a fork) SAC is “policy gradients, made stabler or more sample-efficient.” This chapter is the spine.

2. Why leave value-based methods?

  1. Continuous actions. \(\arg\max_a Q(s,a)\) over \(a\in\mathbb{R}^m\) is an optimization problem at every timestep — expensive and awkward. A Gaussian policy \(\pi_\theta(a\mid s)=\mathcal{N}(\mu_\theta(s),\Sigma_\theta(s))\) outputs actions by sampling, no inner \(\arg\max\).
  2. Stochastic optima. Rock–paper–scissors, mixed strategies, or robots that must inject noise to avoid getting stuck: the optimal policy need not be deterministic. Softmax / Gaussian policies express that naturally; greedy Q does not (unless you add external \(\varepsilon\)).
  3. Direct optimization. You optimize the thing you care about \(J(\theta)\) rather than solving a fixed-point for \(Q^*\) and hoping greedy readout is well-behaved under approximation.

3. The objective

Definition 3.1 · Expected return

Let \(\theta\) parameterize \(\pi_\theta\). Write trajectories \(\tau=(s_0,a_0,r_1,\ldots)\) with density \(p_\theta(\tau)\). Then \[ J(\theta) = \mathbb{E}_{\tau\sim p_\theta}\big[R(\tau)\big], \qquad R(\tau)=\sum_{t\ge 0}\gamma^t r_{t+1}. \] Equivalently \(J(\theta)=\mathbb{E}_{s_0\sim\mu}[V^{\pi_\theta}(s_0)]\).

We want \(\nabla_\theta J(\theta)\). The difficulty: \(R(\tau)\) depends on states visited under \(\pi_\theta\), and the transition kernel \(P(s'|s,a)\) is an opaque black box — you cannot backprop through physics in the general RL setting.

4. The log-derivative trick

For any density \(p_\theta(x)\) with \(p_\theta(x)>0\),

(1) \[ \nabla_\theta p_\theta(x) = p_\theta(x)\, \nabla_\theta \log p_\theta(x). \]

Therefore \(\nabla_\theta \mathbb{E}_{x\sim p_\theta}[f(x)] = \mathbb{E}_{x\sim p_\theta}\big[f(x)\,\nabla_\theta\log p_\theta(x)\big]\) when \(f\) does not depend on \(\theta\) explicitly. Apply this with \(x=\tau\) and \(f=R\):

(2) \[ \nabla_\theta J(\theta) = \mathbb{E}_{\tau\sim p_\theta} \big[ R(\tau)\, \nabla_\theta \log p_\theta(\tau) \big]. \]

Factor the trajectory density (Chapter 01):

(3) \[ p_\theta(\tau) = \mu(s_0) \prod_t \pi_\theta(a_t\mid s_t)\, P(s_{t+1}\mid s_t,a_t). \]

Take \(\log\), then \(\nabla_\theta\): every \(P(\cdot)\) term drops out — it does not depend on \(\theta\). What remains is

(4) \[ \nabla_\theta \log p_\theta(\tau) = \sum_t \nabla_\theta \log \pi_\theta(a_t\mid s_t). \]
Punchline

You never differentiate the environment. You only need to sample trajectories and evaluate \(\nabla_\theta\log\pi_\theta(a\mid s)\) — a property of your own policy network (or softmax table).

5. Policy gradient theorem

Combining (2) and (4), and refining the credit so that action \(a_t\) is weighted only by future return from \(t\) (rewards before \(t\) are independent of \(a_t\) given the past), one obtains the standard form:

(5) \[ \nabla_\theta J(\theta) = \mathbb{E}_{s\sim d^{\pi_\theta},\, a\sim\pi_\theta} \big[ \nabla_\theta \log \pi_\theta(a\mid s)\, Q^{\pi_\theta}(s,a) \big]. \]

Equivalently with advantages \(A^{\pi}=Q^{\pi}-V^{\pi}\) (same expectation — Section 7). Here \(d^{\pi}\) is the discounted occupancy from Chapter 01.

Theorem 5.1 · Policy gradient theorem (statement)

For differentiable \(\pi_\theta\) in a finite MDP (or suitable continuous setups), \(\nabla_\theta J(\theta)\) equals (5). The environment Jacobian never appears.

6. REINFORCE

Replace \(Q^{\pi}(s_t,a_t)\) by the Monte Carlo return \(G_t\) and use a single sampled trajectory:

(6) \[ \theta \leftarrow \theta + \alpha \sum_{t=0}^{T-1} \nabla_\theta \log \pi_\theta(a_t\mid s_t)\, G_t. \]

Intuition: if the episode went well (large \(G_t\)), increase the log-probability of the actions you took; if it went poorly, decrease them. This is Monte Carlo applied to policies — unbiased (for the true gradient, in expectation) but high variance. You must wait until the episode ends to form \(G_t\).

Remark · Softmax policy (discrete lab)

For logits \(h(s,a)\), \(\pi(a\mid s)=\mathrm{softmax}_a(h(s,a))\). Then \(\nabla_{h(s,\cdot)}\log\pi(a\mid s) = \mathbf{1}_a - \pi(\cdot\mid s)\). The laboratory uses exactly this identity on a per-state logit table.

7. Baselines and the advantage

Subtract any action-independent baseline \(b(s_t)\) from \(G_t\):

(7) \[ \theta \leftarrow \theta + \alpha \sum_t \nabla_\theta \log \pi_\theta(a_t\mid s_t)\, \big(G_t - b(s_t)\big). \]

This does not change the expected gradient: \(\mathbb{E}_{a\sim\pi}[\nabla\log\pi(a\mid s)\,b(s)]=b(s)\nabla\sum_a\pi(a\mid s)=0\). Choosing \(b(s)=V^{\pi}(s)\) yields the advantage \(A_t \approx G_t - V(s_t)\): not “was return good?” but “was it better than usual from this state?” — much lower variance.

Definition 7.1 · Advantage

\(A^{\pi}(s,a)=Q^{\pi}(s,a)-V^{\pi}(s)\). Then \(\mathbb{E}_{a\sim\pi}[A^{\pi}(s,a)]=0\), and \[ \nabla_\theta J(\theta) = \mathbb{E}\big[ \nabla_\theta\log\pi_\theta(a\mid s)\, A^{\pi}(s,a) \big]. \]

8. Actor–Critic

REINFORCE still needs full \(G_t\). Replace the Monte Carlo advantage by a one-step TD advantage — the TD error from Chapter 04:

(8) \[ \delta_t = r_{t+1} + \gamma V_w(s_{t+1}) - V_w(s_t), \qquad \theta \leftarrow \theta + \alpha\, \nabla_\theta\log\pi_\theta(a_t\mid s_t)\, \delta_t. \]

Two learners in parallel:

  • Actor \(\pi_\theta\) — updated by (8).
  • Critic \(V_w\) — updated by ordinary TD(0): \(w \leftarrow w + \beta\,\delta_t\,\nabla_w V_w(s_t)\) (tabular: \(V(s)\leftarrow V(s)+\beta\delta_t\)).

The same \(\delta_t\) trains both. This is the skeleton of A2C/A3C and, with GAE and trust regions, of TRPO/PPO. SAC is also an actor–critic, but off-policy with an entropy-augmented critic target.

Analysis · Bias returns

Using \(V_w\) instead of \(V^{\pi}\) biases the gradient unless compatibility conditions hold (compatible function approximation). Deep actor–critics usually accept that bias as an empirical trade-off — the same honesty we owed DQN about the deadly triad.

9. Generalized Advantage Estimation (preview)

Define \(\delta_t = r_{t+1}+\gamma V(s_{t+1})-V(s_t)\). The \(k\)-step advantage is \(\hat A_t^{(k)}=\sum_{l=0}^{k-1}\gamma^l\delta_{t+l}\). GAE exponentially averages all \(k\):

(9) \[ \hat A_t^{\mathrm{GAE}(\gamma,\lambda)} = \sum_{l=0}^{\infty}(\gamma\lambda)^l\, \delta_{t+l}. \]

\(\lambda=0\): pure one-step TD advantage (low variance, more bias). \(\lambda=1\): Monte Carlo advantage (unbiased if \(V\) correct only at end, high variance). Typical deep RL: \(\lambda\approx 0.95\). GAE is the advantage estimator inside standard PPO implementations.

10. Why vanilla PG is fragile — bridge to TRPO

The update \(\theta\leftarrow\theta+\alpha\nabla_\theta J\) is steepest ascent in Euclidean parameter space. A small step in \(\theta\) can be a huge step in policy distribution space (KL divergence) — or almost no step — depending on which weights you touch. One bad update collapses an on-policy method: all new data comes from the broken policy.

The natural policy gradient preconditions with the Fisher information matrix \(F(\theta)=\mathbb{E}[\nabla\log\pi\,\nabla\log\pi^\top]\), which locally approximates KL:

(10) \[ \theta \leftarrow \theta + \alpha\, F(\theta)^{-1}\nabla_\theta J(\theta). \]

TRPO enforces a hard KL trust region and solves the constrained step approximately with conjugate gradients. PPO approximates the same “don’t move \(\pi\) too far” idea with a clipped surrogate — cheap, no Fisher matrix. That is the next chapter.

11. Laboratory · softmax policy on a grid

4×4 grid, goal at bottom-right (\(+1\)), step cost \(0\), \(\gamma\) set below. Policy: per-state logits → softmax. Train with REINFORCE, REINFORCE + learned \(V\) baseline, or one-step actor–critic. Watch episode return and a sample \(\nabla\log\pi\cdot\) signal in the ledger.

Note: the lab omits the usual \(\gamma^t\) discount on \(\nabla\log\pi\) (common practical shortcut when advantages are already discounted via \(G_t\) / \(\delta_t\)). The textbook REINFORCE form sometimes writes \(\sum_t \gamma^t \nabla\log\pi\, G_t\).

π greedy arrows · V shown only for baseline / actor–critic (REINFORCE has no critic).

Policy gradient trainers

REINFORCE · baseline · actor–critic
Method

Active: REINFORCE (MC return)

Rates
Run

π greedy (argmax) · V

Arrow = argmax π(·|s). V shown for baseline/AC only.

Episodes0
Last G₀
Avg G₂₀
|Δlogits|

Gradient ledger

Run an episode. We’ll print G_t, advantage/δ, and a sample logit update.

12. Worked gradient step

Two actions, \(\pi=(\pi_1,\pi_2)=(0.2,0.8)\), took \(a=1\), advantage \(A=2\), learning rate \(\alpha=0.1\). Softmax score-function gradient for the taken action: \(\partial\log\pi_1/\partial h_1 = 1-\pi_1=0.8\), \(\partial\log\pi_1/\partial h_2 = -\pi_2=-0.8\).

(11) \[ h \leftarrow h + 0.1\cdot 2\cdot (0.8,-0.8) = h + (0.16,-0.16). \]

After the update, renormalising the softmax increases \(\pi_1\) — we reinforced the action that had positive advantage.

  1. Sample a ~ π; observe reward / return / δ.
  2. Form weight w = G (REINFORCE), G−V (baseline), or δ (AC).
  3. Compute ∇ log π(a|s) (softmax: 1_a − π).
  4. θ ← θ + α w ∇ log π.
  5. If critic: V(s) ← V(s) + β δ.
  6. Repeat; optionally replace w by GAE(λ).

13. What follows

Vanilla policy gradients work but are brittle in deep nets. Next: TRPO (trust-region / natural gradient done carefully), then PPO (clipped surrogate), then the off-policy continuous-control line toward SAC. Optionally, an imitation-learning detour (behavioral cloning, DAgger) can sit between Phase 1 and those algorithms — your original roadmap allowed either order.

See also

  • Sutton & Barto, Ch. 13 — policy gradient methods.
  • Williams, 1992 — REINFORCE.
  • Schulman et al. — GAE; TRPO; PPO papers.
  • Chapter 05 — the value-based discrete alternative.

Previous

← Q-learning & DQN

Next chapter

TRPO

Trust regions, Fisher metric, conjugate gradients.

Continue →