Actor-Critic Algorithms
Policy gradients get their variance from single-sample reward-to-go estimates. Levine replaces them with learned value functions: fit the value function, bootstrap an advantage, and you have the actor-critic family. Along the way comes the discount factor as fear of death, online and parallel variants, off-policy actor-critic with a replay buffer and a Q-function, and a bias-variance dial running from one-step advantages through n-step returns to generalized advantage estimation.
The variance hiding in reward-to-go
Actor-critic algorithms “build on the policy gradient framework… augmented with learned value functions and Q-functions.” Levine’s choice of the symbol for REINFORCE’s reward-to-go last lecture “was deliberate”: it estimates the expected reward from taking in and then following the policy. But it’s computed by summing rewards along the one trajectory you happened to get. The policy and the MDP are both random; landing in the same state again, the rollout might end completely differently. The reward-to-go is “a single sample estimate of a very complex expectation” — and fewer samples means higher variance. That single-sample estimate is the high variance of the policy gradient. With the true expected reward-to-go — the Q-function — plugged in place of , the variance drops dramatically.
Baselines improve too. The baseline may depend on the state (not the action — that adds bias, for now), and the natural state-dependent choice is the average Q-value over the actions the policy would take there — which is, by definition, the value function.
The gradient becomes — raise the probability of better-than-average actions, lower the rest. The better the advantage estimate, the lower the variance. The catch: an approximate critic makes the gradient biased. Usually “the enormous reduction in variance is often worth the slight increase in bias.” The green box of the RL anatomy — estimate returns — now means fitting a function approximator.
What to fit: the case for the value function
Three candidates: , , or . An identity picks the winner. Since and are known, not random, the current reward pulls out exactly:
Approximate the expectation with the actual next state seen — — a single-sample estimate again, but for just one step; everything after stays integrated out inside . Then
depends only on , which takes just a state as input while and need the action too — fewer inputs, fewer samples needed. So: “let’s just fit ” with a network .
Fitting is policy evaluation: , so the value function at the initial states literally evaluates the policy. Monte Carlo evaluation builds tuples with and regresses: . Ideally you’d average many rollouts from each state, but model-free RL can’t reset the simulator to an arbitrary state — one rollout per visit.
Better targets: the ideal is ; we don’t know , but the previous value network is “probably better than nothing,” so plug it in: . This bootstrap estimate has lower variance ( averages over all futures) but higher bias ( is always wrong somewhere) — the trade running through the whole lecture.
Discount factors: the fear of death
One problem appears immediately in infinite-horizon settings: with, say, all-positive rewards, every bootstrap update grows the value function, which “might become infinite.” The fix is to prefer rewards sooner — you’d take a hundred dollars today over a hundred in a million years, partly because “someday you’re going to die and you’d like to receive the reward before you die.” Multiply the bootstrapped value by a discount factor (“0.99 works really well”):
The interpretation is exact: changes the MDP, adding a death state entered with probability at every step, zero reward, no way out — “there’s no resurrection in this MDP.” Surviving transitions become .
An alternative reading (Philip Thomas, “Bias in natural actor-critic algorithms”): the discount is variance reduction. Infinite reward sums have infinite variance; truncates the far future — exactly the high-variance part — at the cost of bias.
Making it practical: architectures and parallel workers
Two design decisions turn the online recipe into a deep RL algorithm. Architecture: the recommended start is two separate networks, state-to-value and state-to-action-distribution — simple and “fairly stable to train.” A shared trunk with two heads shares representations (attractive from images, where the critic’s features could help the policy), but the shared layers get hit by gradients of different scales and statistics, so it needs more tuning.
Batch size: updating a deep net with single-sample SGD “is not going to work very well.” The classic fix is parallel workers. Synchronous parallel actor-critic steps multiple differently-seeded simulators in lockstep and updates on all threads’ transitions together — batch size equals thread count. The asynchronous version (as in A3C) drops the synchronization point: threads run at their own pace, pulling the latest parameters as they go. Batches then mix transitions from slightly older actors — not mathematically equivalent to the synchronous update, but in practice the speed gains outweigh the small bias, as long as no thread lags far behind.
Off-policy actor-critic: the replay buffer and the Q-trick
If slightly-old transitions are tolerable, why not much older ones from the same actor? Keep a replay buffer — a ring buffer of, say, a million transitions — and update on batches sampled from it. Now the staleness can’t be ignored, and the naive algorithm “is actually quite broken” in two places. First, the target uses an action taken by an old policy, so it estimates the wrong actor’s value. Second, the policy gradient requires actions sampled from the current — buffer actions don’t qualify.
Fix the critic by learning instead of : the Q-function is a valid question for any action — only subsequent steps must follow — so it can be trained on off-policy actions and queried with on-policy ones. For the target, use :
The trick is that is not from the buffer: we ask the current policy what it would have done at the old state — no simulator needed, because “we have functional access to our policy.” Fix the actor with the same trick at : sample and use . In practice the baseline is dropped — plug in directly. Higher variance, but acceptable: extra action samples cost network evaluations, not environment interaction, so averaging more of them is nearly free.
One flaw remains: follows an old policy’s state distribution, and “there’s basically nothing we can do here” — accept the bias. The intuition: we wanted the optimal policy on but get it on a broader distribution — extra work on states we may never visit, but nothing important is missed. This template, plus fancier Q-fitting and the reparameterization trick (both later in the course), underlies soft actor-critic, one of the most widely used actor-critic methods today.
Keeping it unbiased: control variates, n-step returns, and GAE
Can the critic reduce variance without introducing bias? Yes — use it as a baseline rather than as the return estimate.
Push further and let the baseline depend on the action — a control variate. Subtracting drives the first term toward zero as the critic improves, but an action-dependent baseline no longer integrates to zero: an error term must be added back. It looks like the original problem, but it’s evaluable without new environment samples — sum over discrete actions, sample many actions per state for free, or solve analytically (a quadratic under a Gaussian policy has a closed form). The result is a very low-variance unbiased gradient — see Q-Prop (Gu et al.).
Between the unbiased Monte Carlo estimator and the biased one-step critic lies a dial. Near-future predictions are low-variance — asked which city he’ll be in in five minutes, Levine answers “Berkeley” confidently; in twenty years, a single sample is nearly meaningless. Trajectories branch out over time, so use the single-sample rewards where they’re reliable and “cut it off before the variance gets too big,” replacing the tail with the value function — the n-step return:
Larger : less bias (the value term is scaled by ), more variance. Choosing greater than 1 often beats , and the sweet spot is usually in between. You don’t have to choose one : generalized advantage estimation (GAE, Schulman et al.) averages all n-step estimators, , with exponential weights preferring earlier cuts. The infinite sum collapses to
a -discounted sum of one-step advantage estimates. The acts like a discount — which retroactively illuminates itself: even without , the discount was already trading bias for variance by down-weighting exactly the rewards with the highest variance.
Check yourself
Derivation and debugging exercises in the lecture’s spirit — work them on paper.
Levine asserts that subtracting any baseline depending only on the state leaves the policy gradient unbiased, and encourages rederiving it. Do so: show , and point to the exact step that fails if also depends on .
Show solution
Condition on the state: . Because is constant with respect to the inner expectation over actions, it factors out, leaving . The inner integral is , so the whole expression vanishes. If depends on , the factoring-out step fails: stays inside the integral, which becomes — generally nonzero. That residual is exactly the correction term control-variate methods like Q-Prop must evaluate and add back.
For a three-step trajectory, write the discounted policy gradient weights under option 1 (discount inside the reward-to-go) and option 2 (discount by from the trajectory start, then apply causality). Identify the differing factor, state what it means, and explain why practice uses option 1 for a continuous running task.
Show solution
Option 1 weights by : at the weight is ; at , ; at , . Option 2 starts from weights after causality, and factoring gives — option 1’s weight times an extra on each grad-log-pi: the term is scaled by , the term by . The factor says later decisions matter less — the correct gradient of the truly discounted objective, where late decisions influence only heavily discounted rewards. But for a cyclic running task we want a policy that acts well at every time step indefinitely; the discount is only a convenient stand-in for the computationally difficult average-reward objective. The multiplier would starve late-time-step learning, so practice keeps option 1 and drops it.
A colleague adds a replay buffer to online actor-critic: sample transitions from the buffer, compute targets , regress onto them, then update the policy with . The algorithm is broken in exactly two places. Find both and give the fixes.
Show solution
Bug 1 is the value target: was taken by an older policy, so regressing onto estimates the value of that old actor, not the current one. Fix: learn a Q-function — valid for any action, with only subsequent steps following — and build targets with freshly sampled by querying the current policy network at the old state (no simulation required). Bug 2 is the policy gradient: it must be an expectation under the current , but came from an old policy — the wrong sampling distribution. Fix: sample at the buffer state and use , in practice without a baseline since extra action samples are cheap. One bias remains unfixable: follows an old state distribution — accepted, because training on a broader distribution does extra work rather than missing states the current policy visits.
Starting from with , show that recovers the one-step actor-critic advantage and recovers the discounted Monte Carlo estimator with a value-function baseline. What does this say about the role of ?
Show solution
For , every term with carries a factor , so only survives: — the bootstrapped one-step advantage (lowest variance, highest bias). For the sum is , and the value terms telescope: the contributed at step cancels the contributed at step . All that survives is (the trailing value term vanishes in the limit under discounting): the discounted single-sample reward-to-go minus a state-dependent baseline — unbiased, highest variance. So interpolates continuously between the biased low-variance critic estimate and the unbiased high-variance Monte Carlo estimate, softly cutting each trajectory with weight — a second discount whose job is purely bias-variance control.