# PPO eps clip is unbounded when advantage is negative  

**URL:** <https://forums.unrealengine.com/t/ppo-eps-clip-is-unbounded-when-advantage-is-negative/2835449>\
**Category:** AI\
**Tags:** Python, Reinforcement-Learning, platform-support, unreal-engine, Learning-Agents, bug-report\
**Created:** [September 29, 2026, 6:50pm UTC](https://forums.unrealengine.com/t/ppo-eps-clip-is-unbounded-when-advantage-is-negative/2835449 "2026-09-29T18:50:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MannyVindiola](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MannyVindiola](https://forums.unrealengine.com/u/MannyVindiola)\
**Post date:** [September 29, 2026, 6:50pm UTC](https://forums.unrealengine.com/t/ppo-eps-clip-is-unbounded-when-advantage-is-negative/2835449/1 "2026-09-29T18:50:35Z")

</div>

Hello,

I have found an issue with the trust region when adavantage is negative. Take a look at this code snippet.

```auto
surr = torch.min(ratio, ratio.clamp(eps_clip_log_min, eps_clip_log_max))
loss_surr += ... adv[:,i] * -(surr / act_num) ...

```

`min(r, clamp(r, lo, hi))` is `min(r, hi)` for any `lo` — the lower bound is ignored. And the advantage is multiplied in _after_ the min, whereas PPO takes the min over the _products_, so the sign of the advantage decides which side should bind.

**Why it matters**

For a positive advantage PPO stops pushing once the ratio exceeds `1+ε`. For a negative advantage it should stop pushing once the ratio drops below `1−ε`. The old code applied the upper bound to both cases, so negative-advantage samples had no trust region at all — unbounded descent.

It stays harmless only while `MinimumAdvantage = 0`, which zeroes every negative advantage before the surrogate sees it. The editor slider goes to −10 and its tooltip invites negative values. Anecdotally for my environments training would completely fall apart when `MinimumAdvantage < 0` which is what lead me to look into this.

**Example**

A three-condition experiment on the my task. All conditions shared one observation configuration, one set of hyperparameters and `AdvantageMin = −3`; the only variable was whether the trainer carried the fix below. Condition 3 ran the original trainer with extra diagnostics enabled, so the collapse could be watched directly.

| **per-gather** | **condition 2 · fixed** | **condition 3 · original** |
| --- | --- | --- |
| `log_std_mean` at gather 7 | 0.154 | −0.542 |
| `log_std_min`, worst | −0.13 | −4.25 (σ = 0.014) |
| `approx_kl`, peak | 0.009 | 8.55 |
| `grads/policy_preclip`, peak | 0.05 | 679 |
| `clip_fraction` | 0.05 | 0.18 |
| `critic/explained_variance` | 0.21 | 0.53 → 0.20  
 |

Both fixed arms held `log_std_mean` between 0.15 and 0.23 across the same window. Measured `frac_positive` in the collapsing run ran 0.25 → 0.48, so **52–75% of every batch** was negative-advantage and therefore unconstrained.

Clip the side that the advantage sign actually makes binding, staying in Epic’s log-space formulation.

```auto
surr = torch.where(
    adv[:,i] >= 0.0,
    torch.min(ratio, torch.full_like(ratio, eps_clip_log_max)),
    torch.max(ratio, torch.full_like(ratio, eps_clip_log_min)))

```

There is one other issue. The log\_std is only clamped on the positive side.

> log\_std\_clipped = torch.clip(log\_std, None, 10.0)  
> std = torch.exp(log\_std\_clipped)  
> logp = -((value - mean)\*\*2) / (2 \* std\*\*2) - log\_std\_clipped - log\_sqrt\_2pi

If log\_std is pushed very negative (below -54) then std becomes a 0 in the denominator of logp leading to a NaN and training collapse. I saw this occur in my training runs in the current version but not with the fix described above. I was able to prevent this by clamping the negative log\_std as well.

> log\_std\_clipped = torch.clip(log\_std, -10.0, 10.0)

---

<div class="post-metadata">

**Author:** ![Deathcalibur](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deathcalibur/32/1037610_2.png) [@Deathcalibur](https://forums.unrealengine.com/u/Deathcalibur)\
**Post date:** [September 30, 2026, 6:14pm UTC](https://forums.unrealengine.com/t/ppo-eps-clip-is-unbounded-when-advantage-is-negative/2835449/2 "2026-09-30T18:14:01Z")

</div>

Thanks, I will take a look!

---

<div class="post-metadata">

**Author:** ![Deathcalibur](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/deathcalibur/32/1037610_2.png) [@Deathcalibur](https://forums.unrealengine.com/u/Deathcalibur)\
**Post date:** [October 22, 2026, 3:39pm UTC](https://forums.unrealengine.com/t/ppo-eps-clip-is-unbounded-when-advantage-is-negative/2835449/3 "2026-10-22T15:39:45Z")

</div>

A fix for this has been submitted on Main. Thanks for pointing out the issue!
