Policy Gradient Methods
Policy gradients tune the policy itself by climbing expected return, so they handle continuous and stochastic actions where picking a max Q value fails.
Why Does This Exist?
Picking breaks when actions are continuous. A robot arm chooses joint torques from an infinite set, so there is no list to max over. Policy gradients dodge the problem: they parameterize the policy directly and climb expected return with respect to . The symbol holds the policy weights, and is the expected return those weights earn.
There is a second motive. Some tasks demand randomness, like mixing strategies so an opponent cannot exploit you. Value methods produce greedy deterministic policies, while a parameterized policy represents randomness naturally. This page covers the gradient itself; REINFORCE turns it into an algorithm, actor critic methods tame its variance, and policy objective functions define exactly what means.
Think of It Like This
A food truck owner testing two specials
A food truck owner sells a noodle bowl and a taco plate. Each morning she picks the split of ingredients by gut feel, then counts profit at close. A great noodle day means: shift tomorrow's split toward noodles. A bad taco day means: shift away from tacos.
She never writes down the value of every possible split. She just nudges the current split toward whatever sold well, in proportion to how well it sold. Returns play the role of profit, and the score function plays the role of the nudge direction. Over weeks the split settles where profit peaks.
The analogy stops at credit assignment: her daily profit mixes both dishes, while the real update separates each action's contribution through its own log probability gradient.
How It Actually Works
The goal is , the expected return over trajectories the current policy generates. The policy gradient theorem states the gradient without differentiating the environment at all:
In words: for each visited state, take the direction that raises the taken action's log probability, and scale it by the return that followed. Good outcomes pull the policy toward the actions that preceded them; bad ones push away. The term is called the score function, and is the discounted return from step .
Raw returns are noisy, so practitioners subtract a baseline , often a learned state value, forming the advantage . The baseline leaves the expected gradient unchanged but shrinks its variance. And because the update samples actions from the policy, it works unchanged for continuous action spaces: a Gaussian policy just outputs a mean and spread instead of softmax logits.
Worked example
Two lunch specials, one parameter pair . Softmax turns logits into probabilities, so . The truck pushes noodles (action 1) and earns return .
The score function for the taken action is one-hot minus probabilities: . Scale by the return: . Step with learning rate :
New probabilities: and , summing to , so . One good day moved noodles from to . That single stepped update is exactly what the diagram draws.
Code
This snippet replays the worked example with plain arithmetic, no libraries needed.
import math
def softmax(logits): exps = [math.exp(z) for z in logits] total = sum(exps) return [e / total for e in exps]
theta = [0.0, 0.0]probs = softmax(theta)action, G, alpha = 0, 10.0, 0.1score = [(-p if i != action else 1.0 - p) for i, p in enumerate(probs)]theta = [t + alpha * g * s for t, s in zip(theta, score)]print([round(p, 2) for p in probs])print([round(t, 1) for t in theta])print([round(p, 2) for p in softmax(theta)])Watch Out For
Trusting raw Monte Carlo returns
Returns swing wildly between episodes, so the gradient estimate swings with them. Training crawls or diverges even when the code is correct.
Symptom: updates alternate sign and the policy jitters instead of climbing. Fix: subtract a baseline, typically a fitted , and update on advantages. When that still wobbles, move to actor critic updates that bootstrap instead of waiting for full returns.
Stepping so far the policy collapses
One large step can slam an action probability near 1, and sampling then never recovers the alternatives. The policy looks confident and performs badly.
Symptom: entropy crashes in a few updates while return falls. Fix: keep steps small, normalize advantages, and clip the update size the way trust region methods do. A policy you cannot sample from is a policy you cannot improve.
The Quick Version
- Policy gradients climb directly through scaled by return.
- They handle continuous and stochastic actions where cannot even be written down.
- The score function points toward the taken action; the return decides how hard to push.
- Baselines turn returns into advantages, cutting variance without biasing the gradient.
- Here one update with return 10 moved the sampled action from 0.50 to 0.73.