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):
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
Last episode's path
True performance J(p) = vπ(start), with the current p
What to try
- Run one episode. The policy starts at p = 0.05, nearly always left, so episodes are long. The inspector shows each step's return Gt, the gradient ∇ln π, and how θ moved.
- Look at the sign of each step's θ1 change. Every return is negative, so plain REINFORCE pushes down the probability of whatever was taken, but less so for actions followed by shorter episodes. The baseline subtracts a learned average return so that better-than-usual actions get pushed up.
- Run +100 a few times with the baseline. The dot climbs the J curve toward its peak near p ≈ 0.59, and stays stochastic.
REINFORCE vs REINFORCE with baseline (average of 100 runs)
Total reward per episode
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:
μ 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:
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):
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):
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:
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.