从反向传播角度理解 PPO 损失函数(稳定渲染版)
从反向传播角度理解 PPO 损失函数
从反向传播角度看,PPO 的损失函数可以理解为一个三通道协同优化系统,通过三股梯度流共同作用于 Actor(策略网络)和 Critic(价值网络)。
1. 核心公式
Clipped Surrogate Objective
$$
L^{\mathrm{CLIP}}(\theta) = \mathbb{E}_t[\min(r_t(\theta)\hat{A}_t,\ \mathrm{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon)\hat{A}_t)]
$$
总损失函数
$$
L(\theta, \phi) = -L^{\mathrm{CLIP}}(\theta) + c_1 L^{\mathrm{VF}}(\phi) - c_2 H(\pi_\theta)
$$
2. 各部分展开
2.1 Policy Loss (Actor)
$$
L^{\mathrm{CLIP}}(\theta) = \mathbb{E}_t[\min(r_t(\theta)\hat{A}_t,\ \mathrm{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon)\hat{A}_t)]
$$
2.2 Value Loss (Critic)
$$
L^{\mathrm{VF}}(\phi) = \mathbb{E}_t[(V_{\phi}(s_t) - R_t)^2]
$$
2.3 Entropy Bonus
$$
H(\pi_\theta) = \mathbb{E}_t[H(\pi_\theta(\cdot \mid s_t))]
$$
$$
H(\pi_\theta(\cdot \mid s_t)) = -\sum_a \pi_\theta(a \mid s_t)\log \pi_\theta(a \mid s_t)
$$
3. 符号说明
$$
r_t(\theta) = \frac{\pi_\theta(a_t \mid s_t)}{\pi_{\theta_{\mathrm{old}}}(a_t \mid s_t)}
$$
- $r_t(\theta)$:概率比率
- $\hat{A}_t$:优势函数
- $V_\phi(s_t)$:状态价值函数
- $R_t$:回报
- $H(\pi_\theta)$:策略熵
4. Policy 梯度机制
当 $\hat{A}_t > 0$:
- 增大动作概率
- 若 $r_t > 1+\epsilon$,梯度被截断
当 $\hat{A}_t < 0$:
- 减小动作概率
- 若 $r_t < 1-\epsilon$,梯度被截断
👉 min + clip 实现自适应梯度裁剪
5. Value 梯度
$$
\nabla_\phi L^{\mathrm{VF}} = 2 (V_\phi(s_t) - R_t)\nabla_\phi V_\phi(s_t)
$$
6. Entropy 作用
$$
H(\pi_\theta) = -\sum_a \pi_\theta(a)\log \pi_\theta(a)
$$
👉 推动分布更加均匀,防止策略过早收敛
7. 梯度流分解
Actor:
$$
\nabla_\theta L = \nabla_\theta L^{\mathrm{CLIP}} - c_2 \nabla_\theta H(\pi_\theta)
$$
Critic:
$$
\nabla_\phi L = c_1 \nabla_\phi L^{\mathrm{VF}}
$$
8. 训练动态
- 初期:Value Loss 主导,策略接近随机
- 中期:Clipping 开始频繁触发
- 后期:策略与熵达到平衡
9. PPO 稳定性来源
- Clipping 限制更新幅度
- Actor-Critic 解耦
- 熵正则维持探索
- 概率比率提供动态约束
总结
PPO 的本质是:
👉 在反向传播中构建一个受约束的多目标优化系统
同时实现:
- 策略优化
- 价值估计
- 探索控制