Add opt-in tanh squashing for DiagGaussianDistribution mean actions#2249
Draft
cgliner wants to merge 1 commit into
Draft
Add opt-in tanh squashing for DiagGaussianDistribution mean actions#2249cgliner wants to merge 1 commit into
cgliner wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds an opt-in
squash_mean_actionspolicy option for A2C/PPO policies usingDiagGaussianDistribution.When enabled with:
the Gaussian mean action network is wrapped with
nn.Tanh(), constraining the mean actions to[-1, 1].The default behavior is unchanged. The option is only available for non-gSDE Box action spaces. For gSDE, SB3 already has the existing
squash_output=Truepath.This PR also adds:
I used OpenAI Codex to help implement the change, add tests, and run local verification.
Motivation and Context
For continuous Box action spaces, especially normalized spaces like
[-1, 1], the current diagonal Gaussian policy can produce unbounded mean actions. Those actions are then clipped to the action space bounds.That clipping can lead to poor behavior near action-space edges, because the policy may learn means far outside the valid range while the environment only sees clipped boundary actions. This PR provides an opt-in way to keep the deterministic Gaussian mean inside the normalized action range with a smooth
tanhtransformation.This does not fully replace SAC-style squashed Gaussian distributions, but it gives A2C/PPO users a simple bounded-mean option while preserving backward compatibility.
Types of changes
Checklist
docs/misc/changelog.md) (required).make format(required)make check-codestyleandmake lint(required)make pytestandmake typeboth pass. (required)make doc(required)SquashedDiagGaussianDistributionsquashes the sampled action distribution. Your change squashes only the Gaussian mean.Concretely:
SquashedDiagGaussianDistributiondoes this:So both stochastic samples and deterministic actions are bounded in
[-1, 1]. Because the distribution itself is transformed bytanh, it also needs a log-probability correction using the change-of-variables term:That is what SAC uses, where the policy really is a tanh-transformed Gaussian.
Your
squash_mean_actions=Trueoption does this instead:So the center of the Gaussian is bounded in
[-1, 1], but stochastic samples can still go outside the action bounds and will still be clipped by A2C/PPO as before. The probability distribution remains a normal Gaussian, so no tanh log-prob correction is needed.Practical difference:
SquashedDiagGaussianDistribution: bounded actions, transformed distribution, corrected log-probs, no analytical entropy.squash_mean_actions=True: bounded deterministic mean, ordinary Gaussian samples, ordinary log-probs and entropy, still may require clipping sampled actions.So your change is a lighter, more conservative option for A2C/PPO. It improves deterministic/mean behavior near Box edges without changing the underlying probability distribution into a SAC-style squashed Gaussian.