Policy gradient method explained

\pi

that selects actions without consulting a value function. For policy gradient to apply, the policy function

\pi\theta

is parameterized by a differentiable parameter

\theta

.

Overview

In policy-based RL, the actor is a parameterized policy function

\pi\theta

, where

\theta

are the parameters of the actor. The actor takes as argument the state of the environment

s

and produces a probability distribution

\pi\theta( ⋅ \mids)

.

If the action space is discrete, then

\suma\pi\theta(a\mids)=1

. If the action space is continuous, then

\inta\pi\theta(a\mids)da=1

.

The goal of policy optimization is to find some

\theta

that maximizes the expected episodic reward

J(\theta)

:J(\theta) = \mathbb_\left[\sum_{t\in 0:T} \gamma^t R_t \Big| S_0 = s_0 \right]where

\gamma

is the discount factor,

Rt

is the reward at step

t

,

s0

is the starting state, and

T

is the time-horizon (which can be infinite).

The policy gradient is defined as

\nabla\thetaJ(\theta)

. Different policy gradient methods stochastically estimate the policy gradient in different ways. The goal of any policy gradient method is to iteratively maximize

J(\theta)

by gradient ascent. Since the key part of any policy gradient method is the stochastic estimation of the policy gradient, they are also studied under the title of "Monte Carlo gradient estimation".[1]

REINFORCE

Policy gradient

The REINFORCE algorithm, introduced by Ronald J. Williams in 1992, was the first policy gradient method.[2] It is based on the identity for the policy gradient\nabla_\theta J(\theta)= \mathbb_\left[\sum_{t\in 0:T} \nabla_\theta\ln\pi_\theta(A_t \mid S_t)\; \sum_{t \in 0:T} (\gamma^t R_t) \Big|S_0 = s_0 \right] which can be improved via the "causality trick"[3] \nabla_\theta J(\theta)= \mathbb_\left[\sum_{t\in 0:T} \nabla_\theta\ln\pi_\theta(A_t\mid S_t)\sum_{\tau \in t:T} (\gamma^\tau R_\tau) \Big|S_0 = s_0 \right]

Thus, we have an unbiased estimator of the policy gradient:\nabla_\theta J(\theta) \approx \frac 1N \sum_^N \left[\sum_{t\in 0:T} \nabla_\theta\ln\pi_\theta(A_{t,n}\mid S_{t,n})\sum_{\tau \in t:T} (\gamma^{\tau-t} R_{\tau,n}) \right] where the index

n

ranges over

N

rollout trajectories using the policy

\pi\theta

.

\nabla\thetaln\pi\theta(At\midSt)

can be interpreted as the direction in the parameter space that increases the probability of taking action

At

in state

St

. The policy gradient, then, is a weighted average of all possible directions to increase the probability of taking any action in any state, but weighted by reward signals, so that if taking a certain action in a certain state is associated with high reward, then that direction would be highly reinforced, and vice versa.

Algorithm

The REINFORCE algorithm is a loop:

  1. Rollout

N

trajectories in the environment, using
\pi
\thetat
as the policy function.
  1. Compute the policy gradient estimation:

gi\leftarrow

1N
\sum
N
n=1

\left[\sumt\in

\nabla
\thetat

ln\pi\theta(At,n\midSt,n)\sum\tau(\gamma\tauR\tau,n)\right]

  1. Update the policy by gradient ascent:

\thetai+1\leftarrow\thetai+\alphaigi

Here,

\alphai

is the learning rate at update step

i

.

Variance reduction

REINFORCE is an on-policy algorithm, meaning that the trajectories used for the update must be sampled from the current policy

\pi\theta

. This can lead to high variance in the updates, as the returns

R(\tau)

can vary significantly between trajectories. Many variants of REINFORCE have been introduced, under the title of variance reduction.

REINFORCE with baseline

A common way for reducing variance is the REINFORCE with baseline algorithm, based on the following identity:\nabla_\theta J(\theta)= \mathbb_\left[\sum_{t\in 0:T} \nabla_\theta\ln\pi_\theta(A_t| S_t)\left(\sum_{\tau \in t:T} (\gamma^\tau R_\tau) - b(S_t)\right) \Big|S_0 = s_0 \right]for any function

b:States\to\R

. This can be proven by applying the previous lemma.

The algorithm uses the modified gradient estimatorg_i \leftarrow \frac 1N \sum_^N \left[\sum_{t\in 0:T} \nabla_{\theta_t}\ln\pi_\theta(A_{t,n}| S_{t,n})\left(\sum_{\tau \in t:T} (\gamma^\tau R_{\tau,n}) - b_i(S_{t,n})\right) \right] and the original REINFORCE algorithm is the special case where

bi\equiv0

.

Actor-critic methods

See main article: Actor-critic algorithm. If b_i is chosen well, such that b_i(S_t) \approx \sum_ (\gamma^\tau R_\tau) = \gamma^t V^(S_t), this could significantly decrease variance in the gradient estimation. That is, the baseline should be as close to the value function

\pi
\thetai
V

(St)

as possible, approaching the ideal of:\nabla_\theta J(\theta)= \mathbb_\left[\sum_{t\in 0:T} \nabla_\theta\ln\pi_\theta(A_t| S_t)\left(\sum_{\tau \in t:T} (\gamma^\tau R_\tau) - \gamma^t V^{\pi_\theta}(S_t)\right) \Big|S_0 = s_0 \right]Note that, as the policy
\pi
\thetat
updates, the value function
\pi
\thetai
V

(St)

updates as well, so the baseline should also be updated. One common approach is to train a separate function that estimates the value function, and use that as the baseline. This is one of the actor-critic methods, where the policy function is the actor and the value function is the critic.

The Q-function

Q\pi

can also be used as the critic, since\nabla_\theta J(\theta)= E_\left[\sum_{0\leq t \leq T} \gamma^t \nabla_\theta\ln\pi_\theta(A_t| S_t) \cdot Q^{\pi_\theta}(S_t, A_t) \Big|S_0 = s_0 \right] by a similar argument using the tower law.

Subtracting the value function as a baseline, we find that the advantage function

A\pi(S,A)=Q\pi(S,A)-V\pi(S)

can be used as the critic as well:\nabla_\theta J(\theta)= E_\left[\sum_{0\leq t \leq T} \gamma^t \nabla_\theta\ln\pi_\theta(A_t| S_t) \cdot A^{\pi_\theta}(S_t, A_t) \Big|S_0 = s_0 \right]In summary, there are many unbiased estimators for \nabla_\theta J_\theta, all in the form of: \nabla_\theta J(\theta) = E_\left[\sum_{0\leq t \leq T} \nabla_\theta\ln\pi_\theta(A_t| S_t) \cdot \Psi_t \Big|S_0 = s_0 \right] where \Psi_t is any linear sum of the following terms:

Some more possible \Psi_t are as follows, with very similar proofs.

Natural policy gradient

The natural policy gradient method is a variant of the policy gradient method, proposed by Sham Kakade in 2001.[5] Unlike standard policy gradient methods, which depend on the choice of parameters

\theta

(making updates coordinate-dependent), the natural policy gradient aims to provide a coordinate-free update, which is geometrically "natural".

Motivation

Standard policy gradient updates

\thetai+1=\thetai+\alpha\nabla\thetaJ(\thetai)

solve a constrained optimization problem:\begin\max_ J(\theta_i) + (\theta_ - \theta_i)^T \nabla_\theta J(\theta_i)\\\|\theta_ - \theta_\|\leq \alpha \cdot \|\nabla_\theta J(\theta_i)\|\endWhile the objective (linearized improvement) is geometrically meaningful, the Euclidean constraint

\|\thetai+1-\thetai\|

introduces coordinate dependence. To address this, the natural policy gradient replaces the Euclidean constraint with a Kullback–Leibler divergence (KL) constraint:\begin\max_ J(\theta_i) + (\theta_ - \theta_i)^T \nabla_\theta J(\theta_i)\\\bar_(\pi_ \| \pi_) \leq \epsilon\endwhere the KL divergence between two policies is averaged over the state distribution under policy
\pi
\thetai
. That is,\bar_(\pi_ \| \pi_) := \mathbb E_[D_{KL}(\pi_{\theta_{i+1}}(\cdot | s) \| \pi_{\theta_{i}}(\cdot | s))] This ensures updates are invariant to invertible affine parameter transformations.

Fisher information approximation

For small

\epsilon

, the KL divergence is approximated by the Fisher information metric:\bar_(\pi_ \| \pi_) \approx \frac (\theta_ - \theta_i)^T F(\theta_i) (\theta_ - \theta_i) where

F(\theta)

is the Fisher information matrix of the policy, defined as:F(\theta) = \mathbb_\left[\nabla_\theta \ln \pi_\theta(a|s) \left(\nabla_\theta \ln \pi_\theta(a|s)\right)^T \right] This transforms the problem into a problem in quadratic programming, yielding the natural policy gradient update:\theta_ = \theta_i + \alpha F(\theta_i)^ \nabla_\theta J(\theta_i)The step size

\alpha

is typically adjusted to maintain the KL constraint, with \alpha \approx \sqrt.

Inverting

F(\theta)

is computationally intensive, especially for high-dimensional parameters (e.g., neural networks). Practical implementations often use approximations.

Trust Region Policy Optimization (TRPO)

Trust Region Policy Optimization (TRPO) is a policy gradient method that extends the natural policy gradient approach by enforcing a trust region constraint on policy updates.[6] Developed by Schulman et al. in 2015, TRPO improves upon the natural policy gradient method.

The natural gradient descent is theoretically optimal, if the objective is truly a quadratic function, but this is only an approximation. TRPO's line search and KL constraint attempts to restrict the solution to within a "trust region" in which this approximation does not break down. This makes TRPO more robust in practice.

Formulation

Like natural policy gradient, TRPO iteratively updates the policy parameters

\theta

by solving a constrained optimization problem specified coordinate-free:\begin\max_ L(\theta, \theta_i)\\\bar_(\pi_ \| \pi_) \leq \epsilon \end where

L(\theta,\thetai)=

E
s,a\sim
\pi
\thetai

\left[

\pi\theta(a|s)
\pi(a|s)
\thetai
\pi
\thetai
A

(s,a)\right]

is the surrogate advantage, measuring the performance of

\pi\theta

relative to the old policy
\pi
\thetai
.

\epsilon

is the trust region radius.Note that in general, other surrogate advantages are possible:L(\theta, \theta_i) = \mathbb_\left[\frac{\pi_\theta(a|s)}{\pi_{\theta_i}(a|s)}\Psi^{\pi_{\theta_i}}(s, a) \right]where

\Psi

is any linear sum of the previously mentioned type. Indeed, OpenAI recommended using the Generalized Advantage Estimate, instead of the plain advantage
\pi\theta
A
.

The surrogate advantage

L(\theta,\thetat)

is designed to align with the policy gradient

\nabla\thetaJ(\theta)

. Specifically, when

\theta=\thetat

,

\nabla\thetaL(\theta,\thetat)

equals the policy gradient derived from the advantage function:\nabla_\theta J(\theta) = \mathbb_\left[\nabla_\theta \ln \pi_\theta(a | s) \cdot A^{\pi_\theta}(s, a) \right] = \nabla_\theta L(\theta, \theta_t)However, when

\theta ≠ \thetai

, this is not necessarily true. Thus it is a "surrogate" of the real objective.

As with natural policy gradient, for small policy updates, TRPO approximates the surrogate advantage and KL divergence using Taylor expansions around

\thetat

: \begin L(\theta, \theta_i) &\approx g^T (\theta - \theta_i), \\ \bar_(\pi_ \| \pi_) &\approx \frac (\theta - \theta_i)^T H (\theta - \theta_i), \end where:

g=\nabla\thetaL(\theta,\thetai)

|
\theta=\thetai
is the policy gradient.

F=

2
\nabla
\theta

\bar{D}KL(\pi\theta\|

\pi
\thetai

)

|
\theta=\thetai
is the Fisher information matrix.

This reduces the problem to a quadratic optimization, yielding the natural policy gradient update: \theta_ = \theta_i + \sqrt F^ g. So far, this is essentially the same as natural gradient method. However, TRPO improves upon it by two modifications:

x

in

Fx=g

iteratively without explicit matrix inversion.

\theta_ = \theta_i + \sqrt x, \; \theta_i + \alpha \sqrt x, \; \theta_i + \alpha^2 \sqrt x, \; \dots until it finds one that both satisfies the KL constraint

\bar{D}KL

(\pi
\thetai+1

\|

\pi
\thetai

)\leq\epsilon

and results in a higher

L(\thetai+1,\thetai)\geqL(\thetai,\thetai)

. Here,

\alpha\in(0,1)

is the backtracking coefficient.

Proximal Policy Optimization (PPO)

A further improvement is proximal policy optimization (PPO), which avoids even computing

F(\theta)

and

F(\theta)-1

via a first-order approximation using clipped probability ratios.[7]

Specifically, instead of maximizing the surrogate advantage\max_\theta L(\theta, \theta_t) = \mathbb_\left[\frac{\pi_\theta(a|s)}{\pi_{\theta_t}(a|s)} A^{\pi_{\theta_t}}(s, a) \right] under a KL divergence constraint, it directly inserts the constraint into the surrogate advantage:\max_\theta \mathbb_\left[\begin{cases} \min \left(\frac{\pi_\theta(a|s)}{\pi_{\theta_t}(a|s)}, 1 + \epsilon \right) A^{\pi_{\theta_t}}(s, a) & \text{ if } A^{\pi_{\theta_t}}(s, a) > 0 \\ \max \left(\frac{\pi_\theta(a|s)}{\pi_{\theta_t}(a|s)}, 1 - \epsilon \right) A^{\pi_{\theta_t}}(s, a) & \text{ if } A^{\pi_{\theta_t}}(s, a) < 0 \end{cases} \right] and PPO maximizes the surrogate advantage by stochastic gradient descent, as usual.

In words, gradient-ascending the new surrogate advantage function means that, at some state

s,a

, if the advantage is positive:
\pi
\thetat
A

(s,a)>0

, then the gradient should direct

\theta

towards the direction that increases the probability of performing action

a

under the state

s

. However, as soon as

\theta

has changed so much that

\pi\theta(a|s)\geq(1+\epsilon)

\pi
\thetat

(a|s)

, then the gradient should stop pointing it in that direction. And similarly if
\pi
\thetat
A

(s,a)<0

. Thus, PPO avoids pushing the parameter update too hard, and avoids changing the policy too much.

To be more precise, to update

\thetat

to

\thetat+1

requires multiple update steps on the same batch of data. It would initialize

\theta=\thetat

, then repeatedly apply gradient descent (such as the Adam optimizer) to update

\theta

until the surrogate advantage has stabilized. It would then assign

\thetat+1

to

\theta

, and do it again.

During this inner-loop, the first update to

\theta

would not hit the

1-\epsilon,1+\epsilon

bounds, but as

\theta

is updated further and further away from

\thetat

, it eventually starts hitting the bounds. For each such bound hit, the corresponding gradient becomes zero, and thus PPO avoid updating

\theta

too far away from

\thetat

.

This is important, because the surrogate loss assumes that the state-action pair

s,a

is sampled from what the agent would see if the agent runs the policy
\pi
\thetat

, but policy gradient should be on-policy. So, as

\theta

changes, the surrogate loss becomes more and more off-policy. This is why keeping

\theta

proximal to

\thetat

is necessary.

If there is a reference policy

\piref

that the trained policy should not diverge too far from, then additional KL divergence penalty can be added:-\beta \mathbb_\left[\log\left(\frac{\pi_{\theta}(a|s)}{\pi_{\text{ref}}(a|s)}\right) \right]where

\beta

adjusts the strength of the penalty. This has been used in training reasoning language models with reinforcement learning from human feedback.[8] The KL divergence penalty term can be estimated with lower variance using the equivalent form (see f-divergence for details):-\beta \mathbb_\left[\log\left(\frac{\pi_{\theta}(a|s)}{\pi_{\text{ref}}(a|s)}\right) + \frac{\pi_{\text{ref}}(a|s)} {\pi_{\theta}(a|s)} -1 \right]

Group Relative Policy Optimization (GRPO)

The Group Relative Policy Optimization (GRPO) is a minor variant of PPO that omits the value function estimator

V

. Instead, for each state

s

, it samples multiple actions

a1,...,aG

from the policy
\pi
\thetat

, then calculate the group-relative advantage[9] A^(s, a_) = \frac where

\mu,\sigma

are the mean and standard deviation of

r(s,a1),...,r(s,aG)

. That is, it is the standard score of the rewards.

Then, it maximizes the PPO objective, averaged over all actions:\max_\theta \frac \sum_^G \mathbb_\left[\begin{cases} \min \left(\frac{\pi_\theta(a_i|s)}{\pi_{\theta_t}(a_i|s)}, 1 + \epsilon \right) A^{\pi_{\theta_t}}(s, a_i) & \text{ if } A^{\pi_{\theta_t}}(s, a_i) > 0 \\ \max \left(\frac{\pi_\theta(a_i|s)}{\pi_{\theta_t}(a_i|s)}, 1 - \epsilon \right) A^{\pi_{\theta_t}}(s, a_i) & \text{ if } A^{\pi_{\theta_t}}(s, a_i) < 0 \end{cases} \right]Intuitively, each policy update step in GRPO makes the policy more likely to respond to each state with an action that performed relatively better than other actions tried at that state, and less likely to respond with one that performed relatively worse.

As before, the KL penalty term can be applied to encourage the trained policy to stay close to a reference policy. GRPO was first proposed in the context of training reasoning language models by researchers at DeepSeek.

Policy Optimization and the Mirror Descent perspective (MDPO)

Methods like TRPO, PPO and natural policy gradient share a common idea - while the policy should be updated in the direction of the policy gradient, the update should be done in a safe and stable manner, typically measured by some distance with respect to the policy before the update.

A similar notion of update stability is found in proximal convex optimization techniques like Mirror Descent.[10] There, \mathbf , the proposed minimizer of f in some constraint set \mathcal , is iteratively updated in the direction of the gradient \nabla f , with a proximity penalty with respect to the current \mathbf_t measured by some Bregman divergence B_\omega , which can formalized by the following formula:\mathbf_\in\arg \min_ \nabla f(\mathbf_t)^T (\mathbf - \mathbf_t) + \frac B_\omega(x,x_t), where \eta_t controls the proximity between consecutive iterates, similar to the learning rate in gradient descent.

This leads to reconsidering the policy update procedure as an optimization procedure aimed at finding an optimal policy, in the (non-convex) optimization landscape of the underlying Markov decision process (MDP). This optimization viewpoint of using the policy gradient is termed Mirror Descent Policy Optimization (MDPO),[11] [12] leading to the following update when the KL is the chosen Bregman divergence:\pi_\in \arg \max_ \mathbb_ \left[A^{\pi_{t}}(s, a) \right] + \frac D_(\pi ||\pi_t)With a parameterized policy \pi_\theta , the MDPO loss becomes:\max_ L(\theta, \theta_t) = \mathbb_\left[\frac{\pi_\theta(a|s)}{\pi_{\theta_t}(a|s)} A^{\pi_{\theta_t}}(s, a) \right] + \frac D_(\pi_\theta ||\pi_)This objective can be used together with other common techniques like the clipping done in PPO. In fact, the KL divergence penalty also appears in the original PPO paper, suggesting the MDPO perspective as a theoretical unification of the main derivation concepts behind many concurrent policy gradient techniques.

See also

References

External links

Notes and References

  1. Mohamed . Shakir . Rosca . Mihaela . Figurnov . Michael . Mnih . Andriy . 2020 . Monte Carlo Gradient Estimation in Machine Learning . Journal of Machine Learning Research . 21 . 132 . 1–62 . 1906.10652 . 1533-7928.
  2. Williams . Ronald J. . May 1992 . Simple statistical gradient-following algorithms for connectionist reinforcement learning . Machine Learning . en . 8 . 3–4 . 229–256 . 10.1007/BF00992696 . 0885-6125.
  3. Sutton . Richard S . McAllester . David . Singh . Satinder . Mansour . Yishay . 1999 . Policy Gradient Methods for Reinforcement Learning with Function Approximation . Advances in Neural Information Processing Systems . MIT Press . 12.
  4. Schulman . John . High-Dimensional Continuous Control Using Generalized Advantage Estimation . 2018-10-20 . 1506.02438 . Moritz . Philipp . Sergey Levine . Levine . Sergey . Jordan . Michael . Abbeel . Pieter . cs.LG .
  5. Kakade . Sham M . 2001 . A Natural Policy Gradient . Advances in Neural Information Processing Systems . MIT Press . 14.
  6. Schulman . John . Levine . Sergey . Moritz . Philipp . Jordan . Michael . Abbeel . Pieter . 2015-07-06 . Trust region policy optimization . Proceedings of the 32nd International Conference on International Conference on Machine Learning . 37 . Lille, France . JMLR.org . 1889–1897.
  7. Schulman . John . Proximal Policy Optimization Algorithms . 2017-08-28 . 1707.06347 . Wolski . Filip . Dhariwal . Prafulla . Radford . Alec . Klimov . Oleg . cs.LG .
  8. Nisan Stiennon . Long Ouyang . Jeffrey Wu . Daniel Ziegler . Ryan Lowe . Chelsea Voss . Alec Radford . Dario Amodei . Paul F. Christiano . 2020 . Learning to summarize with human feedback . Advances in Neural Information Processing Systems . en . 33.
  9. Shao . Zhihong . DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models . 2024-04-27 . 2402.03300 . Wang . Peiyi . Zhu . Qihao . Xu . Runxin . Song . Junxiao . Bi . Xiao . Zhang . Haowei . Zhang . Mingchuan . Li . Y. K. . cs.CL .
  10. Arkadi Nemirovsky and David Yudin. Problem Complexity and Method Efficiency in Optimization. John Wiley & Sons, 1983.
  11. Shani . Lior . Efroni . Yonathan . Mannor . Shie . 2020-04-03 . Adaptive Trust Region Policy Optimization: Global Convergence and Faster Rates for Regularized MDPS . Proceedings of the AAAI Conference on Artificial Intelligence . 34 . 4 . 5668–5675 . 10.1609/aaai.v34i04.6021 . 2374-3468. 1909.02769 .
  12. Tomar . Manan . Shani . Lior . Efroni . Yonathan . Ghavamzadeh . Mohammad . 2020-05-20 . Mirror Descent Policy Optimization . cs.LG . 2005.09814v5 . en.