fix(tanh_normal): keep TanhNormal finite on saturated samples - #31
Open
mctigger wants to merge 1 commit into
Open
fix(tanh_normal): keep TanhNormal finite on saturated samples#31mctigger wants to merge 1 commit into
mctigger wants to merge 1 commit into
Conversation
float32 tanh rounds to exactly ±1 once |x| exceeds about 9, which a Normal with a scale of a few units samples routinely. ClampedTanhTransform fed such values through a bare atanh (±inf) and a log(1 - y² + 1e-6) log-det (floored at log 1e-6 with a 1/eps derivative), so log_prob became -inf and the Monte-Carlo entropy/mode of the SamplingDistribution wrapper went non-finite with NaN gradients. A TanhNormal actor head initialised at std ≈ 5 hit this on every state. Follow DreamerV2's TanhBijector: - _inverse clamps y to the largest magnitude strictly below 1 that its dtype represents before atanh, so x stays finite and the gradient through a saturated sample is zero instead of infinite; - log_abs_det_jacobian uses 2 (log 2 - x - softplus(-2x)) evaluated on x (the same identity torch's TanhTransform uses), exact for every finite x. Off saturation the density is unchanged (matches the analytic form to ~1e-6). Adds transform-level and distribution-level regression tests for the saturated regime, including half precision. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MUeQAbKsxvBA7JFLTU5vDv
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.
float32 tanh rounds to exactly ±1 once |x| exceeds about 9, which a Normal with a scale of a few units samples routinely. ClampedTanhTransform fed such values through a bare atanh (±inf) and a log(1 - y² + 1e-6) log-det (floored at log 1e-6 with a 1/eps derivative), so log_prob became -inf and the Monte-Carlo entropy/mode of the SamplingDistribution wrapper went non-finite with NaN gradients. A TanhNormal actor head initialised at std ≈ 5 hit this on every state.
Follow DreamerV2's TanhBijector:
Off saturation the density is unchanged (matches the analytic form to ~1e-6). Adds transform-level and distribution-level regression tests for the saturated regime, including half precision.