Foundations to Frontier
Tests
L. Reinforcement Learning and Autonomy › L4 Deep RL: Policy-Based

The policy gradient theorem

Policy-gradient methods adjust the parameters of a policy directly in the direction that increases the expected return. The policy gradient theorem gives that direction in a form that needs no derivative of the environment: visit frequencies times the policy's own score times action values. This page derives the theorem, verifies it exactly against finite differences on the pit gridworld, compares vanilla and natural gradient ascent, and measures how noisy the sampled (score-function) gradient is.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Nudging the probabilities

Instead of learning how good each move is and then picking the best, a policy-gradient agent keeps a probability for each move and nudges those probabilities after seeing how things went: moves that led to better than usual results become a bit more likely, worse ones a bit less likely. The policy gradient theorem says exactly how big each nudge should be to improve the expected result as fast as possible.

Reinforcement learning lab
Tabular softmax policy on the 4×4 pit grid (slip 0.2, γ = 0.95), starting from the uniform policy; the gradient is computed exactly with the policy gradient theorem. Curves: start value J over 300 steps for vanilla ascent with η = 10 (solid), natural gradient with η = 0.1 (dashed) and the optimum J* = 0.3130. After 300 steps: vanilla 0.0939, natural 0.3128. The vanilla gradient is weighted by how often a state is visited and how likely an action already is, so it crawls across plateaus; the natural gradient removes both weightings.
Why engineers care

Policy-gradient methods train the controllers of legged robots and fine-tune large language models from human feedback.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A policy-gradient agent adjusts…
  2. 2. Moves that did better than usual become…
  3. 3. The policy gradient theorem tells…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Parameterised policy: π_θ(a|s), for example a softmax over preferences θ[s, a], or a network.
  • Objective: J(θ) = E_π[Σ_t γ^t r_(t+1)] from the start state(s).
  • Policy gradient theorem: ∇J(θ) = Σ_s d^π(s) Σ_a ∇π(a|s) Q^π(s, a) = E[Σ_t γ^t Q^π(S_t, A_t) ∇ log π(A_t|S_t)], where d^π(s) = Σ_t γ^t P(S_t = s) is the discounted visitation.
  • Score function: ∇ log π(a|s). For a tabular softmax it is e_a − π(·|s) in row s. Its expectation under π is zero.
  • Advantage form: replacing Q by A = Q − V leaves the gradient unchanged, since Σ_a ∇π(a|s)V(s) = 0.
  • Natural gradient: precondition with the inverse Fisher matrix F = E[∇log π ∇log πᵀ]. For a tabular softmax, the natural step is θ ← θ + ηA^π(s, a)/(1 − γ).
📝 Quick check (Level 1)
0/3 answered
  1. 1. The policy gradient theorem needs the derivative of…
  2. 2. The expected score E[∇ log π] is…
  3. 3. Replacing Q by the advantage…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Checking the theorem exactly

Exercise 1.

  • The 4×4 slippery grid with four pits from topic L1-5, γ = 0.95, and a tabular softmax policy with random preferences.
  • Exact V^π and Q^π by a linear solve, and the discounted visitation d(s) = row s0 of (I − γP_π)^(−1).
  • The theorem’s gradient d(s)π(a|s)(Q(s, a) − V(s)), compared with central finite differences of J for all 64 parameters.

Practice.

  • Use policy gradients for continuous or very large action spaces and stochastic policies, where taking a max over actions is impractical.
  • Expect high variance: sampled gradients need many episodes or variance reduction (baselines, critics; next topics).
  • Prefer natural or trust-region steps: plain gradient steps crawl across plateaus where the policy is nearly deterministic (TRPO and PPO, topic L4-4).
  • Check gradients: compare with finite differences on a small problem before scaling up.
  • Keep some randomness: a softmax policy that becomes deterministic too early has vanishing gradients.
  • Tools: PyTorch or JAX autodiff for ∇ log π; Stable-Baselines3 and CleanRL for full algorithms.
def pg_theorem(P, R, theta, gamma, s0=0):
    pi = softmax_policy(theta)
    V, Q = evaluate(P, R, pi, gamma)
    Ppi = np.einsum("sa,sat->st", pi, P)
    e = np.zeros(len(R)); e[s0] = 1.0
    d = np.linalg.solve((np.eye(len(R)) - gamma * Ppi).T, e)
    return d[:, None] * pi * (Q - V[:, None])
🐍 Exercise 1: the theorem against finite differences
Compute the exact policy gradient of a tabular softmax policy on the pit gridworld with the policy gradient theorem and check it against finite differences.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Policy gradients suit…
  2. 2. Plain gradient steps crawl when…
  3. 3. Before scaling up, check gradients with…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Derivation. V(s) = Σ_a π(a|s)Q(s, a). Differentiating, ∇V(s) = Σ_a ∇π(a|s)Q(s, a) + Σ_a π(a|s)∇Q(s, a), and ∇Q(s, a) = γΣ_s’ P(s’|s, a)∇V(s’), because rewards and transitions do not depend on θ. Unrolling this recursion gives ∇V(s0) = Σ_s Σ_t γ^t P(S_t = s | s0) Σ_a ∇π(a|s)Q(s, a) = Σ_s d(s) Σ_a ∇π(a|s)Q(s, a).

Log-derivative trick. ∇π = π∇log π turns the sum over actions into an expectation over actions actually taken: ∇J = E[Σ_t γ^t Q(S_t, A_t)∇log π(A_t|S_t)]. Replacing Q(S_t, A_t) by the sampled return G_t keeps the estimate unbiased (REINFORCE).

Baselines. For any function b(s), Σ_a ∇π(a|s)b(s) = b(s)∇Σ_a π(a|s) = b(s)∇1 = 0, so subtracting b(s) changes the variance but not the mean.

Natural gradient. The steepest-ascent direction under the KL-divergence metric between policies is F⁻¹∇J. For the tabular softmax, F⁻¹∇J reduces to the advantage function divided by 1 − γ, independent of how often each state is visited.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The derivation uses that rewards and transitions…
  2. 2. The log-derivative trick writes ∇π as…
  3. 3. A state-dependent baseline changes…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How fast do vanilla and natural exact gradient ascent reach the optimal policy on the pit gridworld, and how many episodes does a sampled gradient need to point the right way?

History.

  • R. J. Williams (1992) introduced REINFORCE, the score-function policy gradient.
  • R. S. Sutton, D. McAllester, S. Singh and Y. Mansour (2000) proved the policy gradient theorem with function approximation.
  • S. Kakade (2001) introduced the natural policy gradient.
  • J. Mei, C. Xiao, C. Szepesvári and D. Schuurmans (2020) proved that softmax policy gradient converges at rate O(1/t) and can be slowed by plateaus; A. Agarwal, S. Kakade, J. Lee and G. Mahajan (2021) analysed natural policy gradient.

Exercise 2.

  • The pit gridworld, γ = 0.95, J* = 0.3130 from policy iteration. All runs start from the uniform policy (θ = 0).
  • Exact gradient ascent for 1,000 steps: vanilla with η = 1, 10 and 100; natural with η = 0.1. These are deterministic.
  • Score-function estimates of the gradient at the uniform policy from 10, 100 and 1,000 sampled episodes, 10 repetitions each: cosine similarity with the true gradient and relative error.
method J after 10 steps after 100 after 1,000 first step at 95% of J*
vanilla, η = 1 −0.6529 −0.1968 0.0029 not within 1,000
vanilla, η = 10 −0.1948 0.0107 0.0972 not within 1,000
vanilla, η = 100 0.1794 0.2095 0.3085 645
natural, η = 0.1 0.2437 0.3109 0.3129 33

Seed 0, sampled gradient:

episodes mean cosine with true gradient mean relative error
10 0.025 6.157
100 0.452 1.854
1,000 0.852 0.645
  • The theorem is exact: in Exercise 1 it matched finite differences to better than 1e-9.
  • Vanilla gradient ascent was slow: even with η = 100 it needed 645 steps to reach 95% of J*, and with η = 1 it had not reached 0.003 after 1,000 steps (J* = 0.3130).
  • The natural gradient reached 95% of J in 33 steps* and 0.3129 after 1,000. It is not slowed by rarely visited states or by actions the policy already almost never takes.
  • Sampled gradients are noisy: with 10 episodes the estimate was essentially unrelated to the true direction (cosine 0.025); 1,000 episodes were needed for a cosine of 0.85. The relative error fell by about √10 for every tenfold increase in episodes (6.16, 1.85, 0.65).
  • Seeds 1 and 2: cosines −0.002, 0.467, 0.883 and 0.141, 0.422, 0.838 for 10, 100 and 1,000 episodes.
🐍 Exercise 2 (research): how fast do vanilla and natural exact gradient ascent reach the optimal policy on the pit gridworld, and how many episodes does a sampled gradient need to point the right way?
Run exact vanilla and natural policy-gradient ascent on the pit gridworld, and measure how well sampled score-function gradients from 10, 100 and 1,000 episodes match the true gradient.

Policy gradients in practice

  • Variance reduction (next topic): reward-to-go, baselines, and learned critics (actor-critic, topic L4-3).
  • Trust regions: TRPO and PPO (topic L4-4) approximate natural-gradient steps with neural-network policies.
  • Continuous actions: Gaussian policies with learned means and standard deviations; deterministic policy gradients (topic L4-5).
  • Language models: RLHF fine-tunes a language model with PPO-style policy gradients against a learned reward model.

References

  1. R. J. Williams (1992). “Simple statistical gradient-following algorithms for connectionist reinforcement learning.” Machine Learning.
  2. R. S. Sutton, D. McAllester, S. Singh and Y. Mansour (2000). “Policy gradient methods for reinforcement learning with function approximation.” NeurIPS.
  3. S. Kakade (2001). “A natural policy gradient.” NeurIPS.
  4. J. Mei, C. Xiao, C. Szepesvári and D. Schuurmans (2020). “On the global convergence rates of softmax policy gradient methods.” ICML.
  5. A. Agarwal, S. M. Kakade, J. D. Lee and G. Mahajan (2021). “On the theory of policy gradient methods.” JMLR.