Drill · 手撕

Drill: GAE (Generalized Advantage Estimation)

可运行的 from-scratch 实现 + 测试。PPO 里估计优势 AtA_t 的标准做法。

数学 / The math

TD 残差:  δt=rt+γV(st+1)V(st)\;\delta_t = r_t + \gamma V(s_{t+1}) - V(s_t)

GAE 是 δ\delta 的指数加权和:

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

实现上用反向递推(从后往前):  At=δt+γλAt+1\;A_t = \delta_t + \gamma\lambda\,A_{t+1}。回报 Rt=At+V(st)R_t = A_t + V(s_t)

两个极端:

文件 / Files

python from_scratch.py     # 演示
python test_gae.py         # 测试(或 pytest)

追问分层 / Stratified follow-ups