Search

Search papers and lessons

Generalized Advantage Estimation (GAE)

Level 4 · 11 min read

Generalized advantage estimation scores each action by an exponentially weighted sum of one-step TD errors, and a single knob trades bias against variance.

On this page

  • Colour = topic mix
  • Probability
  • Reinforcement learning
  • Linear algebra
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

TL;DR

Generalized advantage estimation (GAE) estimates how much better each action was than expected, using the rewards and value estimates seen along a trajectory. It is a weighted sum of one-step TD errors, where the weights shrink geometrically with distance into the future. A single parameter, λ\lambda, slides the estimate between a low-variance, biased version at one end and a high-variance, unbiased version at the other.

GAE is the advantage estimator used inside most modern policy-gradient training, so the choice of λ\lambda directly shapes how fast and how stably a policy learns.

Where you'll see it: as the advantage signal in PPO and other actor-critic methods that train robots, game agents and language-model policies.

Intuition

Looking a few steps ahead

A policy-gradient update needs to know whether an action helped. The advantage answers that, but it is defined through expectations the agent cannot compute directly. It has to be estimated from one trajectory, and there are two obvious ways to do that, each with a flaw.

The first is to use one step. Take the reward you just got, add the discounted value of where you landed, and subtract the value you expected before acting. This is a single TD error. It depends on only one random reward, so it is steady from run to run. But it relies entirely on the value estimate, and if that estimate is wrong, the error is wrong too.

The second is the Monte Carlo way: follow the trajectory to its end. Add up all the rewards you actually received, discount them, and subtract the baseline. This uses no value estimate beyond the baseline, so it has no bias from a wrong value function. But the total depends on every random event along the way, so it swings a lot between trajectories.

GAE sits between these. It looks ahead a few steps, uses the real rewards for those steps, and leans on the value estimates further out. The further ahead it looks, the more it trusts the rewards and the less it trusts the value function. The parameter λ\lambda sets how quickly that trust fades.

A weighted vote over the future

Each one-step TD error is a vote on whether the action was good, cast with a different amount of evidence. GAE takes a weighted average of these votes, with the votes for distant steps counting less. The decay of the weights is what keeps the estimate from being dominated by the long, noisy tail of a trajectory. The geometric weights are the same shape as those of an exponential moving average, applied here to future TD errors instead of past observations.

05101520253000.20.40.60.81steps ahead (l)weight on the TD error
  • λ = 0.95
  • λ = 0.5
  • λ = 0
With γ = 0.99, the weight on the TD error l steps ahead is (γλ)^l. Smaller λ forgets the future faster; λ = 0 uses only the next step.

The math

Setting

Let VV be the current estimate of the state-value function and γ\gamma the discount factor. For each time step tt the TD error is

δt=Rt+1+γV(St+1)−V(St).\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t).

It is a one-sample estimate of the advantage Aπ(St,At)A_\pi(S_t, A_t), as the advantage-function lesson shows. At the end of an episode the value of the terminal state is taken to be zero.

The k-step estimates

Use kk real rewards and then bootstrap, meaning replace the rest of the future with the value estimate. Define

At(k)=∑l=0k−1γlδt+l.A_t^{(k)} = \sum_{l=0}^{k-1} \gamma^l \delta_{t+l}.

The sum telescopes. Expand each δ\delta and collect terms:

  1. Each term contributes γlRt+l+1\gamma^l R_{t+l+1}, γl+1V(St+l+1)\gamma^{l+1} V(S_{t+l+1}) and −γlV(St+l)-\gamma^l V(S_{t+l}).
  2. The VV terms cancel in pairs: the γl+1V(St+l+1)\gamma^{l+1} V(S_{t+l+1}) from step ll is cancelled by the −γl+1V(St+l+1)-\gamma^{l+1} V(S_{t+l+1}) that step l+1l+1 contributes.
  3. What remains is
At(k)=−V(St)+∑l=0k−1γlRt+l+1+γkV(St+k).A_t^{(k)} = -V(S_t) + \sum_{l=0}^{k-1} \gamma^l R_{t+l+1} + \gamma^k V(S_{t+k}).

This is "discounted real rewards for kk steps, then the value estimate, minus the baseline." Two extremes matter:

  • k=1k = 1 gives At(1)=δtA_t^{(1)} = \delta_t, the one-step TD error. It has low variance and high bias, because it trusts VV a lot.
  • k→∞k \to \infty (or the end of the episode) gives Gt−V(St)G_t - V(S_t), where GtG_t is the full discounted return. It is unbiased but has high variance.

Generalized advantage estimation

GAE is the average of the kk-step estimates, weighted by a geometric distribution:

AtGAE(γ,λ)=(1−λ)∑k=1∞λk−1At(k),0≤λ≤1.A_t^{\text{GAE}(\gamma,\lambda)} = (1 - \lambda) \sum_{k=1}^{\infty} \lambda^{k-1} A_t^{(k)}, \qquad 0 \le \lambda \le 1.

The weights (1−λ)λk−1(1 - \lambda)\lambda^{k-1} sum to one, so this is a proper average. Substitute the definition of A(k)A^{(k)} and swap the order of summation. The weight that multiplies δt+l\delta_{t+l} is

∑k=l+1∞(1−λ)λk−1⋅γl=γlλl,\sum_{k=l+1}^{\infty} (1 - \lambda)\lambda^{k-1} \cdot \gamma^l = \gamma^l \lambda^l,

because ∑k=l+1∞(1−λ)λk−1=(1−λ)λl/(1−λ)=λl\sum_{k=l+1}^{\infty} (1-\lambda)\lambda^{k-1} = (1-\lambda)\lambda^{l}/(1-\lambda) = \lambda^l. So

AtGAE=∑l=0∞(γλ)l δt+l.A_t^{\text{GAE}} = \sum_{l=0}^{\infty} (\gamma\lambda)^l \, \delta_{t+l}.

This is the form used in practice. It can be computed in a single backwards pass over a trajectory with the recursion

A^t=δt+γλ A^t+1,A^T=0 at the end,\hat{A}_t = \delta_t + \gamma\lambda\, \hat{A}_{t+1}, \qquad \hat{A}_T = 0 \text{ at the end},

so the cost is a handful of arithmetic operations per step.

What the two knobs do

The parameter λ\lambda controls the mix of the two extremes. At λ=0\lambda = 0 only δt\delta_t survives, which is the one-step TD estimate. At λ=1\lambda = 1 the weights are γl\gamma^l, which gives the full Monte Carlo advantage Gt−V(St)G_t - V(S_t).

The discount γ\gamma also shrinks the weights, so the product γλ\gamma\lambda sets an effective horizon. Weights fall geometrically, so the estimate looks roughly 1/(1−γλ)1 / (1 - \gamma\lambda) steps ahead. Concretely, a sensible default such as γ=0.99\gamma = 0.99 and λ=0.95\lambda = 0.95 gives γλ=0.9405\gamma\lambda = 0.9405 and a horizon of about 17 steps.

A useful way to read the bias-variance trade is this. The value estimate VV is wrong by some amount. Every δt+l\delta_{t+l} carries VV's error at its two ends. With small λ\lambda, the estimate is dominated by the nearby δ\deltas, so it inherits VV's error. With large λ\lambda, the estimate is dominated by real rewards, which are unbiased but noisy, and the noise grows with the number of terms included.

Worked examples

Example
A three-step episode by hand

Take an episode with three steps, γ=0.9\gamma = 0.9, rewards r0=1r_0 = 1, r1=0r_1 = 0, r2=2r_2 = 2, and the episode ends after step 2. The current value estimates are V(S0)=1.0V(S_0) = 1.0, V(S1)=1.5V(S_1) = 1.5, V(S2)=1.0V(S_2) = 1.0, and V(terminal)=0V(\text{terminal}) = 0.

  1. TD errors. δ0=1+0.9×1.5−1.0=1.35\delta_0 = 1 + 0.9 \times 1.5 - 1.0 = 1.35. δ1=0+0.9×1.0−1.5=−0.6\delta_1 = 0 + 0.9 \times 1.0 - 1.5 = -0.6. δ2=2+0−1.0=1.0\delta_2 = 2 + 0 - 1.0 = 1.0.
  2. Take λ=0.5\lambda = 0.5, so γλ=0.45\gamma\lambda = 0.45. Start from the end: A^2=δ2=1.0\hat{A}_2 = \delta_2 = 1.0.
  3. A^1=δ1+0.45×1.0=−0.6+0.45=−0.15\hat{A}_1 = \delta_1 + 0.45 \times 1.0 = -0.6 + 0.45 = -0.15.
  4. A^0=δ0+0.45×(−0.15)=1.35−0.0675=1.2825\hat{A}_0 = \delta_0 + 0.45 \times (-0.15) = 1.35 - 0.0675 = 1.2825.

Now compare the extremes for the first step. With λ=0\lambda = 0, the advantage is δ0=1.35\delta_0 = 1.35. With λ=1\lambda = 1, it is δ0+0.9 δ1+0.81 δ2=1.35−0.54+0.81=1.62\delta_0 + 0.9\,\delta_1 + 0.81\,\delta_2 = 1.35 - 0.54 + 0.81 = 1.62. This matches the Monte Carlo advantage: the full return from step 0 is 1+0+0.81×2=2.621 + 0 + 0.81 \times 2 = 2.62, and 2.62−V(S0)=1.622.62 - V(S_0) = 1.62.

Interpretation: λ\lambda moves the estimate from 1.351.35 to 1.621.62. Here λ=0.5\lambda = 0.5 gives 1.28251.2825, which sits between them but closer to the one-step end, because the weights on distant errors fall fast.

Example
Running the recursion in code

The same recursion in Python, with the output from running it:

def gae(rewards, values, gamma, lam):
    adv = [0.0] * len(rewards)
    running = 0.0
    for t in reversed(range(len(rewards))):
        delta = rewards[t] + gamma * values[t + 1] - values[t]
        running = delta + gamma * lam * running
        adv[t] = running
    return adv
 
rewards = [1.0, 0.0, 2.0]
values = [1.0, 1.5, 1.0, 0.0]   # one extra entry: the value after the last step
for lam in (0.0, 0.5, 1.0):
    print(lam, [round(a, 4) for a in gae(rewards, values, 0.9, lam)])
0.0 [1.35, -0.6, 1.0]
0.5 [1.2825, -0.15, 1.0]
1.0 [1.62, 0.3, 1.0]

The first column reproduces the hand calculation. The values list has one more entry than rewards because the last transition bootstraps from the value of the state after the final step. Here that is zero because the episode ended.

Example
How far ahead the estimate really looks

Use a typical setting, γ=0.99\gamma = 0.99 and λ=0.95\lambda = 0.95, so γλ=0.9405\gamma\lambda = 0.9405.

  1. Weight on the TD error 10 steps ahead: 0.940510≈0.540.9405^{10} \approx 0.54.
  2. Weight on the TD error 50 steps ahead: 0.940550≈0.0470.9405^{50} \approx 0.047.
  3. Effective horizon: 1/(1−0.9405)≈16.81 / (1 - 0.9405) \approx 16.8 steps.

Interpretation: an error 10 steps away still counts for about half as much as the one-step error. By 50 steps it counts for less than 5 percent. A policy trained on a task with rewards a hundred steps apart needs either a larger λ\lambda or a better value function, because the estimate will not reach that far.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Value function is poorSmall λ\lambda inherits its error, so the advantage is biasedIncrease λ\lambda, or improve the value network before tuning anything else
Long, noisy episodesLarge λ\lambda sums many random rewards, so the estimate has high varianceDecrease λ\lambda, or use more parallel rollouts to average the noise
Rewards far apart in timeEffective horizon is shorter than the reward delay, so credit does not reach the early actionsRaise γ\gamma and λ\lambda together, or add reward shaping
Advantages on wildly different scales across batchesGradient steps change size unpredictablyNormalise advantages within each batch, as most implementations do
Episodes cut off by a time limitBootstrapping from a terminal value of zero is wrong for a truncated episodeBootstrap from the value of the last real state

In practice a value near 0.95 for λ\lambda with γ\gamma near 0.99 has worked across many continuous-control benchmarks, which is why it is a common default. Treat it as a starting point, not a law. GAE also does not fix a bad value function, and it needs a policy that visits the states being evaluated.

History

Richard Sutton introduced temporal-difference learning in the 1980s, and his TD(λ) family of methods used the same weighting idea over multi-step returns. The mixed-horizon weighting that GAE uses is a direct descendant of that work.

GAE was introduced by John Schulman, Philipp Moritz, Sergey Levine, Michael Jordan and Pieter Abbeel in 2015, and published at ICLR in 2016 as a way to reduce the variance of policy-gradient estimates for continuous control. It caught on because it was simple to add to existing policy-gradient code and gave a clean knob for the bias-variance trade. When Schulman and colleagues introduced PPO in 2017, GAE became part of the standard training recipe, and it is still widely used in actor-critic methods.

Check yourself

QuestionRecall: what is the TD error δt\delta_t, and what does it estimate?Show answer

δt=Rt+1+γV(St+1)−V(St)\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t). It is one step of reward plus the discounted value of the next state, minus the value of the current state. It is a single-sample estimate of the advantage Aπ(St,At)A_\pi(S_t, A_t), and it is the quantity TD learning computes at each step.

QuestionComputation: with γ=0.9\gamma = 0.9, λ=0.5\lambda = 0.5, and TD errors δ0=2\delta_0 = 2, δ1=1\delta_1 = 1, δ2=−1\delta_2 = -1 and nothing after step 2, what is the GAE advantage at step 0?Show answer

Use the recursion from the end. A^2=−1\hat{A}_2 = -1. Then A^1=1+0.45×(−1)=0.55\hat{A}_1 = 1 + 0.45 \times (-1) = 0.55. Then A^0=2+0.45×0.55=2.2475\hat{A}_0 = 2 + 0.45 \times 0.55 = 2.2475. Check with the direct sum: 2+0.45×1+0.2025×(−1)=2.24752 + 0.45 \times 1 + 0.2025 \times (-1) = 2.2475.

QuestionWhy: why does setting λ=0\lambda = 0 make the advantage estimate biased but low in variance?Show answer

At λ=0\lambda = 0 the estimate is only the one-step TD error. It uses one real reward, so the randomness comes from a single transition and stays small. But it also depends on the value estimates V(St)V(S_t) and V(St+1)V(S_{t+1}) directly. If those are wrong, the error passes straight into the advantage, and nothing in the estimate corrects it.

QuestionApplied: a team trains a robot policy on episodes of 2000 steps with γ=0.999\gamma = 0.999. Their advantages are very noisy, and training is unstable. They try λ=1\lambda = 1, and training gets worse. What should they consider?Show answer

With λ=1\lambda = 1 the estimate is the full Monte Carlo advantage over a 2000-step episode. Summing that many random rewards produces high variance, which is the likely cause of the instability. They should lower λ\lambda, for example to around 0.9 to 0.95, so the estimate depends mostly on nearby steps. They should also check that the value function is reasonable, since a lower λ\lambda will then inherit its error, and consider normalising advantages within each batch.

Sources