Policy Gradient Methods
Push up the probability of actions that paid off. The estimate is unbiased and wildly noisy, so every refinement after REINFORCE is about shrinking variance.
Why Does This Exist?
A robotic arm on a parcel sorting line picks up boxes arriving at a slightly different angle every time. Its actions are real numbers: wrist angle in degrees, gripper force in newtons, descent speed. No list of options to score.
Q-learning needs that list, because taking a maximum means enumerating. Ten buckets of wrist angle throw away the precision that makes a grasp hold. A thousand buckets times two more dimensions, and the count passes a billion.
Second reason: sometimes the best behaviour is genuinely random. With a parcel's pose only roughly known, a controller that always tries the identical grip either always succeeds on that box shape or always fails.
So stop learning what actions are worth. Optimise the policy itself.
Think of It Like This
The kitchen graded by the night's total takings
A kitchen tries slight variations each service: more acid in the dressing, thirty seconds less on the fish. At the end of the night, one number: total takings.
Every dish shares it. The best dessert anyone ate all week gets marked down because the kitchen ran out of fish at eight; a mediocre plate on a full Saturday gets a gold star. The signal is real, just buried.
Then someone judges tonight against a normal Tuesday instead of zero. Same information, but the number now means better or worse than usual. The noise drowning everything was a constant nobody needed.
Where it stops: a kitchen can taste tonight's dishes directly and blame one plate. An agent sees only the nightly total, so credit assignment across forty actions stays statistical no matter how good the baseline is.
How It Actually Works
Score the action, then push its probability
The policy is a network: state in, parameters of a distribution over actions out. For continuous control that is usually a mean and a spread per dimension, so wrist angle is drawn from a bell centred wherever the network thinks it belongs. Exploration comes free, and the policy can narrow that spread itself.
You maximise expected return , with the network's weights. The policy gradient theorem says the gradient of that return is an average over behaviour the policy actually produced:
is the density the policy puts on action in state , is the direction in weight space that makes it more likely, and is the discounted return that followed. REINFORCE is that expression, sampled.
Notice what is absent: nothing differentiates through the conveyor. You need only your own network's gradient, which is why this works on black boxes and real hardware.
The wall: an unbiased estimate you cannot use
It is unbiased, and it converges given enough episodes. The trouble is how many enough is.
One sorting cycle is forty actions and one return, and all forty get multiplied by the same number. A textbook grasp in a cycle where the conveyor jammed downstream gets a negative weight and is trained away; a sloppy grasp on a lucky cycle gets reinforced.
Scale makes it worse. Say a cycle earns between 190 and 210. Every action gets multiplied by roughly 200, a constant that says nothing about any individual action while inflating every sample's variance. Monte Carlo estimation has a fixed rate: halve the noise, quadruple the samples. Here, samples are grasps.
Baselines, critics, and a leash on the update
Everything after REINFORCE is variance reduction wearing a different hat.
A baseline subtracts , anything that depends on the state but not the action. The expectation does not move: the expected value of under the policy's own actions is zero, so an action-independent factor vanishes in the average. Variance falls hard.
The natural is the state value , what you would normally get from here. Then is the advantage: how much better this action turned out than usual. Absolute number becomes comparison, the same move the kitchen made. Actor-critic learns with a second network while the policy trains, as actor-critic methods describe.
A large update also changes the data you collect next, so an over-eager step can leave the policy gathering useless experience. PPO limits the move bluntly: take the ratio of new to old probability for an action, and stop granting credit once it leaves a narrow band around one.
The bill is sample efficiency: that expectation is over the current policy, so one step leaves your data describing a policy that no longer exists. Q-learning replays a transition dozens of times; this spends a batch once. Sorting arms learn in simulation for a reason.
Code
The point is the check: a one-step policy whose return carries a large constant offset, with and without a mean baseline.
import random
def grad_samples(baseline: float, trials: int = 20000) -> list[float]: """One-step Gaussian policy at mean 0.5; the return peaks at 2.0, plus a flat offset.""" rng = random.Random(3) out: list[float] = [] for _ in range(trials): a: float = rng.gauss(0.5, 1.0) ret: float = 40.0 - (a - 2.0) ** 2 # the offset is the point: it carries no information out.append((a - 0.5) * (ret - baseline)) # score gradient times return return out
def summarize(samples: list[float]) -> tuple[float, float]: mean: float = sum(samples) / len(samples) var: float = sum((x - mean) ** 2 for x in samples) / len(samples) return mean, var**0.5
average_return: float = 40.0 - (0.5 - 2.0) ** 2 - 1.0 # what this policy earns on averagefor b, name in ((0.0, "no baseline"), (average_return, "mean baseline")): m, s = summarize(grad_samples(b)) print(f"{name:14s} mean {m:+.3f} spread {s:.3f}") # -> no baseline mean +2.806 spread 35.109 # -> mean baseline mean +3.046 spread 5.396The true gradient is exactly 3.0 and both estimates sit near it: unbiased, as promised. The spread drops from 35.1 to 5.4, more than sixfold, purely from subtracting a number that says nothing about the action taken.
Watch Out For
Raw episode returns, and a learning curve that is mostly luck
The return curve climbs, dives, then climbs higher than before. Re-run with a different seed and the shape is unrecognisable: two seeds look like two algorithms, and you cannot tell whether last week's change helped.
That is REINFORCE with no baseline. Variance is large enough that which episodes went well dominates which actions were good, so training follows the sampling noise. Keep a running average of recent returns and subtract it; the curves tighten immediately. Then move to a state-dependent baseline, because one scalar cannot separate a hard state from a badly chosen action.
Bolting a replay buffer onto an on-policy method
Value learning got dozens of updates per transition, so the temptation is obvious: keep the last hundred thousand grasps and sample from them. Throughput looks great, then the policy quietly gets worse while the loss stays healthy.
The expectation is over actions the current policy would take. Data from three updates ago came from a different distribution, so averaging it in estimates the gradient of a policy you no longer have: biased, not just noisy, and more data does not fix bias. Correct it with an importance ratio between new and old policy, which is what PPO's clipping keeps from exploding, or just collect fresh batches.
The Quick Version
- A network outputs a distribution over actions; you do gradient ascent on expected return. Continuous actions need no discretisation.
- The gradient is the log-probability gradient of the action taken, weighted by the return that followed. REINFORCE is that, sampled.
- Nothing differentiates through the environment.
- Unbiased, and brutally noisy: one return is shared across every action in the episode, so good actions in bad episodes get punished.
- Our offset test reads means near the true 3.0 with spreads 35.1 without a baseline and 5.4 with one.
- A baseline cuts variance sharply. Set it to for the advantage; learn it with a second network for actor-critic.
- Limit each update, because it changes the data you collect next. PPO clips the probability ratio.
- On-policy, so data goes stale every step: orders of magnitude more interaction than off-policy value learning.