Policy gradients: REINFORCE
Optimise the policy directly by gradient ascent on expected return, using the score-function trick.
Animated explanation
The idea
Value-based methods learn how good actions are and act greedily. Policy-gradient methods skip that and learn the policy itself: a parameterised distribution , often a neural network with a softmax output. We adjust to make high-return behaviour more likely.
The magic is the policy gradient theorem: the gradient of expected return is the expectation of times the return. No model of the environment, no differentiating through it: just run the policy, see what happened, and push up the log-probability of each action by how good the outcome was. That is REINFORCE. It handles stochastic policies and continuous actions naturally, at the cost of noisy gradients.
Reducing variance
- Use returns-to-go (rewards after ), not the whole episode's return: an action can't affect earlier rewards.
- Subtract a baseline , e.g. a learned or the batch average: the gradient stays unbiased because , and its variance drops a lot.
- Normalise returns within a batch; average over several episodes per update.
Why learn a policy directly?
Policies can be simpler than value functions; they can be stochastic (useful in games with bluffing and with aliased states); they handle continuous actions (output a Gaussian's mean and variance); and small parameter changes give smooth behaviour changes, unlike greedy argmax. Downsides: local optima, sample inefficiency (on-policy data is used once) and noisy gradients.
The algorithm, step by step
- 1Initialise policy parameters θ (and optionally a baseline).
- 2Run an episode with : .
- 3Compute returns-to-go .
- 4Update ; repeat.
The maths
Policy gradient theorem
REINFORCE estimator
Baseline keeps it unbiased
Softmax score
Strengths and weaknesses
Strengths
- Directly optimises what we care about
- Stochastic and continuous-action policies
- Simple; works with any differentiable policy
Weaknesses
- High-variance gradients; sample-inefficient (on-policy)
- Can converge to local optima
- Sensitive to step size
Reach for it when…
Check your understanding
Lambdian Lab
REINFORCE on the corridor
A tabular softmax policy: theta has shape (N, 2), one row of scores per state.
- policy(theta, s): action probabilities (stable softmax).
- grad_log_pi(theta, s, a): gradient of log π(a | s) w.r.t. theta (a full N × 2 array).
- returns_to_go(rewards, gamma).
- reinforce(episodes, lr, baseline, seed): one update per episode (cap episodes at 200 steps); with baseline=True subtract the mean of the episode's returns-to-go. Return (theta, episode_returns).