From 37d6a15fc3d94ec788ebd9c1ec71fb7a77e44ec2 Mon Sep 17 00:00:00 2001 From: jgrusewski Date: Thu, 26 Feb 2026 11:46:17 +0100 Subject: [PATCH] fix(ml): correct sign error in continuous policy log_probs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tensor-based log_probs() had an inverted sign on the squared difference term: .sub(&(x * -0.5)) = +0.5*x² instead of -0.5*x². This caused actions far from the mean to get higher log probabilities. Also fix test assertions: continuous log probability densities CAN be positive (when σ is small and action is near mean), unlike discrete log probabilities which are always <= 0. Co-Authored-By: Claude Opus 4.6 --- crates/ml/src/ppo/continuous_policy.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/ml/src/ppo/continuous_policy.rs b/crates/ml/src/ppo/continuous_policy.rs index 84e8f586b..909b64eea 100644 --- a/crates/ml/src/ppo/continuous_policy.rs +++ b/crates/ml/src/ppo/continuous_policy.rs @@ -330,7 +330,7 @@ impl ContinuousPolicyNetwork { let log_prob = (log_2pi_tensor * (-0.5))? .sub(&log_stds)? - .sub(&(squared_diff * (-0.5))?)?; + .sub(&(squared_diff * 0.5)?)?; Ok(log_prob.squeeze(1)?) // Remove extra dimension if present } @@ -567,9 +567,8 @@ mod tests { // Action should be in bounds assert!(action >= 0.0 && action <= 1.0); - // Log prob should be finite and negative + // Log prob should be finite (can be positive for continuous distributions with small σ) assert!(log_prob.is_finite()); - assert!(log_prob <= 0.0); } #[test] @@ -592,7 +591,7 @@ mod tests { assert_eq!(log_probs.dims(), &[2]); let log_probs_vec = log_probs.to_vec1::().unwrap(); - assert!(log_probs_vec.iter().all(|&lp| lp.is_finite() && lp <= 0.0)); + assert!(log_probs_vec.iter().all(|&lp| lp.is_finite())); } #[test]