Skip to content
ml-recall

Topic notes

Policy Gradient & Actor-Critic

Learning a parameterised policy directly by gradient ascent on the expected return — the log-derivative trick, REINFORCE, reward-to-go and baselines, actor-critic with advantages, GAE and PPO.

3-min read AI-checked

Practise 28 questions Take a test

Key ideas

Formulas

∇θJ(θ)=Eτ∼πθ[∑t∇θlog⁡πθ(at∣st) Φt],θ←θ+α∇θJ(θ)\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\Big[\sum_{t} \nabla_\theta \log \pi_\theta(a_t \mid s_t)\, \Phi_t\Big], \qquad \theta \leftarrow \theta + \alpha \nabla_\theta J(\theta)

Valid weights Φt\Phi_t with the same expectation3: R(τ)R(\tau), the reward-to-go R^t=∑t′≥trt′\hat R_t = \sum_{t' \ge t} r_{t'}, R^t−b(st)\hat R_t - b(s_t), Qπ(st,at)Q^\pi(s_t, a_t), Aπ(st,at)A^\pi(s_t, a_t).

δt=rt+γV(st+1)−V(st),A^tGAE=∑l≥0(γλ)l δt+l\delta_t = r_t + \gamma V(s_{t+1}) - V(s_t), \qquad \hat A_t^{\text{GAE}} = \sum_{l \ge 0} (\gamma\lambda)^l\, \delta_{t+l} LCLIP(θ)=E^t[min⁡(rt(θ)A^t, clip⁡(rt(θ),1−ϵ,1+ϵ)A^t)],rt(θ)=πθ(at∣st)πθold(at∣st)L^{\text{CLIP}}(\theta) = \hat{\mathbb{E}}_t\big[\min\big(r_t(\theta)\hat A_t,\ \operatorname{clip}(r_t(\theta), 1 - \epsilon, 1 + \epsilon)\hat A_t\big)\big], \qquad r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{\text{old}}}(a_t \mid s_t)}

Worked example

Softmax policy with preferences h=(1,0,0)h = (1, 0, 0), so π≈(0.576,0.212,0.212)\pi \approx (0.576, 0.212, 0.212). The agent takes the third action and the episode returns G=3G = 3; with α=0.1\alpha = 0.1:

PPO with ϵ=0.2\epsilon = 0.2: for A^=+2\hat A = +2 and ratio r=1.5r = 1.5, L=min⁡(3.0,1.2⋅2)=2.4L = \min(3.0, 1.2 \cdot 2) = 2.4, clipped, so there is no incentive to push further. For A^=−2\hat A = -2 and r=1.5r = 1.5, L=min⁡(−3.0,−2.4)=−3.0L = \min(-3.0, -2.4) = -3.0: the unclipped, worse value is kept.

In code

import torch
from torch.distributions import Normal
rewards = torch.tensor([1.0, 2.0, 0.0, 3.0])                  # r_0 .. r_3 of one episode
rtg = rewards.flip(0).cumsum(0).flip(0)                       # reward-to-go
mu = torch.zeros(4, 2, requires_grad=True)                    # policy means (stand-in for a net)
log_std = torch.zeros(2, requires_grad=True)                  # state-independent log sigma
acts = torch.tensor([[0.5, -1.0]] * 4)                        # the 2-D actions that were taken
logp = Normal(mu, log_std.exp()).log_prob(acts).sum(-1)       # sum over action dimensions
loss = -(logp * rtg).mean()                                   # minimising -J = gradient ascent on J
loss.backward()
print(rtg.tolist(), logp.shape)       # [6.0, 5.0, 3.0, 3.0] torch.Size([4])
print(mu.grad[0].tolist())            # [-0.75, 1.5]

Common pitfalls

References

  1. Hugging Face Deep Reinforcement Learning Course — Unit 4, What are the policy-based methods? (The difference between policy-based and policy-gradient methods)
  2. Hugging Face Deep Reinforcement Learning Course — Unit 4, The advantages and disadvantages of policy-gradient methods
  3. OpenAI Spinning Up, Part 3: Intro to Policy Optimization — Deriving the Simplest Policy Gradient; Implementing the Simplest Policy Gradient; Expected Grad-Log-Prob Lemma; Don't Let the Past Distract You; Baselines in Policy Gradients; Other Forms of the Policy Gradient
  4. Hugging Face Deep Reinforcement Learning Course — Unit 6, Advantage Actor-Critic (A2C): Reducing variance with Actor-Critic methods; Adding Advantage in Actor-Critic
  5. Schulman et al. (2016), High-Dimensional Continuous Control Using Generalized Advantage Estimation, arXiv:1506.02438 — Sec. 3 Advantage Function Estimation (Eqs. 16–18)
  6. Schulman et al. (2017), Proximal Policy Optimization Algorithms, arXiv:1707.06347 — Abstract; Sec. 2.1 Policy Gradient Methods; Sec. 3 Clipped Surrogate Objective
  7. OpenAI Spinning Up, Part 1: Key Concepts in RL — Policies › Stochastic Policies › Diagonal Gaussian Policies