Policy gradient method

From testwiki
Revision as of 19:58, 28 February 2025 by imported>Atcold (Overview: Partial notation fix)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:Short description

Policy gradient methods are a class of reinforcement learning algorithms.

Policy gradient methods are a sub-class of policy optimization methods. Unlike value-based methods which learn a value function to derive a policy, policy optimization methods directly learn a policy function π that selects actions without consulting a value function. For policy gradient to apply, the policy function πθ is parameterized by a differentiable parameter θ.

Overview

In policy-based RL, the actor is a parameterized policy function πθ, where θ are the parameters of the actor. The actor takes as argument the state of the environment s and produces a probability distribution πθ(s).

If the action space is discrete, then aπθ(as)=1. If the action space is continuous, then aπθ(as)da=1.

The goal of policy optimization is to find some θ that maximizes the expected episodic reward J(θ):J(θ)=𝔼πθ[t0:TγtRt|S0=s0]where γ 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 θJ(θ). Different policy gradient methods stochastically estimate the policy gradient in different ways. The goal of any policy gradient method is to iteratively maximize J(θ) 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

Template:Anchor

Policy gradient

The REINFORCE algorithm was the first policy gradient method.[2] It is based on the identity for the policy gradientθJ(θ)=𝔼πθ[t0:Tθlnπθ(AtSt)t0:T(γtRt)|S0=s0]which can be improved via the "causality trick"[3]θJ(θ)=𝔼πθ[t0:Tθlnπθ(AtSt)τt:T(γτRτ)|S0=s0] Template:Math theorem

Template:Hidden begin

Template:Math proofTemplate:Hidden end

Template:Hidden begin

Template:Math proof Template:Hidden endThus, we have an unbiased estimator of the policy gradient:θJ(θ)1Nn=1N[t0:Tθlnπθ(At,nSt,n)τt:T(γτRτ,n)]where the index n ranges over N rollout trajectories using the policy πθ.

The score function θlnπθ(AtSt) 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 πθt as the policy function.
  2. Compute the policy gradient estimation: gt1Nn=1N[t0:Tθtlnπθ(At,nSt,n)τt:T(γτRτ,n)]
  3. Update the policy by gradient ascent: θt+1θt+αtgt

Here, αt is the learning rate at update step t.

Variance reduction

REINFORCE is an on-policy algorithm, meaning that the trajectories used for the update must be sampled from the current policy πθ. This can lead to high variance in the updates, as the returns R(τ) can vary significantly between trajectories. Many variants of REINFORCE has 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:θJ(θ)=𝔼πθ[j0:Tθlnπθ(Aj|Sj)(ij:T(γiRi)b(Sj))|S0=s0]for any function b:States. This can be proven by applying the previous lemma.

The algorithm uses the modified gradient estimatorgt1Nk=1N[j0:Tθtlnπθ(Aj,k|Si,k)(ij:T(γiRi,k)bt(Sj,k))]and the original REINFORCE algorithm is the special case where bt=0.

Actor-critic methods

Template:Main If bt is chosen well, such that bt(Sj)ij:T(γiRi)=γjVπθt(Sj), this could significantly decrease variance in the gradient estimation. That is, the baseline should be as close to the value function Vπθt(Sj) as possible, approaching the ideal of:θJ(θ)=𝔼πθ[j0:Tθlnπθ(Aj|Sj)(ij:T(γiRi)γjVπθ(Sj))|S0=s0]Note that, as the policy πθt updates, the value function Vπθt(Sj) 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π can also be used as the critic, sinceθJ(θ)=Eπθ[0jTγjθlnπθ(Aj|Sj)Qπθ(Sj,Aj)|S0=s0]by a similar argument using the tower law.

Subtracting the value function as a baseline, we find that the advantage function Aπ(S,A)=Qπ(S,A)Vπ(S) can be used as the critic as well:θJ(θ)=Eπθ[0jTγjθlnπθ(Aj|Sj)Aπθ(Sj,Aj)|S0=s0]In summary, there are many unbiased estimators for θJθ, all in the form of: θJ(θ)=Eπθ[0jTθlnπθ(Aj|Sj)Ψj|S0=s0] where Ψj is any linear sum of the following terms:

  • 0iT(γiRi): never used.
  • γjjiT(γijRi): used by the REINFORCE algorithm.
  • γjjiT(γijRi)b(Sj): used by the REINFORCE with baseline algorithm.
  • γj(Rj+γVπθ(Sj+1)Vπθ(Sj)): 1-step TD learning.
  • γjQπθ(Sj,Aj).
  • γjAπθ(Sj,Aj).

Some more possible Ψj are as follows, with very similar proofs.

  • γj(Rj+γRj+1+γ2Vπθ(Sj+2)Vπθ(Sj)): 2-step TD learning.
  • γj(k=0n1γkRj+k+γnVπθ(Sj+n)Vπθ(Sj)): n-step TD learning.
  • γjn=1λn11λ(k=0n1γkRj+k+γnVπθ(Sj+n)Vπθ(Sj)): TD(λ) learning, also known as GAE (generalized advantage estimate).[4] This is obtained by an exponentially decaying sum of the n-step TD learning ones.

Natural policy gradient

Template:Anchor

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 θ (making updates coordinate-dependent), the natural policy gradient aims to provide a coordinate-free update, which is geometrically "natural".

Motivation

Standard policy gradient updates θt+1=θt+αθJ(θt) solve a constrained optimization problem:{maxθt+1J(θt)+(θt+1θt)TθJ(θt)θt+1θtαθJ(θt) While the objective (linearized improvement) is geometrically meaningful, the Euclidean constraint θt+1θt introduces coordinate dependence. To address this, the natural policy gradient replaces the Euclidean constraint with a Kullback–Leibler divergence (KL) constraint:{maxθt+1J(θt)+(θt+1θt)TθJ(θt)D¯KL(πθt+1πθt)ϵwhere the KL divergence between two policies is averaged over the state distribution under policy πθt. That is,D¯KL(πθt+1πθt):=𝔼sπθt[DKL(πθt+1(|s)πθt(|s))]This ensures updates are invariant to invertible affine parameter transformations.

Fisher information approximation

For small ϵ, the KL divergence is approximated by the Fisher information metric:D¯KL(πθt+1πθt)12(θt+1θt)TF(θt)(θt+1θt)where F(θ) is the Fisher information matrix of the policy, defined as:F(θ)=𝔼s,aπθ[θlnπθ(a|s)(θlnπθ(a|s))T]This transforms the problem into a problem in quadratic programming, yielding the natural policy gradient update:θt+1=θt+αF(θt)1θJ(θt)The step size α is typically adjusted to maintain the KL constraint, with α2ϵ(θJ(θt))TF(θt)1θJ(θt).

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

Trust Region Policy Optimization (TRPO)

Template:Anchor

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 ensures stable policy improvements by limiting the KL divergence between successive policies, addressing key challenges in natural policy gradient methods.

TRPO builds on the natural policy gradient by incorporating a trust region constraint. While the natural gradient provides a theoretically optimal direction, TRPO's line search and KL constraint mitigate errors from Taylor approximations, ensuring monotonic policy improvement. This makes TRPO more robust in practice, particularly for high-dimensional policies.

Formulation

Like natural policy gradient, TRPO iteratively updates the policy parameters θ by solving a constrained optimization problem specified coordinate-free:{maxθL(θ,θt)D¯KL(πθπθt)ϵwhere

  • L(θ,θt)=𝔼s,aπθt[πθ(a|s)πθt(a|s)Aπθt(s,a)] is the surrogate advantage, measuring the performance of πθ relative to the old policy πθk.
  • ϵ is the trust region radius.

Note that in general, other surrogate advantages are possible:L(θ,θt)=𝔼s,aπθt[πθ(a|s)πθt(a|s)Ψπθt(s,a)]where Ψ is any linear sum of the previously mentioned type. Indeed, OpenAI recommended using the Generalized Advantage Estimate, instead of the plain advantage Aπθ.

The surrogate advantage L(θ,θt) is designed to align with the policy gradient θJ(θ). Specifically, when θ=θt, θL(θ,θt) equals the policy gradient derived from the advantage function: θJ(θ)=𝔼(s,a)πθ[θlnπθ(a|s)Aπθ(s,a)]=θL(θ,θt)However, when θθt, 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 θt:L(θ,θt)gT(θθt),D¯KL(πθπθt)12(θθt)TH(θθt), where:

  • g=θL(θ,θt)|θ=θt is the policy gradient.
  • F=θ2D¯KL(πθπθt)|θ=θt is the Fisher information matrix.

This reduces the problem to a quadratic optimization, yielding the natural policy gradient update: θt+1=θt+2ϵgTF1gF1g.So far, this is essentially the same as natural gradient method. However, TRPO improves upon it by two modifications:

  • Use conjugate gradient method to solve for x in Fx=g iteratively without explicit matrix inversion.
  • Use backtracking line search to ensure the trust-region constraint is satisfied. Specifically, it backtracks the step size to ensure the KL constraint and policy improvement by repeatedly tryingθt+1=θt+2ϵxTFxx,θt+α2ϵxTFxx,until a θt+1 is found that both satisfies the KL constraint D¯KL(πθt+1πθt)ϵ and results in a higher L(θt+1,θt)L(θt,θt). Here, α(0,1) is the backtracking coefficient.

Proximal Policy Optimization (PPO)

Template:Anchor

A further improvement is proximal policy optimization (PPO), which avoids even computing F(θ) and F(θ)1 via a first-order approximation using clipped probability ratios.[7]

Specifically, instead of maximizing the surrogate advantagemaxθL(θ,θt)=𝔼s,aπθt[πθ(a|s)πθt(a|s)Aπθt(s,a)]under a KL divergence constraint, it directly inserts the constraint into the surrogate advantage:maxθ𝔼s,aπθt[{min(πθ(a|s)πθt(a|s),1+ϵ)Aπθt(s,a) if Aπθt(s,a)>0max(πθ(a|s)πθt(a|s),1ϵ)Aπθt(s,a) if Aπθt(s,a)<0]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: Aπθt(s,a)>0, then the gradient should direct θ towards the direction that increases the probability of performing action a under the state s. However, as soon as θ has changed so much that πθ(a|s)(1+ϵ)πθt(a|s), then the gradient should stop pointing it in that direction. And similarly if Aπθt(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 θt to θt+1 requires multiple update steps on the same batch of data. It would initialize θ=θt, then repeatedly apply gradient descent (such as the Adam optimizer) to update θ until the surrogate advantage has stabilized. It would then assign θt+1 to θ, and do it again.

During this inner-loop, the first update to θ would not hit the 1ϵ,1+ϵ bounds, but as θ is updated further and further away from θt, it eventually starts hitting the bounds. For each such bound hit, the corresponding gradient becomes zero, and thus PPO avoid updating θ too far away from θt.

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 πθt, but policy gradient should be on-policy. So, as θ changes, the surrogate loss becomes more and more off-policy. This is why keeping θ proximal to θt is necessary.

If there is a reference policy πref that the trained policy should not diverge too far from, then additional KL divergence penalty can be added:β𝔼s,aπθt[log(πθ(a|s)πref(a|s))]where β 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):[9]β𝔼s,aπθt[log(πθ(a|s)πref(a|s))+πref(a|s)πθ(a|s)1]

Group Relative Policy Optimization (GRPO)

Template:Anchor

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 πθt, then calculate the group-relative advantage[9]Aπθt(s,aj)=r(s,aj)μσwhere μ,σ 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θ1Gi=1G𝔼(s,a1,,aG)πθt[{min(πθ(ai|s)πθt(ai|s),1+ϵ)Aπθt(s,ai) if Aπθt(s,ai)>0max(πθ(ai|s)πθt(ai|s),1ϵ)Aπθt(s,ai) if Aπθt(s,ai)<0]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.[9]

See also

References

Template:Reflist

Template:Artificial intelligence navbox