From 7f34108ed6fddc63f037831b07db830f6ae8197e Mon Sep 17 00:00:00 2001 From: Antonin Raffin Date: Fri, 20 Dec 2019 18:02:01 +0100 Subject: [PATCH] Fix `exp_ln` computation --- torchy_baselines/common/distributions.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/torchy_baselines/common/distributions.py b/torchy_baselines/common/distributions.py index 97eaf6f..ba37cd3 100644 --- a/torchy_baselines/common/distributions.py +++ b/torchy_baselines/common/distributions.py @@ -272,10 +272,11 @@ class StateDependentNoiseDistribution(Distribution): if self.use_expln: # From SDE paper, it allows to keep variance # above zero and prevent it from growing too fast - if log_std <= 0: - std = th.exp(log_std) - else: - std = th.log(log_std + 1.0) + 1.0 + below_threshold = th.exp(log_std) * (log_std <= 0) + # Avoid NaN: zeros values that are below zero + safe_log_std = log_std * (log_std > 0) + self.epsilon + above_threshold = (th.log1p(safe_log_std) + 1.0) * (log_std > 0) + std = below_threshold + above_threshold else: # Use normal exponential std = th.exp(log_std)