The code you provided appears to be implementing the Dual Clip PPO algorithm, which is a variation of the Proximal Policy Optimization (PPO) algorithm.
Here's a breakdown of the code:
-
Value Loss: The value loss is calculated using a clipped version of the value function. The clip parameter is used to limit the change in the value function.
-
Entropy Loss: The entropy loss is calculated as the negative entropy of the policy.
-
Policy Loss: The policy loss is calculated using the Dual Clip PPO method.
- For positive advantages, it uses the standard clipped policy loss.
- For negative advantages, it uses a more lenient clipping.
Here's the specific part of the code that implements the Dual Clip PPO:
# Dual clip for negative advantage
# 负advantage的双重裁剪
# When advantage < 0, apply more lenient clipping
# 当advantage < 0时,应用更宽松的裁剪
dual_clip_mask = (adv < 0).float()
policy_loss3 = -self.dual_clip_param * adv
dual_clipped_loss = torch.minimum(policy_loss1, policy_loss3)
# Combine standard clip (for positive adv) and dual clip (for negative adv)
# 结合标准裁剪(正advantage)和双重裁剪(负advantage)
policy_loss = ((1 - dual_clip_mask) * clipped_policy_loss +
dual_clip_mask * dual_clipped_loss).mean()
In this code, self.dual_clip_param seems to be the parameter that controls the leniency of the clipping for negative advantages. You set param=2.0, which means that when the advantage is negative, the policy loss will be clipped to -2.0 * adv.
However, there seems to be a mistake in the code. The ratio is calculated as:
new_prob = (one_hot_action * prob).sum(1, keepdim=True)
ratio = new_prob / old_prob
But new_prob is not necessarily the probability of the action in the new policy. It seems that prob is the probability of all actions, not just the action taken.
To fix this, you should calculate new_prob as the probability of the action taken in the new policy.
Here's the correct way to calculate ratio:
new_prob = prob.gather(1, old_action[:, 0].unsqueeze(1))
ratio = new_prob / old_prob
In this code, prob.gather(1, old_action[:, 0].unsqueeze(1)) gets the probability of the action taken in the new policy.
Here's the complete corrected code:
def compute_loss(self, data_list, rst_list):
(
feature,
reward,
old_value,
tdret,
adv,
old_action,
old_prob,
legal_action,
) = data_list
# value loss
# 价值损失
value = rst_list[1].squeeze(1)
old_value = old_value
adv = adv
tdret = tdret
value_clip = old_value + (value - old_value).clamp(-self.clip_param, self.clip_param)
value_loss1 = torch.square(tdret - value_clip)
value_loss2 = torch.square(tdret - value)
value_loss = 0.5 * torch.maximum(value_loss1, value_loss2).mean()
# entropy loss
# 熵损失
prob = rst_list[0]
entropy_loss = (-prob * torch.log(prob.clamp(1e-9, 1))).sum(1).mean()
# policy loss with Dual Clip PPO
# 使用Dual Clip PPO的策略损失
clip_fracs = []
new_prob = prob.gather(1, old_action[:, 0].unsqueeze(1))
ratio = new_prob / old_prob
clip_fracs.append((ratio - 1.0).abs().gt(self.clip_param).float().mean())
# Standard clipped policy loss
# 标准裁剪策略损失
policy_loss1 = -ratio * adv
policy_loss2 = -ratio.clamp(1 - self.clip_param, 1 + self.clip_param) * adv
clipped_policy_loss = torch.maximum(policy_loss1, policy_loss2)
# Dual clip for negative advantage
# 负advantage的双重裁剪
# When advantage < 0, apply more lenient clipping
# 当advantage < 0时,应用更宽松的裁剪
dual_clip_mask = (adv < 0).float()
policy_loss3 = -self.dual_clip_param * adv
dual_clipped_loss = torch.minimum(policy_loss1, policy_loss3)
# Combine standard clip (for positive adv) and dual clip (for negative adv)
# 结合标准裁剪(正advantage)和双重裁剪(负advantage)
policy_loss = ((1 - dual_clip_mask) * clipped_policy_loss +
dual_clip_mask * dual_clipped_loss).mean()
total_loss = value_loss * self.vf_coef + policy_loss - self.var_beta * entropy_loss
# Additional monitoring for Dual Clip PPO
# Dual Clip PPO的额外监控信息
negative_adv_ratio = dual_clip_mask.mean() # 负advantage的比例
# Fraction of steps where ratio exceeds the dual clip threshold when advantage is negative
dual_clip_fracs = (ratio.gt(self.dual_clip_param).float() * dual_clip_mask).sum() / (dual_clip_mask.sum() + 1e-8)
info_list = [tdret.mean(), value_loss, policy_loss, entropy_loss] + clip_fracs
info_list += [adv.mean(), adv.std(), reward.mean(), negative_adv_ratio, dual_clip_fracs]
return total_loss, info_list