Skip to content
Ahmed Haroon
← Reinforcement Learning

Policy Gradients

Optimizing the Policy Directly

Value-based methods learn Q(s,a)Q(s,a) and obtain a policy through an argmax. Policy-gradient methods instead represent the policy itself:

πθ(a∣s)=Pr⁡(At=a∣St=s;θ).\pi_\theta(a\mid s) =\Pr(A_t=a\mid S_t=s;\theta).

We optimize its parameters to maximize expected return:

θ∗∈arg⁡max⁡θJ(θ).\boxed{ \theta^*\in\arg\max_\theta J(\theta) }.

Direct policy optimization is useful when the action space is continuous, smooth changes in action probabilities matter, or the best policy is genuinely stochastic. It also avoids solving

arg⁡max⁡aQ(s,a)\arg\max_a Q(s,a)

every time the agent acts.

For discrete actions, a neural policy commonly produces logits zθ(s,a)z_\theta(s,a) and applies softmax:

πθ(a∣s)=exp⁡(zθ(s,a))∑a′exp⁡(zθ(s,a′)).\pi_\theta(a\mid s) =\frac{\exp(z_\theta(s,a))} {\sum_{a'}\exp(z_\theta(s,a'))}.

For continuous actions, a common policy is

at∼N(μθ(st),Σθ(st)).a_t\sim\mathcal N \left(\mu_\theta(s_t),\Sigma_\theta(s_t)\right).

Stochasticity is not merely an exploration trick. It is part of the policy class being optimized.

Suppose the same observation can be produced by two hidden states that require different actions. A deterministic observation policy must choose the same action in both. A stochastic policy can assign probability to both possibilities:

πθ(a∣o).\pi_\theta(a\mid o).

We can also condition on a recurrent summary of the observation history:

ht=fθ(ht−1,ot,at−1),πθ(at∣ht).h_t=f_\theta(h_{t-1},o_t,a_{t-1}), \qquad \pi_\theta(a_t\mid h_t).

The derivation below does not require the policy input to be a fully observed Markov state. It only requires the sampled action probability to be differentiable with respect to θ\theta.

The Policy Objective

For a finite-horizon trajectory

τ=(s0,a0,r0,…,sH−1,aH−1,rH−1,sH),\tau=(s_0,a_0,r_0,\ldots,s_{H-1},a_{H-1},r_{H-1},s_H),

define the return

R(τ)=∑t=0H−1rt.R(\tau)=\sum_{t=0}^{H-1}r_t.

The objective is the expected return under trajectories generated by the current policy:

J(θ)=Eτ∼pθ(τ)[R(τ)].\boxed{ J(\theta) =\mathbb E_{\tau\sim p_\theta(\tau)}[R(\tau)] }.

The trajectory probability factorizes as

pθ(τ)=ρ0(s0)∏t=0H−1πθ(at∣st)P(st+1∣st,at),p_\theta(\tau) =\rho_0(s_0) \prod_{t=0}^{H-1} \pi_\theta(a_t\mid s_t) P(s_{t+1}\mid s_t,a_t),

where ρ0\rho_0 is the initial-state distribution and PP contains the environment dynamics.

Taking logs turns the product into a sum:

log⁡pθ(τ)=log⁡ρ0(s0)+∑t=0H−1log⁡πθ(at∣st)+∑t=0H−1log⁡P(st+1∣st,at).\begin{aligned} \log p_\theta(\tau) &=\log\rho_0(s_0) +\sum_{t=0}^{H-1}\log\pi_\theta(a_t\mid s_t)\\ &\quad+\sum_{t=0}^{H-1} \log P(s_{t+1}\mid s_t,a_t). \end{aligned}

Only the policy depends on θ\theta. Therefore,

∇θlog⁡pθ(τ)=∑t=0H−1∇θlog⁡πθ(at∣st).\boxed{ \nabla_\theta\log p_\theta(\tau) =\sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta(a_t\mid s_t) }.

This is why policy gradients are model-free. The dynamics determine which trajectories occur, but we never differentiate through them.

The Likelihood-Ratio Identity

Once a trajectory has been sampled, its observed rewards are fixed numbers. The dependence on θ\theta lies in the probability of sampling that trajectory.

For any differentiable positive density,

∇θlog⁡pθ(x)=∇θpθ(x)pθ(x).\nabla_\theta\log p_\theta(x) =\frac{\nabla_\theta p_\theta(x)}{p_\theta(x)}.

Rearranging gives the likelihood-ratio identity

∇θpθ(x)=pθ(x)∇θlog⁡pθ(x).\boxed{ \nabla_\theta p_\theta(x) =p_\theta(x)\nabla_\theta\log p_\theta(x) }.

It converts a derivative of a sampling distribution into the derivative of a log-probability that we can evaluate on samples.

See derivation

Let

F(θ)=Ex∼pθ[f(x)]F(\theta)=\mathbb E_{x\sim p_\theta}[f(x)]

and assume ff does not explicitly depend on θ\theta. Then

∇θF(θ)=∇θ∫pθ(x)f(x) dx=∫∇θpθ(x)f(x) dx=∫pθ(x)∇θpθ(x)pθ(x)f(x) dx=Ex∼pθ[f(x)∇θlog⁡pθ(x)].\begin{aligned} \nabla_\theta F(\theta) &=\nabla_\theta\int p_\theta(x)f(x)\,dx\\ &=\int\nabla_\theta p_\theta(x)f(x)\,dx\\ &=\int p_\theta(x) \frac{\nabla_\theta p_\theta(x)}{p_\theta(x)} f(x)\,dx\\ &=\mathbb E_{x\sim p_\theta} \left[f(x)\nabla_\theta\log p_\theta(x)\right]. \end{aligned}

Hence

∇θEx∼pθ[f(x)]=Ex∼pθ[f(x)∇θlog⁡pθ(x)].\boxed{ \nabla_\theta\mathbb E_{x\sim p_\theta}[f(x)] =\mathbb E_{x\sim p_\theta} \left[f(x)\nabla_\theta\log p_\theta(x)\right] }.

Applying the identity to trajectories gives

∇θJ(θ)=Eτ∼pθ[R(τ)∇θlog⁡pθ(τ)]=Eτ∼pθ[R(τ)∑t=0H−1∇θlog⁡πθ(at∣st)].\begin{aligned} \nabla_\theta J(\theta) &=\mathbb E_{\tau\sim p_\theta} \left[R(\tau)\nabla_\theta\log p_\theta(\tau)\right]\\ &=\mathbb E_{\tau\sim p_\theta} \left[ R(\tau) \sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta(a_t\mid s_t) \right]. \end{aligned}

Therefore,

∇θJ(θ)=Eτ∼pθ[∑t=0H−1∇θlog⁡πθ(at∣st)R(τ)].\boxed{ \nabla_\theta J(\theta) =\mathbb E_{\tau\sim p_\theta} \left[ \sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta(a_t\mid s_t) R(\tau) \right] }.

The score

∇θlog⁡πθ(at∣st)\nabla_\theta\log\pi_\theta(a_t\mid s_t)

says how the sampled action's probability changes, while the return says whether that sampled behavior was useful.

For every state,

Ea∼πθ(⋅∣s)[∇θlog⁡πθ(a∣s)]=0.\boxed{ \mathbb E_{a\sim\pi_\theta(\cdot\mid s)} \left[\nabla_\theta\log\pi_\theta(a\mid s)\right] =0 }.
See derivation

For a continuous action,

Ea∼πθ[∇θlog⁡πθ(a∣s)]=∫πθ(a∣s)∇θlog⁡πθ(a∣s) da=∫∇θπθ(a∣s) da=∇θ∫πθ(a∣s) da=∇θ1=0.\begin{aligned} \mathbb E_{a\sim\pi_\theta} [\nabla_\theta\log\pi_\theta(a\mid s)] &=\int\pi_\theta(a\mid s) \nabla_\theta\log\pi_\theta(a\mid s)\,da\\ &=\int\nabla_\theta\pi_\theta(a\mid s)\,da\\ &=\nabla_\theta\int\pi_\theta(a\mid s)\,da\\ &=\nabla_\theta1\\ &=0. \end{aligned}

This identity lets us remove irrelevant rewards and subtract baselines without changing the expected gradient.

REINFORCE

Given NN trajectories sampled from the current policy, the Monte Carlo estimator is

∇θJ^=1N∑i=1NR(τ(i))∑t=0H−1∇θlog⁡πθ(at(i)∣st(i)).\boxed{ \widehat{\nabla_\theta J} =\frac1N\sum_{i=1}^{N} R(\tau^{(i)}) \sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta \left(a_t^{(i)}\mid s_t^{(i)}\right) }.

We perform gradient ascent:

θ←θ+α∇θJ^.\theta\leftarrow\theta+ \alpha\widehat{\nabla_\theta J}.

This resembles weighted maximum likelihood. Ordinary maximum likelihood uses

∑i,t∇θlog⁡πθ(at(i)∣st(i)),\sum_{i,t}\nabla_\theta \log\pi_\theta(a_t^{(i)}\mid s_t^{(i)}),

while REINFORCE weights each score by its observed return. Positive weights increase the sampled action's probability; negative weights decrease it.

This statement is statistical, not deterministic. A good action can receive a poor return because of a later mistake, and a bad action can receive a good return because of luck. These errors only cancel in expectation.

Reward-to-go

The full return credits an action at time tt for rewards that occurred before it. Those past rewards cannot have been caused by ata_t, so they only add noise.

Define reward-to-go

Gt=∑t′=tH−1rt′.G_t=\sum_{t'=t}^{H-1}r_{t'}.

We can replace the episode return by GtG_t without changing the expected gradient:

∇θJ=E[∑t=0H−1∇θlog⁡πθ(at∣st)Gt].\boxed{ \nabla_\theta J =\mathbb E \left[ \sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta(a_t\mid s_t)G_t \right] }.
See derivation

Expand the full-return estimator:

∇θJ=E[∑t=0H−1∇θlog⁡πθ(at∣st)∑t′=0H−1rt′].\nabla_\theta J =\mathbb E \left[ \sum_{t=0}^{H-1} \nabla_\theta\log\pi_\theta(a_t\mid s_t) \sum_{t'=0}^{H-1}r_{t'} \right].

For a past reward rt′r_{t'} with t′<tt'<t, condition on the history through sts_t:

E[rt′∇θlog⁡πθ(at∣st)]=E[rt′Eat∼πθ(⋅∣st)[∇θlog⁡πθ(at∣st)∣st,past]]=E[rt′⋅0]=0.\begin{aligned} &\mathbb E \left[ r_{t'}\nabla_\theta\log\pi_\theta(a_t\mid s_t) \right]\\ &=\mathbb E \left[ r_{t'} \mathbb E_{a_t\sim\pi_\theta(\cdot\mid s_t)} \left[ \nabla_\theta\log\pi_\theta(a_t\mid s_t) \mid s_t,\text{past} \right] \right]\\ &=\mathbb E[r_{t'}\cdot0]\\ &=0. \end{aligned}
Past rewards are fixed before

Only rewards at or after tt remain, producing GtG_t.

For discounted problems, we use

Gtγ=∑t′=tH−1γt′−trt′.G_t^\gamma =\sum_{t'=t}^{H-1}\gamma^{t'-t}r_{t'}.

Discounting controls the influence of uncertain distant outcomes and makes infinite-horizon returns finite when γ<1\gamma<1. It also changes the objective by preferring earlier rewards.

REINFORCE\textbf{REINFORCE}Initialize policy parameters θ.Repeat:collect N complete trajectories using πθ,GH−1(i)←rH−1(i),Gt(i)←rt(i)+γGt+1(i)backward through each trajectory,g^←1N∑i∑tGt(i)∇θlog⁡πθ(at(i)∣st(i)),θ←θ+αg^.\begin{aligned} &\text{Initialize policy parameters }\theta.\\[0.25em] &\text{Repeat:}\\[0.25em] &\qquad \text{collect }N\text{ complete trajectories using }\pi_\theta,\\[0.25em] &\qquad G_{H-1}^{(i)}\leftarrow r_{H-1}^{(i)},\\[0.25em] &\qquad G_t^{(i)}\leftarrow r_t^{(i)}+\gamma G_{t+1}^{(i)} \quad\text{backward through each trajectory},\\[0.25em] &\qquad \widehat g\leftarrow\frac1N\sum_i\sum_t G_t^{(i)}\nabla_\theta\log\pi_\theta (a_t^{(i)}\mid s_t^{(i)}),\\[0.25em] &\qquad \theta\leftarrow\theta+\alpha\widehat g. \end{aligned}

REINFORCE is on-policy: the trajectories must come from the policy whose gradient we estimate unless we add an explicit distribution correction.

Baselines and Advantages

Even reward-to-go varies greatly between trajectories. We can subtract a state-dependent baseline:

∇θJ=E[∑t∇θlog⁡πθ(at∣st)(Gt−b(st))].\boxed{ \nabla_\theta J =\mathbb E \left[ \sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t) \left(G_t-b(s_t)\right) \right] }.

The baseline supplies a reference. An action is reinforced when its return is better than expected from that state and discouraged when it is worse.

Why a baseline does not change the gradient

An action-independent baseline contributes zero in expectation.

See derivation

At one time step,

Bt=E[∇θlog⁡πθ(at∣st)b(st)].B_t =\mathbb E \left[ \nabla_\theta\log\pi_\theta(a_t\mid s_t)b(s_t) \right].

Using iterated expectation,

Bt=Est[b(st)Eat∼πθ(⋅∣st)[∇θlog⁡πθ(at∣st)]]=Est[b(st)⋅0]=0.\begin{aligned} B_t &=\mathbb E_{s_t} \left[ b(s_t) \mathbb E_{a_t\sim\pi_\theta(\cdot\mid s_t)} [\nabla_\theta\log\pi_\theta(a_t\mid s_t)] \right]\\ &=\mathbb E_{s_t}[b(s_t)\cdot0]\\ &=0. \end{aligned}

A constant baseline such as the batch-average return is valid, but it ignores that typical return varies by state. The canonical choice is a learned value function:

Vϕ(st)≈Vπθ(st)=Eπθ[Gt∣st].V_\phi(s_t)\approx V^{\pi_\theta}(s_t) =\mathbb E_{\pi_\theta}[G_t\mid s_t].

This produces the Monte Carlo advantage estimate

A^tMC=Gt−Vϕ(st).\boxed{ \widehat A_t^{\mathrm{MC}} =G_t-V_\phi(s_t) }.

The policy-gradient estimator becomes

∇θJ^=1N∑i=1N∑t∇θlog⁡πθ(at(i)∣st(i))A^t(i).\boxed{ \widehat{\nabla_\theta J} =\frac1N\sum_{i=1}^{N}\sum_t \nabla_\theta\log\pi_\theta (a_t^{(i)}\mid s_t^{(i)}) \widehat A_t^{(i)} }.

An inaccurate state-only baseline does not bias the actor gradient. It simply removes less variance. We fit it to Monte Carlo returns:

LV(ϕ)=1M∑i,t(Vϕ(st(i))−Gt(i))2.\mathcal L_V(\phi) =\frac1M\sum_{i,t} \left(V_\phi(s_t^{(i)})-G_t^{(i)}\right)^2.

As the policy changes, VπθV^{\pi_\theta} changes too, so the baseline must continually track a moving target.

The variance-minimizing baseline

The value function is a strong baseline, but not always the exact minimum-variance choice. Let

z(a,s)=∇θlog⁡πθ(a∣s).z(a,s)=\nabla_\theta\log\pi_\theta(a\mid s).

At a fixed state, minimizing the estimator's second moment gives

b∗(s)=E[∥z(a,s)∥2G∣s]E[∥z(a,s)∥2∣s].\boxed{ b^*(s) =\frac{ \mathbb E[\|z(a,s)\|^2G\mid s] }{ \mathbb E[\|z(a,s)\|^2\mid s] } }.
See derivation

Because the baseline does not change the mean gradient, minimizing variance is equivalent to minimizing

E[∥z∥2(G−b)2∣s].\mathbb E[\|z\|^2(G-b)^2\mid s].

Differentiate with respect to bb:

ddbE[∥z∥2(G−b)2∣s]=−2E[∥z∥2(G−b)∣s].\frac{d}{db} \mathbb E[\|z\|^2(G-b)^2\mid s] =-2\mathbb E[\|z\|^2(G-b)\mid s].

Setting this to zero gives

E[∥z∥2G∣s]−b∗(s)E[∥z∥2∣s]=0,\mathbb E[\|z\|^2G\mid s] -b^*(s)\mathbb E[\|z\|^2\mid s] =0,

which yields the stated result.

If the squared score norm is approximately independent of return, then

b∗(s)≈E[G∣s]=Vπ(s).b^*(s)\approx\mathbb E[G\mid s]=V^\pi(s).

This explains why the value function works well in practice.

Advantage normalization is another common heuristic:

A~t=A^t−μAσA+ε.\widetilde A_t =\frac{\widehat A_t-\mu_A}{\sigma_A+\varepsilon}.

It improves numerical conditioning and reduces sensitivity to reward scale, but it is a batch-level optimization heuristic rather than a state-dependent baseline derived from the score identity.

Policy Distributions

The score function determines how a weighted sample changes the policy. Its form depends on the action distribution.

Softmax policies

Let a linear preference be

hθ(s,a)=ϕ(s,a)⊤θ.h_\theta(s,a)=\phi(s,a)^\top\theta.

The softmax policy is

πθ(a∣s)=eϕ(s,a)⊤θ∑a′eϕ(s,a′)⊤θ.\pi_\theta(a\mid s) =\frac{e^{\phi(s,a)^\top\theta}} {\sum_{a'}e^{\phi(s,a')^\top\theta}}.

Its score is

∇θlog⁡πθ(a∣s)=ϕ(s,a)−Ea′∼πθ(⋅∣s)[ϕ(s,a′)].\boxed{ \nabla_\theta\log\pi_\theta(a\mid s) =\phi(s,a) -\mathbb E_{a'\sim\pi_\theta(\cdot\mid s)}[\phi(s,a')] }.
See derivation

Taking logs,

log⁡πθ(a∣s)=ϕ(s,a)⊤θ−log⁡∑a′eϕ(s,a′)⊤θ.\log\pi_\theta(a\mid s) =\phi(s,a)^\top\theta -\log\sum_{a'}e^{\phi(s,a')^\top\theta}.

The log-partition derivative is

∇θlog⁡∑a′eϕ(s,a′)⊤θ=∑a′eϕ(s,a′)⊤θϕ(s,a′)∑a′eϕ(s,a′)⊤θ=∑a′πθ(a′∣s)ϕ(s,a′).\begin{aligned} \nabla_\theta \log\sum_{a'}e^{\phi(s,a')^\top\theta} &=\frac{ \sum_{a'}e^{\phi(s,a')^\top\theta}\phi(s,a') }{ \sum_{a'}e^{\phi(s,a')^\top\theta} }\\ &=\sum_{a'}\pi_\theta(a'\mid s)\phi(s,a'). \end{aligned}

Subtracting this from ϕ(s,a)\phi(s,a) gives the score.

For neural-network logits zjz_j,

∂log⁡π(a∣s)∂zj=1[j=a]−π(j∣s).\frac{\partial\log\pi(a\mid s)}{\partial z_j} =\mathbb 1[j=a]-\pi(j\mid s).

A positive advantage pushes the sampled action's logit upward and competing logits downward. A negative advantage reverses the direction.

Gaussian policies

For a scalar action with fixed variance,

πθ(a∣s)=N(a;μθ(s),σ2).\pi_\theta(a\mid s) =\mathcal N(a;\mu_\theta(s),\sigma^2).

The score is

∇θlog⁡πθ(a∣s)=a−μθ(s)σ2∇θμθ(s).\boxed{ \nabla_\theta\log\pi_\theta(a\mid s) =\frac{a-\mu_\theta(s)}{\sigma^2} \nabla_\theta\mu_\theta(s) }.
See derivation

The log-density is

log⁡πθ(a∣s)=−12log⁡(2πσ2)−(a−μθ(s))22σ2.\log\pi_\theta(a\mid s) =-\frac12\log(2\pi\sigma^2) -\frac{(a-\mu_\theta(s))^2}{2\sigma^2}.

Only the quadratic term depends on θ\theta, so

∇θlog⁡πθ(a∣s)=−12σ22(a−μθ(s))(−∇θμθ(s))=a−μθ(s)σ2∇θμθ(s).\begin{aligned} \nabla_\theta\log\pi_\theta(a\mid s) &=-\frac1{2\sigma^2} 2(a-\mu_\theta(s))(-\nabla_\theta\mu_\theta(s))\\ &=\frac{a-\mu_\theta(s)}{\sigma^2} \nabla_\theta\mu_\theta(s). \end{aligned}

For a multivariate Gaussian with fixed covariance Σ\Sigma,

∇θlog⁡πθ(a∣s)=(∇θμθ(s))⊤Σ−1(a−μθ(s)).\nabla_\theta\log\pi_\theta(a\mid s) =\left(\nabla_\theta\mu_\theta(s)\right)^\top \Sigma^{-1}(a-\mu_\theta(s)).

A diagonal Gaussian often learns both μ\mu and ℓ=log⁡σ\ell=\log\sigma. For one dimension,

∂log⁡π∂μ=a−μσ2,\frac{\partial\log\pi}{\partial\mu} =\frac{a-\mu}{\sigma^2},

and

∂log⁡π∂ℓ=(a−μ)2σ2−1.\boxed{ \frac{\partial\log\pi}{\partial\ell} =\frac{(a-\mu)^2}{\sigma^2}-1 }.

An unexpectedly distant, positively weighted action can increase the variance; an action close to the mean can decrease it. The policy learns both where to act and how much randomness to retain.

The Surrogate Loss

Automatic-differentiation libraries minimize scalar losses, while the desired update is gradient ascent:

g^=1M∑j=1MA^j∇θlog⁡πθ(aj∣sj).\widehat g =\frac1M\sum_{j=1}^{M} \widehat A_j \nabla_\theta\log\pi_\theta(a_j\mid s_j).

We define the actor loss

Lactor(θ)=−1M∑j=1Mstopgrad⁡(A^j)log⁡πθ(aj∣sj).\boxed{ \mathcal L_{\mathrm{actor}}(\theta) =-\frac1M\sum_{j=1}^{M} \operatorname{stopgrad}(\widehat A_j) \log\pi_\theta(a_j\mid s_j) }.

Then

−∇θLactor=g^.-\nabla_\theta\mathcal L_{\mathrm{actor}} =\widehat g.

This is a surrogate loss. Its numerical value is not the negative expected return; its gradient is constructed to equal the sampled policy-gradient estimator.

Returns and advantages must be treated as fixed weights during the actor update:

∇θstopgrad⁡(A^t)=0.\nabla_\theta\operatorname{stopgrad}(\widehat A_t)=0.

Otherwise gradients can flow through the critic or return computation in ways not justified by the likelihood-ratio derivation.

For variable-length episodes, flatten the valid time steps or use a mask mt(i)m_t^{(i)}:

Lactor=−∑i,tmt(i)log⁡πθ(at(i)∣st(i))A^t(i)∑i,tmt(i).\mathcal L_{\mathrm{actor}} =- \frac{ \sum_{i,t}m_t^{(i)} \log\pi_\theta(a_t^{(i)}\mid s_t^{(i)}) \widehat A_t^{(i)} }{ \sum_{i,t}m_t^{(i)} }.
Vanilla Policy-Gradient Update\textbf{Vanilla Policy-Gradient Update}Roll out πθ to collect an on-policy batch.Compute discounted reward-to-go Gt backward.ϕ←arg⁡min⁡ϕ∑t(Vϕ(st)−Gt)2.A^t←stopgrad⁡(Gt−Vϕ(st)).Optionally normalize A^t within the batch.Lactor←−1M∑tlog⁡πθ(at∣st)A^t.θ←θ−α∇θLactor.\begin{aligned} &\text{Roll out }\pi_\theta\text{ to collect an on-policy batch}.\\[0.25em] &\text{Compute discounted reward-to-go }G_t\text{ backward}.\\[0.25em] &\phi\leftarrow\arg\min_\phi\sum_t \left(V_\phi(s_t)-G_t\right)^2.\\[0.25em] &\widehat A_t\leftarrow \operatorname{stopgrad}\left(G_t-V_\phi(s_t)\right).\\[0.25em] &\text{Optionally normalize }\widehat A_t\text{ within the batch}.\\[0.25em] &\mathcal L_{\mathrm{actor}} \leftarrow-\frac1M\sum_t \log\pi_\theta(a_t\mid s_t)\widehat A_t.\\[0.25em] &\theta\leftarrow\theta-\alpha\nabla_\theta \mathcal L_{\mathrm{actor}}. \end{aligned}

Why Policy Gradients Are Difficult

REINFORCE is mathematically clean, but its estimator is noisy. Variance comes from stochastic actions, stochastic dynamics, long horizons, delayed rewards, and using one sampled future to estimate many possible futures.

On-policy sample cost. The gradient is an expectation under the current policy:

∇θJ(θ)=Eτ∼pθ[⋯ ].\nabla_\theta J(\theta) =\mathbb E_{\tau\sim p_\theta}[\cdots].

After a substantial update, trajectories from pθoldp_{\theta_{\mathrm{old}}} are no longer distributed according to pθnewp_{\theta_{\mathrm{new}}}. Reusing them naively biases the estimate.

Importance sampling can correct the action-distribution mismatch with

ρt(θ)=πθ(at∣st)πθold(at∣st).\rho_t(\theta) =\frac{\pi_\theta(a_t\mid s_t)} {\pi_{\theta_{\mathrm{old}}}(a_t\mid s_t)}.

But products or large values of these ratios create severe variance. This motivates algorithms that reuse recent data while explicitly limiting policy change.

Sensitivity to step size. A vanilla update is

θk+1=θk+αkg^k.\theta_{k+1}=\theta_k+\alpha_k\widehat g_k.

If αk\alpha_k is too small, learning is slow. If it is too large, the policy can collapse after one batch.

Euclidean parameter distance is not a reliable measure of policy change. A small parameter update may drastically change action probabilities in one part of the state space. Adaptive optimizers and normalized advantages help numerical conditioning, but they do not guarantee a safe change in the represented policy.

The Bridge to Actor-Critic

The Monte Carlo advantage

A^tMC=Gt−Vϕ(st)\widehat A_t^{\mathrm{MC}} =G_t-V_\phi(s_t)

still depends on one complete sampled future. A critic can bootstrap from the next state:

A^tTD=rt+γVϕ(st+1)−Vϕ(st).\boxed{ \widehat A_t^{\mathrm{TD}} =r_t+\gamma V_\phi(s_{t+1})-V_\phi(s_t) }.

This follows from

Qπ(st,at)=E[rt+γVπ(st+1)∣st,at]Q^\pi(s_t,a_t) =\mathbb E \left[r_t+\gamma V^\pi(s_{t+1})\mid s_t,a_t\right]

and

Aπ(st,at)=Qπ(st,at)−Vπ(st).A^\pi(s_t,a_t)=Q^\pi(s_t,a_t)-V^\pi(s_t).

The actor update becomes

∇θJ^=1M∑t∇θlog⁡πθ(at∣st)[rt+γVϕ(st+1)−Vϕ(st)].\boxed{ \widehat{\nabla_\theta J} =\frac1M\sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t) \left[r_t+\gamma V_\phi(s_{t+1})-V_\phi(s_t)\right] }.

The critic averages over possible futures rather than using one full sampled return, so variance is usually much lower. An inaccurate critic introduces bias.

A one-step critic minimizes

LVTD(ϕ)=E[(Vϕ(st)−stopgrad⁡[rt+γVϕ(st+1)])2].\boxed{ \mathcal L_V^{\mathrm{TD}}(\phi) =\mathbb E \left[ \left( V_\phi(s_t) -\operatorname{stopgrad} \left[r_t+\gamma V_\phi(s_{t+1})\right] \right)^2 \right] }.

The target is detached during critic optimization, just as in value-based reinforcement learning.

MethodActor weightBiasVariance
Full-return REINFORCER(τ)R(\tau)UnbiasedVery high
Reward-to-go REINFORCEGtG_tUnbiasedHigh
REINFORCE with baselineGt−Vϕ(st)G_t-V_\phi(s_t)Actor remains unbiasedLower
One-step actor-criticrt+γVϕ(st+1)−Vϕ(st)r_t+\gamma V_\phi(s_{t+1})-V_\phi(s_t)Biased with an inaccurate criticUsually much lower

Actor-critic methods deliberately trade some estimator bias for lower variance. Proximal and trust-region methods address the second major weakness by controlling how far the policy moves during each update.

Practical Diagnosis

SymptomLikely causeUseful response
Returns vary wildly between batchesHigh Monte Carlo varianceUse reward-to-go, a value baseline, and larger batches
Learning reinforces obviously bad actionsNoisy long-horizon creditImprove the baseline or use actor-critic estimates
Policy collapses after one updatePolicy step is too largeLower the learning rate or constrain policy change
Learning stalls with tiny gradientsReward scale or advantage scale is poorNormalize advantages and inspect reward scaling
Old batches hurt performanceOn-policy distribution mismatchDiscard stale data or use explicit importance corrections
Continuous-action variance explodesUnstable standard deviationParameterize log standard deviation and monitor entropy
Actor changes while critic loss backpropagatesMissing gradient separationDetach advantages and use separate actor/critic losses

The entire derivation can be remembered as one chain:

J(θ)=Eτ∼pθ[R(τ)]∇θJ(θ)=E[R(τ)∇θlog⁡pθ(τ)]=E[∑t∇θlog⁡πθ(at∣st)Gt]=E[∑t∇θlog⁡πθ(at∣st)(Gt−b(st))].\begin{aligned} J(\theta) &=\mathbb E_{\tau\sim p_\theta}[R(\tau)]\\ \nabla_\theta J(\theta) &=\mathbb E \left[R(\tau)\nabla_\theta\log p_\theta(\tau)\right]\\ &=\mathbb E \left[ \sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t)G_t \right]\\ &=\mathbb E \left[ \sum_t \nabla_\theta\log\pi_\theta(a_t\mid s_t) \left(G_t-b(s_t)\right) \right]. \end{aligned}

The likelihood-ratio identity makes the environment differentiable in expectation, temporal causality removes past rewards, and a state-dependent baseline removes predictable variation. Replacing the remaining Monte Carlo return with a learned critic produces actor-critic methods.