Reinforcement Learning III
10.1 Policy-Based Reinforcement Learning
The methods of the previous chapter learn a value function \(Q_\theta(s,a)\) and derive the policy from it (e.g., \(\varepsilon\)-greedy). Policy-based RL instead parameterizes the policy \(\pi_\theta(s,a) = \Pr(a \mid s;\theta)\) directly and optimizes its value.
Modeling vs. prediction: Q-learning tries to make every \(Q\)-value accurate, but acting only needs their ordering (“right is better than left in \(s\)”, not \(Q(s,\text{left}) = 0.3554\)). The feature-based policies that win games are often not the ones that approximate \(V\) or \(Q\) best, and a value function can be far more complex to represent than the policy it induces.
Three families:
Family Value Function Policy Value-based Learned Implicit (e.g., \(\varepsilon\)-greedy) Policy-based None Learned Actor–critic Learned Learned Advantages: Better convergence properties, effective in high-dimensional or continuous action spaces, and able to learn stochastic policies.
Disadvantages: Typically converges to a local rather than global optimum, and evaluating a policy is inefficient and high-variance, so learning is slow.
10.1.1 When Stochastic Policies Are Optimal
- Iterated rock–paper–scissors: Any deterministic policy is easily exploited; the uniform random policy is optimal (the Nash equilibrium).
- Aliased gridworld: The two grey cells have identical features \(\phi(s,a) = \mathbf 1(\text{wall to N}, a = \text{move E})\), so the agent cannot tell them apart.
- An optimal deterministic policy must move the same way in both grey cells (both W or both E), so from one side it never reaches the money. Value-based RL learns a near-deterministic (greedy or \(\varepsilon\)-greedy) policy and wanders the corridor for a long time.
- The optimal stochastic policy moves E or W with probability \(0.5\) each in grey cells and reaches the goal in a few steps with high probability. Policy-based RL can represent and learn it.

10.2 Policy Objectives and Gradient Ascent
Definition 10.1 (Policy Objective Functions) The quality of a policy \(\pi_\theta\) is measured by one of:
- Start value (episodic environments): \(J_1(\theta) = V^{\pi_\theta}(s_1) = \mathbb E_{\pi_\theta}[v_1]\).
- Average value (continuing environments): \(J_{avV}(\theta) = \sum_s d^{\pi_\theta}(s)\, V^{\pi_\theta}(s)\).
- Average reward per time step: \(J_{avR}(\theta) = \sum_s d^{\pi_\theta}(s) \sum_a \pi_\theta(s,a)\, \mathcal R_s^a\).
Here \(d^{\pi_\theta}\) is the stationary distribution of the Markov chain induced by \(\pi_\theta\).
Policy-based RL is an optimization problem: find \(\theta\) maximizing \(J(\theta)\).
Gradient-free methods: Hill climbing, simplex (Nelder–Mead), genetic algorithms.
Gradient-based methods (usually more efficient): Gradient ascent, conjugate gradient, quasi-Newton; the methods below also exploit the sequential structure of the problem.
Policy gradient ascent searches for a local maximum of \(J\) by
\[ \Delta\theta = \alpha\, \nabla_\theta J(\theta), \qquad \nabla_\theta J(\theta) = \Bigl(\frac{\partial J(\theta)}{\partial\theta_1}, \dots, \frac{\partial J(\theta)}{\partial\theta_n}\Bigr)^{\!\top}, \]
repeated until convergence, possibly with random restarts against bad local maxima. The questions are how to represent \(\pi_\theta\) and how to compute the gradient.
10.2.1 Finite-Difference Policy Gradient
Estimate each partial derivative by perturbing \(\theta\) by a small \(\epsilon\) along the unit vector \(u_k\):
\[ \frac{\partial J(\theta)}{\partial\theta_k} \approx \frac{J(\theta + \epsilon u_k) - J(\theta)}{\epsilon}, \qquad k = 1, \dots, n, \]
which costs \(n\) policy evaluations per gradient.
AIBO walking (Kohl & Stone, ICRA 2004): A gait controlled by \(12\) numbers (elliptical foot loci), tuned by finite-difference gradients with field traversal time as the objective, learned a fast walk for RoboCup.
Pros: Simple, sometimes effective, and works for arbitrary policies, even non-differentiable ones.
Cons: Noisy and inefficient, fails in high-dimensional parameter spaces, and so limits the policy’s representational capacity.
10.3 The Policy Gradient Theorem
10.3.1 Score Functions
Assume \(\pi_\theta\) is differentiable wherever it is nonzero. The likelihood-ratio trick rewrites its gradient as
\[ \nabla_\theta \pi_\theta(s,a) = \pi_\theta(s,a)\,\frac{\nabla_\theta \pi_\theta(s,a)}{\pi_\theta(s,a)} = \pi_\theta(s,a)\, \nabla_\theta \log \pi_\theta(s,a), \]
so gradients of expectations under \(\pi_\theta\) become expectations that can be sampled.
Definition 10.2 (Score Function) The score function of a policy \(\pi_\theta\) is \(\nabla_\theta \log \pi_\theta(s,a)\). For the two standard parameterizations:
Softmax policy (discrete actions), with weights linear in features: \(\pi_\theta(s,a) \propto e^{\phi(s,a)^\top\theta}\) and
\[ \nabla_\theta \log \pi_\theta(s,a) = \phi(s,a) - \mathbb E_{\pi_\theta}[\phi(s,\cdot)]. \]
Gaussian policy (continuous actions), with mean \(\mu(s) = \phi(s)^\top\theta\) and fixed (or also parameterized) variance \(\sigma^2\), \(a \sim \mathcal N(\mu(s), \sigma^2)\) and
\[ \nabla_\theta \log \pi_\theta(s,a) = \frac{\bigl(a - \mu(s)\bigr)\,\phi(s)}{\sigma^2}. \]
One-step MDPs: Start in \(s \sim d(s)\), take one action, and terminate with reward \(r = \mathcal R_{s,a}\). Then
\[ \nabla_\theta J(\theta) = \nabla_\theta \sum_{s} d(s) \sum_{a} \pi_\theta(s,a)\, \mathcal R_{s,a} = \sum_{s} d(s) \sum_{a} \pi_\theta(s,a)\, \nabla_\theta \log \pi_\theta(s,a)\, \mathcal R_{s,a} = \mathbb E_{\pi_\theta}\bigl[\nabla_\theta \log \pi_\theta(s,a)\, r\bigr]. \]
The policy gradient theorem extends this to multi-step MDPs by replacing the instantaneous reward \(r\) with the long-term value \(Q^{\pi_\theta}(s,a)\).
Theorem 10.1 (Policy Gradient Theorem) For any differentiable policy \(\pi_\theta(s,a)\) and any of the objectives \(J = J_1\), \(J_{avR}\), or \(\frac{1}{1-\gamma} J_{avV}\),
\[ \nabla_\theta J(\theta) = \mathbb E_{\pi_\theta}\bigl[\nabla_\theta \log \pi_\theta(s,a)\; Q^{\pi_\theta}(s,a)\bigr]. \]
10.3.2 REINFORCE
Monte-Carlo policy gradient (REINFORCE) performs stochastic gradient ascent using the return \(v_t\) as an unbiased sample of \(Q^{\pi_\theta}(s_t, a_t)\):
\[ \Delta\theta_t = \alpha\, \nabla_\theta \log \pi_\theta(s_t, a_t)\, v_t. \]
def reinforce(alpha):
theta = initialize()
for episode in episodes(pi_theta): # s_1, a_1, r_2, ..., s_T ~ pi_theta
for t in range(1, T):
v_t = return_from(t, episode) # sum of rewards after step t
theta += alpha * grad_log_pi(theta, s[t], a[t]) * v_t
return theta- Short corridor with switched actions: In a corridor of three cells before the goal, with reward \(-1\) per step, the second cell reverses the effect of left and right, and all cells have the same features. There the best policy chooses “right” with probability about \(0.59\) (\(J = -11.6\)). Both \(\varepsilon\)-greedy policies, the best that action-value methods can reach, are far worse.
- REINFORCE with a suitable step size (\(\alpha = 2^{-13}\)) approaches \(v_*(s_0)\) within about \(1000\) episodes; with \(\alpha = 2^{-12}\) it settles far below, and with \(2^{-14}\) it learns more slowly.

10.4 Actor–Critic Methods
10.4.1 Reducing Variance with a Critic
Monte-Carlo returns make REINFORCE unbiased but high-variance. Actor–critic algorithms keep two sets of parameters:
Critic: Updates action-value parameters \(w\) so that \(Q_w(s,a) \approx Q^{\pi_\theta}(s,a)\). This is policy evaluation, solved with TD learning and function approximation or least-squares policy evaluation.
Actor: Updates policy parameters \(\theta\) in the direction suggested by the critic, following an approximate policy gradient:
\[ \nabla_\theta J(\theta) \approx \mathbb E_{\pi_\theta}\bigl[\nabla_\theta \log \pi_\theta(s,a)\, Q_w(s,a)\bigr], \qquad \Delta\theta = \alpha\, \nabla_\theta \log \pi_\theta(s,a)\, Q_w(s,a). \]
Q actor–critic (QAC) uses a linear critic \(Q_w(s,a) = \phi(s,a)^\top w\) updated by TD(0):
def qac(alpha, beta):
s, theta, w = initial_state(), initialize(), initialize()
a = sample(pi_theta, s)
for step in steps():
r, s_next = env.step(s, a) # r = R(s, a), s_next ~ P(s, a, .)
a_next = sample(pi_theta, s_next)
delta = r + gamma * Q_w(s_next, a_next) - Q_w(s, a)
theta += alpha * grad_log_pi(theta, s, a) * Q_w(s, a) # actor
w += beta * delta * phi(s, a) # critic
s, a = s_next, a_next10.4.2 Baselines and the Advantage Function
Subtracting a baseline \(B(s)\) from \(Q\) reduces variance without changing the gradient.
Proposition 10.1 (Baselines Do Not Bias the Policy Gradient) For any function \(B(s)\) of the state alone,
\[ \mathbb E_{\pi_\theta}\bigl[\nabla_\theta \log \pi_\theta(s,a)\, B(s)\bigr] = \sum_{s} d^{\pi_\theta}(s)\, B(s)\, \nabla_\theta \sum_{a} \pi_\theta(s,a) = \sum_{s} d^{\pi_\theta}(s)\, B(s)\, \nabla_\theta 1 = 0. \]
A good baseline is the state value \(B(s) = V^{\pi_\theta}(s)\), which gives the advantage function \(A^{\pi_\theta}(s,a) = Q^{\pi_\theta}(s,a) - V^{\pi_\theta}(s)\) and
\[ \nabla_\theta J(\theta) = \mathbb E_{\pi_\theta}\bigl[\nabla_\theta \log \pi_\theta(s,a)\; A^{\pi_\theta}(s,a)\bigr]. \]
Estimating the advantage with two critics: Learn \(V_v(s) \approx V^{\pi_\theta}(s)\) and \(Q_w(s,a) \approx Q^{\pi_\theta}(s,a)\), both by TD, and set \(A(s,a) = Q_w(s,a) - V_v(s)\).
Estimating it with one critic: The TD error of \(V^{\pi_\theta}\) is itself an unbiased advantage estimate.
Proposition 10.2 (The TD Error Estimates the Advantage) The TD error \(\delta^{\pi_\theta} = r + \gamma V^{\pi_\theta}(s') - V^{\pi_\theta}(s)\) satisfies
\[ \mathbb E_{\pi_\theta}\bigl[\delta^{\pi_\theta} \mid s, a\bigr] = \mathbb E_{\pi_\theta}\bigl[r + \gamma V^{\pi_\theta}(s') \mid s, a\bigr] - V^{\pi_\theta}(s) = Q^{\pi_\theta}(s,a) - V^{\pi_\theta}(s) = A^{\pi_\theta}(s,a), \]
so \(\nabla_\theta J(\theta) = \mathbb E_{\pi_\theta}\bigl[\nabla_\theta \log \pi_\theta(s,a)\, \delta^{\pi_\theta}\bigr]\).
In practice the approximate TD error \(\delta_v = r + \gamma V_v(s') - V_v(s)\) is used, which needs only one set of critic parameters \(v\). On the short corridor, REINFORCE with a learned baseline (\(\alpha^\theta = 2^{-9}\), \(\alpha^w = 2^{-6}\)) reaches \(v_*(s_0)\) in a few dozen episodes, against several hundred for plain REINFORCE.

10.4.3 Summary of Policy Gradient Algorithms
The policy gradient has many equivalent forms, each giving a stochastic gradient ascent algorithm; the critic uses policy evaluation (Monte-Carlo or TD learning) to estimate \(Q^\pi\), \(A^\pi\), or \(V^\pi\):
| Algorithm | \(\nabla_\theta J(\theta) = \mathbb E_{\pi_\theta}[\nabla_\theta \log \pi_\theta(s,a)\,\cdot\,]\) |
|---|---|
| REINFORCE | return \(v_t\) |
| Q actor–critic | \(Q_w(s,a)\) |
| Advantage actor–critic | \(A_w(s,a)\) |
| TD actor–critic | TD error \(\delta\) |
10.5 Proximal Policy Optimization
10.5.1 Challenges with Policy Gradients
- Data inefficiency: Policy gradients are on-policy, so every new policy needs freshly generated trajectories, and each batch is discarded after one gradient step. Large networks need many updates, so training is slow.
- Unstable updates: The step size is critical. Too large a step gives a bad policy, which collects bad samples, which give a worse policy; unlike supervised learning, later batches cannot correct the error, because the data depend on the policy. Too small a step makes learning slow.
- Advanced methods:
- TRPO (trust region policy optimization) guarantees monotonic improvement.
- PPO (proximal policy optimization) is simple to implement.
- DDPG (deep deterministic policy gradient) is off-policy.
- TD3 (twin delayed DDPG) fixes DDPG’s value overestimation.
10.5.2 Relative Performance of Two Policies
An update step should use rollouts from the most recent policy as efficiently as possible and respect distance in policy space rather than in parameter space. Both follow from comparing the performance of two policies. Let
\[ d^\pi(s) = (1-\gamma) \sum_{t=0}^{\infty} \gamma^t \Pr(s_t = s \mid \pi) \]
be the discounted future state distribution of \(\pi\).
Lemma 10.1 (Performance Difference Lemma) For any policies \(\pi\) and \(\pi'\),
\[ J(\pi') - J(\pi) = \mathbb E_{\tau \sim \pi'}\Bigl[\sum_{t=0}^{\infty} \gamma^t A^\pi(s_t, a_t)\Bigr] = \frac{1}{1-\gamma}\, \mathbb E_{\substack{s \sim d^{\pi'} \\ a \sim \pi'}}\bigl[A^\pi(s,a)\bigr]. \]
Use for policy improvement: \(\max_{\pi'} J(\pi') = \max_{\pi'} \bigl(J(\pi') - J(\pi)\bigr)\) measures the new policy \(\pi'\) by the advantages of the old policy \(\pi\), but still needs trajectories from \(\pi'\).
Importance sampling over actions removes \(a \sim \pi'\):
\[ J(\pi') - J(\pi) = \frac{1}{1-\gamma}\, \mathbb E_{\substack{s \sim d^{\pi'} \\ a \sim \pi}}\Bigl[\frac{\pi'(a \mid s)}{\pi(a \mid s)}\, A^\pi(s,a)\Bigr], \]
leaving only \(s \sim d^{\pi'}\).
Surrogate objective: Replace \(d^{\pi'}\) by \(d^\pi\):
\[ J(\pi') - J(\pi) \approx \mathcal L_\pi(\pi') = \frac{1}{1-\gamma}\, \mathbb E_{\substack{s \sim d^{\pi} \\ a \sim \pi}}\Bigl[\frac{\pi'(a \mid s)}{\pi(a \mid s)}\, A^\pi(s,a)\Bigr] = \mathbb E_{\tau \sim \pi}\Bigl[\sum_{t=0}^{\infty} \gamma^t\, \frac{\pi'(a_t \mid s_t)}{\pi(a_t \mid s_t)}\, A^\pi(s_t, a_t)\Bigr]. \]
\(\mathcal L_\pi(\pi')\) can be optimized with trajectories from the old policy \(\pi\). Unlike trajectory-level importance sampling, each weight depends only on the current time step, so the weights do not vanish or explode.
The approximation is good when the policies are close in KL divergence (Achiam, Held, Tamar & Abbeel, 2017):
\[ \bigl|J(\pi') - \bigl(J(\pi) + \mathcal L_\pi(\pi')\bigr)\bigr| \le C \sqrt{\mathbb E_{s \sim d^\pi}\bigl[D_{\mathrm{KL}}(\pi' \,\|\, \pi)[s]\bigr]}, \qquad D_{\mathrm{KL}}(\pi' \,\|\, \pi)[s] = \sum_{a} \pi'(a \mid s) \log \frac{\pi'(a \mid s)}{\pi(a \mid s)}. \]
10.5.3 PPO with a Clipped Objective
Proximal policy optimization (Schulman, Wolski, Dhariwal, Radford & Klimov, 2017) maximizes the surrogate while approximately enforcing a KL constraint, without computing natural gradients. It has two variants:
- Adaptive KL penalty: Solve the unconstrained problem \(\theta_{k+1} = \arg\max_\theta \mathcal L_{\theta_k}(\theta) - \beta_k\, \bar D_{\mathrm{KL}}(\theta \,\|\, \theta_k)\), changing \(\beta_k\) between iterations to approximately enforce the KL constraint.
- Clipped objective, defined below.
Definition 10.3 (PPO Clipped Objective) Let \(r_t(\theta) = \pi_\theta(a_t \mid s_t) / \pi_{\theta_k}(a_t \mid s_t)\) be the probability ratio and \(\hat A_t^{\pi_k}\) an advantage estimate. The clipped objective is
\[ \mathcal L_{\theta_k}^{\mathrm{CLIP}}(\theta) = \mathbb E_{\tau \sim \pi_k}\Bigl[\sum_{t=0}^{T} \min\Bigl(r_t(\theta)\, \hat A_t^{\pi_k},\; \operatorname{clip}\bigl(r_t(\theta), 1-\epsilon, 1+\epsilon\bigr)\, \hat A_t^{\pi_k}\Bigr)\Bigr], \]
with a hyperparameter \(\epsilon\) (e.g., \(\epsilon = 0.2\)), and the update is \(\theta_{k+1} = \arg\max_\theta \mathcal L_{\theta_k}^{\mathrm{CLIP}}(\theta)\).
- Shape of one term as a function of \(r = r_t(\theta)\):
- For \(A > 0\) it grows like \(rA\) up to \(r = 1+\epsilon\) and is flat beyond, so there is no incentive to make a good action much more likely than before.
- For \(A < 0\) it is flat for \(r < 1-\epsilon\) and falls like \(rA\) above, so there is no incentive to make a bad action much less likely.
- Clipping keeps the new policy close by making the objective as pessimistic as possible about performance far from \(\theta_k\): \(\mathcal L^{\mathrm{CLIP}}\) is a lower bound on the unclipped surrogate \(L^{\mathrm{CPI}} = \hat{\mathbb E}_t[r_t A_t]\) and peaks near \(\theta_k\), while \(L^{\mathrm{CPI}}\) keeps increasing as the KL divergence grows.

def ppo_clip(theta, epsilon, K):
for k in range(num_iterations):
D = collect_partial_trajectories(pi(theta)) # on-policy data from pi_k
A_hat = estimate_advantages(D) # any advantage estimator
theta_k = theta
for epoch in range(K): # K steps of minibatch SGD (Adam)
theta = adam_step(theta, grad_L_clip(theta, theta_k, D, A_hat, epsilon))
return theta- Clipping works at least as well as the KL penalty and is simpler to implement.
- On MuJoCo control tasks (HalfCheetah, Hopper, Walker2d, …), PPO-Clip matches or beats A2C, A2C with a trust region, CEM, vanilla policy gradients, and TRPO on most tasks.
- PPO is widely used, notably to optimize the RLHF objective of InstructGPT and ChatGPT.