Simple Algorithm
Reinforcement Learning · Exploration · 18 min

Policy Gradients & Actor–Critic

Improving behavior directly by following the gradient of expected return.

A robot's motor can apply any torque in a continuous range, so there is no short list of actions to score and compare. Policy-gradient methods skip the scoring: they tune the policy itself, making whatever turned out well a little more likely next time. The same idea, with a learned critic and a few safety rails, trains walking robots and fine-tunes language models.

What you’ll learn

  • Explain why some problems call for learning a policy directly: continuous actions, and situations where a random policy beats every deterministic one.
  • Describe the core policy-gradient update: make each action taken more likely in proportion to how well the episode went from there.
  • Explain why that update is noisy, and how a baseline or a learned critic reduces the noise without steering it wrong.
  • Explain why the size of a policy step matters, and how the natural gradient and PPO keep steps safe.
  • Name where policy-gradient methods are used: robot control, games and the fine-tuning of language models.

Learning the behavior directly

Many reinforcement learning methods learn values. Q-learning, for example, keeps a score for every action in every state and then takes the action with the best score. Policy-gradient methods take a shorter road. The policy is a function with adjustable numbers inside, its parameters θ\boldsymbol\theta (think of them as dials), that turns a state into probabilities over actions. Learning means turning the dials so that the expected return, the total reward collected, goes up.

Why learn the behavior instead of the scores? Three reasons.

  • Continuous actions. A steering angle or a joint torque can take any value in a range. Taking "the action with the best score" then means solving a maximization problem at every single step, while a policy can simply output a torque, or a distribution over torques.
  • Randomness can be optimal. When the agent cannot tell two situations apart, or faces an opponent who would exploit any pattern (think rock–paper–scissors), the best behavior flips a biased coin. An agent that always takes its top-scoring action cannot represent that.
  • Smooth change. Turning a dial slightly changes the probabilities slightly. In a value-based agent, a tiny change to two nearly tied scores can flip the chosen action completely.

When randomness wins: the switched corridor

Sutton and Barto's short corridor (their Example 13.1) makes the second reason concrete. Three cells lead to a goal, and every step costs 1, so fewer steps is better. In the first cell, right moves forward and left bumps into a wall and stays put. In the third cell, right reaches the goal and left moves back. The middle cell is the trap: there the two actions are swapped, so right sends the agent back toward the start and left carries it ahead.

The catch: all three cells look identical to the agent. It must use one rule everywhere, "choose right with probability pp", and the only thing it can learn is the number pp.

Think first Before computing anything: what happens under the two deterministic rules, always right (p=1p = 1) and always left (p=0p = 0)?

Neither ever arrives. Always right moves from the first cell to the middle cell, where right sends it back, forever. Always left stays against the wall in the first cell, forever. Both cost infinitely many steps. To get through, the agent must go left in the middle cell and right in the third, so only a rule that sometimes does each can finish.

Predict before you look

Now flip a fair coin in every cell (p=12p = \tfrac12). How many steps does it take, on average, to reach the goal from the first cell?

Show the answer

12 steps

Exactly 12 on average. For any pp, the expected number of steps is (4−2p)/(p(1−p))(4-2p)/\big(p(1-p)\big) (the Bootstrapping lesson derives it), which is 3/0.25=123/0.25 = 12 at p=12p=\tfrac12. The best coin leans slightly right: p=2−2≈0.59p = 2-\sqrt2\approx0.59 needs about 11.66 steps. Leaning right helps the agent leave the wall in the first cell sooner, but it still needs left in the middle cell, so leaning too far backfires.

A value-based agent with ε-greedy exploration cannot express p≈0.59p\approx0.59. With ε=0.1\varepsilon = 0.1 it goes right either 95% or 5% of the time, which take about 44 and 82 steps on average. An agent with a dial for pp can find the best coin, and in the next step you will watch one do it.

Make good actions more likely

How should the dials move? Here is the idea behind REINFORCE (Williams, 1992). Play an episode. Then, for every action you took, turn the dials a little in the direction that makes that action, in that situation, more likely, and scale the turn by the return that followed it. Actions followed by good returns get big pushes; actions followed by poor returns get small ones.

No model of the world is needed, and no table of action values: just episodes, their returns, and a way to make one action more probable.

Check your understanding

In the corridor every reward is −1-1, so every return is negative and every REINFORCE push lowers the probability of the action taken. How can it still improve?

  1. It cannot: REINFORCE needs some positive returns to learn
  2. Actions from short episodes are pushed down less than actions from long ones, so on average probability shifts toward actions that end episodes sooner
  3. It flips the sign of negative returns before updating
  4. It only updates the last action of each episode, which is always the step into the goal
Show the answer

B. Actions from short episodes are pushed down less than actions from long ones, so on average probability shifts toward actions that end episodes sooner

Only differences matter. Every action taken loses some probability, but actions from short episodes lose less, and since probabilities must add up to 1, the difference flows toward them. The average update still points uphill. The cost is noise: many large pushes that mostly cancel each other, which is exactly what the next step fixes.

Baselines and critics: taming the noise

REINFORCE judges each action by the raw return, so luck anywhere in the episode leaks into every verdict. The fix is to compare the return with a baseline: a prediction of how well things usually go from that state. Push an action up if the return beat the prediction and down if it fell short. Because the baseline does not depend on which action was taken, subtracting it leaves the average direction of the updates unchanged and affects only the noise, which a good prediction reduces.

Now let the prediction do more of the work. An actor–critic method trains two parts side by side. The actor is the policy. The critic learns to predict, for each state, the return to come. After every single step, the critic compares what just happened, the reward plus its prediction for the new state, with what it had predicted before the step. That difference, the TD error, measures surprise: positive if the step went better than expected, negative if worse. The actor makes the action more likely in proportion to the surprise, right away, without waiting for the episode to end.

surprise  =  Rt+1+γ V(St+1)⏟what happened  −  V(St)⏟what was expected\text{surprise}\;=\;\underbrace{R_{t+1}+\gamma\,V(S_{t+1})}_{\text{what happened}}\;-\;\underbrace{V(S_t)}_{\text{what was expected}}
VV is the critic's prediction, Rt+1R_{t+1} the reward just received, and the discount factor γ\gamma weights the future slightly less than the present.

How big a step?

The slope says which way is uphill, not how far to go. And policy steps carry a special risk: the policy collects its own data, so one bad step makes the next batch of experience worse too, and performance can collapse without recovering.

Two ideas keep steps safe. The first measures the size of a step by how much the behavior changes, not the dials. A small turn of a dial can change behavior a lot or barely at all: when the best action is almost never chosen, for instance, the plain gradient toward it is tiny. The natural policy gradient rescales each step to correct for this.

The second idea is PPO, proximal policy optimization (Schulman et al., 2017). After collecting a batch of experience, PPO improves the policy on it for several passes, but it stops rewarding a change once an action's probability has moved more than a fraction ϵ\epsilon (often 0.2) away from its value when the data were collected. The probability ratio rr, new probability divided by old, measures that change: r=1r=1 means no change.

Check your understanding

With ϵ=0.2\epsilon=0.2, an action that turned out better than expected has already had its probability raised from 0.30 to 0.39 during this round of updates. What does PPO's objective do if the update tries to raise it further?

  1. It keeps rewarding the increase, only more slowly
  2. It gives no further reward: the ratio 0.39/0.30=1.30.39/0.30=1.3 is past 1.21.2, where the objective is flat
  3. It penalizes the increase and pushes the probability back to 0.30
  4. It stops the whole training run
Show the answer

B. It gives no further reward: the ratio 0.39/0.30=1.30.39/0.30=1.3 is past 1.21.2, where the objective is flat

The ratio is 0.39/0.30=1.3>1+ϵ=1.20.39/0.30=1.3>1+\epsilon=1.2, so this sample's term is clipped at 1.2A1.2A: a constant, with zero slope. PPO does not pull the probability back; it just stops pushing. Other samples can still move it, so clipping limits the change without strictly bounding it, a subtlety the Fixed Point lesson on PPO examines.

Robots, games and language models

Continuous control. Robots and simulated bodies, such as the MuJoCo benchmarks, act with continuous torques, and their policies typically output a Gaussian (bell-curve) distribution over each torque. Off-policy actor–critics that reuse old experience, such as DDPG and SAC (soft actor–critic, which also rewards the policy for staying random), are sample-efficient there; PPO is simpler and popular when simulation is cheap.

Games. OpenAI Five, which beat the reigning world champions at the video game Dota 2 in April 2019, was trained with PPO on an enormous number of games played in parallel over about ten months (Berner et al., 2019).

Language models. Reinforcement learning from human feedback fine-tunes a language model with a policy-gradient method, classically PPO (Ouyang et al., 2022). The model is the policy, each generated token is an action, and a reward model trained on human preferences scores the finished answer. Critic-free variants such as GRPO score several answers to the same prompt and use their average as the baseline.

In the Bootstrapping lessons you'll derive The Policy Gradient Theorem & REINFORCE and solve the corridor exactly, build Actor–Critic Methods with n-step advantages and parallel actors, take safe steps with Trust Regions: TRPO & PPO, and handle motors and joints in Continuous Control: DDPG, TD3 & SAC.

Key takeaways

  • Policy-gradient methods adjust a policy's parameters directly to increase the expected return. They handle continuous actions and can represent policies that must be random to be optimal.
  • REINFORCE makes each action taken more likely in proportion to the return that followed it; averaged over many episodes, these noisy pushes follow the slope of the expected return.
  • Subtracting a baseline, a prediction of the usual return, cuts the noise without changing the average direction. An actor–critic judges every step by its critic's TD error instead of waiting for the episode to end.
  • Step size matters because the policy collects its own data: the natural gradient measures steps by how much behavior changes, and PPO stops rewarding changes beyond a small range of probability ratios.
  • These methods train robot controllers, game agents such as OpenAI Five, and language models fine-tuned from human feedback.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Which task most clearly calls for a policy-gradient method rather than Q-learning with a table of action values?

  1. Choosing one of 4 moves in a small grid maze
  2. Setting the continuous torques of a robot arm's six motors at every step
  3. Playing tic-tac-toe against a fixed opponent
  4. Picking the best of 5 slot machines
Show the answer

B. Setting the continuous torques of a robot arm's six motors at every step

With continuous actions there is no finite list of actions to score: maximizing over all torque combinations at every step is itself a hard optimization problem. A policy can output the torques, or a distribution over them, directly. The other tasks have small discrete action sets, where value-based methods work well.

Question 2

In the switched corridor all cells look the same to the agent. Why can no deterministic policy reach the goal?

  1. Because the goal moves after every episode
  2. Because deterministic policies cannot be learned by gradient methods
  3. Because one fixed action everywhere either bounces between the first two cells (always right) or stays at the wall (always left)
  4. Because all the rewards are negative
Show the answer

C. Because one fixed action everywhere either bounces between the first two cells (always right) or stays at the wall (always left)

Getting through needs left in the middle cell and right in the third. A rule that cannot tell the cells apart and always does the same thing gets stuck either way, so the best policy must randomize, with p=2−2≈0.59p = 2-\sqrt2\approx0.59.

Question 3

REINFORCE with a baseline replaces the return GtG_t by Gt−b(St)G_t - b(S_t), where b(St)b(S_t) predicts the return from state StS_t. What does this change?

  1. It lowers the noise of the updates while leaving their average direction unchanged
  2. It makes the updates point in a different, better direction on average
  3. It turns REINFORCE into a value-based method that acts greedily
  4. Nothing at all, since a prediction is subtracted from every return
Show the answer

A. It lowers the noise of the updates while leaving their average direction unchanged

The baseline does not depend on which action was taken, so on average it pushes every action equally, and because probabilities add up to 1 those pushes cancel. What remains is the same average direction with much of the shared luck removed: each action is judged by whether it beat the prediction.

Question 4

In an actor–critic method, what does the critic contribute?

  1. It chooses which action to take in each state
  2. It stores past episodes so that they can be replayed
  3. It replaces the reward, so the environment no longer needs to provide one
  4. It predicts the return to come, so each action can be judged right after it is taken by the surprise (the TD error)
Show the answer

D. It predicts the return to come, so each action can be judged right after it is taken by the surprise (the TD error)

The actor, the policy, acts; the critic predicts. Comparing the reward plus the predicted value of the next state with the earlier prediction gives the TD error, an immediate and much less noisy verdict on the action than the full return. The price is some bias whenever the critic's predictions are wrong.

Question 5

Why does PPO clip the probability ratio between the new and the old policy?

  1. To make the policy deterministic as quickly as possible
  2. To stop rewarding changes that move the policy far from the one that collected the data, since an overly large step can collapse performance
  3. To keep every action's probability at least 0.2
  4. To keep the rewards between −1 and 1
Show the answer

B. To stop rewarding changes that move the policy far from the one that collected the data, since an overly large step can collapse performance

The batch was collected by the old policy, so it says little about policies far from it, and a bad step also spoils the next batch. Clipping removes the incentive to move an action's probability outside 1−ϵ1-\epsilon to 1+ϵ1+\epsilon times its old value, which keeps each round of updates conservative.

Go deeper

Further reading

  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 13 (MIT Press, 2nd ed., 2018).
  • R. J. Williams, "Simple statistical gradient-following algorithms for connectionist reinforcement learning," Machine Learning 8, 229–256 (1992).
  • J. Schulman, F. Wolski, P. Dhariwal, A. Radford & O. Klimov, "Proximal policy optimization algorithms," arXiv:1707.06347 (2017).
  • C. Berner et al. (OpenAI), "Dota 2 with large scale deep reinforcement learning," arXiv:1912.06680 (2019).
  • L. Ouyang, J. Wu, X. Jiang et al., "Training language models to follow instructions with human feedback," NeurIPS (2022).