Introduction to Reinforcement LearningPart 4 · Beyond value functions

Lecture 10Policy gradients and actor-critics

Forget values for a while and learn the policy directly: why stochastic policies help, the score-function trick, the policy gradient theorem, REINFORCE and baselines, actor-critics, trust regions, and continuous actions.

By the end you should be able to derive the score-function gradient, run REINFORCE and one-step actor-critic by hand, and explain what a baseline and a trust region do.

Key ideas, one per episode

  • Ep. 38Sometimes the best policy is simpler than the world.
  • Ep. 39Weigh ∇log π by the reward, minus a baseline.
  • Ep. 40Policy gradients never need the dynamics.
  • Ep. 41The critic judges; the actor follows the TD error.
  • Ep. 42Let a distribution do the exploring.

Based on: Lecture 9: Policy Gradients and Actor Critics, DeepMind × UCL Reinforcement Learning Lecture Series (2021), by Hado van Hasselt. The slides follow that order of topics, in our own words, figures and examples; they are not official material of the original course.

The title slide of Lecture 10: Policy gradients and actor-critics

43 slides, written to be read on your own, with worked examples, “try it” boxes, small simulations and check-yourself questions (answers at the end). PDF, 1.2 MB.

Watch

5 short episodes of ACML Explains for this lecture. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 38 · 5:12

Why learn the policy?

What's the best way to beat a casino? Don't play. Sometimes the best policy is simpler than the world.

Sometimes the best policy is simpler than the world.

TranscriptEpisode 38

The long way around

Module ten: policy gradients and actor critics. Quick recap. So far we always took the long way around. Learn values, then read a policy off them. Or learn a model, plan with it, and get a policy. Even the question of which policy is better, we answered with values. Policy pi beats policy pi prime if its value is at least as high in every state. And remember the flips? A tiny error in the values, and the greedy policy suddenly jumps to a completely different action. So today, a new idea. Why not learn the policy directly?

Three roads

There's a lovely line from Vladimir Vapnik: do not solve a more general problem as an intermediate step. Three roads lead to the same place, the optimal policy. Model-based, value-based, and policy-based. Sometimes the model is the easy part. If the dynamics are simple, a few samples teach you how the world moves. And sometimes the dynamics are a nightmare, but the best policy is simple. Always move forward, for example.

Beat the casino

A well known joke in statistics. What's the best way to beat a casino? Don't play. It sounds silly, but statistically it's the right answer. The expected value of every casino game is against the player. Poker, roulette, pick your favourite. The dynamics are anything but simple. The optimal policy? Whatever the state, do nothing. Complex world, trivial policy. In a case like that, learning a model or every value is a lot of work for

Pros and cons

nothing. The advantages. First, we optimise the true objective. For the first time, we learn exactly the thing we wanted to learn. It extends easily to huge or continuous action spaces. It can learn stochastic policies. And sometimes the policy is just simpler. The disadvantages. It can get stuck in a local optimum. The knowledge it learns can be very specific. And on its own, it doesn't squeeze every drop of information out of the data. Sound familiar? Gradient descent with backpropagation has exactly the same problems. They come from optimising over functions, not from reinforcement learning.

A policy, directly

A policy, written directly: the probability of taking action a in state s, given parameters theta. If the policy is deterministic, those probabilities are zero or one. If it's stochastic, anything in between. The same notation covers both. Now, in a fully observed MDP, there's always an optimal deterministic policy. So why would we ever want randomness? Because most problems aren't fully observed. Board games, maybe. Almost everything else, no. And with function approximation, states blur together all the time. Randomness also makes the landscape smooth, so gradients can work. And it explores for free while we learn.

The aliased corridor

Here's a small world. A corridor of five cells. Gold below the middle one, and a skull below each end. The agent only feels the walls around it. And the two grey cells feel exactly the same: a wall above, a wall below, open on both sides. A deterministic policy must do the same thing in both grey cells. Say it always goes right. From the left half, great, you reach the gold. From the right half, you step right, the corner sends you back left, and you bounce between those two cells forever. We simulated it. From a random start, the deterministic policy reaches the gold sixty percent of the time. The other forty percent, it walks back and forth until we stop it.

Flip a coin

Now flip a coin in the grey cells. Fifty percent left, fifty percent right. What's the chance you go left ten times in a row? One half to the power of ten. About one in a thousand. Left forever? Zero. In our runs, the coin-flip policy reached the gold every single time, in about three point eight steps on average. And this isn't random tie-breaking. When we learn the policy directly, the probabilities can be anything, not just fifty fifty. Whatever works best.

What we maximise

So what exactly are we maximising? Imagine you love diving. I give you a policy: every minute, go down one more metre, no matter what. Good policy? At some point you die. Call that minus infinity. Not a great dive. For episodes like a dive, we judge a policy by its expected total return from the start. That's the value of the start state. Where do we start? From a start state distribution, d zero. In chess, it's a single board. In blackjack, it's every pair of cards for you and the dealer. A lot. For tasks that never end, we use the average reward per step instead. Physicists will tell you the world isn't really continuous. The computer certainly isn't. There's always a discretisation somewhere.

The pawn nobody meets

One more strange thing. Remember the white pawn on the first row? A legal board you can never reach. A value table would eventually meet that state and learn it. This objective never will. It only sees what the start distribution can reach. In theory, that's a gap. In practice? If I want to make you the best chess player in the world, do I care that you can't solve an impossible position? No. Next time: how do we actually climb this objective? With one beautiful trick.

Interactive lab

Policy-gradient bandit lab

Learn a softmax policy directly with the gradient-bandit (REINFORCE) update, with or without a baseline. Shift every reward up by a constant and see why the baseline matters.

Ten arms, each paying a noisy reward around its own hidden mean. The policy is a softmax over preferences H, and each pull moves H along the score-function gradient: H(a) += α (R − b)(1[a = A] − π(a)). The setup follows Sutton and Barto (Section 2.8).

Simple math

Learn the policy, not the values

Ten arms. Arm a pays a reward drawn around its hidden mean q*(a), with spread 1; the means are drawn around the reward offset.

Softmax policy
π(a) = e^H(a) / Σ_b e^H(b)

Preferences H start at 0, so every arm starts with probability 1/10.

Score-function update
H(a) ← H(a) + α (R − b)(1[a = A] − π(a))

Raise the arm you pulled if its reward beat the baseline b, lower the others, and the reverse if it did worse.

Why a baseline
b = R̄, the average reward so far

Any b leaves the expected gradient unchanged, but a good b cuts its variance. With b = 0 and rewards around +4, every pulled arm gets pushed up.

Pulls
0
Average reward
–
Baseline b
–
Best arm, last 100
–

A fresh bandit: every arm has probability 0.10.

Policy over the arms

Bar height: π(a). Under each bar, the hidden mean q*(a); the best arm is green.

How often it picks the best arm

Your run: share of the last 50 pulls that went to the best arm.

your run

Play

A game that goes with this lecture, and what to look for while you play.

Echo Heist

Teach a little robot. Steal back a city.

In this lecture: Echo Heist can train a policy-gradient learner too: a policy that is learned directly, with no value table behind it.

Choose what your partner Echo senses, which actions it can take and what success means, then let a real learner train in your browser: tabular, planning, policy-gradient, deep-RL or imitation. When the door opens, the policy is frozen and Echo runs with what it learned.