Introduction to Robot Learning · Lecture 9 of 25 · CMU 16-831

Policy Gradient Methods

Stop learning a value function and then arguing about it. Optimize the policy directly — one elegant identity turns "good trajectories should be more likely" into a gradient you can actually estimate from rollouts.

Prerequisites: gradients & the chain rule + what a policy and a return are (Lecture 7's MDP). A little Python.
10
Chapters
5
Simulations
3
Code Labs

Chapter 0: When the Value Function Gets in the Way

For two lectures you have learned to be a value-function person. You estimate Q(s, a) — "how good is taking action a in state s?" — and then you act by picking the action with the biggest Q. Q-learning, DQN, all of it, is built on that one move: learn the value, then read the policy off it.

That move quietly assumes one thing: that you can take a max over actions. With four buttons on a joystick, fine — check all four Q-values, pick the biggest. But a robot arm's action is a vector of seven continuous joint torques. There is no list of actions to scan. "Take the max over a continuous, seven-dimensional space" is its own hard optimization problem, solved inside every single decision, at fifty hertz, forever.

The reframe that starts this lecture. Guanya Shi opens with a blunt question: "Why learn a value function when all we need is a policy?" The policy is the only thing the robot actually uses. So why not skip the middleman entirely and optimize the policy directly? That is the whole idea of policy gradient methods.

Here is the cleanest example, straight from the slides. The Linear Quadratic Regulator (LQR) is a classic control problem — quadratic costs, linear dynamics. When you solve it, two objects fall out. The optimal value function is a quadratic form in the state. The optimal policy is just a linear map: action = −K times state. One is a fat matrix sandwiched in a quadratic; the other is a single gain matrix K.

Which would you rather learn? A short linear rule, or the bulky quadratic surface whose only purpose is to imply that linear rule? Sometimes the policy is simply the smaller, friendlier object. Learning it directly skips a hard intermediate problem.

A stochastic policy sharpening toward reward

A one-state world with three actions; only the middle one pays off. The bars are the policy's action probabilities — what π would output. Press Step to nudge the policy by the policy-gradient rule (rewarded actions get pushed up). Watch a vague, near-uniform policy sharpen onto the good action. No Q-table, no max — the policy itself moves.

Near-uniform policy. Press Step.

Notice what the demo is doing and what it is not. It never asks "what is the value of being in this state?" It only ever asks "did that action lead to reward? then make it more likely." That single instinct — turn up the probability of actions that paid off — is everything. The rest of this lesson is the math that makes it precise and the engineering that makes it usable.

Three reasons to optimize the policy directly.
  • Continuous actions are natural. No max over actions to solve — the policy outputs the action directly.
  • Stochastic policies are first-class. The output is a probability distribution, which builds in exploration and handles partial observability gracefully.
  • Sometimes the policy is just simpler than the value function it would imply (the LQR linear-vs-quadratic story).
For a robot arm with seven continuous joint torques, why is the value-function recipe (estimate Q, then act greedily) awkward?

Chapter 1: What Exactly Are We Maximizing?

Before we can take a gradient, we need a thing to take the gradient of. Let's nail down the objective with no hand-waving, because every later step refers back to it.

Our policy is πθ(a | s) — a probability distribution over actions, with knobs θ (the network weights). A run of the robot produces a trajectory τ = (s0, a0, s1, a1, …). Each trajectory earns a return R(τ) — the total reward summed along it. Different runs give different trajectories because the policy is random and the world is random.

So the only sensible thing to maximize is the expected return — the average return if you rolled out this policy over and over:

J(θ) = Eτ ∼ pθ(τ) [ R(τ) ]

Read it aloud: J of theta is the average total reward of trajectories drawn from the policy with parameters theta. The subscript τ ∼ pθ(τ) is doing real work — it says the trajectories are sampled using this very policy. Change θ and you change which trajectories you tend to see. That coupling is the whole difficulty, and we will return to it.

What is pθ(τ), the probability of a whole trajectory? Multiply the chances of every step. You start in some state s0 with probability p(s0). Then for each step: the policy picks the action with probability πθ(at|st), and the world moves you with probability p(st+1|st, at). The trajectory's probability is the product of all of these.

pθ(τ) = p(s0) · ∏t πθ(at|st) · p(st+1|st, at)

Stare at that product. Only the middle factor — the policy — depends on θ. The start distribution p(s0) is fixed by the environment; the transition dynamics p(st+1|st, at) are the laws of physics, which the robot does not control. We will use that fact in Chapter 3 to make most of this expression vanish. Tuck it away.

Step one: can we even evaluate J(θ)?

Yes — with the most honest tool there is, Monte Carlo sampling. Roll out the current policy N times, get N trajectories, average their returns. That sample average is an unbiased estimate of the true expected return.

J(θ) ≈ (1/N) ∑i=1N R(τ(i))

This is the same trick you would use to estimate "the average roll of a die": you cannot reason it out from a formula you do not have, so you roll many times and average. Evaluating J is easy. The hard part — the part this whole lecture is about — is getting its gradientθJ(θ), so we know which way to nudge θ.

The plan from here. We have an objective J(θ) we can sample. We want to do gradient ascent on it: θ ← θ + η·∇θJ(θ). The entire challenge is computing that gradient from rollouts — without ever differentiating the unknown physics. The next chapter is the one clever identity that makes it possible.

In J(θ) = Eτ ∼ pθ[R(τ)], why must we take an expectation over trajectories rather than maximizing R(τ) for one fixed τ?

Chapter 2: The Log-Derivative Trick

Everything in this lecture hinges on one tiny calculus identity. The slides call it the "useful identity"; in the wider literature it is the log-derivative trick or the score-function trick. It is two lines, and once you have it the policy gradient theorem is almost free.

Start from the ordinary derivative of a logarithm. For any positive function f(θ), the chain rule gives:

θ log f(θ) = ∇θ f(θ) / f(θ)

Now just multiply both sides by f(θ) to clear the denominator:

θ f(θ) = f(θ) · ∇θ log f(θ)

That is the whole trick. Apply it with f = pθ(τ), the trajectory probability:

θ pθ(τ) = pθ(τ) · ∇θ log pθ(τ)
Why this is the key that unlocks everything. On the left is a gradient of a probability — a thing we cannot sample (you don't draw "the gradient of a probability" from a rollout). On the right is a probability times a gradient of a log. The probability out front is exactly the weight an expectation uses. So the trick converts "gradient of a probability" into "an expectation of a gradient of a log" — and expectations are precisely what Monte Carlo rollouts estimate.

The quantity θ log πθ(a | s) that this produces has a name worth knowing: the score function. Intuitively it points in the direction of θ-space that would make the action a more probable in state s. Push θ along the score and you turn up the dial on "do a here." That phrase — "the direction that makes this action more likely" — is the mental image to carry for the rest of the lecture.

A concrete score function

Make it real. Suppose in some state the policy is a softmax over two actions with a single logit knob θ: P(left) = σ(θ), P(right) = 1 − σ(θ), where σ is the logistic function. Then a short calculation gives the scores:

θ log P(left) = 1 − σ(θ)    ∇θ log P(right) = −σ(θ)

Read the meaning off the signs. If you took left, the score is positive — raising θ makes left more likely, exactly the action you took. If you took right, the score is negative — you'd lower θ to make right more likely. The score always points "toward making the action I just took more probable." Multiply it by how good the outcome was, and you have a learning signal.

The score vector — "which way makes my action more likely?"

A two-action softmax policy with one logit θ (slider). The two bars are P(left) and P(right). The arrows are the scoreθlogπ for each action — right-pointing means "raise θ to make me likelier". Slide θ and watch the scores: the action that is currently rare has the bigger-magnitude score (more room to grow).

logit θ0.00
The log-derivative trick rewrites ∇θ pθ(τ) as pθ(τ)·∇θ log pθ(τ). Why is the right-hand form so much more useful?

Chapter 3: The Policy Gradient Theorem — Derived

This is the "black-board derivation" the slides promise. It is three steps, and at the end something beautiful happens: the unknown physics cancels out completely. Follow each line; none of them is harder than the chain rule.

Step 1 — write the objective as an integral

By the definition of expectation, the expected return is the return of each trajectory weighted by how probable that trajectory is, summed (integrated) over all trajectories:

J(θ) = Eτ ∼ pθ[ R(τ) ] = ∫ pθ(τ) R(τ) dτ

Step 2 — differentiate, and apply the trick

Push the gradient inside the integral. Only pθ(τ) depends on θ (the return R(τ) is just a number for a given trajectory), so:

θ J(θ) = ∫ ∇θ pθ(τ) R(τ) dτ

Now use the log-derivative trick from Chapter 2 to replace ∇θ pθ(τ) with pθ(τ)·∇θ log pθ(τ):

θ J(θ) = ∫ pθ(τ) · ∇θ log pθ(τ) · R(τ) dτ

The leading pθ(τ) re-creates an expectation. So:

θ J(θ) = Eτ ∼ pθ[ ∇θ log pθ(τ) · R(τ) ]
We already won, in principle. The gradient is now an expectation — something we can estimate by averaging over sampled rollouts. But there is still the unknown trajectory-probability log term inside. Step 3 demolishes most of it.

Step 3 — expand the log, and watch physics vanish

Recall the trajectory probability from Chapter 1: a product of the start, the policy at each step, and the transition at each step. The log of a product is a sum of logs:

log pθ(τ) = log p(s0) + ∑t [ log πθ(at|st) + log p(st+1|st, at) ]

Now take the gradient with respect to θ. Look at each piece:

θ log pθ(τ) = ∑tθ log πθ(at|st)
The miracle. The transition dynamics — the part of the problem we do not know and could never differentiate — dropped out, because they do not depend on the policy's parameters. We never had to model the world. This is exactly what makes policy gradient model-free: it learns from the outcomes physics produced without ever learning physics itself.

Substitute back, and you have the Policy Gradient Theorem in its clean form:

θ J(θ) = Eτ ∼ pθ[ R(τ) · ∑tθ log πθ(at|st) ]

Read it as a sentence: the gradient of expected return is the average, over trajectories, of (how good the trajectory was) times (the sum of scores of every action you took in it). Good trajectories (big R) push their actions' scores up hard; bad ones push gently or down. That is the entire algorithm in one line.

In Step 3, the transition-dynamics term log p(st+1|st, at) disappears from the gradient. Why — and what does its disappearance buy us?

Chapter 4: REINFORCE — Make the Theorem an Algorithm

The theorem gives an expectation. We already know how to turn an expectation into something computable: Monte Carlo sampling. Roll out N trajectories with the current policy, and replace the expectation with the sample average. That is the entire REINFORCE algorithm (Williams, 1992; the slides credit the Sutton et al. lineage):

θ J(θ) ≈ (1/N) ∑i=1N R(τ(i)) · ∑tθ log πθ(at(i)|st(i))

Spelled out as a loop:

1. Roll out
Run the current policy πθ for N episodes. Record every (state, action) and each episode's return R(τ).
↓ for each episode
2. Score every action
Compute ∇θ log πθ(at|st) at each step — the direction that makes that action likelier.
↓ weight by outcome
3. Weight by return
Multiply each episode's summed scores by its return R(τ). Average over the N episodes.
↓ ascend
4. Gradient ascent
θ ← θ + η · (that average). Then go collect fresh rollouts and repeat.
Notice step 4's "fresh rollouts". The gradient is an expectation over trajectories drawn from the current policy. The instant you update θ, your old rollouts came from the wrong policy. So you must throw them away and collect new ones — REINFORCE is strictly on-policy. (Contrast Q-learning, which is off-policy and can replay a buffer of old transitions forever. We'll come back to this trade-off in Chapter 9.)

Let's actually estimate a gradient

Time to make the estimator concrete in code. The lab below puts a one-step policy on a 2-armed bandit — the simplest world where the policy-gradient idea is visible end to end. You will assemble the REINFORCE estimate from its two ingredients (return times score) and then check that following it actually raises expected reward.

REINFORCE must use fresh rollouts after every update, while Q-learning can replay an old buffer. What property of REINFORCE forces this?

Chapter 5: Why It Works — "Good Stuff Made More Likely"

The formula is correct, but you should never trust a formula you cannot feel. Let's build the intuition the slides emphasize, and then compute one full REINFORCE step by hand so the abstraction touches the ground.

The trial-and-error reading

Look again at the REINFORCE update: each action's score — the "make me more likely" direction — is weighted by the return R(τ) of the trajectory it lived in. So:

That is it. Good stuff is made more likely; bad stuff is made less likely. Guanya Shi's phrase: REINFORCE "simply formalizes the notion of trial and error." There is no clever credit-assignment machinery here — just "did the whole episode go well? then do more of what you did."

A second, sharper reading (the rarity factor). Rewrite one action's contribution without the log: R(τ) · ∇θπθ(a|s) / πθ(a|s). The probability sits in the denominator. So the update encourages an action when (1) it gave high return and (2) it was rare — low π means dividing by a small number, a bigger nudge. A good action you almost never take gets the loudest "do this more!" That is the score-function's built-in exploration bias.

One REINFORCE step, fully by hand

Two actions, one state, softmax policy with logit θ = 0, so π(left) = π(right) = 0.5. The scores at θ = 0 (from Chapter 2, with σ(0) = 0.5):

score(left) = 1 − 0.5 = +0.5    score(right) = −0.5

We sample 4 one-step episodes. Suppose the outcomes are:

EpisodeActionScoreReturn R(τ)R · score
1left+0.5+1+0.5
2left+0.5+1+0.5
3right−0.500
4right−0.500

The REINFORCE gradient estimate is the average of the last column:

ĝ = (0.5 + 0.5 + 0 + 0) / 4 = +0.25

It is positive, so gradient ascent raises θ: θ ← 0 + η·0.25. Raising θ raises π(left). And indeed left was the rewarding action — the estimator correctly decided to make left more likely. With learning rate η = 1, θ moves to +0.25, so π(left) climbs from 0.50 to σ(0.25) ≈ 0.562. One step, one nudge in the right direction. Repeat and it sharpens onto left.

Sanity-check the sign yourself. Both rewarding episodes chose left, whose score is positive; both zero-reward episodes chose right, contributing nothing. So the only surviving terms vote "more left", and the gradient is positive. If instead right had paid off, the right-action scores (negative) would be the survivors, the gradient would be negative, θ would drop, and π(right) would rise. The sign always tracks the rewarded action.

Build the by-hand gradient, episode by episode

The same four episodes. Toggle each episode's return between the rewarding action being left or right, and watch the running gradient estimate — and which way θ (hence π) would move. Confirm: the gradient always points toward making the rewarded action likelier.

In the rarity reading R(τ)·∇π/π, an action with the policy probability π in the denominator. What does that imply?

Chapter 6: What's Wrong with REINFORCE — High Variance

REINFORCE is unbiased: average enough rollouts and the estimate converges to the true gradient. But "enough" is the catch. The slides put it bluntly: "What is wrong with REINFORCE? It has high variance!" Two trajectories from the same policy can produce wildly different gradient estimates, and you are averaging only a handful of them.

Why the variance is so bad

Three compounding reasons:

Bias vs. variance — keep them straight. REINFORCE is unbiased (correct on average) but high-variance (any single estimate is far from the average). High variance means each gradient step points in a noisy direction, so you take tiny learning rates and learning crawls. The next two chapters are both variance-reduction tricks that keep the estimator unbiased — you get a cleaner signal for free.

Gradient estimates scatter — the variance you're fighting

Each warm dot is a REINFORCE gradient estimate from a small batch of N noisy episodes; the teal line is the true gradient. Shrink the batch (slider) and watch the estimates spray all over — some even point the wrong way. The cloud's spread is the variance.

Batch size N8
REINFORCE is described as "unbiased but high-variance." What does that combination mean in practice?

Chapter 7: Variance Fix #1 — Reward-to-Go & Causality

The first variance reduction is so obvious in hindsight that it feels like cheating — and it costs us nothing in bias. It comes from a single physical fact: the future cannot influence the past.

In vanilla REINFORCE, every action at is weighted by the entire trajectory return R(τ) — including rewards that arrived before that action was even taken. But a decision at time t' cannot possibly have caused a reward at an earlier time t < t'. Weighting at by rewards from the past is pure noise: it adds variance without adding any real credit.

Causality. The action at time t can only affect rewards from time t onward. So weight each action only by the rewards that came after it — the reward-to-go. Dropping the earlier rewards removes a chunk of irrelevant noise, and because those terms had expected contribution zero, the estimator stays unbiased.

Formally, replace the single trajectory return R(τ) multiplying action at with the sum of rewards from t onward:

θ J(θ) ≈ (1/N) ∑itθ log πθ(at|st) · ( ∑t' ≥ t r(st', at') )

That inner sum — the reward-to-go from step t — is exactly an estimate of the action-value Q(st, at): "expected return if I take this action here and continue." So a second, equivalent way to write the policy gradient theorem is:

θ J(θ) ∝ Es ∼ pθ, a ∼ πθ[ Q(s, a) · ∇θ log πθ(a|s) ]
Why this matters for the rest of the course. The moment you write Q(s, a) in front of the score, you've opened the door to learning that Q with a critic instead of estimating it with one noisy rollout. Replace the high-variance reward-to-go sum with a low-variance learned estimate, and REINFORCE becomes actor-critic (the very next lecture). Reward-to-go is the bridge.

Full return vs. reward-to-go — which rewards credit which action?

A 6-step episode with a reward at each step (bars). Click an action step. Red = the rewards vanilla REINFORCE wrongly credits to it (including past ones); teal = the reward-to-go (only t' ≥ t) that causality says it can actually influence. The red-only past rewards are the noise we throw away.

Action at step t2

Now let's verify the policy-gradient estimator is actually the gradient — by checking it against a brute-force finite difference. This is the gold-standard correctness test for any gradient estimator, and it's how you'd debug your own implementation.

Reward-to-go weights each action only by rewards from that step onward, dropping earlier ones. Why is this valid, and what does it buy?

Chapter 8: Variance Fix #2 — Baselines

Here is the most important — and most misunderstood — variance trick. We are going to subtract a number from the return before weighting the score, and somehow that lowers variance without changing the gradient we are estimating. It sounds like a free lunch. The slides prove it is real.

The claim: a baseline doesn't move the gradient

Pick any function b(s) that depends only on the state, not the action — call it a baseline. Subtract it from Q before weighting:

θ J(θ) ∝ Es, a[ ( Q(s, a) − b(s) ) · ∇θ log πθ(a|s) ]

The astonishing part: this equals the gradient without the baseline. The reason is that the extra term we added — E[ b(s)·∇θ log πθ(a|s) ] — is exactly zero. Here is the slides' proof, in plain steps:

Ea[ b(s) ∇θ log πθ(a|s) ] = ∫ πθ(a|s) b(s) (∇θ πθ(a|s) / πθ(a|s)) da
= b(s) ∫ ∇θ πθ(a|s) da = b(s) ∇θ ∫ πθ(a|s) da = b(s) ∇θ (1) = 0

Walk it: the log-derivative trick turns ∇logπ back into ∇π/π, the π cancels, b(s) pulls out front (it doesn't depend on a), the gradient swaps with the integral, and the integral of a probability distribution over all actions is 1 — a constant, whose gradient is 0. The baseline term vanishes identically. Any b(s) is allowed.

The misconception to kill right now. A baseline does not change the expected gradient — it is provably unbiased for any b(s). What it changes is the variance of the estimate. Subtracting a well-chosen number makes the per-sample weights smaller and better-centered, so the cloud of estimates tightens around the same true direction. "Doesn't change the gradient, only the noise" is the whole point.

Why subtracting the average helps — intuitively

Without a baseline, suppose every trajectory in some region has return between +90 and +110. Vanilla REINFORCE pushes every action's probability up (all returns positive), then sorts them out only by the small differences. Huge weights, tiny relative signal — that is high variance. Subtract the average return, ~100, and now returns become −10 to +10: above-average actions go up, below-average go down. Same expected direction, far smaller weights, far less noise.

The slides give the most common choice: b(s) = V(s), the state-value function. Then Q(s, a) − V(s) is the advantage A(s, a) — "how much better was this action than the state's average action?" That advantage-weighted gradient is the backbone of every modern policy-gradient method (A2C, PPO).

A baseline tightens the cloud — same center, less spread

Two clouds of gradient estimates from the same noisy episodes: warm = no baseline, teal = baseline subtracted (the average return). The dashed line is the true gradient. Slide the return offset up — without a baseline the warm cloud's spread explodes; the teal cloud stays tight because it cancels the offset.

Reward offset0

Let's prove the free lunch with our own numbers: measure the variance of the gradient estimate with and without a baseline, and confirm the baseline cuts variance while leaving the mean (the true gradient) untouched.

A teammate worries that subtracting a baseline b(s) will bias the policy gradient. What's the correct response?

Chapter 9: Connections & Cheat Sheet

You now own a complete, self-contained method: a way to improve a policy directly, derived from one calculus identity, made practical by Monte Carlo, and de-noised by two unbiased tricks. Let's place it in the landscape and hand you the one-page summary.

Policy gradient vs. Q-learning — the two families

Q-Learning (value-based)Policy Gradient (policy-based)
What it learnsA value Q(s,a); policy is implicit (argmax)The policy πθ directly
Continuous actionsAwkward (max over actions)Natural (output the action)
On/off-policyOff-policy — can replay an old bufferOn-policy — needs fresh rollouts
ModelModel-freeModel-free (physics cancels in the gradient)
Main weaknessInstability with function approximationHigh variance, sample-hungry
Stochastic policyNot naturallyYes — it is a distribution

Neither family dominates. The future of the course is the marriage of the two: keep the policy-gradient structure (the score weighted by how good the action was) but replace the noisy reward-to-go with a learned value estimate — a critic. That hybrid is actor-critic, the subject of the very next lecture, and the foundation of PPO and SAC that run real robots today.

Cheat sheet — everything you now own.
  • Objective: maximize expected return J(θ) = Eτ∼pθ[R(τ)] by gradient ascent.
  • Log-derivative trick: ∇p = p·∇log p — turns a gradient-of-a-probability into an expectation.
  • Score function:θlogπθ(a|s) = the direction that makes action a more likely.
  • Policy gradient theorem:θJ = E[ R(τ) · ∑tθlogπθ(at|st) ]; physics cancels ⇒ model-free.
  • REINFORCE: Monte-Carlo estimate of that expectation; on-policy; "good stuff made more likely."
  • Problem: unbiased but high variance; reward scale (the R=0 trap) matters.
  • Reward-to-go: credit each action only with future rewards (causality) → less variance, still unbiased; reward-to-go ≈ Q(s,a).
  • Baseline: subtract b(s) from Q; provably unchanged expected gradient, lower variance; b=V gives the advantage A=Q−V.

Where to go next on this site

This lesson sits between the value-based and actor-critic chapters of the 16-831 series. Continue the thread:

The test of this lesson. Open the original Lecture 9 slides — the "black-board derivation" ones. You should now be able to reproduce, from memory: the LQR motivation for learning a policy directly; the log-derivative identity; all three steps of the policy gradient theorem (and why the dynamics cancel); the REINFORCE update; the "good stuff made more likely" reading; why the variance is high; and the two unbiased fixes (reward-to-go and baselines). If you can derive ∇θJ on a blank board, you're ready for actor-critic.

"Make the good trajectories more likely. The rest is variance reduction."