#215Generalised Advantage Estimation (GAE)MediumLLMsFine TuningAgentic AIAsked atOpenAI · Anthropic · Meta
Generalised Advantage Estimation (GAE)
Background
Generalised Advantage Estimation (GAE) interpolates between the high-bias TD(0) advantage estimator and the high-variance Monte-Carlo return. The knob trades bias for variance. GAE is the standard advantage estimator inside PPO and most modern RLHF stacks.
Problem statement
Implement gae(rewards, values, gamma=0.99, lam=0.95). Walk backwards:
Assume (episode boundary) and .
Input
rewards— array-like length .values— array-like length , the critic's value estimates at each step.gamma—floatin , discount factor (default0.99).lam—floatin , GAE mixing parameter (default0.95).
Output
Returns np.ndarray length of per-step advantages.
Examples
Example 1 — single step
Input: rewards=[1.0], values=[0.2]
Output: A_0 = r_0 - V_0 = 0.8
Example 2 — hand-computed two-step
Input: rewards=[0, 1.0], values=[0, 0]; gamma=0.5, lam=1.0
Output: A_1 = 1.0; A_0 = 0 + 0.5 * 1.0 * 1.0 = 0.5
Example 3 — output length matches input
Input: zeros of length 5
Output: shape (5,)
Constraints
rewardsandvaluesmust be the same length.- Returns
np.ndarrayof floats. - Final advantage uses (assume terminal state).
Notes
- is TD(0). . Lowest variance, highest bias.
- is Monte Carlo. . Lowest bias, highest variance.
- PPO defaults. Most PPO implementations use , .
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example: three-step trajectory advantages
- •Reference: assorted four-step trajectory
- •Sample: lambda=1 gives discounted Monte-Carlo return minus baseline