Key ideas
- Policy-based methods learn the policy directly, usually a network that outputs a probability distribution over actions, and maximise the expected return . Policy-gradient methods are the subset that do this by gradient ascent on ; other policy-based methods optimise indirectly, e.g. by hill climbing, simulated annealing or evolution strategies1. It is ascent because is a return to maximise.
- Pros: they can learn genuinely stochastic policies (useful against perceptual aliasing, where two states look the same but need different actions), they handle high-dimensional and continuous action spaces by outputting a distribution, and action preferences change smoothly. Cons: they often converge to a local rather than a global maximum, learn slowly and have high variance2.
- Log-derivative trick: turns the gradient of an expectation into an expectation that can be estimated from sampled trajectories. In the start-state and transition terms do not depend on , so only remains: no model of the environment is needed3.
- REINFORCE (Monte Carlo policy gradient) weights each by a return from a complete episode: unbiased, but noisy. Two variance reducers keep the expected gradient unchanged3:
- reward-to-go: weight action only by rewards from on, since earlier rewards cannot be its consequences;
- baseline: subtract any that depends on the state only (not on the action), usually a learned value function trained by regression on the returns. This follows from the expected grad-log-prob (EGLP) lemma, .
- The PyTorch "loss"
-(logp * weights).mean()only has the right gradient at the current parameters on fresh data; its value says nothing about performance, so watch the average return instead3. - Actor-critic: the actor is the policy , the critic a learned value function that judges the actions; replacing full Monte Carlo returns by the critic's estimates reduces variance4. A2C uses the advantage , how much better is than the state's average; a positive advantage pushes the policy toward , a negative one away, and the TD error estimates it without a separate 4.
- GAE sums discounted TD errors with a parameter : gives the TD error (low variance, biased unless is exact), the discounted return minus (unbiased, high variance)5.
- On-policy data. The estimate assumes trajectories from the current policy, so vanilla policy gradient takes one step per batch; repeated steps on the same batch are not well-justified and often destroy the policy. PPO (also on-policy) clips the probability ratio, giving a pessimistic lower bound on the unclipped objective, so several epochs of minibatch updates on one batch become workable6.
- Continuous actions: a diagonal Gaussian policy outputs means and often keeps a separate, state-independent parameter vector . It learns rather than because may take any real value while must stay positive; is then positive with no constraint to enforce7.
Formulas
Valid weights with the same expectation3: , the reward-to-go , , , .
- Softmax policy over preferences : .
- Diagonal Gaussian over dimensions: , a sum over dimensions7.
Worked example
Softmax policy with preferences , so . The agent takes the third action and the episode returns ; with :
- ;
- , and rises to while the actions not taken lose preference.
PPO with : for and ratio , , clipped, so there is no incentive to push further. For and , : 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
- Policy-gradient methods are a subset of policy-based methods, not the other way round1.
- Policy gradients usually find a local maximum; global convergence is not guaranteed2.
- A baseline that depends only on the state keeps the gradient unbiased whatever it is (it need not equal ), and reward-to-go likewise only removes zero-mean noise; a baseline that depends on the action does bias it3.
- A decreasing policy-gradient "loss" does not mean the return is improving3.
Normal(...).log_prob(act)returns one value per action dimension; sum them (not average) to get 7.- The critic is a learned value function trained on observed returns or TD targets; it is not the environment's reward function4.
- GAE's extremes are easy to swap: is the one-step TD error, the Monte Carlo advantage5.
- PPO is still on-policy: it reuses only the latest batch, for a few epochs, inside the clipping range6.
References
- Hugging Face Deep Reinforcement Learning Course — Unit 4, What are the policy-based methods? (The difference between policy-based and policy-gradient methods)
- Hugging Face Deep Reinforcement Learning Course — Unit 4, The advantages and disadvantages of policy-gradient methods
- 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
- Hugging Face Deep Reinforcement Learning Course — Unit 6, Advantage Actor-Critic (A2C): Reducing variance with Actor-Critic methods; Adding Advantage in Actor-Critic
- Schulman et al. (2016), High-Dimensional Continuous Control Using Generalized Advantage Estimation, arXiv:1506.02438 — Sec. 3 Advantage Function Estimation (Eqs. 16–18)
- Schulman et al. (2017), Proximal Policy Optimization Algorithms, arXiv:1707.06347 — Abstract; Sec. 2.1 Policy Gradient Methods; Sec. 3 Clipped Surrogate Objective
- OpenAI Spinning Up, Part 1: Key Concepts in RL — Policies › Stochastic Policies › Diagonal Gaussian Policies