fix(ml): correct sign error in continuous policy log_probs

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 <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-02-26 11:46:17 +01:00
parent 1a3e89f0c7
commit 37d6a15fc3

View File

@@ -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::<f32>().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]