CS 285, Lecture 6, Actor-Critic Algorithms

12/12/2025 tech

CS 285 Deep Reinforcement Learning, Sergey Levine, Lecture 6, Actor-Critic Algorithms notes.

# Recap: Policy Gradients

REINFORCE algorithm:

  1. Sample {τi}\{\tau^i\} from πθ(atst)\pi_{\theta}(\mathbf{a}_t | \mathbf{s}_t) (run the policy)
  2. θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)Q^i,tπ\nabla_{\theta}J(\theta) \approx \frac{1}{N}\sum_{i=1}^{N} \sum_{t=1}^T \nabla_{\theta}\log\pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \hat{Q}_{i,t}^\pi, where the "reward to go" is Q^π(si,t,ai,t)=t=tTr(si,t,ai,t)\hat{Q}^\pi(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) = \sum_{t^\prime=t}^T r(\mathbf{s}_{i,t^\prime}, \mathbf{a}_{i,t^\prime})
  3. θθ+αθJ(θ)\theta \leftarrow \theta + \alpha \nabla_{\theta} J(\theta)

# Improving the Policy Gradient

The policy gradient estimate relies on a sample estimate of the return:

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)Q^i,t \nabla_{\theta}J(\theta) \approx \frac{1}{N}\sum_{i=1}^{N}\sum_{t=1}^T\nabla_{\theta}\log\pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \hat{Q}_{i, t}

The "reward to go" Q^i,tt=tTr(si,t,ai,t)\hat{Q}_{i,t} \approx \sum_{t^\prime=t}^T r(\mathbf{s}_{i,t^\prime}, \mathbf{a}_{i,t^\prime}) is an estimate of expected return if we take action ai,t\mathbf{a}_{i,t} in state si,t\mathbf{s}_{i,t}.

A single sample estimate has high var, so generate many samples and calc the exp of reward to go with a lower var.

Can we get a better estimate? The ideal is the true expected reward-to-go, Q(st,at)Q(\mathbf{s}_t, \mathbf{a}_t):

Q(st,at)=t=tTEπθ[r(st,at)st,at] Q(\mathbf{s}_t, \mathbf{a}_t) = \sum_{t^\prime=t}^T E_{\pi_{\theta}}[r(\mathbf{s}_{t^\prime},\mathbf{a}_{t^\prime}) | \mathbf{s}_t, \mathbf{a}_t]

If we can replace Q^\hat{Q} with true Q function, the var of policy gradient would be much lower:

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)Q(si,t,ai,t) \nabla_{\theta} J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^T \nabla_{\theta}\log\pi_{\theta}(\mathbf{a}_{i,t}|\mathbf{s}_{i,t})Q(\mathbf{s}_{i,t},\mathbf{a}_{i,t})
many samples for Q

# Subtract the Baseline

Previously, we subtract the average reward as baseline. Now, if we have true expected reward-to-go:

Q(st,at)=t=tTEπθ[r(st,at)st,at] Q(\mathbf{s}_t,\mathbf{a}_t) = \sum_{t^\prime=t}^T E_{\pi_{\theta}}[r(\mathbf{s}_{t^\prime}, \mathbf{a}_{t^\prime}) | \mathbf{s}_t, \mathbf{a}_t]

Then rewrite policy gradient w/ true QQ to subtract baseline:

θJ(θ)1Ni=1Nθlogπθ(ai,tsi,t)(Q(si,t,ai,t)bt)bt=1NiQ(si,t,ai,t) \begin{aligned} \nabla_\theta J(\theta) &\approx \frac{1}{N} \sum_{i=1}^N \nabla_\theta \log \pi_\theta(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) (Q(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) - b_t) \\ b_t &= \frac{1}{N} \sum_i Q(\mathbf{s}_{i,t},\mathbf{a}_{i,t}) \end{aligned}

Baseline depending on actions would lead to bias, so instead depend on state, using average reward over all actions for the state si,t\mathbf{s}_{i,t}:

V(st)=Eatπθ(atst)[Q(st,at)] V(\mathbf{s}_t) = E_{\mathbf{a}_t \sim \pi_{\theta}(\mathbf{a}_t | \mathbf{s}_t)}[Q(\mathbf{s}_t, \mathbf{a}_t)]

Choose value function as the baseline:

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)A(si,t,ai,t) \nabla_{\theta} J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^T \nabla_{\theta}\log \pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) A(\mathbf{s}_{i,t}, \mathbf{a}_{i,t})

where the advantage function A(si,t,ai,t)A(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) is defined as:

A(si,t,ai,t)=Q(si,t,ai,t)V(si,t) A(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) = Q(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) - V(\mathbf{s}_{i,t})

If take the actions better than avg in the state, then increase the prob. If take the actions worse than avg in the state, then decrease the prob.

# State & state-action value function (Q function)

Total expected reward starting from st\mathbf{s}_t, taking action at\mathbf{a}_t, and following policy π\pi:

Qπ(st,at)=t=tTEπθ[r(st,at)st,at] Q^\pi(\mathbf{s}_t, \mathbf{a}_t) = \sum_{t^\prime=t}^T E_{\pi_{\theta}}[r(\mathbf{s}_{t^\prime}, \mathbf{a}_{t^\prime}) | \mathbf{s}_t, \mathbf{a}_t]
state-action function

We add the superscript π\pi to emphasize QQ function depends on π\pi. Every policy has a different QQ function.

The value function is the expected value of QQ values over all actions in state st\mathbf{s}_t under policy π\pi. Or, the total reward starting from st\mathbf{s}_t and following policy π\pi:

Vπ(st)=Eatπθ(atst)[Qπ(st,at)] V^\pi(\mathbf{s}_t) = E_{\mathbf{a}_t \sim \pi_{\theta}(\mathbf{a}_t | \mathbf{s}_t)}[Q^\pi(\mathbf{s}_t, \mathbf{a}_t)]
value function

The advantage function represents how much better the action at\mathbf{a}_t is compared to the avg performance of policy π\pi in state st\mathbf{s}_t:

Aπ(st,at)=Qπ(st,at)Vπ(st) A^\pi(\mathbf{s}_t, \mathbf{a}_t) = Q^\pi(\mathbf{s}_t, \mathbf{a}_t) - V^\pi(\mathbf{s}_t)

Conventionally, policy gradient uses a Monte Carlo estimate of the advantage, is calculated by using 1 sample in the remainder of current traj, summing up rewards in the traj, and subtracting a baseline.

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)(t=1Tr(si,t,ai,t)b) \nabla_{\theta}J(\theta) \approx \frac{1}{N}\sum_{i=1}^{N}\sum_{t=1}^T \nabla_{\theta}\log\pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \left(\sum_{t^\prime=1}^T r(\mathbf{s}_{i,t^\prime}, \mathbf{a}_{i,t^\prime}) - b\right)

This is an unbiased single sample estimate with high var, but we now replace it with lower var approximate advantage function.

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)Aπ(si,t,ai,t) \nabla_{\theta}J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N}\sum_{t=1}^T \nabla_{\theta}\log\pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \textcolor{red}{A^\pi(\mathbf{s}_{i,t}, \mathbf{a}_{i,t})}

In practice, we don't have the correct value of advantage, but can only estimate it. The better estimate of advanatage, the lower var is. If the advantage is incorrect, policy gradients can be biased. Usually, we trade large var reduction for slight increase in bias from value function.

Q, V, A fitting

Now, in the 2nd step (green box), when we "fit a model to estimate return", we fit Qπ,VπQ^\pi, V^\pi, or AπA^\pi.

# Policy Evaluation

The policy is held fixed while we estimate the return it induces from each state. This step is called policy evaluation because it evaluates the current policy rather than improving it.

# Value function fitting

Among Qπ,VπQ^\pi, V^\pi, or AπA^\pi, which should we fit to?

Because st\mathbf{s}_t and at\mathbf{a}_t are not RVs, can rewrite the QπQ^\pi as current reward (exact, not random) ++ expected value of future rewards:

Qπ(st,at)=r(st,at)+t=t+1TEπθ[r(st,at)st,at]=r(st,at)+Est+1p(st+1st,at)[Vπ(st+1)] \begin{aligned} Q^\pi(\mathbf{s}_t, \mathbf{a}_t) &= r(\mathbf{s}_t, \mathbf{a}_t) + \sum_{t'=t+1}^T E_{\pi_\theta} [r(\mathbf{s}_{t'}, \mathbf{a}_{t'}) | \mathbf{s}_t, \mathbf{a}_t] \\ &= r(\mathbf{s}_t, \mathbf{a}_t) + E_{\mathbf{s}_{t+1} \sim p(\mathbf{s}_{t+1} | \mathbf{s}_t, \mathbf{a}_t)} [V^\pi(\mathbf{s}_{t+1})] \end{aligned}

Assuming for st+1\mathbf{s}_{t+1}, the actual state in current traj is representative of the average state. For the next time step, approximate the distr of states with a single-sample estimator:

Qπ(st,at)r(st,at)+Vπ(st+1) Q^\pi(\mathbf{s}_t, \mathbf{a}_t) \approx r(\mathbf{s}_t, \mathbf{a}_t) + V^\pi(\mathbf{s}_{t+1})

The advantage function is approximated as current reward ++ next value - current value:

Aπ(st,at)r(st,at)+Vπ(st+1)Vπ(st) A^\pi(\mathbf{s}_t, \mathbf{a}_t) \approx r(\mathbf{s}_t, \mathbf{a}_t) + V^\pi(\mathbf{s}_{t+1}) - V^\pi(\mathbf{s}_t)

We do the approximation b/c VV only depends on state, whereas QQ and AA depend on both state and action. Function approximator is easier to learn due to fewer samples.

So, fit Vπ(s)V^\pi(\mathbf{s}).

value function fitting

# Monte Carlo Policy Evaluation

Policy evaluation: calc Vπ(s)V^\pi(\mathbf{s}), the value of policy at every state.

Vπ(st)=t=tTEπθ[r(st,at)st] V^\pi (\mathbf{s}_t) = \sum_{t'=t}^T E_{\pi_\theta} [r(\mathbf{s}_{t'}, \mathbf{a}_{t'}) | \mathbf{s}_t]

The RL objective can be written as:

J(θ)=Es1p(s1)[Vπ(s1)] J(\theta) = E_{\mathbf{s}_1 \sim p(\mathbf{s}_1)} [V^\pi (\mathbf{s}_1)]

To perform policy evaluation, use Monte Carlo policy evaluation. Like policy gradients, run policy many times, sum rewards obtained along the generated trajectories, use it as unbiased but high-var single sample estimate of the policy's total reward.

sum up 1 trajectory

single sample estimate:

Vπ(st)t=tTr(st,at) V^\pi (\mathbf{s}_t) \approx \sum_{t'=t}^T r(\mathbf{s}_{t'}, \mathbf{a}_{t'})

Ideally, want to sum over all possible trajectories. Unfortunately, need to reset to st\mathbf{s}_t in model-free setting and run multiple trials starting from that state (can't do this unless in a simulator). But usually we only assume we can run multiple trials from the initial-state distribution.

sum up many trajectories

multi-sample estimate (but can’t reset the world):

Vπ(st)1Ni=1Nt=tTr(si,t,ai,t) V^\pi (\mathbf{s}_t) \approx \frac{1}{N} \sum_{i=1}^N \sum_{t'=t}^T r(\mathbf{s}_{i, t'}, \mathbf{a}_{i, t'})

# Monte Carlo evaluation with function approximation

Function approximator can generalize: it understands that similar states visited in different trajectories take similar values:

sum up 1 trajectory w/ function approximation

training data: {(st,yi,t)}\{(\mathbf{s}_t, y_{i,t})\}, where

yi,t=t=tTr(si,t,ai,t) y_{i,t} = \sum_{t'=t}^T r(\mathbf{s}_{i,t'}, \mathbf{a}_{i,t'})

supervised regression (over all samples, minimize the MSE btw value function's prediction and the single-sample Monte-Carlo estimate of value at the state):

L(ϕ)=12iV^ϕπ(si)yi2 \mathcal{L}(\phi) = \frac{1}{2} \sum_i \left\lVert \hat{V}_\phi^\pi(\mathbf{s}_i) - y_i \right\rVert^2

# Bootstrapped estimate

ideal target can be approximated by current reward ++ next value:

yi,t=t=tTEπθ[r(st,at)si,t]=r(si,t,ai,t)+t=t+1TEπθ[r(st,at)si,t+1]r(si,t,ai,t)+Vπ(si,t+1) \begin{aligned} y_{i,t} &= \sum_{t'=t}^T E_{\pi_\theta} \left[r(\mathbf{s}_{t'}, \mathbf{a}_{t'}) | \mathbf{s}_{i,t}\right] \\ &= r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \sum_{t'=t+1}^T E_{\pi_\theta} \left[r(\mathbf{s}_{t'}, \mathbf{a}_{t'}) | \mathbf{s}_{i,t+1}\right] \\ &\approx r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + V^\pi(\mathbf{s}_{i,t+1}) \end{aligned}

Using previously fitted value function, get the bootstrapped estimate:

yi,tr(si,t,ai,t)+V^ϕπ(si,t+1) y_{i,t} \approx r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1})

training data becomes: {(si,t,yi,t)}\{(\mathbf{s}_{i,t}, y_{i,t})\}

Then do supervised regression like before.

Compared with single sample estimator, bootstrapped estimate has lower var, but higher bias b/c V^ϕπ\hat{V}_\phi^\pi may be incorrect.

# Estimating VπV^\pi: Monte Carlo vs. Bootstrap

Monte Carlo (supervise with roll-out's summed rewards):

yi,t=t=tTr(si,t,ai,t) y_{i,t} = \sum_{t'=t}^T r(\mathbf{s}_{i,t'}, \mathbf{a}_{i,t'})

Bootstrapped (supervise using reward ++ next value estimate):

yi,t=r(si,t,ai,t)+V^ϕπ(si,t+1) y_{i,t} = r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1})
policy evaluation: MC vs. Bootstrap
V^MCπ(s)0V^MCπ(s)1V^TDπ(s)0V^TDπ(s)0 \begin{aligned} \hat{V}_{MC}^\pi(\textcolor{blue}{\mathbf{s}}) \approx 0 &\quad \hat{V}_{MC}^\pi(\textcolor{magenta}{\mathbf{s}}) \approx -1 \\ \hat{V}_{TD}^\pi(\textcolor{blue}{\mathbf{s}}) \approx 0 &\quad \hat{V}_{TD}^\pi(\textcolor{magenta}{\mathbf{s}}) \approx 0 \end{aligned}

# Actor-Critic algorithm

actor: policy
critic: value function

# Aside: discount factor

Bootstrap rule in infinite horizon

if TT \to \infty, V^ϕπ(si)\hat{V}_\phi^\pi(\mathbf{s}_i) can get infinitely large.

better to get rewards sooner than later w/ disount factor γ[0,1]\gamma \in [0, 1] (0.990.99 works well):

yi,tr(si,t,ai,t)+γV^ϕπ(si,t+1) y_{i,t} \approx r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \gamma \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1})

Mathematically, say there is an MDP of 4 states with transition probability p(ss,a)p(\mathbf{s}^\prime | \mathbf{s}, \mathbf{a}):

MDP w/ 4 states

By modifying the MDP to introduce the death state (never leaves once enter) w/ 00 reward and prob 1γ1 - \gamma, the MDP becomes:

MDP w/ an additioanl death state

with critic, the policy gradient uses the bootstrapped advantage (TD error):

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)A^π(si,t,ai,t)A^π(si,t,ai,t)=r(si,t,ai,t)+γV^ϕπ(si,t+1)V^ϕπ(si,t) \begin{aligned} \nabla_{\theta} J(\theta) &\approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^T \nabla_{\theta} \log \pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \hat{A}^\pi(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) \\ \hat{A}^\pi(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) &= r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \gamma \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1}) - \hat{V}_\phi^\pi(\mathbf{s}_{i,t}) \end{aligned}

Monte Carlo policy gradients:

θJ(θ)1Ni=1Nt=1Tθlogπθ(ai,tsi,t)(t=tTγttr(si,t,ai,t)) \nabla_{\theta} J(\theta) \approx \frac{1}{N} \sum_{i=1}^{N} \sum_{t=1}^T \nabla_{\theta} \log \pi_{\theta}(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \left( \sum_{t^\prime=t}^T \gamma^{t^\prime-t} r(\mathbf{s}_{i,t^\prime}, \mathbf{a}_{i,t^\prime}) \right)

# Batch actor-critic algorithm (with discount)

In episodic batch setting, we collect a batch of trajectories, each trajectory runs to the end, use the batch to eval gradient and update policy.

  1. sample {si,1,ai,1,,si,T,ai,T}\{\mathbf{s}_{i,1}, \mathbf{a}_{i,1}, \cdots, \mathbf{s}_{i,T}, \mathbf{a}_{i,T}\} from πθ(as)\pi_\theta(\mathbf{a}|\mathbf{s})
  2. fit V^ϕπ(s)\hat{V}_\phi^\pi(\mathbf{s}) to sampled reward sums
  3. evaluate A^π(si,t,ai,t)=r(si,t,ai,t)+γV^ϕπ(si,t+1)V^ϕπ(si,t)i,t\hat{A}^\pi(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) = r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \textcolor{red}{\gamma} \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1}) - \hat{V}_\phi^\pi(\mathbf{s}_{i,t}) \quad \forall i,t
  4. evaluate θJ(θ)i,tθlogπθ(ai,tsi,t)A^π(si,t,ai,t)\nabla_\theta J(\theta) \approx \sum_{i,t} \nabla_\theta \log \pi_\theta(\mathbf{a}_{i,t} | \mathbf{s}_{i,t}) \hat{A}^\pi(\mathbf{s}_{i,t}, \mathbf{a}_{i,t})
  5. update θθ+αθJ(θ)\theta \leftarrow \theta + \alpha \nabla_\theta J(\theta)

need to train 2 NN's: actor πθ(as)\pi_\theta(\mathbf{a} | \mathbf{s}) and critic V^ϕπ(s)\hat{V}_\phi^\pi(\mathbf{s}).

yi,ty_{i,t} can be Monte Carlo or Bootstrapped. With discount factor γ\gamma, the bootstrapped estimate is

yi,t=r(si,t,ai,t)+γV^ϕπ(si,t+1) y_{i,t} = r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \gamma \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1})

The MSE loss is same as before:

L(ϕ)=12iV^ϕπ(si)yi2 \mathcal{L}(\phi) = \frac{1}{2} \sum_i \left\| \hat{V}_\phi^\pi(\mathbf{s}_i) - y_i \right\|^2

# Online actor-critic algorithm (with discount)

The update equations are the same as in batch actor-critic; the difference is the update schedule. Batch actor-critic freezes the policy while collecting many transitions, averages their gradients, and then updates. Online actor-critic updates the actor and critic immediately after each transition, so the policy can change during an episode.

  1. take action aπθ(as)\mathbf{a} \sim \pi_\theta(\mathbf{a} | \mathbf{s}), get (s,a,s,r)(\mathbf{s}, \mathbf{a}, \mathbf{s}^\prime, r)
  2. update V^ϕπ\hat{V}_\phi^\pi using target r+γV^ϕπ(s)r + \gamma \hat{V}_\phi^\pi(\mathbf{s}^\prime)
  3. evaluate A^π(s,a)=r(s,a)+γV^ϕπ(s)V^ϕπ(s)\hat{A}^\pi(\mathbf{s}, \mathbf{a}) = r(\mathbf{s}, \mathbf{a}) + \gamma \hat{V}_\phi^\pi(\mathbf{s}^\prime) - \hat{V}_\phi^\pi(\mathbf{s})
  4. evaluate θJ(θ)θlogπθ(as)A^π(s,a)\nabla_\theta J(\theta) \approx \nabla_\theta \log \pi_\theta(\mathbf{a} | \mathbf{s})\, \hat{A}^\pi(\mathbf{s}, \mathbf{a})
  5. update θθ+αθJ(θ)\theta \leftarrow \theta + \alpha \nabla_\theta J(\theta)

collect transitions for bootstrapped estimate of VπV^\pi.

# Architecture Design

2 networks to learn value and policy:
1 maps from s\mathbf{s} to V^ϕπ(s)\hat{V}_\phi^\pi(\mathbf{s})
1 maps from s\mathbf{s} to πθ(as)\pi_\theta(\mathbf{a} | \mathbf{s})

+: simple & stable
-: no shared features btw actor & critic

or shared network design:
map from s\mathbf{s} to 2 heads: V^ϕπ(s)\hat{V}_\phi^\pi(\mathbf{s}) and πθ(as)\pi_\theta(\mathbf{a} | \mathbf{s})

+: can be more efficient due to shared internal representations
-: harder to train, unstable b/c of different gradients, need more hyperparameter tuning

# Online actor-critic in practice: w/ multi-threading

synchronized and asynchronous parallel actor-critic

synchronized parallel actor-critic
batch size = # of worker threads, e.g., 32
step 1: multiple simulators use diff random seeds, choose an action, generate diff transitions
step 2 & 4: update using data from all threads
step 5: update 1 policy

faster: asynchronous parallel actor-critic
each thread runs at diff speed
as soon as get 32 transitions, update policy
problem: the transitions may be collected by diff actors
this creates policy lag, a small off-policy mismatch: workers may collect transitions using stale policy parameters, while the updates are applied to the latest policy

in practice, the async method leads to perf gain that outweighs bias incurred from using slightly older actors.

# Eligibility trace & n-step return

for critic training, the 1-step bootstrapped target is:

yi,t=r(si,t,ai,t)+γV^ϕπ(si,t+1) y_{i,t} = r(\mathbf{s}_{i,t}, \mathbf{a}_{i,t}) + \gamma \hat{V}_\phi^\pi(\mathbf{s}_{i,t+1})

+: lower variance than Monte Carlo target
-: higher bias if value is wrong (always is)

Monte-Carlo target:

yi,t=t=tγttr(si,t,ai,t) y_{i,t} = \sum_{t'=t}^{\infty} \gamma^{t'-t} r(\mathbf{s}_{i,t'}, \mathbf{a}_{i,t'})

+: no bias
-: higher variance (b/c single-sample estimate)

want to combine these 2, to control bias-variance tradeoff
higher var when far in the future
lower var when closer to the present

n-step return

estimating V^ϕπ(si,t)\hat{V}_\phi^\pi(\mathbf{s}_{i,t}) using n-step return: sum up rewards of a single-sample estimator until some time step nn, then cut off before var gets too big, and replace the remainder with a value function estimate:

yi,t=t=tt+n1γttr(si,t,ai,t)+γnV^ϕπ(si,t+n) y_{i,t} = \sum_{t'=t}^{t+n-1} \gamma^{t'-t} r(\mathbf{s}_{i,t'}, \mathbf{a}_{i,t'}) + \gamma^n \hat{V}_\phi^\pi(\mathbf{s}_{i,t+n})

the larger nn is, the lower the bias (smaller 2nd term), the higher the var (larger 1st term)
sweet spot is an intermediate value btw 11 and \infty
less variance than MC, lower bias than 1-step bootstrap

# Generalized advantage estimation

While nn-step return helps, we still face the question: which nn should we choose? Ideally, we want a method that combines all possible nn-step estimators to get the best of both worlds (low bias and low variance).

# Core Definitions

Generalized Advantage Estimation (GAE) is defined by three core mathematical components:

  1. The nn-step Advantage (A^nπ\hat{A}_n^\pi):
    The difference between our nn-step sampled return and our current baseline:

    A^nπ(st,at)=(k=0n1γkrt+k+γnV^ϕπ(st+n))V^ϕπ(st) \hat{A}_n^\pi(\mathbf{s}_t, \mathbf{a}_t) = \left( \sum_{k=0}^{n-1} \gamma^k r_{t+k} + \gamma^n \hat{V}_\phi^\pi(\mathbf{s}_{t+n}) \right) - \hat{V}_\phi^\pi(\mathbf{s}_t)
    • Intuition: It balances "reality" (actual rewards collected for nn steps) with our "belief" (the Critic’s estimate of the future from step nn onwards).
  2. Weighted Combination of Estimators:
    Instead of picking one nn, take a weighted average of all nn-step advantages. To ensure the weights sum to 1 while favoring shorter (lower variance) horizons, use an exponential decay: wn=(1λ)λn1w_n = (1 - \lambda)\lambda^{n-1}

    A^GAEπ(st,at)=(1λ)n=1λn1A^nπ(st,at) \hat{A}_{GAE}^\pi(\mathbf{s}_t, \mathbf{a}_t) = (1-\lambda) \sum_{n=1}^{\infty} \lambda^{n-1} \hat{A}_n^\pi(\mathbf{s}_t, \mathbf{a}_t)
    • Intuition: This is a "blending" strategy. λ\lambda acts as a secondary discount factor that decides how much we trust our multi-step reward samples versus our single-step TD estimates.
  3. The TD error (temporal difference error) δt\delta_t:
    the one-step "surprise" — the difference between the bootstrapped target and the current value estimate:

    δt=r(st,at)+γV^ϕπ(st+1)V^ϕπ(st) \delta_t = r(\mathbf{s}_t, \mathbf{a}_t) + \gamma \hat{V}_\phi^\pi(\mathbf{s}_{t+1}) - \hat{V}_\phi^\pi(\mathbf{s}_t)

    This is exactly the 1-step advantage estimate A^π(st,at)\hat{A}^\pi(\mathbf{s}_t, \mathbf{a}_t) used in the actor-critic algorithms above.

# The Telescoping Derivation

The nn-step advantage can be rewritten as a discounted sum of TD errors. When we expand the sum k=0n1γkδt+k\sum_{k=0}^{n-1} \gamma^k \delta_{t+k}, the intermediate value function terms cancel out due to the telescoping property:

k=0n1γkδt+k=(rt+γV(st+1)V(st))+(γrt+1+γ2V(st+2)γV(st+1))+(γ2rt+2+γ2V(st+2))+(γn1rt+n1+γnV(st+n)) \begin{aligned} \sum_{k=0}^{n-1} \gamma^k \delta_{t+k} = \quad & (\color{red}{r_t} + \color{blue}{\gamma V(\mathbf{s}_{t+1})} - \color{red}{V(\mathbf{s}_t)}) \\ + \quad & (\color{red}{\gamma r_{t+1}} + \color{green}{\gamma^2 V(\mathbf{s}_{t+2})} - \color{blue}{\gamma V(\mathbf{s}_{t+1})}) \\ + \quad & (\color{red}{\gamma^2 r_{t+2}} + \dots - \color{green}{\gamma^2 V(\mathbf{s}_{t+2})}) \\ \dots \quad & \\ + \quad & (\color{red}{\gamma^{n-1} r_{t+n-1}} + \color{red}{\gamma^n V(\mathbf{s}_{t+n})} - \dots) \end{aligned}
  • The blue terms\color{blue}{\text{blue terms}} (+γVt+1+\gamma V_{t+1} and γVt+1-\gamma V_{t+1}) cancel.
  • The green terms\color{green}{\text{green terms}} (+γ2Vt+2+\gamma^2 V_{t+2} and γ2Vt+2-\gamma^2 V_{t+2}) cancel.
  • The red terms\color{red}{\text{red terms}} are left behind, which exactly match the definition of the nn-step advantage:
A^nπ(st,at)=k=0n1γkδt+k \hat{A}_n^\pi(\mathbf{s}_t, \mathbf{a}_t) = \sum_{k=0}^{n-1} \gamma^k \delta_{t+k}

# Final Form and Implementation

Substituting the telescoping sum into the weighted average definition simplifies the entire estimator into a single discounted sum of TD errors:

A^GAEπ(st,at)=t=t(γλ)ttδt \hat{A}_{GAE}^\pi(\mathbf{s}_t, \mathbf{a}_t) = \sum_{t'=t}^{\infty} (\gamma\lambda)^{t'-t} \delta_{t'}

In practice, this is implemented using a recursive structure that "mixes" rewards and value estimates at every step:

A^GAEπ(st,at)=rtV(st)+γ[(1λ)V(st+1)+λ(rt+1+γ[(1λ)V(st+2)+])] \hat{A}_{GAE}^{\pi}(\mathbf{s}_{t}, \mathbf{a}_{t}) = r_t - V(\mathbf{s}_t) + \gamma \Big[ (1-\lambda)V(\mathbf{s}_{t+1}) + \lambda \big( r_{t+1} + \gamma [ (1-\lambda)V(\mathbf{s}_{t+2}) + \dots ] \big) \Big]

Or, implement this recursively (backwards from the end of an episode):

A^GAEπ(st,at)=δt+(γλ)A^GAEπ(st+1,at+1) \hat{A}_{GAE}^{\pi}(\mathbf{s}_{t}, \mathbf{a}_{t}) = \delta_t + (\gamma \lambda) \hat{A}_{GAE}^{\pi}(\mathbf{s}_{t+1}, \mathbf{a}_{t+1})

This recursive view shows the specific choice at each step:

  • (1λ)V(st+1)(1-\lambda)V(\mathbf{s}_{t+1}): Trust the Critic's immediate prediction (Lower variance).
  • λ(rt+1+)\lambda(r_{t+1} + \dots): Trust the actual reward and the subsequent trajectory (Lower bias).

λ\lambda controls the bias-variance tradeoff: smaller λ\lambda cuts earlier and reduces variance, while a larger λ\lambda moves closer to an unbiased Monte Carlo estimate.

# Review so far

Algorithms

  • Policy gradients: observe what is good vs. bad, then do more of good stuff
  • Actor-critic: learn to estimate what is good vs. bad, then do more of the good stuff
    i.e. get a better policy gradient by using neural network to estimate value function

How to estimate value (policy evaluation):

  • Supervised learning directly on observed sum of future rewards
  • Supervised learning on current reward + value estimate of next state
  • Hybrid: Supervised learning on sum of next nn rewards + value of state after that

# Off-policy actor critic

replay buffer stores all previously seen transitions.
implementation: FIFO ring buffer

  1. take action aπθ(as)\mathbf{a} \sim \pi_\theta(\mathbf{a}|\mathbf{s}), get (s,a,s,r)(\mathbf{s}, \mathbf{a}, \mathbf{s}^\prime, r), store in R\mathcal{R}
  2. sample a batch {si,ai,ri,si}\{\mathbf{s}_i, \mathbf{a}_i, r_i, \mathbf{s}_i^\prime\} from buffer R\mathcal{R}
  3. update V^ϕπ\hat{V}_\phi^\pi using targets yi=ri+γV^ϕπ(si)y_i = r_i + \gamma \hat{V}_\phi^\pi(\mathbf{s}_i^\prime) to each si\mathbf{s}_i
  4. evaluate A^π(si,ai)=r(si,ai)+γV^ϕπ(si)V^ϕπ(si)\hat{A}^\pi(\mathbf{s}_i, \mathbf{a}_i) = r(\mathbf{s}_i, \mathbf{a}_i) + \gamma \hat{V}_\phi^\pi(\mathbf{s}_i^\prime) - \hat{V}_\phi^\pi(\mathbf{s}_i)
  5. evaluate θJ(θ)1Niθlogπθ(aisi)A^π(si,ai)\nabla_\theta J(\theta) \approx \frac{1}{N} \sum_i \nabla_\theta \log \pi_\theta(\mathbf{a}_i | \mathbf{s}_i) \, \hat{A}^\pi(\mathbf{s}_i, \mathbf{a}_i)
  6. update θθ+αθJ(θ)\theta \leftarrow \theta + \alpha \nabla_\theta J(\theta)

use older transitions from the same but old actor
collect transitions, store in replay buffer, sample a batch w/ size NN (e.g., 32 transitions) from the replay buffer
transitions s,a,s\mathbf{s}, \mathbf{a}, \mathbf{s}^\prime in replay buffer did not come from latest policy πθ\pi_\theta \to need to modify the classical online value-based actor-critic algorithm

online off-policy actor-critic algo has 2 problems

2 problems:

  1. In step 3, the actions in the transitions from replay buffer were taken by older actors, which would not give value of the latest actor. Formally, ai\mathbf{a}_i did not come from the latest policy πθ\pi_\theta, but from an older policy. Thus, si\mathbf{s}_i^\prime was not the results of taking an action with the latest actor.
  2. In step 5, because ai\mathbf{a}_i did not come from the latest πθ\pi_\theta, cannot compute off-policy policy gradient this way. Previously, use importance sampling.

# Problem 1 \to fix the value function

value function: expected return if start in state st\mathbf{s}_t and follow the policy π\pi
QQ function: expected return if start in state st\mathbf{s}_t, take action at\mathbf{a}_t, then follow policy π\pi

Keep the stored action ai\mathbf{a}_i paired with the reward rir_i and next state si\mathbf{s}_i^\prime it produced. Learn Qπ(si,ai)Q^\pi(\mathbf{s}_i, \mathbf{a}_i) rather than Vπ(si)V^\pi(\mathbf{s}_i): take the stored action first, then follow the current policy π\pi.

So step 3 should be: update Q^ϕπ\textcolor{red}{\hat{Q}_\phi^\pi} using targets yi=ri+γV^ϕπ(si)y_i = r_i + \gamma \textcolor{red}{\hat{V}_\phi^\pi}(\mathbf{s}_i^\prime) for each si,ai\mathbf{s}_i, \mathbf{a}_i
then regress against MSE: L(ϕ)=1NiQ^ϕπ(si,ai)yi2L(\phi) = \frac{1}{N} \sum_i \| \hat{Q}_\phi^\pi(\mathbf{s}_i, \mathbf{a}_i) - y_i \|^2

We want to learn QQ, but how do we get the target value?
Because Vπ(s)=t=tTEπθ[r(st,at)st]=Eaπ(as)[Qπ(s,a)]V^\pi(\mathbf{s}) = \sum_{t'=t}^T E_{\pi_\theta} [r(\mathbf{s}_{t'}, \mathbf{a}_{t'}) | \mathbf{s}_t] = E_{\mathbf{a} \sim \pi(\mathbf{a}|\mathbf{s})} [Q^\pi(\mathbf{s}, \mathbf{a})], we can replace VV with QQ and use the target value

yi=ri+γQ^ϕπ(si,ai,π) y_i = r_i + \gamma \textcolor{red}{\hat{Q}_\phi^\pi}(\mathbf{s}_i^\prime, \mathbf{a}_i^{\prime,\pi})

where ai,ππθ(asi)\mathbf{a}_i^{\prime,\pi} \sim \pi_\theta(\mathbf{a} | \mathbf{s}_i^\prime) is a fresh action sampled from the current policy at the replay-buffer next state, not an action stored in the replay buffer.

# Problem 2 \to fix the policy update

For the actor update, do not replace the action stored in the replay buffer. Instead, sample a separate fresh action aiππθ(asi)\mathbf{a}_i^\pi \sim \pi_\theta(\mathbf{a} | \mathbf{s}_i) from the current policy at the replay-buffer state si\mathbf{s}_i. Step 5 becomes:

θJ(θ)1Niθlogπθ(aiπsi)A^π(si,aiπ) \nabla_\theta J(\theta) \approx \frac{1}{N} \sum_i \nabla_\theta \log \pi_\theta(\textcolor{red}{\mathbf{a}_i^\pi} | \mathbf{s}_i) \hat{A}^\pi(\mathbf{s}_i, \textcolor{red}{\mathbf{a}_i^\pi})

Instead of using advantage A^π\hat{A}^\pi, use Q^π\hat{Q}^\pi directly in the equation.

θJ(θ)1Niθlogπθ(aiπsi)Q^π(si,aiπ) \nabla_\theta J(\theta) \approx \frac{1}{N} \sum_i \nabla_\theta \log \pi_\theta(\mathbf{a}_i^\pi | \mathbf{s}_i) \textcolor{red}{\hat{Q}^\pi}(\mathbf{s}_i, \mathbf{a}_i^\pi)

Higher var b/c there is no baseline, but OK b/c sampling aiπ\mathbf{a}_i^\pi does not require the simulator; we only need extra forward passes through the policy network. In practice this trades lower bias from the action mismatch for higher variance, and the larger replay buffer batch size can offset some of that variance. Also, after switching to Q^π\hat{Q}^\pi, we can drop step 4.

# Another small problem

si\mathbf{s}_i did not come from state marginal of latest policy pθ(s)p_\theta (\mathbf{s}), but from an old policy. Unfortunately, we can't do anything and this is a source of bias. It's acceptable b/c we want the optimal policy on pθ(s)p_\theta (\mathbf{s}), but we get optimal policy on a broader distr. The replay buffer contains many samples from the latest policy, and also many samples from older policies. The states from latest policy are not missed, but the trained policy is also good at other not so probable states.

# Some implementation details

In step 4, do reparameterization trick to better estimate the gradient (for Gaussian policy)

there are better way to fit Q function (more on this in next two lectures)
e.g., Q learning with deterministic actor