A robot learns which way to turn at a fork. Weight each update by the raw return, pay a steady reward on every step, and half the runs learn the wrong turn. Subtract the critic's estimate and all 8 learn the right one.
Pick a learning signal and drag the reward every step. Watch the 8 runs. Then we build, piece by piece, the baseline, the advantage, the critic, the TD error and GAE.
You need a little Python, what a probability is and the idea of a return, the sum of rewards. We build the rest from zero.
Lecture 10 of 25 in Introduction to Robot Learning, CMU 16-831 (Spring 2024), taught by Guanya Shi. Slides on the course lectures page. Every idea is re-taught here in our own words and devices. Before: Lecture 9, Policy Gradients.
Illustrative toy. The short turn pays 2 more than the long turn. Nine corridor steps follow, each with the steady reward plus random noise (standard deviation 1). Discount 0.9, actor step 0.05, critic step 0.2. A run is "wrong" if it ends below 20% on the short turn.
Chapter 0
Roll the same good turn again and again, and watch its learning signal jump around
A delivery robot reaches a fork. The short turn is better by 2 reward. After the fork come nine corridor steps, and each step pays a random reward that has nothing to do with the turn.
Lecture 9 built REINFORCE. Run an episode and add up its return, the discounted sum of rewards. Then make each action more likely in proportion to that return. The return is the weight of the update. The trouble is what goes into it.
The robot always takes the short turn. The top bars are one episode's rewards. Each roll adds a dot: the weight REINFORCE puts on that turn. Switch on the critic to see the weight it uses instead.
Illustrative toy. Corridor reward 1 per step plus noise of standard deviation 1, discount 0.9, a 50/50 policy. The critic here knows the true values, so its weight is the fork reward's own surprise.
Work the spread. The fork reward has a standard deviation of 0.5. The nine corridor rewards add their own noise, shrunk by the discount. Together the return has a standard deviation of about 2.0, four times the fork's own noise. The turn's real edge is +1, so one roll often says nothing useful.
The lecture's fix has two parts. Keep a learned estimate of how good each state is. Then weight each action only by the part of the outcome that beat that estimate. The estimate is the critic. The policy that picks actions is the actor.
The symptom is a learning curve that wanders, with seeds that disagree. To measure it, compute the gradient estimate many times at one fixed policy and take its standard deviation. The fix is a baseline and then a critic, the subject of this lesson.
Chapter 1
Subtract a number that depends only on the state, and prove the average gradient stays put
Lecture 9 ended with the policy gradient. Call the policy's parameters θ (theta) and the chance it picks action a in state s πθ(a|s). Call the expected return after taking a in s Q(s,a). The lecture then subtracts a baseline b(s): any number that depends on the state and not on the action.
Grad-theta of J is proportional to the expected value of Q of s and a minus b of s, times grad-theta of log pi-theta of a given s.
For each sampled pair of s and a, find the direction that makes a more likely. Scale it by Q of s and a minus b of s. Average over many samples. That is the gradient, up to a constant.
With made-up numbers: two turns at 50/50, Q 101 for short and 99 for long. For the short turn's logit, grad log pi is +0.5 for short and −0.5 for long. With b = 0 the two samples are 50.5 and −49.5. With b = 100 both are +0.5. The average is +0.5 in both cases, but the spread falls to zero.
Why is subtracting b(s) free? Its extra term is b(s) times the sum over actions of π(a|s) times grad log π(a|s). Because b(s) does not depend on the action, it moves outside the sum. What stays inside is zero.
The sum over a of pi-theta of a given s, times grad-theta of log pi-theta of a given s, equals grad-theta of 1, which is 0.
Weight each action's push direction by its chance pi. Add them up. The pushes cancel, because the chances always add to 1. So b of s times this sum adds nothing on average.
With made-up numbers: three actions with chances 0.2, 0.5 and 0.3. For the logit of the middle action, grad log pi is −0.5, +0.5 and −0.5. The weighted terms are −0.10 + 0.25 − 0.15 = 0.
Three turns at a junction. Change the preference for "straight", then pick any baseline. The weighted terms always add to zero, so the baseline adds zero.
Exact softmax arithmetic. The left and right logits stay at 0.4 and 0.
The common choice of b(s) is V(s), the expected return from s under the current policy. It is not exactly the lowest-variance baseline, but it is close and easy to learn.
The symptom is a baseline that changes which policy you learn. To measure it, check whether b reads the action. The fix is to feed it the state only.
Chapter 2
Set the baseline to V, and see the weight measure only what the action changed
The robot is at a junction where every turn earns about 60, because the battery bonus pays the same on every route. Raw REINFORCE weights every sampled turn by about 60 and pushes it up hard. It learns almost nothing about which turn is better.
Set the baseline to the state's value. Call the expected return from state s under the current policy V(s). It is the average of Q(s,a) over the actions the policy would pick. What is left has a name, the advantage.
A of s and a equals Q of s and a minus V of s.
Take the value of the action, Q. Subtract the value of the state, V. If A is above zero, make the action more likely. If A is below zero, make it less likely.
With made-up numbers: Q = 12 for the short turn and V = 10 at the fork. A = 12 − 10 = +2. The short turn beats the policy's average by 2, so the actor raises its chance.
The policy gradient becomes the expected value of A(s,a) times grad log π(a|s). The rest of deep reinforcement learning, A2C and PPO included, starts from this form.
Three turns at 1/3 each. Each turn's Q is the common level plus its own edge. Raise the level, then switch to the advantage. Arrows show how hard the actor pushes each turn when it samples it.
Illustrative toy. Edges +2, −1 and +0.5, so V = level + 0.5 under the uniform policy.
The lab below does the same with 4,000 sampled returns. You write one line.
The symptom is a policy that drifts toward whatever it sampled most, not toward the best action. To measure it, log the mean of the weights: a raw return far from zero is the offset. The fix is to weight by the advantage, which centres the weights near zero.
Chapter 3
Fit a second network to the returns you logged, and see what a wrong critic does and does not break
The advantage needs V(s), and nobody gives us V. So we learn it. The critic is a second network with its own parameters φ (phi). It reads a state and outputs one number, Vφ(s), its guess of the expected return from there.
Learning V for a fixed policy is policy evaluation, from Lecture 7. The plainest way is regression. Run the policy, log each state st and the return that followed it, Gt, then fit the critic to those pairs.
L of phi equals one half the sum over t of G-t minus V-phi of s-t, squared.
For each logged state, subtract the critic's guess V-phi of s-t from the return G-t. Square the gap. Add the squares and halve the total. Change phi to make L smaller.
With made-up numbers: three states with returns 6, 4 and 8, and a critic that says 5 for each. L = ½ (1 + 1 + 9) = 5.5. Most of the loss comes from the return of 8.
Each blue dot is one logged state: its distance to the charger and the return that followed. Press Fit to take gradient steps on L. Log fewer episodes and watch the fit wobble.
Illustrative toy. True value 10 − 0.8 × distance, return noise of standard deviation 1.5. The critic is a line, V = w0 + w1 × distance.
Each G-t is a full noisy rollout, so the critic's targets carry the same noise as REINFORCE. The critic averages that noise over many states. That helps, but every target still waits for the episode to end.
The symptom is a critic loss that falls on training states and rises on new ones. To measure it, hold out some episodes and compare the loss. The fix is more episodes per update, or a smaller critic.
Chapter 4
Take one real step, trust the critic for the rest, and trade a little bias for much less noise
The lecture asks: can we do better than waiting for the episode to end? The Bellman equation from Lecture 7 gives a shortcut. The value of a state is the average next reward plus the discounted value of the next state.
So take one real step. Call the reward r, the next state s′ (s-prime) and the discount γ (gamma). The bootstrapped target is r plus gamma times V-phi of s-prime: one real reward and the critic's guess for the rest. The gap between that target and the current guess is the temporal-difference (TD) error.
Delta equals r plus gamma times V-phi of s-prime, minus V-phi of s.
Add the real reward r to gamma times the critic's guess for s-prime. That is the target. Subtract the critic's guess for s. If delta is above zero, the step beat the critic's expectation.
With made-up numbers: a step costs r = −0.1, V-phi of s-prime = 3.0, V-phi of s = 2.5, gamma = 0.9. Target = −0.1 + 2.7 = 2.6. Delta = 2.6 − 2.5 = +0.1. The step went a little better than expected.
Delta does two jobs. It trains the critic: move V-phi of s toward the target, which shrinks delta. It also steers the actor, because the target estimates Q(s,a), so delta estimates the advantage A(s,a). There is no max in it, unlike Q-learning. The critic scores the policy you have, not the best possible one.
The dashed line is the true value, 5. Yellow dots are full observed returns. Pink dots are TD targets, which use the critic for everything after one step. Give the critic an error and watch where each cloud sits.
Illustrative toy. Full returns have standard deviation 2.0. The one-step reward has standard deviation 0.5. Gamma 0.9, so a critic error e at s-prime shifts the TD target by 0.9 e.
The symptom is an actor that learns a steady wrong preference while the critic loss looks small. To measure it, compare V-phi with Monte Carlo returns on a held-out batch. The fix is a better critic, or more real steps per target (Chapter 7).
Chapter 5
Step one transition through the actor, the world and the critic, and see delta split into two updates
Actor-critic is two function approximators on one stream of experience. Here is one transition, in the order the loop runs.
The critic update moves phi along delta times the gradient of V-phi of o. The actor update is the policy-gradient step with delta in place of the advantage. Call the actor's step size απ (alpha-pi).
Theta becomes theta plus alpha-pi times delta times grad-theta of log pi-theta of a given o.
Find the direction that makes action a more likely. Scale it by delta and by alpha-pi. Add it to theta. A negative delta flips the direction, so a worse-than-expected action becomes less likely.
With made-up numbers: two actions at 50/50 and one logit for the taken action. Its grad log pi is 1 − 0.5 = 0.5. With delta = 2.4 and alpha-pi = 0.5, the logit rises by 0.5 × 2.4 × 0.5 = 0.6.
Pick what the robot does, then press Step. Each row lights in the order the loop runs and shows its shape and value.
Illustrative numbers. V-phi of o = 5.0, V-phi of o-prime = 6.0, gamma 0.9, both step sizes 0.5, a tabular critic.
| Choice | Reason |
|---|---|
| Two heads | The actor outputs a distribution; the critic outputs one number. Different shapes need different losses. Many systems share the early layers, then split. |
| Two step sizes | A wrong critic gives the actor wrong deltas, so practitioners often let the critic learn faster. |
| No max | The critic scores the current policy, a fixed target for this batch. |
| On-policy | The gradient is an average over actions of the current policy. Old data comes from another policy, so it biases the estimate. |
The symptom is a policy that gets worse after you add a replay buffer. To measure it, compare the policy that collected a batch with the policy you update. The fix is to use fresh data each update, or move to an off-policy method with a Q-critic (Lecture 11).
Chapter 6
Compute a TD error, a critic update, an actor update and a 3-step return with a calculator
The critic believes V-phi of s = 5.0. The robot takes action a, gets r = 2.0 and lands where V-phi of s-prime = 6.0. Gamma is 0.9 and both step sizes are 0.5. The actor has two actions with equal logits, so each has chance 0.5.
The top line is the critic: its old guess, the target, and where it moves. The bars are the actor: the chance of action a before and after. Drag the reward and the next state's value.
Exact arithmetic. V(s) = 5.0, gamma 0.9, both step sizes 0.5, two actions starting at 50/50.
Now a 3-step target, the bridge to Chapter 7. Rewards 2, 1 and 3, then the critic says V(s3) = 4. The 3-step return is 2 + 0.9 × 1 + 0.81 × 3 + 0.729 × 4 = 2 + 0.9 + 2.43 + 2.916 = 8.246. It trusts three real rewards before the critic. The 1-step target trusts the critic after one.
The lab runs the same step in code. You write the TD error and the actor step.
The symptom is a policy that jumps to one action after a few updates and never explores again. To measure it, track the policy's entropy and the size of each delta. The fix is a smaller actor step, or clipping the advantage.
Chapter 7
Choose how many real rewards to use before trusting the critic, then blend every choice with one number
We have two ends. The 1-step TD error is quiet but inherits the critic's error. The Monte Carlo advantage has no bias but much noise. The n-step advantage sits between them: use n real rewards, then the critic.
A-n at t equals the sum over k from 0 to n minus 1 of gamma to the k times r at t plus k; plus gamma to the n times V of s at t plus n; minus V of s-t.
Add n real rewards, each shrunk by gamma once per step of delay. Add the critic's guess at step t plus n, shrunk n times. Subtract the critic's guess at step t.
With the numbers from Chapter 6: rewards 2, 1, 3, V(s3) = 4, gamma 0.9, and V(s0) = 5. A-3 = 8.246 − 5 = 3.246.
Generalized Advantage Estimation (GAE), from Schulman and others (2016), does not pick one n. Write the TD error at each step as δt. GAE adds the future TD errors, each shrunk by gamma times a knob λ (lambda) once per step.
A-GAE at t equals the sum over k of gamma-lambda to the k, times delta at t plus k.
Take this step's delta. Add the next delta times gamma-lambda, the one after times gamma-lambda squared, and so on. Lambda 0 keeps only the first delta. Lambda 1 sums them all, and the V terms cancel into the full return minus V of s-t.
With made-up numbers: deltas 1.0, −0.5 and 2.0, gamma 0.9, lambda 0.5, so gamma-lambda = 0.45. A = 1.0 − 0.225 + 0.2025 × 2.0 = 1.18. At lambda 0 it is 1.0. At lambda 1 it is 1.0 − 0.45 + 1.62 = 2.17.
Six pink bars are one rollout's TD errors. Their colour strength is the weight GAE gives each one for step 0. Drag lambda from 0 to 1.
The rollout from the lab below: rewards 1, 0, 2, −1, 1, 3, critic values 5.0 to 2.0, gamma 0.95. Exact arithmetic.
In practice, lambda near 0.95 is a common default. The GAE paper reports good results for lambda between 0.92 and 0.98 on its tasks. The lab asks you to write the one-line backward recursion.
The symptom at lambda near 1 is noisy advantages and seeds that disagree; near 0 it is a steady wrong preference from a poor critic. To measure it, sweep lambda on a few seeds and plot the final return. The fix is to pick the lambda with the best mean and the smallest spread.
Chapter 8
Train both on a 5 by 5 grid, then break it with noise and lambda
A robot starts in the top left corner. The charger sits in the bottom right and pays +1. Every other step costs 0.04. The critic keeps one value per cell. The actor keeps four logits per cell, one per direction. After each episode, both update from GAE advantages.
Pink shading is the critic's value. Purple arrows are the actor's favourite move. Press Train. Then set reward noise high, try lambda 1, and compare with lambda 0.5.
Illustrative toy. Gamma 0.95, critic step 0.1, actor step 0.3, advantages clipped to ±2, episodes capped at 60 steps. The shortest route is 8 steps.
Watch two things. The value spreads backward from the charger: the cell next to it learns first, then its neighbours. Each cell's target borrows its neighbour's guess, which is bootstrapping. The arrows then turn toward higher value, because a move into a better cell gives a positive delta.
Now break it. We ran four seeds for 400 episodes at reward noise 1.0. With lambda 1, three of four runs still wandered for the full 60 steps. Lambda 0.5 solved all four in about 8 steps. Lambda 0 also solved them but learned more slowly at first, because one-step deltas move value back one cell per visit.
The symptom is arrows that keep flipping and episodes that hit the step cap. To measure it, plot the mean episode length over the last 20 episodes. The fix is a lower lambda, which leans on the critic, or a smaller actor step.
Chapter 9
Keep the terms, the numbers, a checklist and the core update in code
REINFORCE weights each action by a noisy return. A state baseline removes the shared part for free. The best-known baseline, V, turns the weight into the advantage. A critic learns V. Its TD error trains itself and steers the actor. GAE sets how far to trust the critic.
| Term | One line | Number to remember |
|---|---|---|
| Baseline b(s) | Subtract a state-only number; the average gradient does not change. | −0.10 + 0.25 − 0.15 = 0 |
| Advantage | A = Q − V: better than the policy's average? | Q 12, V 10: A = +2 |
| Critic loss | ½ sum of (G-t − V-phi of s-t) squared. | Returns 6, 4, 8 vs 5: L = 5.5 |
| TD error | δ = r + γV(s′) − V(s); trains the critic, steers the actor. | 2 + 5.4 − 5 = +2.4 |
| Actor step | θ += α δ grad log π(a|o). | 50/50 becomes 0.769 |
| n-step return | n real rewards, then the critic. | 2, 1, 3, then 4: 8.246 |
| GAE | Sum of (γλ) to the k times δ-t+k. | Lambda 0.92 to 0.98 in the GAE paper |
| On-policy | Update on data from the current policy only. | Stale data biases the gradient |
pythonimport numpy as np def gae(r, V, gamma=0.99, lam=0.95): # r: rewards, length T. V: critic values, length T+1 (last = bootstrap, 0 if terminal) T = len(r); A = np.zeros(T); acc = 0.0 for t in reversed(range(T)): delta = r[t] + gamma * V[t + 1] - V[t] # TD error at step t acc = delta + gamma * lam * acc # A_t = delta_t + (gamma*lam) * A_{t+1} A[t] = acc return A, A + V[:-1] # advantages, critic targets # one update on a fresh batch (PyTorch-style pseudocode) # A, target = gae(rewards, values) # actor_loss = -(A.detach() * logp_taken_actions).mean() # critic_loss = 0.5 * ((target.detach() - V_phi(states)) ** 2).mean()
This lesson follows Lecture 10, "Actor-Critic Methods", of CMU 16-831 Introduction to Robot Learning, Spring 2024, taught by Guanya Shi. The slides are on the lectures page. GAE is from Schulman, Moritz, Levine, Jordan and Abbeel, High-Dimensional Continuous Control Using Generalized Advantage Estimation (ICLR 2016).
Now press Present or Teach and explain from memory: why a baseline is free, what the advantage means, why the TD error does two jobs, why actor-critic is on-policy, and what lambda trades. Then go back to the fork, set the reward every step to 6, and say why the raw-return runs split while the other two agree.