Part 12 - Policy Gradient Methods & Actor-Critic

Introduction

In the previous part, we continued our discussion on Deep Reinforcement Learning (Deep RL) and explored how to use function approximation for control tasks in RL. We covered key algorithms such as semi-gradient SARSA and Deep Q-Networks (DQN) along with its variants. In this part, we will shift our focus to policy gradient methods and actor-critic algorithms. We will discuss the fundamental concepts behind policy gradients, explore various algorithms such as REINFORCE and Proximal Policy Optimization (PPO), and understand how actor-critic methods combine value-based and policy-based approaches to achieve efficient learning in complex environments.

Policy Gradient Methods

So far, we have focused on state/action-value functions.

Here, instead of state/action-value methods, we consider methods that learn a parameterized policy that can select actions without consulting a value function.

Notation (Policy Gradient Methods)

We use the notation θRd\boldsymbol{\theta} \in \mathbb{R}^{d^{\prime}} for the policy’s parameter vector. Thus, π(as,θ)=P(At=aSt=s,θt=θ)\pi(a \mid s, \boldsymbol{\theta}) = P(A_t = a \mid S_t = s, \boldsymbol{\theta}_t = \boldsymbol{\theta}) denotes the probablity that action aa is taken at time tt given that the environment is in state ss at time tt with parameter vector θ\boldsymbol{\theta}.

If a method uses a learned value function as well, then the value function’s weight vector is denoted wRd\mathbf{w} \in \mathbb{R}^d as usual, as in v^(s,w)\hat{v}(s, \mathbf{w}).

Intuition (Policy Gradient)

We consider learning the policy parameter based on the gradient (ascent) of some scalar performance measure J(θ)J(\boldsymbol{\theta}),

θt+1θt+αJ(θt),\boldsymbol{\theta}_{t + 1} \coloneqq \boldsymbol{\theta}_t + \alpha \nabla J(\boldsymbol{\theta}_t),

where J(θ)\nabla J(\boldsymbol{\theta}) is the gradient of J(θ)J(\boldsymbol{\theta}) with respect to θ\boldsymbol{\theta} (or an estimate of it) and α>0\alpha > 0 is a step-size parameter.

Such methods are called policy gradient methods. They can also learn an approximate value function.

Further, methods that learn approximations to both policy and value functions are often called actor-critic methods, where actor refers to the learned policy, and critic refers to the learned value function, usually a state-value function.

Intuition (Pros & Cons of Policy-based RL)

Advantages:

  • Good convergence properties.
  • Better extended to high-dimensional or continuous action spaces.
  • Can learn stochastic policies, in addition to being deterministic if needed.
  • A way of injecting prior knowledge about the desired form of the policy.
  • Somtimes policies are simple while values and models are complex. E.g., rich domain, but optimal is always “go left”.
  • The action probabilities change smoothly as a function of the parameter (with ϵ\epsilon-greedy the action probabilities may change dramatically).

Disadvantages:

  • Susceptible to local optima (especially with non-linear models).
  • Obtained knowledge is specific, does not always generalize well.
  • Ignores a lot of information in the data (when used in isolation).
Definition 1 (Policy Objective Functions)

Given a policy πθ(as)\pi_{\boldsymbol{\theta}}(a \mid s), with parameters θ\boldsymbol{\theta}, we want to find the best θ\boldsymbol{\theta}.

But how do we measure the quality of a policy πθ\pi_{\boldsymbol{\theta}}?

In episodic environments we can use the start value,

J0(θ)vπθ(s0).J_0(\boldsymbol{\theta}) \coloneqq v_{\pi_{\boldsymbol{\theta}}}(s_0).

In continuing environments we can use the average value,

JavV(θ)sμπθ(s)vπθ(s),J_{avV}(\boldsymbol{\theta}) \coloneqq \sum_s \mu_{\pi_{\boldsymbol{\theta}}}(s) v_{\pi_{\boldsymbol{\theta}}}(s),

where μπθ(s)=p(St=sπθ)\mu_{\pi_{\boldsymbol{\theta}}}(s) = p(S_t = s \mid \pi_{\boldsymbol{\theta}}) is the probability of being in state ss in the long run, i.e., the on-policy distribution under policy πθ\pi_{\boldsymbol{\theta}}.

Alternatively, we can use the average reward per time step,

JavR(θ)sμπθ(s)aπθ(as)rrp(rs,a).J_{avR}(\boldsymbol{\theta}) \coloneqq \sum_s \mu_{\pi_{\boldsymbol{\theta}}}(s) \sum_a \pi_{\boldsymbol{\theta}}(a \mid s) \sum_{r} r p(r \mid s, a).
Intuition (Policy Optimization)

Policy based RL is an optimization problem, i.e., find θ=argmaxθJ(θ)\boldsymbol{\theta}^{\star} = \arg\max_{\boldsymbol{\theta}} J(\boldsymbol{\theta}). It is possible to not use the gradient, e.g., hill climbing and genetic algorithms.

However, we will focus on stochastic gradient ascent, which is often quite efficient (and easy to use with deep nets).

Definition 2 (Policy Gradient)

Let J(θ)J(\boldsymbol{\theta}) be any objective function. Policy gradient algorithms search for a (local) maximum in J(θ)J(\boldsymbol{\theta}) by ascending the gradient of the policy, with respect to θ\boldsymbol{\theta},

Δθ=αθJ(θ),\Delta \boldsymbol{\theta} = \alpha \nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}),

where θJ(θ)\nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) is the policy gradient,

θJ(θ)[J(θ)θ1J(θ)θd].\nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) \coloneqq \begin{bmatrix} \frac{\partial J(\boldsymbol{\theta})}{\partial \theta_1} \newline \vdots \newline \frac{\partial J(\boldsymbol{\theta})}{\partial \theta_{d^{\prime}}} \end{bmatrix}.
Note

However, we face a challenge; J(θ)J(\boldsymbol{\theta}) depends on both the action selections and the distribution of states in which those selections are made.

Given a staate, the effect of the policy parameter on the actions, and thus on the reward, can be computed in a relatively straightforward way.

But the effect of the policy on the state distribution is a function of the environment and is typically unknown.

Thus, how can we estimate the gradient of the performance measure with respect to the policy parameters when the gradient depends on the unknown effect of policy changes on the state distribution?

Theorem 1 (Policy Gradient Theorem)

The policy gradient theorem answers this question.

It provides an analytical expression for θJ(θ)\nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) that does not involve the derivative of the state distribution,

θJ(θ)sμ(s)aqπ(s,a)θπ(as,θ),\nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) \propto \sum_s \mu(s) \sum_a q_{\pi}(s, a) \nabla_{\boldsymbol{\theta}} \pi(a \mid s, \boldsymbol{\theta}),

Any constant of proportionality can be absorbed into α\alpha.

Further, in the episodic case, this holds for γ=1\gamma = 1; for arbitrary γ\gamma then μ(s)\mu(s) is replaced by some more complicated term depending on discounting factor at different tt.

Definition 3 (Naïve Policy Gradient Algorithm)

The RHS of the policy gradient theorem is a sum over states weighted by how often the states occur under the policy π\pi. Thus,

θJ(θ)sμ(s)aqπ(s,a)θπ(as,θ)=Eπ[aqπ(St,a)θπ(aSt,θ)]\begin{align*} \nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) & \propto \sum_s \mu(s) \sum_a q_{\pi}(s, a) \nabla_{\boldsymbol{\theta}} \pi(a \mid s, \boldsymbol{\theta}) \newline & = \mathbb{E}_{\pi} \left[\sum_a q_{\pi}(S_t, a) \nabla_{\boldsymbol{\theta}} \pi(a \mid S_t, \boldsymbol{\theta})\right] \newline \end{align*}

We could stop here and instantiate our stochastic gradient ascent algorithm (called an all-actions method) as,

θt+1θt+αaq^(St,a,w)θπ(aSt,θt),\boldsymbol{\theta}_{t + 1} \coloneqq \boldsymbol{\theta}_t + \alpha \sum_a \hat{q}(S_t, a, \mathbf{w}) \nabla_{\boldsymbol{\theta}} \pi(a \mid S_t, \boldsymbol{\theta}_t),

where q^\hat{q} is some learned approximation of qπq_{\pi}.

It is promising, but we want to focus on a method whose update at time tt involves only AtA_t.

Intuition (REINFORCE Monte-Carlo Policy Gradient)

An idea is to replace the sum over the random variable’s possible values by an expectation under π\pi and then sampling the expectation,

θJ(θ)Eπ[aπ(aSt,θ)qπ(St,a)θπ(aSt,θ)π(aSt,θ)]=Eπ[qπ(St,At)θπ(AtSt,θ)π(AtSt,θ)]=Eπ[Gtθπ(AtSt,θ)π(AtSt,θ)]\begin{align*} \nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) & \propto \mathbb{E}_{\pi} \left[\sum_a \pi(a \mid S_t, \boldsymbol{\theta}) q_{\pi}(S_t, a) \frac{\nabla_{\boldsymbol{\theta}} \pi(a \mid S_t, \boldsymbol{\theta})}{\pi(a \mid S_t, \boldsymbol{\theta})}\right] \newline & = \mathbb{E}_{\pi} \left[q_{\pi}(S_t, A_t) \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta})}{\pi(A_t \mid S_t, \boldsymbol{\theta})}\right] \newline & = \mathbb{E}_{\pi} \left[G_t \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta})}{\pi(A_t \mid S_t, \boldsymbol{\theta})}\right] \newline \end{align*}

The final expression, read in words, is a quantity that can be sampled on each time step whose expectation is proportional to the gradient!

Then, the REINFORCE update becomes (which is very intuitive!),

θt+1θt+αGtθπ(AtSt,θt)π(AtSt,θt).\boldsymbol{\theta}_{t + 1} \coloneqq \boldsymbol{\theta}_t + \alpha G_t \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta}_t)}{\pi(A_t \mid S_t, \boldsymbol{\theta}_t)}.

It increases θt\boldsymbol{\theta}_t in the direction of θπ(AtSt,θt)\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta}_t) proportional to the return and inversely proportional to the action probability.

The former makes sense because it causes the parameter to move most in the directions that favor actions that yield the highest sum.

The latter makes sense, otherwise actions that are selected frequently are at an advantage (the updates will be more often in their direction) and might win out even if they do not yield the highest return.

Algorithm (REINFORCE: Monte-Carlo Policy Gradient)

Input: a differentiable policy parameterization π(as,θ)\pi(a \mid s, \boldsymbol{\theta})

Algorithm parameters: step-size α>0\alpha > 0

Initialize policy parameter θRd\boldsymbol{\theta} \in \mathbb{R}^{d^{\prime}} (e.g., to 0\mathbf{0})

Loop forever (for each episode):

  • Generate an episode S0,A0,R1,S1,A1,R2,,ST1,AT1,RTS_0, A_0, R_1, S_1, A_1, R_2, \ldots, S_{T - 1}, A_{T - 1}, R_T, following π(,θ)\pi(\cdot \mid \cdot, \boldsymbol{\theta})
  • Loop for each step of the episode, t=0,1,,T1t = 0, 1, \ldots, T - 1:
    • Gk=t+1Tγkt1RkG \leftarrow \sum_{k = t + 1}^T \gamma^{k - t - 1} R_k
  • θθ+αγtGθlnπ(AtSt,θ)\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} + \alpha \gamma^t G \nabla_{\boldsymbol{\theta}} \ln \pi(A_t \mid S_t, \boldsymbol{\theta})

We note that θπ(AtSt,θ)π(AtSt,θ)=θlnπ(AtSt,θ)\frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta})}{\pi(A_t \mid S_t, \boldsymbol{\theta})} = \nabla_{\boldsymbol{\theta}} \ln \pi(A_t \mid S_t, \boldsymbol{\theta}), sometimes called the eligibility vector.

Note (Softmax Policy)

In policy gradient methods, the policy can be parameterized in any way, as long as θπ(as,θ)\nabla_{\boldsymbol{\theta}} \pi(a \mid s, \boldsymbol{\theta}) exists and is finite s,a,θ\forall s, a, \boldsymbol{\theta}.

The policy can be defined for example according to an exponential softmax distribution,

π(as,θ)eh(s,a,θ)beh(s,b,θ),\pi(a \mid s, \boldsymbol{\theta}) \coloneqq \frac{e^{h(s, a, \boldsymbol{\theta})}}{\sum_b e^{h(s, b, \boldsymbol{\theta})}},

where h(s,a,θ)Rh(s, a, \boldsymbol{\theta}) \in \mathbb{R} is a parameterized preference function.

Further, the gradient of the log probability is,

θlnπ(as,θ)=θh(s,a,θ)bπθ(bs)θh(s,b,θ).\nabla_{\boldsymbol{\theta}} \ln \pi(a \mid s, \boldsymbol{\theta}) = \nabla_{\boldsymbol{\theta}} h(s, a, \boldsymbol{\theta}) - \sum_b \pi_{\boldsymbol{\theta}}(b \mid s) \nabla_{\boldsymbol{\theta}} h(s, b, \boldsymbol{\theta}).

Action preferences (hh functions) can be parameterized arbitrarily. For example, they might be computed by a deep neural network where θ\boldsymbol{\theta} is the vector of all the connetion weights of the network.

They can also be linear, using feature vectors x(s,a)Rd\mathbf{x}(s, a) \in \mathbb{R}^{d^{\prime}},

h(s,a,θ)θTx(s,a).h(s, a, \boldsymbol{\theta}) \coloneqq \boldsymbol{\theta}^T \mathbf{x}(s, a).
Intuition (REINFORCE with Baseline)

The policy gradient theorem can be generalized to include a comparison of the action value to an arbitrary baseline b(s)b(s),

θJ(θ)sμ(s)a(qπ(s,a)b(s))θπ(as,θ).\nabla_{\boldsymbol{\theta}} J(\boldsymbol{\theta}) \propto \sum_s \mu(s) \sum_a (q_{\pi}(s, a) - b(s)) \nabla_{\boldsymbol{\theta}} \pi(a \mid s, \boldsymbol{\theta}).

The baseline can be any function, even a random variable, as long as it does not vary with aa. The equation remains valid because the subtracted quantity is zero,

ab(s)θπ(as,θ)=b(s)θaπ(as,θ)=b(s)θ1=0.\begin{align*} \sum_a b(s) \nabla_{\boldsymbol{\theta}} \pi(a \mid s, \boldsymbol{\theta}) & = b(s) \nabla_{\boldsymbol{\theta}} \sum_a \pi(a \mid s, \boldsymbol{\theta}) \newline & = b(s) \nabla_{\boldsymbol{\theta}} 1 \newline & = 0. \newline \end{align*}
Definition 4 (Update Rule for REINFORCE with Baseline)

Thus, a new version of the REINFORCE algorithm, with baseline b(s)b(s), has the update,

θt+1θt+α(Gtb(St))θπ(AtSt,θt)π(AtSt,θt).\boldsymbol{\theta}_{t + 1} \coloneqq \boldsymbol{\theta}_t + \alpha (G_t - b(S_t)) \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta}_t)}{\pi(A_t \mid S_t, \boldsymbol{\theta}_t)}.

The baseline leaves the expected value of the update unchanged, but it can have a large effect on its variance.

A good choice of b(St)b(S_t) is an estimate of the state value v^(St,w)\hat{v}(S_t, \mathbf{w}), i.e., use a Monte-Carlo method to learn the state-value weights w\mathbf{w}.

Algorithm (REINFORCE with Baseline: Monte-Carlo Policy Gradient)

Input: a differentiable policy parameterization π(as,θ)\pi(a \mid s, \boldsymbol{\theta})

Input: a differentiable state-value function parameterization v^(s,w)\hat{v}(s, \mathbf{w})

Algorithm parameters: step-sizes αθ>0\alpha_{\boldsymbol{\theta}} > 0, αw>0\alpha_{\mathbf{w}} > 0

Initialize policy parameter θRd\boldsymbol{\theta} \in \mathbb{R}^{d^{\prime}} and state-value weights wRd\mathbf{w} \in \mathbb{R}^d (e.g., to 0\mathbf{0})

Loop forever (for each episode):

  • Generate an episode S0,A0,R1,S1,A1,R2,,ST1,AT1,RTS_0, A_0, R_1, S_1, A_1, R_2, \ldots, S_{T - 1}, A_{T - 1}, R_T, following π(,θ)\pi(\cdot \mid \cdot, \boldsymbol{\theta})
  • Loop for each step of the episode, t=0,1,,T1t = 0, 1, \ldots, T - 1:
    • Gk=t+1Tγkt1RkG \leftarrow \sum_{k = t + 1}^T \gamma^{k - t - 1} R_k
    • δGv^(St,w)\delta \leftarrow G - \hat{v}(S_t, \mathbf{w})
    • ww+αwδwv^(St,w)\mathbf{w} \leftarrow \mathbf{w} + \alpha_{\mathbf{w}} \delta \nabla_{\mathbf{w}} \hat{v}(S_t, \mathbf{w})
    • θθ+αθγtδθlnπ(AtSt,θ)\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} + \alpha_{\boldsymbol{\theta}} \gamma^t \delta \nabla_{\boldsymbol{\theta}} \ln \pi(A_t \mid S_t, \boldsymbol{\theta})

Actor-Critic Methods

Intuition (Actor-Critic Methods)

A way to combine value-based and policy-based methods is to use an actor-critic architecture.

The actor learns the policy and the critic learns the value function. The overall policy-gradient method with values function estimation is called an actor-critic method.

We consider one-step actor-critic methods, the analog of the TD methods such as TD(0), SARSA, and Q-learning.

One-step actor-critic methods replace the full return of the REINFORCE algorithm by a one-step return (and use a learned state-value function as a baseline).

Definition 5 (One-step Actor-Critic)

The one-step actor-critic update is,

θt+1θt+α(Gt:t+1v^(St,wt))θπ(AtSt,θt)π(AtSt,θt)=θt+α(Rt+1+γv^(St+1,wt)v^(St,wt))θπ(AtSt,θt)π(AtSt,θt)=θt+αδtθπ(AtSt,θt)π(AtSt,θt),\begin{align*} \boldsymbol{\theta}_{t + 1} & \coloneqq \boldsymbol{\theta}_t + \alpha \left(G_{t:t+1} - \hat{v}(S_t, \mathbf{w}_t)\right) \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta}_t)}{\pi(A_t \mid S_t, \boldsymbol{\theta}_t)} \newline & = \boldsymbol{\theta}_t + \alpha \left(R_{t + 1} + \gamma \hat{v}(S_{t + 1}, \mathbf{w}_t) - \hat{v}(S_t, \mathbf{w}_t)\right) \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta}_t)}{\pi(A_t \mid S_t, \boldsymbol{\theta}_t)} \newline & = \boldsymbol{\theta}_t + \alpha \delta_t \frac{\nabla_{\boldsymbol{\theta}} \pi(A_t \mid S_t, \boldsymbol{\theta}_t)}{\pi(A_t \mid S_t, \boldsymbol{\theta}_t)}, \newline \end{align*}

We note that this is fully online and incremental.

The critic uses TD errors δ\delta to criticize the actor’s action choices.

Positive δ\delta means that the action was “good” because it led to a state with a better-than-expected value. Negative δ\delta means that the action was “bad” because it led to a state with a worse-than-expected value.

Algorithm (One-step Actor-Critic (episodic))

Input: a differentiable policy parameterization π(as,θ)\pi(a \mid s, \boldsymbol{\theta})

Input: a differentiable state-value function parameterization v^(s,w)\hat{v}(s, \mathbf{w})

Parameters: step-sizes αθ>0\alpha_{\boldsymbol{\theta}} > 0, αw>0\alpha_{\mathbf{w}} > 0

Initialize policy parameter θRd\boldsymbol{\theta} \in \mathbb{R}^{d^{\prime}} and state-value weights wRd\mathbf{w} \in \mathbb{R}^d (e.g., to 0\mathbf{0})

Loop forever (for each episode):

  • Initialize SS (first state of episode)
  • I1I \leftarrow 1
  • Loop while SS is not terminal (for each time step):
    • Aπ(S,θ)A \sim \pi(\cdot \mid S, \boldsymbol{\theta})
    • Take action AA, observe S,RS^{\prime}, R
    • δR+γv^(S,w)v^(S,w)\delta \leftarrow R + \gamma \hat{v}(S^{\prime}, \mathbf{w}) - \hat{v}(S, \mathbf{w}) (if SS^{\prime} is terminal, then v^(S,w)=0\hat{v}(S^{\prime}, \mathbf{w}) = 0)
    • ww+αwδwv^(S,w)\mathbf{w} \leftarrow \mathbf{w} + \alpha_{\mathbf{w}} \delta \nabla_{\mathbf{w}} \hat{v}(S, \mathbf{w})
    • θθ+αθIδθlnπ(AS,θ)\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} + \alpha_{\boldsymbol{\theta}} I \delta \nabla_{\boldsymbol{\theta}} \ln \pi(A \mid S, \boldsymbol{\theta})
    • IγII \leftarrow \gamma I
    • SSS \leftarrow S^{\prime}

The generalization to nn-step method is straightforward, using the nn-step return Gt:t+nG_{t:t+n} instead of the one-step return Gt:t+1G_{t:t+1}.