Explanation and derivation of policy gradients, from the objective to a sample estimate.
· 3 min read · Draft
Material adapted from Sergey Levine’s CS294-112 Fall 2018 and Josh Achiam’s Spinning Up With RL
Correction — October 2026: This 2019 draft is preserved here with corrected notation and a completed derivation below. The draft label remains; no original implementation was recovered.
What is a Policy Gradient and how it is useful?
The goal in reinforcement learning is to find a policy with high expected return. Policy-gradient methods adjust a policy’s action probabilities using rewards from sampled trajectories. Following the gradient can improve the policy, but noisy estimates and local optima mean that reaching an optimal policy is not guaranteed.
Derivation
Consider a stochastic policy πθ and a trajectory τ=(s0,a0,…,sT−1,aT−1,sT) with T actions. We use the finite-horizon, undiscounted return:
R(τ)=t=0∑T−1r(st,at)
The goal is to choose the policy parameters that maximize expected return:
θ⋆∈θargmaxJ(θ)
Here J(θ) is the expectation of the total reward. Writing pθ(τ) for the trajectory distribution gives:
J(θ)=Eτ∼pθ[R(τ)]=∫pθ(τ)R(τ)dτ
Assume the initial-state distribution, environment transitions, and reward function do not depend on θ. Also assume the policy is differentiable and we can move the derivative inside the integral. Then:
∇θJ(θ)=∇θ∫pθ(τ)R(τ)dτ=∫∇θpθ(τ)R(τ)dτ
For trajectories with positive probability, the log-derivative identity is:
∇θpθ(τ)=pθ(τ)∇θlogpθ(τ)
Substituting it into the integral:
∇θJ(θ)=∫pθ(τ)∇θlogpθ(τ)R(τ)dτ
Going back to expectation form:
∇θJ(θ)=Eτ∼pθ[∇θlogpθ(τ)R(τ)]
The trajectory probability factors into the initial-state distribution ρ0, the environment transitions P, and the policy:
We can use this estimate for a gradient-ascent step, θ←θ+αg, with step size α>0. A finite sample is noisy; an individual update is not guaranteed to improve return.