Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Policy Gradients and Actor-Critic

The previous chapters optimized policies through Q-functions, soft Bellman relations, or path-consistency equations. Can expected return itself supply the policy objective without differentiating the environment dynamics? The score function estimator applies directly to trajectories because the policy terms are the only factors in their probability law that depend on the policy parameters.

The estimator requires only the ability to evaluate and differentiate logπw(as)\log \pi_{\boldsymbol{w}}(a|s), so it also works with discrete actions where reparameterization is unavailable.

Let G(τ)t=0Tr(st,at)G(\tau) \equiv \sum_{t=0}^T r(s_t, a_t) be the sum of undiscounted rewards in a trajectory τ\tau. The stochastic optimization problem we face is to maximize:

J(w)=Eτp(τ;w)[G(τ)]J(\boldsymbol{w}) = \mathbb{E}_{\tau \sim p(\tau;\boldsymbol{w})}[G(\tau)]

where τ=(s0,a0,s1,a1,...)\tau = (s_0,a_0,s_1,a_1,...) is a trajectory and G(τ)G(\tau) is the total return. Applying the score function estimator, we get:

wJ(w)=wEτ[G(τ)]=Eτ[G(τ)wlogp(τ;w)]=Eτ[G(τ)wt=0Tlogπw(atst)]=Eτ[G(τ)t=0Twlogπw(atst)]\begin{align*} \nabla_{\boldsymbol{w}}J(\boldsymbol{w}) &= \nabla_{\boldsymbol{w}}\mathbb{E}_{\tau}[G(\tau)] \\ &= \mathbb{E}_{\tau}\left[G(\tau)\nabla_{\boldsymbol{w}}\log p(\tau;\boldsymbol{w})\right] \\ &= \mathbb{E}_{\tau}\left[G(\tau)\nabla_{\boldsymbol{w}}\sum_{t=0}^T\log \pi_{\boldsymbol{w}}(a_t|s_t)\right] \\ &= \mathbb{E}_{\tau}\left[G(\tau)\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\right] \end{align*}

We have eliminated the need to know the transition probabilities in this estimator since the probability of a trajectory factorizes as:

p(τ;w)=p(s0)t=0Tπw(atst)p(st+1st,at)p(\tau;\boldsymbol{w}) = p(s_0)\prod_{t=0}^T \pi_{\boldsymbol{w}}(a_t|s_t)p(s_{t+1}|s_t,a_t)

Therefore, only the policy depends on w\boldsymbol{w}. When taking the logarithm of this product, we get a sum where all the w\boldsymbol{w}-independent terms vanish. The final estimator samples trajectories under the distribution p(τ;w)p(\tau; \boldsymbol{w}) and computes:

wJ(w)1Ni=1N[G(τ(i))t=0Twlogπw(at(i)st(i))]\nabla_{\boldsymbol{w}}J(\boldsymbol{w}) \approx \frac{1}{N}\sum_{i=1}^N\left[G(\tau^{(i)})\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t^{(i)}|s_t^{(i)})\right]

This is a direct application of the score function estimator. However, we rarely use this form in practice and instead make several improvements to further reduce the variance.

Leveraging Conditional Independence

Given the Markov property of the MDP, rewards rkr_k for k<tk < t are conditionally independent of action ata_t given the history ht=(s0,a0,...,st1,at1,st)h_t = (s_0,a_0,...,s_{t-1},a_{t-1},s_t). This allows us to only need to consider future rewards when computing policy gradients.

wJ(w)=Eτ[t=0Twlogπw(atst)k=0Trk]=Eτ[t=0Twlogπw(atst)(k=0t1rk+k=tTrk)]=Eτ[t=0Twlogπw(atst)k=tTrk]\begin{align*} \nabla_{\boldsymbol{w}}J(\boldsymbol{w}) &= \mathbb{E}_{\tau}\left[\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\sum_{k=0}^T r_k\right] \\ &= \mathbb{E}_{\tau}\left[\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\left(\sum_{k=0}^{t-1} r_k + \sum_{k=t}^T r_k\right)\right] \\ &= \mathbb{E}_{\tau}\left[\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\sum_{k=t}^T r_k\right] \end{align*}

The conditional independence assumption means that the term Eτ[t=0Twlogπw(atst)k=0t1rk]\mathbb{E}_{\tau}\left[\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\sum_{k=0}^{t-1} r_k \right] vanishes. To see this, factor the trajectory distribution as:

p(τ)=p(s0,...,st,a0,...,at1)πw(atst)p(st+1,...,sT,at+1,...,aTst,at)p(\tau) = p(s_0,...,s_t,a_0,...,a_{t-1}) \pi_{\boldsymbol{w}}(a_t|s_t) p(s_{t+1},...,s_T,a_{t+1},...,a_T|s_t,a_t)

We can now re-write a single term of this summation as:

Eτ[wlogπw(atst)k=0t1rk]=Es0:t,a0:t1[k=0t1rkEat[wlogπw(atst)]]\mathbb{E}_{\tau}\left[\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\sum_{k=0}^{t-1} r_k\right] = \mathbb{E}_{s_{0:t},a_{0:t-1}}\left[\sum_{k=0}^{t-1} r_k \, \mathbb{E}_{a_t}\left[\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\right]\right]

The inner expectation is zero because

Eat[wlogπw(atst)]=wlogπw(atst)πw(atst)dat=wπw(atst)πw(atst)πw(atst)dat=wπw(atst)dat=wπw(atst)dat=w1=0\begin{align*} \mathbb{E}_{a_t}\left[\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\right] &= \int \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\pi_{\boldsymbol{w}}(a_t|s_t)da_t \\ &= \int \frac{\nabla_{\boldsymbol{w}}\pi_{\boldsymbol{w}}(a_t|s_t)}{\pi_{\boldsymbol{w}}(a_t|s_t)}\pi_{\boldsymbol{w}}(a_t|s_t)da_t \\ &= \int \nabla_{\boldsymbol{w}}\pi_{\boldsymbol{w}}(a_t|s_t)da_t \\ &= \nabla_{\boldsymbol{w}}\int \pi_{\boldsymbol{w}}(a_t|s_t)da_t \\ &= \nabla_{\boldsymbol{w}}1 = 0 \end{align*}

The Monte Carlo estimator becomes:

wJ(w)1Ni=1N[t=0Twlogπw(at(i)st(i))k=tTrk(i)]\nabla_{\boldsymbol{w}}J(\boldsymbol{w}) \approx \frac{1}{N}\sum_{i=1}^N\left[\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t^{(i)}|s_t^{(i)})\sum_{k=t}^T r_k^{(i)}\right]

This gives us the REINFORCE algorithm:

The benefit of this estimator compared to the naive one (which would weight each score function by the full trajectory return G(τ)G(\tau)) is that it generally has less variance. This variance reduction arises from the conditional independence structure we exploited: past rewards do not depend on future actions. More formally, this estimator is an instance of a variance reduction technique known as the Extended Conditional Monte Carlo Method.

The Surrogate Loss Perspective

The algorithm above computes a gradient estimate g^\hat{g} explicitly. In practice, implementations using automatic differentiation frameworks take a different approach: they define a surrogate loss whose gradient matches the REINFORCE estimator. For a single trajectory, consider:

Lsurrogate(w)=t=0Tlogπw(atst)GtL_{\text{surrogate}}(\boldsymbol{w}) = -\sum_{t=0}^T \log \pi_{\boldsymbol{w}}(a_t|s_t) \, G_t

where the returns GtG_t and actions ata_t are treated as fixed constants (detached from the computation graph). Taking the gradient with respect to w\boldsymbol{w}:

wLsurrogate=t=0Twlogπw(atst)Gt\nabla_{\boldsymbol{w}} L_{\text{surrogate}} = -\sum_{t=0}^T \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t) \, G_t

Minimizing this surrogate loss via gradient descent yields the same update as maximizing expected return via REINFORCE. The negative sign converts our maximization problem into a minimization suitable for standard optimizers.

This surrogate loss is not the expected return J(w)J(\boldsymbol{w}) we are trying to maximize. It is a computational device that produces the correct gradient at the current parameter values. Several properties distinguish it from a true loss function:

  1. It changes each iteration. The returns GtG_t come from trajectories sampled under the current policy. After updating w\boldsymbol{w}, we must collect new trajectories and construct a new surrogate loss.

  2. Its value is not meaningful. Unlike supervised learning where the loss measures prediction error, the numerical value of LsurrogateL_{\text{surrogate}} has no direct interpretation. Only its gradient matters.

  3. It is valid only locally. The surrogate loss provides the correct gradient only at the parameters used to collect the data. Moving far from those parameters invalidates the gradient estimate.

This perspective explains why policy gradient code often looks different from the pseudocode above. Instead of computing g^\hat{g} explicitly, implementations define the surrogate loss and call loss.backward():

# Surrogate loss implementation (single trajectory)
log_probs = [policy.log_prob(a_t, s_t) for s_t, a_t in trajectory]
returns = compute_returns(rewards)
surrogate_loss = -sum(lp * G for lp, G in zip(log_probs, returns))
surrogate_loss.backward()  # computes REINFORCE gradient
optimizer.step()

Variance Reduction via Control Variates

Recall that the REINFORCE gradient estimator, after leveraging conditional independence, takes the form:

wJ(w)1Ni=1N[t=0Twlogπw(at(i)st(i))k=tTrk(i)]\nabla_{\boldsymbol{w}}J(\boldsymbol{w}) \approx \frac{1}{N}\sum_{i=1}^N\left[\sum_{t=0}^T\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t^{(i)}|s_t^{(i)})\sum_{k=t}^T r_k^{(i)}\right]

This is a sum over trajectories and timesteps. The gradient contribution at timestep tt of trajectory ii is:

wlogπw(at(i)st(i))k=tTrk(i)\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t^{(i)}|s_t^{(i)})\sum_{k=t}^T r_k^{(i)}

While unbiased, this estimator suffers from high variance because the return k=tTrk\sum_{k=t}^T r_k can vary significantly across trajectories even for the same state-action pair. The control variate method provides a principled way to reduce this variance.

General Control Variate Theory

For a general estimator ZZ of some quantity μ=E[Z]\mu = \mathbb{E}[Z], and a control variate CC with known expectation E[C]=0\mathbb{E}[C]=0, we can construct:

Zcv=ZαCZ_{\text{cv}} = Z - \alpha C

This remains unbiased since E[Zcv]=E[Z]αE[C]=E[Z]\mathbb{E}[Z_{\text{cv}}] = \mathbb{E}[Z] - \alpha\mathbb{E}[C] = \mathbb{E}[Z]. The variance is:

Var(Zcv)=Var(Z)+α2Var(C)2αCov(Z,C)\text{Var}(Z_{\text{cv}}) = \text{Var}(Z) + \alpha^2\text{Var}(C) - 2\alpha\text{Cov}(Z,C)

The 2αCov(Z,C)-2\alpha\text{Cov}(Z,C) term is what enables variance reduction. If ZZ and CC are positively correlated, we can choose α>0\alpha > 0 to make this term negative and large in magnitude, reducing the overall variance. However, the α2Var(C)\alpha^2\text{Var}(C) term grows quadratically with α\alpha, so if we make α\alpha too large, this quadratic term will eventually dominate and the variance will increase rather than decrease. The variance as a function of α\alpha is a parabola opening upward, with a unique minimum. Setting ddαVar(Zcv)=0\frac{d}{d\alpha}\text{Var}(Z_{\text{cv}}) = 0 gives:

α=Cov(Z,C)Var(C)\alpha^* = \frac{\text{Cov}(Z,C)}{\text{Var}(C)}

This is the coefficient from ordinary least squares regression: we predict the estimator ZZ using the control variate CC as the predictor. Since E[C]=0\mathbb{E}[C] = 0, the linear model is ZE[Z]+αCZ \approx \mathbb{E}[Z] + \alpha^* C, where α\alpha^* is the OLS slope coefficient. The control variate estimator Zcv=ZαCZ_{\text{cv}} = Z - \alpha^* C computes the residual: the part of ZZ that cannot be explained by CC.

Substituting α\alpha^* into the variance formula yields:

Var(Zcv)=Var(Z)[Cov(Z,C)]2Var(C)=(1R2)Var(Z)\text{Var}(Z_{\text{cv}}) = \text{Var}(Z) - \frac{[\text{Cov}(Z,C)]^2}{\text{Var}(C)} = (1 - R^2) \text{Var}(Z)

where R2=[Cov(Z,C)]2Var(Z)Var(C)R^2 = \frac{[\text{Cov}(Z,C)]^2}{\text{Var}(Z)\text{Var}(C)} is the coefficient of determination from regressing ZZ on CC. The variance reduction is R2Var(Z)R^2 \text{Var}(Z): the better CC predicts ZZ, the more variance we eliminate.

Application to REINFORCE

In the reinforcement learning setting, our REINFORCE gradient estimator is a sum over timesteps: t=0TZt\sum_{t=0}^T Z_t where each ZtZ_t represents the gradient contribution at timestep tt. We apply control variates separately to each term. Since Var(tZt)=tVar(Zt)+tsCov(Zt,Zs)\text{Var}(\sum_t Z_t) = \sum_t \text{Var}(Z_t) + \sum_{t \neq s} \text{Cov}(Z_t, Z_s), reducing the variance of each ZtZ_t reduces the total variance, though we do not explicitly address the cross-timestep covariance terms.

For a given trajectory at state sts_t, the gradient contribution at time tt is:

Zt=wlogπw(atst)k=tTrkZ_t = \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\sum_{k=t}^T r_k

This is the product of the score function wlogπw(atst)\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t) and the return-to-go k=tTrk\sum_{k=t}^T r_k. We can subtract any state-dependent function b(st)b(s_t) from the return without introducing bias, as long as b(st)b(s_t) does not depend on ata_t. This is because:

Eatπw(st)[wlogπw(atst)b(st)]=b(st)Eat[wlogπw(atst)]=0\mathbb{E}_{a_t \sim \pi_{\boldsymbol{w}}(\cdot|s_t)}\left[\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)b(s_t)\right] = b(s_t)\mathbb{E}_{a_t}\left[\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\right] = 0

where the last equality follows from the score function identity (8).

We can now define our control variate as:

Ct=wlogπw(atst)b(st)C_t = \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t) \cdot b(s_t)

where b(st)b(s_t) is a baseline function that depends only on the state. This satisfies E[Ctst]=0\mathbb{E}[C_t|s_t] = 0. Our control variate estimator becomes:

Zt,cv=ZtCt=wlogπw(atst)(k=tTrkb(st))Z_{t,\text{cv}} = Z_t - C_t = \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\left(\sum_{k=t}^T r_k - b(s_t)\right)

The optimal baseline b(st)b^*(s_t) minimizes the variance. To find it, consider the scalar parameter case for simplicity. Write g(at)wlogπw(atst)g(a_t) \equiv \nabla_w \log \pi_w(a_t|s_t) and Gt=k=tTrkG_t = \sum_{k=t}^T r_k. We want to minimize:

b(st)=argminbVaratπw(st)[g(at)(Gtb)]b^*(s_t) = \arg\min_{b} \text{Var}_{a_t \sim \pi_{\boldsymbol{w}}(\cdot|s_t)}\left[g(a_t)(G_t - b)\right]

Since the mean does not depend on bb, minimizing the variance is equivalent to minimizing the second moment E[g(at)2(Gtb)2st]\mathbb{E}[g(a_t)^2(G_t - b)^2|s_t]. Expanding and taking the derivative with respect to bb gives:

b(st)=Eatst[g(at)2Gt]Eatst[g(at)2]b^*(s_t) = \frac{\mathbb{E}_{a_t|s_t}\left[g(a_t)^2 G_t\right]}{\mathbb{E}_{a_t|s_t}\left[g(a_t)^2\right]}

For vector-valued parameters w\boldsymbol{w}, we minimize a scalar proxy such as the trace of the covariance matrix, which yields the same formula with wlogπw(atst)2\|\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\|^2 in place of g(at)2g(a_t)^2:

b(st)=Eatst[wlogπw(atst)2Gt]Eatst[wlogπw(atst)2]b^*(s_t) = \frac{\mathbb{E}_{a_t|s_t}\left[\|\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\|^2 G_t\right]}{\mathbb{E}_{a_t|s_t}\left[\|\nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\|^2\right]}

This is the exact optimal baseline: a weighted average of returns where the weights are the squared norms of the score function. In practice, we treat the squared norm as roughly constant across actions at a given state, which leads to the simpler and widely used choice:

b(st)E[Gtst]=vπw(st)b(s_t) \approx \mathbb{E}[G_t|s_t] = v^{\pi_{\boldsymbol{w}}}(s_t)

With this approximation, the variance-reduced gradient contribution at timestep tt becomes:

Zcv,t=wlogπw(atst)(k=tTrkvπw(st))Z_{\text{cv},t} = \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\left(\sum_{k=t}^T r_k - v^{\pi_{\boldsymbol{w}}}(s_t)\right)

The term in parentheses is exactly the advantage function: Aπw(st,at)=qπw(st,at)vπw(st)A^{\pi_{\boldsymbol{w}}}(s_t, a_t) = q^{\pi_{\boldsymbol{w}}}(s_t, a_t) - v^{\pi_{\boldsymbol{w}}}(s_t), where the Q-function is approximated by the Monte Carlo return k=tTrk\sum_{k=t}^T r_k. The full gradient estimate for a trajectory is then the sum over all timesteps:

g^=t=0TZcv,t=t=0Twlogπw(atst)(Gtvπw(st))\hat{g} = \sum_{t=0}^T Z_{\text{cv},t} = \sum_{t=0}^T \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t)\left(G_t - v^{\pi_{\boldsymbol{w}}}(s_t)\right)

In practice, we do not have access to the true value function and must learn it. Unlike the methods in the amortization chapter, where we learned value functions to approximate the optimal Q-function, here our goal is policy evaluation: estimating the value of the current policy πw\pi_{\boldsymbol{w}}. The same function approximation techniques apply, but we target vπwv^{\pi_{\boldsymbol{w}}} rather than vv^*. The simplest approach is to regress from states to Monte Carlo returns, learning what Williams (1992) called a “baseline”:

When implementing this algorithm nowadays, we always use mini-batching to make full use of our GPUs. Therefore, a more representative variant for this algorithm would be:

The value function is trained by regressing states directly to their sampled Monte Carlo returns GG. Advantage normalization (step 2.5) is not part of the optimal baseline derivation but improves optimization in practice and is standard in modern implementations.

Generalized Advantage Estimation

The baseline construction gave us a gradient estimator of the form:

wJ(w)1Ni=1Nt=0Twlogπw(at(i)st(i))(Gt(i)v(st(i)))\nabla_{\boldsymbol{w}}J(\boldsymbol{w}) \approx \frac{1}{N}\sum_{i=1}^N \sum_{t=0}^T \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t^{(i)}|s_t^{(i)}) \left(G_t^{(i)} - v(s_t^{(i)})\right)

where Gt=k=tTrkG_t = \sum_{k=t}^T r_k is the Monte Carlo return from time tt. For each visited state-action pair (st,at)(s_t, a_t), the term in parentheses

A^tMC=Gtv(st)\widehat{A}_t^{\text{MC}} = G_t - v(s_t)

is a Monte Carlo estimate of the advantage Aπ(st,at)=qπ(st,at)vπ(st)A^{\pi}(s_t, a_t) = q^{\pi}(s_t, a_t) - v^{\pi}(s_t). If the baseline equals the true value function, v=vπv = v^{\pi}, then E[A^tMCst,at]=Aπ(st,at)\mathbb{E}[\widehat{A}_t^{\text{MC}} | s_t, a_t] = A^{\pi}(s_t, a_t), so this estimator is unbiased.

However, as an estimator it has two limitations. First, it has high variance because GtG_t depends on all future rewards. Second, it uses the value function only as a baseline, not as a predictor of long-term returns. We essentially discard the information in v(st+1),v(st+2),v(s_{t+1}), v(s_{t+2}), \ldots

GAE addresses these issues by constructing a family of estimators that interpolate between pure Monte Carlo and pure bootstrapping. A parameter λ\lambda controls the bias-variance tradeoff.

Decomposing the Monte Carlo Advantage

Fix a value function v(s)v(s) (not necessarily equal to vπv^{\pi}) and define the one-step residual:

δt=rt+γv(st+1)v(st)\delta_t = r_t + \gamma v(s_{t+1}) - v(s_t)

Start from the Monte Carlo advantage and add and subtract γv(st+1)\gamma v(s_{t+1}):

Gtv(st)=rt+γGt+1v(st)=rt+γv(st+1)v(st)+γ(Gt+1v(st+1))=δt+γ(Gt+1v(st+1))\begin{align*} G_t - v(s_t) &= r_t + \gamma G_{t+1} - v(s_t) \\ &= r_t + \gamma v(s_{t+1}) - v(s_t) + \gamma(G_{t+1} - v(s_{t+1})) \\ &= \delta_t + \gamma(G_{t+1} - v(s_{t+1})) \end{align*}

Applying this decomposition recursively yields:

Gtv(st)=l=0Ttγlδt+lG_t - v(s_t) = \sum_{l=0}^{T-t} \gamma^l \delta_{t+l}

The Monte Carlo advantage is exactly the discounted sum of future residuals. This is an algebraic identity, not an approximation.

The sequence {δt+l}l0\{\delta_{t+l}\}_{l \geq 0} provides incremental corrections to the value function as we move forward in time. The term δt\delta_t depends only on (st,at,st+1)(s_t, a_t, s_{t+1}); δt+1\delta_{t+1} depends on (st+1,at+1,st+2)(s_{t+1}, a_{t+1}, s_{t+2}), and so on. As ll increases, the corrections become more noisy (they depend on more random outcomes) and more sensitive to errors in the value function at later states. Although the full sum is unbiased when v=vπv = v^{\pi}, it can have high variance and can be badly affected by approximation error in vv.

GAE as a Shrinkage Estimator

The decomposition above suggests a family of estimators that downweight residuals farther in the future. Let λ[0,1]\lambda \in [0,1] and define:

Atλ=l=0Tt(γλ)lδt+lA_t^{\lambda} = \sum_{l=0}^{T-t} (\gamma\lambda)^l \delta_{t+l}

This is the generalized advantage estimator AtGAE(γ,λ)A_t^{\text{GAE}(\gamma,\lambda)}.

Two special cases illustrate the extremes. When λ=1\lambda = 1, we recover the Monte Carlo advantage:

Atλ=1=l=0Ttγlδt+l=Gtv(st)A_t^{\lambda=1} = \sum_{l=0}^{T-t} \gamma^l \delta_{t+l} = G_t - v(s_t)

When λ=0\lambda = 0, we keep only the immediate residual:

Atλ=0=δt=rt+γv(st+1)v(st)A_t^{\lambda=0} = \delta_t = r_t + \gamma v(s_{t+1}) - v(s_t)

Intermediate values 0<λ<10 < \lambda < 1 interpolate between these extremes. The influence of δt+l\delta_{t+l} decays geometrically as (γλ)l(\gamma\lambda)^l. The parameter λ\lambda acts as a shrinkage parameter: small λ\lambda shrinks the estimator toward the one-step residual; large λ\lambda allows the estimator to behave more like the Monte Carlo advantage.

If v=vπv = v^{\pi} is the true value function, then E[δtst,at]=Aπ(st,at)\mathbb{E}[\delta_t | s_t, a_t] = A^{\pi}(s_t, a_t) and E[δt+lst,at]=0\mathbb{E}[\delta_{t+l} | s_t, a_t] = 0 for l1l \geq 1. In this case:

E[Atλst,at]=l=0Tt(γλ)lE[δt+lst,at]=Aπ(st,at)\mathbb{E}[A_t^{\lambda} | s_t, a_t] = \sum_{l=0}^{T-t} (\gamma\lambda)^l \mathbb{E}[\delta_{t+l} | s_t, a_t] = A^{\pi}(s_t, a_t)

for all λ[0,1]\lambda \in [0,1]. When the value function is exact, GAE is unbiased regardless of λ\lambda; changing λ\lambda only affects variance.

In practice, we approximate vπv^{\pi} with a function approximator, and the residuals δt+l\delta_{t+l} inherit approximation error. Distant residuals involve multiple applications of the approximate value function and are more contaminated by modeling error. Downweighting them (choosing λ<1\lambda < 1) introduces bias but can reduce variance and limit the impact of those errors.

Mixture of Multi-Step Estimators

Another perspective on GAE comes from multi-step returns. Define the kk-step return from time tt:

Gt(k)=l=0k1γlrt+l+γkv(st+k)G_t^{(k)} = \sum_{l=0}^{k-1} \gamma^l r_{t+l} + \gamma^k v(s_{t+k})

and the corresponding kk-step advantage estimator At(k)=Gt(k)v(st)A_t^{(k)} = G_t^{(k)} - v(s_t). Each At(k)A_t^{(k)} uses kk rewards before bootstrapping; larger kk means more variance but less bootstrapping error.

The GAE estimator can be written as a geometric mixture:

Atλ=(1λ)k=1Ttλk1At(k)A_t^{\lambda} = (1-\lambda) \sum_{k=1}^{T-t} \lambda^{k-1} A_t^{(k)}

GAE is a weighted average of the kk-step advantage estimators, with shorter horizons weighted more heavily when λ\lambda is small.

Using GAE in the Policy Gradient

Once we choose λ\lambda, we plug AtλA_t^{\lambda} in place of Gtv(st)G_t - v(s_t) in the policy gradient estimator:

wJ(w)1Ni=1Nt=0Twlogπw(at(i)st(i))Atλ,(i)\nabla_{\boldsymbol{w}}J(\boldsymbol{w}) \approx \frac{1}{N}\sum_{i=1}^N \sum_{t=0}^T \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t^{(i)}|s_t^{(i)}) A_t^{\lambda,(i)}

We still use a control variate to reduce variance (the baseline vv), but now we construct the advantage target by smoothing the sequence of residuals {δt}\{\delta_t\} with a geometrically decaying kernel.

For the value function, it is convenient to define the λ\lambda-return:

Gtλ=Atλ+v(st)G_t^{\lambda} = A_t^{\lambda} + v(s_t)

When λ=1\lambda = 1, GtλG_t^{\lambda} reduces to the Monte Carlo return; when λ=0\lambda = 0, it becomes the one-step bootstrapped target rt+γv(st+1)r_t + \gamma v(s_{t+1}).

When λ=1\lambda = 1, this reduces (up to advantage normalization) to the Monte Carlo baseline algorithm earlier in the chapter. When λ=0\lambda = 0, advantages become the one-step residuals δt\delta_t, and the λ\lambda-returns reduce to standard one-step bootstrapped targets.

Actor-Critic as the λ=0\lambda = 0 Limit

The case λ=0\lambda = 0 is particularly simple. The advantage becomes:

Atλ=0=δt=rt+γv(st+1)v(st)A_t^{\lambda=0} = \delta_t = r_t + \gamma v(s_{t+1}) - v(s_t)

and the policy update reduces to:

ww+αwwlogπw(atst)δt\boldsymbol{w} \leftarrow \boldsymbol{w} + \alpha_w \nabla_{\boldsymbol{w}}\log \pi_{\boldsymbol{w}}(a_t|s_t) \delta_t

while the value update becomes a standard one-step regression toward rt+γv(st+1)r_t + \gamma v(s_{t+1}). This gives the online actor-critic algorithm:

This algorithm was derived by Sutton in his 1984 thesis as an “adaptive heuristic” for temporal credit assignment. In the language of this chapter, it is the λ=0\lambda = 0 member of the GAE family: it uses the most local residual δt\delta_t as both the target for the value function and the advantage estimate for the policy gradient.

Likelihood Ratio Methods in Reinforcement Learning

How do importance ratios and constrained or clipped surrogates reuse trajectories collected under an earlier policy?

The score function estimator from the previous section is a special case of the likelihood ratio method where the proposal distribution equals the target distribution. We now consider the general case where they differ.

Recall the likelihood ratio gradient estimator from the beginning of this chapter. For objective J(θ)=Exp(x;θ)[f(x)]J(\theta) = \mathbb{E}_{x \sim p(x;\theta)}[f(x)] and any proposal distribution q(x)q(x):

θJ(θ)=Exq(x)[f(x)ρ(x;q,θ)θlogp(x;θ)]\nabla_\theta J(\theta) = \mathbb{E}_{x \sim q(x)}\left[f(x) \rho(x; q, \theta) \nabla_\theta \log p(x;\theta)\right]

where ρ(x;q,θ)=p(x;θ)q(x)\rho(x; q, \theta) = \frac{p(x;\theta)}{q(x)} is the likelihood ratio. The partial derivative ρθ=ρθlogp\frac{\partial \rho}{\partial \theta} = \rho \nabla_\theta \log p holds because xx is treated as fixed, having been sampled from qq, which does not depend on θ\theta.

In reinforcement learning, let x=τx = \tau be a trajectory, f(τ)=G(τ)f(\tau) = G(\tau) the return, p(τ;w)p(\tau;\boldsymbol{w}) the trajectory distribution under policy πw\pi_{\boldsymbol{w}}, and q(τ)q(\tau) the trajectory distribution under some other policy πq\pi_q. The gradient becomes:

wJ(w)=Eτπq[G(τ)ρ(τ)t=0Twlogπw(atst)]\nabla_{\boldsymbol{w}} J(\boldsymbol{w}) = \mathbb{E}_{\tau \sim \pi_q}\left[G(\tau) \rho(\tau) \sum_{t=0}^T \nabla_{\boldsymbol{w}} \log \pi_{\boldsymbol{w}}(a_t|s_t)\right]

where the trajectory likelihood ratio simplifies because transition probabilities cancel:

ρ(τ)=p(τ;w)q(τ)=t=0Tπw(atst)πq(atst)=t=0Tρt\rho(\tau) = \frac{p(\tau;\boldsymbol{w})}{q(\tau)} = \prod_{t=0}^T \frac{\pi_{\boldsymbol{w}}(a_t|s_t)}{\pi_q(a_t|s_t)} = \prod_{t=0}^T \rho_t

This product of T+1T+1 ratios can become extremely large or small as TT grows, leading to high variance. The temporal structure provides some relief: since Eaπq[ρ]=1\mathbb{E}_{a \sim \pi_q}[\rho] = 1, future ratios ρk\rho_{k} for k>tk > t that do not affect the reward rtr_t can be marginalized out. However, past ratios ρ0:t1\rho_{0:t-1} are still needed to correctly weight the probability of reaching state sts_t.

In practice, algorithms like PPO and TRPO make an additional approximation: they use only the per-step ratio ρt\rho_t rather than the cumulative product ρ0:t\rho_{0:t}. This ignores the mismatch between the state distributions induced by the two policies. Combined with a baseline b(st)b(s_t), the approximate estimator is:

wJ(w)Eτπq[t=0Tρtwlogπw(atst)(Gtb(st))]\nabla_{\boldsymbol{w}} J(\boldsymbol{w}) \approx \mathbb{E}_{\tau \sim \pi_q}\left[\sum_{t=0}^T \rho_t \nabla_{\boldsymbol{w}} \log \pi_{\boldsymbol{w}}(a_t|s_t) (G_t - b(s_t))\right]

This approximation corresponds to maximizing the importance-weighted surrogate objective:

LIS(w)=Eτπq[t=0TρtAt]L^{\text{IS}}(\boldsymbol{w}) = \mathbb{E}_{\tau \sim \pi_q}\left[\sum_{t=0}^T \rho_t A_t\right]

where At=Gtb(st)A_t = G_t - b(s_t). Taking the gradient with respect to w\boldsymbol{w}, only ρt\rho_t depends on w\boldsymbol{w} (since trajectories are sampled from πq\pi_q):

wLIS(w)=Eτπq[t=0TAtwρt]\nabla_{\boldsymbol{w}} L^{\text{IS}}(\boldsymbol{w}) = \mathbb{E}_{\tau \sim \pi_q}\left[\sum_{t=0}^T A_t \nabla_{\boldsymbol{w}} \rho_t\right]

The gradient of the ratio is:

wρt=wπw(atst)πq(atst)=wπw(atst)πq(atst)=ρtwlogπw(atst)\nabla_{\boldsymbol{w}} \rho_t = \nabla_{\boldsymbol{w}} \frac{\pi_{\boldsymbol{w}}(a_t|s_t)}{\pi_q(a_t|s_t)} = \frac{\nabla_{\boldsymbol{w}} \pi_{\boldsymbol{w}}(a_t|s_t)}{\pi_q(a_t|s_t)} = \rho_t \nabla_{\boldsymbol{w}} \log \pi_{\boldsymbol{w}}(a_t|s_t)

Substituting back:

wLIS(w)=Eτπq[t=0Tρtwlogπw(atst)At]\nabla_{\boldsymbol{w}} L^{\text{IS}}(\boldsymbol{w}) = \mathbb{E}_{\tau \sim \pi_q}\left[\sum_{t=0}^T \rho_t \nabla_{\boldsymbol{w}} \log \pi_{\boldsymbol{w}}(a_t|s_t) A_t\right]

This matches equation (46). When πq=πw\pi_q = \pi_{\boldsymbol{w}}, the ratios ρt=1\rho_t = 1 and we recover the score function estimator. The approximation error grows as the policies diverge, which motivates the trust region and clipping mechanisms discussed below.

Variance and the Dominance Condition

The ratio ρt=πw(atst)/πq(atst)\rho_t = \pi_{\boldsymbol{w}}(a_t|s_t)/\pi_q(a_t|s_t) is well-behaved only when the two policies are similar. If πw\pi_{\boldsymbol{w}} assigns high probability to an action where πq\pi_q assigns low probability, the ratio explodes. For example, if πq(as)=0.01\pi_q(a|s) = 0.01 and πw(as)=0.5\pi_{\boldsymbol{w}}(a|s) = 0.5, then ρ=50\rho = 50, amplifying any noise in the advantage estimate.

Importance sampling also requires the dominance condition: the support of πw\pi_{\boldsymbol{w}} must be contained in the support of πq\pi_q. If πw(as)>0\pi_{\boldsymbol{w}}(a|s) > 0 but πq(as)=0\pi_q(a|s) = 0, the ratio is undefined. Stochastic policies typically have full support, but the ratio can still become arbitrarily large as πq(as)0\pi_q(a|s) \to 0.

A common use case is to set πq=πwold\pi_q = \pi_{\boldsymbol{w}_{\text{old}}}, a previous version of the policy. This allows reusing data across multiple gradient steps: collect trajectories once, then update w\boldsymbol{w} several times. But each update moves w\boldsymbol{w} further from wold\boldsymbol{w}_{\text{old}}, making the ratios more extreme. Eventually, the gradient signal is dominated by a few samples with large weights.

Proximal Policy Optimization

The variance issues suggest a natural solution: keep the ratio ρt\rho_t close to 1 by ensuring the new policy stays close to the behavior policy. This keeps the importance-weighted surrogate LIS(w)L^{\text{IS}}(\boldsymbol{w}) from (47) well-behaved.

Trust Region Policy Optimization (TRPO) formalizes this by adding a constraint on the KL divergence between the old and new policies:

maxwLIS(w)subject toEs[DKL(πwold(s)πw(s))]δ\max_{\boldsymbol{w}} L^{\text{IS}}(\boldsymbol{w}) \quad \text{subject to} \quad \mathbb{E}_s\left[D_{\text{KL}}(\pi_{\boldsymbol{w}_{\text{old}}}(\cdot|s) \| \pi_{\boldsymbol{w}}(\cdot|s))\right] \leq \delta

The KL constraint ensures that the two distributions remain similar, which bounds how extreme the importance weights can become. This is a constrained optimization problem, and one could in principle apply standard methods such as projected gradient descent or augmented Lagrangian approaches (as discussed in the trajectory optimization chapter). TRPO takes a different approach: it uses a second-order Taylor approximation of the KL constraint around the current parameters and solves the resulting trust region subproblem using conjugate gradient methods. This involves computing the Fisher information matrix (the Hessian of the KL divergence), which adds computational overhead.

Proximal Policy Optimization (PPO) achieves similar behavior through a simpler mechanism: rather than constraining the distributions to be similar, it directly clips the ratio ρt\rho_t to prevent it from moving too far from 1. This is a construction-level guarantee rather than an optimization-level constraint.

From Trajectory Expectations to State-Action Averages

Before defining the PPO objective, we need to clarify the relationship between the trajectory-level surrogate (47) and the state-action level objective that PPO actually optimizes. The importance-weighted surrogate is defined as an expectation over trajectories:

LIS(w)=Eτπq[t=0TρtAt]L^{\text{IS}}(\boldsymbol{w}) = \mathbb{E}_{\tau \sim \pi_q}\left[\sum_{t=0}^T \rho_t A_t\right]

We can rewrite this as an expectation over state-action pairs by introducing a sampling distribution. For a finite horizon TT, define the averaged time-marginal distribution:

ξπq(s,a)=1T+1t=0Tdtπq(s)πq(as)\xi_{\pi_q}(s, a) = \frac{1}{T+1} \sum_{t=0}^{T} d_t^{\pi_q}(s) \pi_q(a|s)

where dtπq(s)d_t^{\pi_q}(s) is the probability of being in state ss at time tt when following policy πq\pi_q from the initial distribution. This is the uniform mixture over the time-indexed state-action distributions: we pick a timestep tt uniformly at random from {0,1,,T}\{0, 1, \ldots, T\}, then sample (s,a)(s, a) from the joint distribution at that timestep.

With this definition, the trajectory expectation becomes:

Eτπq[t=0TρtAt]=(T+1)E(s,a)ξπq[ρ(s,a)A(s,a)]\mathbb{E}_{\tau \sim \pi_q}\left[\sum_{t=0}^T \rho_t A_t\right] = (T+1) \cdot \mathbb{E}_{(s,a) \sim \xi_{\pi_q}}\left[\rho(s, a) A(s, a)\right]

The factor (T+1)(T+1) is just a constant that does not affect the optimization. This reformulation shows that the importance-weighted surrogate is equivalent to an expectation over state-action pairs drawn from the averaged time-marginal distribution. This is not a stationary distribution or a discounted visitation distribution, but the empirical mixture induced by the finite-horizon rollout procedure.

The Clipped Surrogate Objective

PPO replaces the linear importance-weighted term ρA\rho A with a clipped version. For a state-action pair (s,a)(s, a) with advantage AA and importance ratio ρ(w)=πw(as)/πwold(as)\rho(\boldsymbol{w}) = \pi_{\boldsymbol{w}}(a|s) / \pi_{\boldsymbol{w}_{\text{old}}}(a|s), define the per-sample clipped objective:

CLIP(w;s,a,A)=min(ρ(w)A,clip(ρ(w),1ϵ,1+ϵ)A)\ell^{\text{CLIP}}(\boldsymbol{w}; s, a, A) = \min\left(\rho(\boldsymbol{w}) A, \, \text{clip}(\rho(\boldsymbol{w}), 1-\epsilon, 1+\epsilon) A\right)

where ϵ\epsilon is a hyperparameter (typically 0.1 or 0.2) and clip(x,a,b)=max(a,min(x,b))\text{clip}(x, a, b) = \max(a, \min(x, b)) restricts xx to the interval [a,b][a, b].

The population-level PPO objective is then:

LCLIP(w)=E(s,a,A)ξπwold[CLIP(w;s,a,A)]L^{\text{CLIP}}(\boldsymbol{w}) = \mathbb{E}_{(s,a,A) \sim \xi_{\pi_{\boldsymbol{w}_{\text{old}}}}}\left[\ell^{\text{CLIP}}(\boldsymbol{w}; s, a, A)\right]

where the expectation is taken over the averaged time-marginal distribution (53) induced by πwold\pi_{\boldsymbol{w}_{\text{old}}}.

In practice, we never compute this expectation exactly. Instead, we collect a batch of transitions D={(st(i),at(i),At(i))}\mathcal{D} = \{(s_t^{(i)}, a_t^{(i)}, A_t^{(i)})\} by running πwold\pi_{\boldsymbol{w}_{\text{old}}} and approximate the expectation with an empirical average:

L^CLIP(w;D)=1D(s,a,A)DCLIP(w;s,a,A)\hat{L}^{\text{CLIP}}(\boldsymbol{w}; \mathcal{D}) = \frac{1}{|\mathcal{D}|} \sum_{(s,a,A) \in \mathcal{D}} \ell^{\text{CLIP}}(\boldsymbol{w}; s, a, A)

This is the same plug-in approximation used in fitted Q-iteration: replace the unknown population distribution with the empirical distribution P^D\hat{P}_{\mathcal{D}} induced by the collected batch, then compute the sample average. The empirical surrogate L^CLIP\hat{L}^{\text{CLIP}} is simply an expectation under P^D\hat{P}_{\mathcal{D}}. No assumptions about stationarity or discounted visitation are needed. We just average over the transitions we collected.

Intuition for the Clipping Mechanism

The min\min operator in (55) selects the more pessimistic estimate. Consider the two cases:

In both cases, the clipping removes the incentive to move the probability ratio beyond the interval [1ϵ,1+ϵ][1-\epsilon, 1+\epsilon]. This keeps the new policy close to the old policy without explicitly computing or constraining the KL divergence.

The algorithm collects a batch of trajectories, then performs KK epochs of mini-batch updates on the same data. The empirical surrogate L^CLIP\hat{L}^{\text{CLIP}} approximates the population objective (56) using samples from the averaged time-marginal distribution. The ratio ρ\rho is computed in log-space for numerical stability. Clipping removes the local incentive to move a sampled probability ratio beyond the chosen interval. It does not guarantee that the sampled updates will find a successful policy, and it says nothing about physical constraints absent from the reward or environment.

Experiment: PPO on the SwingRL Plant

The SwingRL model from the modeling chapter provides a matched comparison between supplied structure and sampled policy optimization. Both controllers act on the same articulated standing rider, use the same two bounded commands, receive the same observations, and count one full unwrapped rotation as success. The structured controller receives a phase-locked pumping rule whose phases were selected by a model sweep. PPO receives trajectories and the environment reward.

The experiment asks whether the clipped policy-gradient update discovers a full rotation under one fixed, reproducible protocol. The protocol was chosen before inspecting the five final runs.

quantityvalue
policy and value networksseparate 2×642 \times 64 tanh networks
requested interactions1,000,000 per seed
rollout batch8 environments ×\times 256 steps
PPO epochs and minibatch4 epochs, 256 samples
optimizerAdam, learning rate 3×1043\times10^{-4} with linear decay
discount and GAEγ=0.995\gamma=0.995, λ=0.95\lambda=0.95
clipping and entropy coefficients0.2 and 0.001
seeds0, 1, 2, 3, 4
evaluation100 fixed initial states every 50,000 requested interactions

Each complete rollout batch contains 2,048 transitions, so the last update is recorded at 1,001,472 interactions. A checkpoint is the first complete update at or beyond its nominal 50,000-interaction target. Evaluation angles are uniform between 5-5^\circ and 55^\circ, angular velocities are uniform between -0.1 and 0.1 rad/s, and the deterministic policy uses the tanh of the Gaussian mean. The environment and the structured baseline use the same 100 states.

Figure 1:Recorded training replay for seed 0. Checkpoints occur every 50,000 requested interactions, with complete rollout batches producing the recorded counts shown by the slider. Each checkpoint uses the saved deterministic policy from the same 33^\circ initial angle. The curve shows raw completed-episode returns and an eight-point trailing mean. The 21 saved policies are compressed into 63 seconds; the overlay reports the original elapsed training time.

Loading...

The replay is an illustration from one prespecified run, not the evidential comparison. Future observations remain hidden until the movie reaches their checkpoint. The five-seed result below carries the comparison across runs.

Download the recorded seed-0 replay

Two plots compare five PPO seeds with a structured SwingRL controller. PPO remains at zero success while its mean return improves slightly. The structured controller succeeds from every evaluation state and has a much larger return.

Figure 2:Five prespecified PPO seeds evaluated on the same 100 fixed initial states. Thin blue traces show individual seeds; the solid trace and band show the mean and a two-sided 95% tt interval with seed as the statistical unit. PPO improves its return early but never completes a rotation. The structured controller succeeds from every state and obtains mean return 191.8; this value is annotated off scale in the return panel so that the smaller PPO change remains visible.

The final policies settle on low-motion behavior. Across the five showcase rollouts, the largest angle lies between 5.455.45^\circ and 5.625.62^\circ. Return improves because the policy changes the rider while keeping effort and time penalties modest, but the behavior never approaches the 360360^\circ success condition.

controllertraining transitionsheld-out successmean returnworst suspension tension
structured phase controller0100%191.79-637 N
PPO, mean over five final policies1,001,472 per seed0%-5.11+387 N

The comparison does not establish a general ranking between control and RL. The structured controller receives the oscillation phase and a controller family adapted to the mechanism, while PPO must infer useful coordination from sampled returns. Conversely, the structured controller’s nominal success requires negative suspension tension during 7.37 percent of its active steps. A rigid rod can supply that outward force; a playground chain cannot. PPO avoids the violation by barely moving, which is feasible but does not solve the task.

The two failures answer different audit questions. Optimization has not found a successful sampled policy under this protocol. The successful structured policy exposes an inadequate rigid-link model. More interactions or a revised learning objective might address the first failure. A unilateral chain model or an explicit tension constraint is required for the second.

The complete Python implementation includes policy sampling, tanh-corrected log probabilities, GAE, PPO updates, batched SwingRL evaluation, checkpoint serialization, and replay rendering. The experiment record specifies the artifact layout and reproduction command. Policy training is deliberately absent from the MyST build; the page reads the recorded checkpoints, tables, figures, and movie.

The Policy Gradient Theorem

How can the trajectory-level score estimator be rewritten as an expectation over discounted state visitation and action values?

The algorithms developed so far (REINFORCE, actor-critic, GAE, and PPO) all estimate policy gradients from sampled trajectories. We now establish the theoretical foundation for these estimators by deriving the policy gradient theorem in the discounted infinite-horizon setting.

Sutton et al. (1999) provided the original derivation. Here we present an alternative approach using the Implicit Function Theorem, which frames policy optimization as a bilevel problem:

maxwαvγπw\max_{\mathbf{w}} \alpha^\top \mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}}

subject to:

(IγPπw)vγπw=rπw(\mathbf{I} - \gamma \mathbf{P}_{\pi_{\boldsymbol{w}}}) \mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}} = \mathbf{r}_{\pi_{\boldsymbol{w}}}

The Implicit Function Theorem states that if there is a solution to the problem F(v,w)=0F(\mathbf{v}, \mathbf{w}) = 0, then we can “reparameterize” our problem as F(v(w),w)F(\mathbf{v}(\mathbf{w}), \mathbf{w}) where v(w)\mathbf{v}(\mathbf{w}) is an implicit function of w\mathbf{w}. If the Jacobian Fv\frac{\partial F}{\partial \mathbf{v}} is invertible, then:

dv(w)dw=(F(v(w),w)v)1F(v(w),w)w\frac{d\mathbf{v}(\mathbf{w})}{d\mathbf{w}} = -\left(\frac{\partial F(\mathbf{v}(\mathbf{w}), \mathbf{w})}{\partial \mathbf{v}}\right)^{-1}\frac{\partial F(\mathbf{v}(\mathbf{w}), \mathbf{w})}{\partial \mathbf{w}}

Here we made it clear in our notation that the derivative must be evaluated at root (v(w),w)(\mathbf{v}(\mathbf{w}), \mathbf{w}) of FF. For the remaining of this derivation, we will drop this dependence to make notation more compact.

Applying this to our case with F(v,w)=(IγPπw)vrπwF(\mathbf{v}, \mathbf{w}) = (\mathbf{I} - \gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})\mathbf{v} - \mathbf{r}_{\pi_{\boldsymbol{w}}}:

vγπww=(IγPπw)1(rπww+γPπwwvγπw)\frac{\partial \mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}} = (\mathbf{I} - \gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^{-1}\left(\frac{\partial \mathbf{r}_{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}} + \gamma \frac{\partial \mathbf{P}_{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}}\mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}}\right)

Then:

wJ(w)=αvγπww=xα(rπww+γPπwwvγπw)\begin{align*} \nabla_{\mathbf{w}}J(\mathbf{w}) &= \alpha^\top \frac{\partial \mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}} \\ &= \mathbf{x}_\alpha^\top\left(\frac{\partial \mathbf{r}_{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}} + \gamma \frac{\partial \mathbf{P}_{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}}\mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}}\right) \end{align*}

where we have defined the discounted state visitation distribution:

xαα(IγPπw)1.\mathbf{x}_\alpha^\top \equiv \alpha^\top(\mathbf{I} - \gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^{-1}.

Recall the vector notation for MDPs from the infinite-horizon MDP chapter:

rπ(s)aAsπ(as)r(s,a),[Pπ]s,saAsπ(as)p(ss,a).\begin{align*} \mathbf{r}_\pi(s) &\equiv \sum_{a \in \mathcal{A}_s} \pi(a \mid s) \, r(s, a), \\ [\mathbf{P}_\pi]_{s,s'} &\equiv \sum_{a \in \mathcal{A}_s} \pi(a \mid s) \, p(s' \mid s, a). \end{align*}

Taking derivatives with respect to w\mathbf{w} gives:

[rπww]s=aAswπw(as)r(s,a),[Pπwwvγπw]s=aAswπw(as)sp(ss,a)vγπw(s).\begin{align*} \left[\frac{\partial \mathbf{r}_{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}}\right]_s &= \sum_{a \in \mathcal{A}_s} \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s) \, r(s,a), \\ \left[\frac{\partial \mathbf{P}_{\pi_{\boldsymbol{w}}}}{\partial \mathbf{w}}\mathbf{v}_\gamma^{\pi_{\boldsymbol{w}}}\right]_s &= \sum_{a \in \mathcal{A}_s} \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s)\sum_{s'} p(s' \mid s,a) \, v_\gamma^{\pi_{\boldsymbol{w}}}(s'). \end{align*}

Substituting back:

wJ(w)=sxα(s)(awπw(as)r(s,a)+γawπw(as)sp(ss,a)vγπw(s))=sxα(s)awπw(as)(r(s,a)+γsp(ss,a)vγπw(s))\begin{align*} \nabla_{\mathbf{w}}J(\mathbf{w}) &= \sum_s x_\alpha(s)\left(\sum_a \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s) \, r(s,a) + \gamma\sum_a \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s)\sum_{s'} p(s' \mid s,a) \, v_\gamma^{\pi_{\boldsymbol{w}}}(s')\right) \\ &= \sum_s x_\alpha(s)\sum_a \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s)\left(r(s,a) + \gamma \sum_{s'} p(s' \mid s,a) \, v_\gamma^{\pi_{\boldsymbol{w}}}(s')\right) \end{align*}

This is the policy gradient theorem, where xα(s)x_\alpha(s) is the discounted state visitation distribution and the term in parentheses is the state-action value function qπw(s,a)q^{\pi_{\boldsymbol{w}}}(s,a).

Normalized Discounted State Visitation Distribution

The discounted state visitation xα(s)x_\alpha(s) is not normalized. Therefore the expression we obtained above is not an expectation. However, we can transform it into one by normalizing by 1γ1 - \gamma. Note that for any initial distribution α\alpha:

sxα(s)=α(IγPπw)11=α11γ=11γ\sum_s x_\alpha(s) = \alpha^\top(\mathbf{I} - \gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^{-1}\mathbf{1} = \frac{\alpha^\top\mathbf{1}}{1-\gamma} = \frac{1}{1-\gamma}

Therefore, defining the normalized state distribution ξα(s)=(1γ)xα(s)\xi_\alpha(s) = (1-\gamma)x_\alpha(s), we can write:

wJ(w)=11γsξα(s)awπw(as)(r(s,a)+γsp(ss,a)vγπw(s))=11γEsξα[awπw(as)qπw(s,a)]\begin{align*} \nabla_{\mathbf{w}}J(\mathbf{w}) &= \frac{1}{1-\gamma}\sum_s \xi_\alpha(s)\sum_a \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s)\left(r(s,a) + \gamma \sum_{s'} p(s' \mid s,a) \, v_\gamma^{\pi_{\boldsymbol{w}}}(s')\right) \\ &= \frac{1}{1-\gamma}\mathbb{E}_{s\sim\xi_\alpha}\left[\sum_a \nabla_{\mathbf{w}}\pi_{\boldsymbol{w}}(a \mid s) \, q^{\pi_{\boldsymbol{w}}}(s,a)\right] \end{align*}

Now we have expressed the policy gradient theorem in terms of expectations under the normalized discounted state visitation distribution. But what does sampling from ξα\xi_\alpha mean? Recall that xα=α(IγPπw)1\mathbf{x}_\alpha^\top = \alpha^\top(\mathbf{I} - \gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^{-1}. Using the Neumann series expansion (valid when γPπw<1\|\gamma \mathbf{P}_{\pi_{\boldsymbol{w}}}\| < 1, which holds for γ<1\gamma < 1 since Pπw\mathbf{P}_{\pi_{\boldsymbol{w}}} is a stochastic matrix) we have:

ξα=(1γ)αk=0(γPπw)k\boldsymbol{\xi}_\alpha^\top = (1-\gamma)\alpha^\top\sum_{k=0}^{\infty} (\gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^k

We can then factor out the first term from this summation to obtain:

ξα=(1γ)αk=0(γPπw)k=(1γ)α+(1γ)αk=1(γPπw)k=(1γ)α+(1γ)αγPπwk=0(γPπw)k=(1γ)α+γξαPπw\begin{align*} \boldsymbol{\xi}_\alpha^\top &= (1-\gamma)\alpha^\top\sum_{k=0}^{\infty} (\gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^k \\ &= (1-\gamma)\alpha^\top + (1-\gamma)\alpha^\top\sum_{k=1}^{\infty} (\gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^k \\ &= (1-\gamma)\alpha^\top + (1-\gamma)\alpha^\top\gamma\mathbf{P}_{\pi_{\boldsymbol{w}}}\sum_{k=0}^{\infty} (\gamma \mathbf{P}_{\pi_{\boldsymbol{w}}})^k \\ &= (1-\gamma)\alpha^\top + \gamma\boldsymbol{\xi}_\alpha^\top \mathbf{P}_{\pi_{\boldsymbol{w}}} \end{align*}

The balance equation:

ξα=(1γ)α+γξαPπw\boldsymbol{\xi}_\alpha^\top = (1-\gamma)\alpha^\top + \gamma\boldsymbol{\xi}_\alpha^\top \mathbf{P}_{\pi_{\boldsymbol{w}}}

shows that ξα\boldsymbol{\xi}_\alpha is a mixture distribution: with probability 1γ1-\gamma you draw a state from the initial distribution α\alpha (reset), and with probability γ\gamma you follow the policy dynamics Pπw\mathbf{P}_{\pi_{\boldsymbol{w}}} from the current state (continue). This interpretation directly connects to the geometric process: at each step you either terminate and resample from α\alpha (with probability 1γ1-\gamma) or continue following the policy (with probability γ\gamma).

import numpy as np

def sample_from_discounted_visitation(
    alpha, 
    policy, 
    transition_model, 
    gamma, 
    n_samples=1000
):
    """Sample states from the discounted visitation distribution.
    
    Args:
        alpha: Initial state distribution (vector of probabilities)
        policy: Function (state -> action probabilities)
        transition_model: Function (state, action -> next state probabilities)
        gamma: Discount factor
        n_samples: Number of states to sample
    
    Returns:
        Array of sampled states
    """
    samples = []
    n_states = len(alpha)
    rng = np.random.default_rng(2026)
    
    # Initialize state from alpha
    current_state = rng.choice(n_states, p=alpha)
    
    for _ in range(n_samples):
        samples.append(current_state)
        
        # With probability (1-gamma): reset
        if rng.random() > gamma:
            current_state = rng.choice(n_states, p=alpha)
        # With probability gamma: continue
        else:
            # Sample action from policy
            action_probs = policy(current_state)
            action = rng.choice(len(action_probs), p=action_probs)
            
            # Sample next state from transition model
            next_state_probs = transition_model(current_state, action)
            current_state = rng.choice(n_states, p=next_state_probs)
    
    return np.array(samples)

# Example usage for a simple 2-state MDP
alpha = np.array([0.7, 0.3])  # Initial distribution
policy = lambda s: np.array([0.8, 0.2])  # Dummy policy
transition_model = lambda s, a: np.array([0.9, 0.1])  # Dummy transitions
gamma = 0.9

samples = sample_from_discounted_visitation(alpha, policy, transition_model, gamma)

# Check empirical distribution
print("Empirical state distribution:")
print(np.bincount(samples) / len(samples))
Empirical state distribution:
[0.867 0.133]

While the math shows that sampling from the discounted visitation distribution ξα\boldsymbol{\xi}_\alpha would give us unbiased policy gradient estimates, Thomas (2014) demonstrated that this implementation can be detrimental to performance in practice. The issue arises because terminating trajectories early (with probability 1γ1-\gamma) reduces the effective amount of data we collect from each trajectory. This early termination weakens the learning signal, as many trajectories don’t reach meaningful terminal states or rewards.

Therefore, in practice, we typically sample complete trajectories from the undiscounted process (running the policy until natural termination or a fixed horizon) while still using γ\gamma in the advantage estimation. This approach preserves the full learning signal from each trajectory and has been empirically shown to lead to better performance.

This is one of several cases in RL where the theoretically optimal procedure differs from the best practical implementation.

The Actor-Critic Architecture

The policy gradient theorem shows that the gradient depends on the action-value function qπw(s,a)q^{\pi_{\boldsymbol{w}}}(s,a). In practice, we do not have access to the true qq-function and must estimate it. This leads to the actor-critic architecture: the actor maintains the policy πw\pi_{\boldsymbol{w}}, while the critic maintains an estimate of the value function.

This architecture traces back to Sutton’s 1984 thesis, where he proposed the Adaptive Heuristic Critic. The actor uses the critic’s value estimates to compute advantage estimates for the policy gradient, while the critic learns from the same trajectories generated by the actor. The algorithms we developed earlier (REINFORCE with baseline, GAE, and the one-step actor-critic) are all instances of this architecture.

We are simultaneously learning two functions that depend on each other, which creates a stability challenge. The actor’s gradient uses the critic’s estimates, but the critic is trained on data generated by the actor’s policy. If both change too quickly, the learning process can become unstable.

Konda (2002) analyzed this coupled learning problem and established convergence guarantees under a two-timescale condition: the critic must update faster than the actor. Intuitively, the critic needs to “track” the current policy’s value function before the actor uses those estimates to update. If the actor moves too fast, it uses stale or inaccurate value estimates, leading to poor gradient estimates.

In practice, this is implemented by using different learning rates: a larger learning rate αθ\alpha_\theta for the critic and a smaller learning rate αw\alpha_w for the actor, with αθ>αw\alpha_\theta > \alpha_w. Alternatively, one can perform multiple critic updates per actor update. The soft actor-critic algorithm discussed earlier in the amortization chapter follows this same principle, inheriting the actor-critic structure while incorporating entropy regularization and learning Q-functions directly.

The actor-critic architecture also connects to the bilevel optimization perspective of the policy gradient theorem: the outer problem optimizes the policy, while the inner problem solves for the value function given that policy. The two-timescale condition ensures that the inner problem is approximately solved before taking a step on the outer problem.

Reparameterization Methods in Reinforcement Learning

When actions and dynamics admit differentiable sampling paths, how does pathwise differentiation change the variance and model requirements of policy optimization?

When dynamics are known or can be learned, reparameterization provides an alternative to score function methods. By expressing actions and state transitions as deterministic functions of noise, we can backpropagate through trajectories to compute policy gradients with lower variance than score function estimators.

Stochastic Value Gradients

The reparameterization trick requires that we can express our random variable as a deterministic function of noise. In reinforcement learning, this applies naturally when we have a learned model of the dynamics. Consider a stochastic policy πw(as)\pi_{\boldsymbol{w}}(a|s) that we can reparameterize as a=πw(s,ϵ)a = \pi_{\boldsymbol{w}}(s,\epsilon) where ϵp(ϵ)\epsilon \sim p(\epsilon), and a dynamics model s=f(s,a,ξ)s' = f(s,a,\xi) where ξp(ξ)\xi \sim p(\xi) represents environment stochasticity. Both transformations are deterministic given the noise variables.

With these reparameterizations, we can write an nn-step return as a differentiable function of the noise:

Rn(s0,{ϵi},{ξi})=i=0n1γir(si,ai)R_n(s_0,\{\epsilon_i\},\{\xi_i\}) = \sum_{i=0}^{n-1} \gamma^i r(s_i,a_i)

where ai=πw(si,ϵi)a_i = \pi_{\boldsymbol{w}}(s_i,\epsilon_i) and si+1=f(si,ai,ξi)s_{i+1} = f(s_i,a_i,\xi_i) for i=0,...,n1i=0,...,n-1. The objective becomes:

J(w)=E{ϵi},{ξi}[Rn(s0,{ϵi},{ξi})]J(\boldsymbol{w}) = \mathbb{E}_{\{\epsilon_i\},\{\xi_i\}}[R_n(s_0,\{\epsilon_i\},\{\xi_i\})]

We can now apply the reparameterization gradient estimator:

wJ(w)=E{ϵi},{ξi}[wRn(s0,{ϵi},{ξi})]\nabla_{\boldsymbol{w}}J(\boldsymbol{w}) = \mathbb{E}_{\{\epsilon_i\},\{\xi_i\}}\left[\nabla_{\boldsymbol{w}}R_n(s_0,\{\epsilon_i\},\{\xi_i\})\right]

This gradient can be computed by automatic differentiation through the sequence of policy and model evaluations. The computation requires backpropagating through nn steps of model rollouts, which becomes expensive for large nn but avoids the high variance of score function estimators.

The Stochastic Value Gradients (SVG) framework Heess et al., 2015 uses this approach while introducing a hybrid objective that combines model rollouts with value function bootstrapping:

JSVG(n)(w)=E{ϵi},{ξi}[i=0n1γir(si,ai)+γnq(sn,an;θ)]J^{\text{SVG}(n)}(\boldsymbol{w}) = \mathbb{E}_{\{\epsilon_i\},\{\xi_i\}}\left[\sum_{i=0}^{n-1} \gamma^i r(s_i,a_i) + \gamma^n q(s_n,a_n;\theta)\right]

The terminal value function q(sn,an;θ)q(s_n,a_n;\theta) approximates the value beyond horizon nn, allowing shorter rollouts while still capturing long-term value. This creates a spectrum of algorithms parameterized by nn.

SVG(0): Model-Free Reparameterization

When n=0n=0, the objective collapses to:

JSVG(0)(w)=EsρEϵp(ϵ)[q(s,πw(s,ϵ);θ)]J^{\text{SVG}(0)}(\boldsymbol{w}) = \mathbb{E}_{s \sim \rho}\mathbb{E}_{\epsilon \sim p(\epsilon)}\left[q(s,\pi_{\boldsymbol{w}}(s,\epsilon);\theta)\right]

No model is required. We simply differentiate the critic with respect to actions sampled from the reparameterized policy. This is the approach used in DDPG Lillicrap et al., 2015 (with a deterministic policy where ϵ\epsilon is absent) and SAC Haarnoja et al., 2018 (where ϵ\epsilon produces the stochastic component). The gradient is:

wJSVG(0)=Es,ϵ[aq(s,a;θ)a=πw(s,ϵ)wπw(s,ϵ)]\nabla_{\boldsymbol{w}} J^{\text{SVG}(0)} = \mathbb{E}_{s,\epsilon}\left[\nabla_a q(s,a;\theta)\big|_{a=\pi_{\boldsymbol{w}}(s,\epsilon)} \nabla_{\boldsymbol{w}} \pi_{\boldsymbol{w}}(s,\epsilon)\right]

This requires only that the critic qq be differentiable with respect to actions, not a learned dynamics model. All bias comes from errors in the value function approximation.

SVG(1) to SVG(nn): Model-Based Rollouts

For n1n \geq 1, we unroll a learned dynamics model for nn steps before bootstrapping with the critic. Consider SVG(1):

JSVG(1)(w)=Es,ϵ,ξ[r(s,πw(s,ϵ))+γq(f(s,πw(s,ϵ),ξ),πw(s,ϵ);θ)]J^{\text{SVG}(1)}(\boldsymbol{w}) = \mathbb{E}_{s,\epsilon,\xi}\left[r(s,\pi_{\boldsymbol{w}}(s,\epsilon)) + \gamma q(f(s,\pi_{\boldsymbol{w}}(s,\epsilon),\xi), \pi_{\boldsymbol{w}}(s',\epsilon');\theta)\right]

where s=f(s,πw(s,ϵ),ξ)s' = f(s,\pi_{\boldsymbol{w}}(s,\epsilon),\xi) is the next state predicted by the model. The gradient now flows through both the reward and the model transition. Increasing nn propagates reward information more directly through the model rollout, reducing reliance on the critic. However, model errors compound over the horizon. If the model is inaccurate, longer rollouts can degrade performance.

SVG(\infty): Pure Model-Based Optimization

As nn \to \infty, we eliminate the critic entirely:

JSVG()(w)=E{ϵi},{ξi}[i=0T1γir(si,πw(si,ϵi))]J^{\text{SVG}(\infty)}(\boldsymbol{w}) = \mathbb{E}_{\{\epsilon_i\},\{\xi_i\}}\left[\sum_{i=0}^{T-1} \gamma^i r(s_i,\pi_{\boldsymbol{w}}(s_i,\epsilon_i))\right]

This is pure model-based policy optimization, differentiating through the entire trajectory. Approaches like PILCO Deisenroth & Rasmussen, 2011 and Dreamer Hafner et al., 2019 operate in this regime. With an accurate model, this provides the most direct gradient signal. The tradeoff is computational: backpropagating through hundreds of time steps is expensive, and gradient magnitudes can explode or vanish over long horizons.

The choice of nn reflects a fundamental bias-variance tradeoff. Small nn relies on the critic for long-term value estimation, inheriting its approximation errors. Large nn relies on the model, accumulating its prediction errors. In practice, intermediate values like n=5n=5 or n=10n=10 often work well when combined with a reasonably accurate learned model.

Noise Inference for Off-Policy Learning

A subtle issue arises when combining reparameterization with experience replay. SVG naturally supports off-policy learning: states ss can be sampled from a replay buffer rather than the current policy. However, reparameterization requires the noise variables ϵ\epsilon that generated each action.

For on-policy data, we can simply store ϵ\epsilon alongside each transition (s,a,r,s)(s, a, r, s'). For off-policy data collected under a different policy, the noise is unknown. To apply reparameterization gradients to such data, we must infer the noise that would have produced the observed action under the current policy.

For invertible policies, this is straightforward. If a=πw(s,ϵ)a = \pi_{\boldsymbol{w}}(s, \epsilon) with ϵN(0,I)\epsilon \sim \mathcal{N}(0, I), and the policy takes the form a=μw(s)+σw(s)ϵa = \mu_{\boldsymbol{w}}(s) + \sigma_{\boldsymbol{w}}(s) \odot \epsilon (as in a Gaussian policy), we can recover the noise exactly:

ϵ=aμw(s)σw(s)\epsilon = \frac{a - \mu_{\boldsymbol{w}}(s)}{\sigma_{\boldsymbol{w}}(s)}

This recovered ϵ\epsilon can then be used for gradient computation. However, this introduces a subtle dependence: the inferred ϵ\epsilon depends on the current policy parameters w\boldsymbol{w}, not just the data. As the policy changes during training, the same action aa corresponds to different noise values.

For dynamics noise ξ\xi, the situation is more complex. If we have a probabilistic model s=f(s,a,ξ)s' = f(s, a, \xi) and observe the actual next state ss', we could in principle infer ξ\xi. In practice, environment stochasticity is often treated as irreducible: we cannot replay the exact same noise realization. SVG handles this by either: (1) using deterministic models and ignoring environment stochasticity, (2) re-simulating from the model rather than using observed next states, or (3) using importance weighting to correct for the distribution mismatch.

The noise inference perspective connects reparameterization gradients to the broader question of credit assignment in RL. By explicitly tracking which noise realizations led to which outcomes, we can more precisely attribute value to policy parameters rather than to lucky or unlucky samples.

When dynamics are deterministic or can be accurately reparameterized, SVG-style methods offer an efficient alternative to the score function methods developed in the previous section. However, many reinforcement learning problems involve unknown dynamics or dynamics that resist accurate modeling. In those settings, score function methods remain the primary tool since they require only the ability to sample trajectories under the policy.

Summary

The trajectory score supplies a model-free policy gradient because the transition terms do not depend on the policy parameters. Conditional returns, state-dependent baselines, generalized advantage estimates, and learned critics reduce its variance. Importance ratios then compare data from an older policy with the current one, while PPO clips those ratios to limit the update.

The SwingRL experiment separated a correct PPO implementation from a successful control result. Five million total training interactions improved the shaped return but produced no full rotations. The structured controller solved the nominal rigid-link problem without policy training, then failed a different test because the resulting trajectory required a chain to push. Algorithm diagnostics, task success, and model validity therefore remain separate parts of the evaluation.

The policy-gradient theorem expresses the same derivative through discounted state visitation and action values. Approximating those values produces the actor-critic architecture, with a faster critic update supplying the signal used by the actor.

When dynamics models are available, reparameterization through stochastic value gradients supplies a lower-variance alternative. SVG(0) recovers actor-critic methods such as DDPG and SAC, while SVG(\infty) differentiates through a complete simulated trajectory. The resulting choice is now explicit: score methods trade variance for minimal model assumptions; pathwise methods trade stronger differentiability assumptions for lower-variance credit assignment.

Self-checks

Solution to Exercise 1

No. It differentiates the log probability of sampled actions under the policy and weights that score by sampled returns or advantages; the environment need only generate trajectories.

Solution to Exercise 2

Conditioned on a state, the expected policy score is zero: aπ(as)logπ(as)=0\sum_a\pi(a|s)\nabla\log\pi(a|s)=0. Multiplying a state-only baseline by that score therefore has zero expectation.

References
  1. Williams, R. J. (1992). Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning. Machine Learning, 8(3), 229–256. 10.1007/BF00992696
  2. Sutton, R. S., McAllester, D., Singh, S., & Mansour, Y. (1999). Policy Gradient Methods for Reinforcement Learning with Function Approximation. Advances in Neural Information Processing Systems, 12, 1057–1063.
  3. Konda, V. R. (2002). Actor-Critic Algorithms [Phdthesis]. Massachusetts Institute of Technology.
  4. Heess, N., Wayne, G., Silver, D., Lillicrap, T., Erez, T., & Tassa, Y. (2015). Learning Continuous Control Policies by Stochastic Value Gradients. Advances in Neural Information Processing Systems, 28, 2944–2952.
  5. Lillicrap, T. P., Hunt, J. J., Pritzel, A., Heess, N., Erez, T., Tassa, Y., Silver, D., & Wierstra, D. (2015). Continuous Control with Deep Reinforcement Learning. arXiv Preprint arXiv:1509.02971.
  6. Haarnoja, T., Zhou, A., Abbeel, P., & Levine, S. (2018). Soft actor-critic: Off-policy maximum entropy deep reinforcement learning with a stochastic actor. Proceedings of the 35th International Conference on Machine Learning (ICML), 1861–1870.
  7. Deisenroth, M. P., & Rasmussen, C. E. (2011). PILCO: A Model-Based and Data-Efficient Approach to Policy Search. Proceedings of the 28th International Conference on Machine Learning (ICML), 465–472.
  8. Hafner, D., Lillicrap, T., Ba, J., & Norouzi, M. (2019). Dream to Control: Learning Behaviors by Latent Imagination. arXiv Preprint arXiv:1912.01603.