Chapter 13 · Sutton & Barto

Policy gradient methods

Every method so far learned values and derived a policy from them. Policy gradient methods learn the policy directly: a parameterized π(a|s, θ) whose parameters climb the gradient of performance. They can learn stochastic policies, handle continuous actions naturally, and change smoothly as θ changes, which is what makes their convergence theory work.

01Parameterized policies

For discrete actions, a common choice is a softmax over action preferences h(s, a, θ), for example linear preferences θ⊤x(s, a):

Softmax in action preferencesπ(a | s, θ) = eh(s,a,θ) / Σb eh(s,b,θ)

Unlike ε-greedy action values, this can approach a deterministic policy if one is optimal, since preferences can grow without limit. It can also settle on a genuinely stochastic policy when that is what's best. With action values, a small change in an estimate can flip the greedy action abruptly. With a parameterized policy, action probabilities change smoothly in θ.

02Worked example: a corridor where randomness is optimal

A short corridor has three non-terminal states and a goal on the right. Every step costs −1. In the first and third states the actions do what they say. In the middle state they are switched: right moves left and left moves right. The catch: all three states look identical to the agent (same features), so it must use one action distribution everywhere: go right with probability p.

Any deterministic policy fails. Always-right gets stuck bouncing in the middle state, and always-left never leaves the start. The best p is in between. With action-value methods and ε-greedy, the policy can only be "right with probability 1 − ε/2" or "left with probability 1 − ε/2", both poor here. The demo below learns p directly. Preferences are θ1 for right and θ2 for left, so p = σ(θ1 − θ2). As is common in implementations of this example, p is kept within [0.05, 0.95]. Without that floor, an occasional run drifts to p ≈ 0, where episodes never end and the gradient vanishes, and it never recovers.

REINFORCE on the short corridor

episode 1 · p(right) – · J(p) –

Last episode's path

True performance J(p) = vπ(start), with the current p

What to try

REINFORCE vs REINFORCE with baseline (average of 100 runs)

Total reward per episode

REINFORCE, α = 2⁻¹³with baseline, αθ = 2⁻⁹, αw = 2⁻⁶v*(start) ≈ −11.6

03The policy gradient theorem

We want the gradient of J(θ) = vπθ(s0). The difficulty: changing θ changes both the action probabilities and the distribution of states visited, and the second effect depends on unknown environment dynamics. The policy gradient theorem shows the state-distribution term drops out:

Policy gradient theorem (episodic)∇J(θ) ∝ Σs μ(s) Σa qπ(s, a) ∇π(a | s, θ)

μ is the on-policy distribution. Since the right side is an expectation over states visited under π, sampling it from experience gives an unbiased gradient estimate.

04REINFORCE

Replace the sum over actions with the action actually taken, dividing by its probability to keep the estimate unbiased, and replace qπ with the sampled return Gt. The result is Monte Carlo policy gradient:

REINFORCEθ ← θ + α γt Gt ∇ln π(At | St, θ)

The vector ∇ln π, called the eligibility vector, points in the direction that most increases the probability of the action taken. For a linear softmax it is x(s, a) − Σb π(b|s) x(s, b). Here that is (1−p, −(1−p)) after going right and (−p, p) after going left. The update moves in that direction in proportion to the return, so actions followed by better outcomes become more likely.

05Baselines

Subtracting any baseline b(s) that doesn't depend on the action leaves the expected update unchanged, since Σa b(s)∇π(a|s) = b(s)∇1 = 0. It can greatly reduce variance. A natural choice is a learned state-value estimate v̂(s, w):

REINFORCE with baselineδ = Gt − v̂(St, w),   w ← w + αw δ ∇v̂(St, w),   θ ← θ + αθ γt δ ∇ln π(At | St, θ)

In the chart above this allows a 16× larger policy step size without becoming unstable, and learning is much faster.

06Actor–critic methods

REINFORCE waits for full returns. Actor–critic methods bootstrap instead. The critic learns v̂ by TD, and the one-step TD error replaces Gt − v̂(St):

One-step actor–criticδ = R + γ v̂(S′, w) − v̂(S, w),   w ← w + αw δ ∇v̂(S, w),   θ ← θ + αθ I δ ∇ln π(A | S, θ),   I ← γI

This is fully online and incremental, and the bootstrapping introduces bias but cuts variance, the same trade-off as TD versus MC. Eligibility traces extend both actor and critic to actor–critic with traces. For continuing tasks the same structure works with the average-reward TD error of chapter 10.

07Continuous actions

When actions are real numbers, such as a torque or a steering angle, learn the parameters of a distribution instead of a probability per action. A Gaussian policy outputs a mean μ(s, θ) and a standard deviation σ(s, θ), typically linear in features for the mean and exp-linear for σ to keep it positive:

Gaussian policyπ(a | s, θ) = 1/(σ(s,θ)√(2π)) · exp( −(a − μ(s,θ))² / (2σ(s,θ)²) )

The same REINFORCE and actor–critic updates apply unchanged. Only ∇ln π differs. For the mean it is (a − μ)/σ² × xμ(s), so the mean moves toward actions that did better than expected. The policy can explore a continuum of actions and narrow σ as it becomes confident.