class

TanhTransform

extendsTransform
TanhTransform()
source

Element-wise hyperbolic tangent bijection y=tanh(x)y = \tanh(x).

Maps R(1,1)\mathbb{R} \to (-1, 1) element-wise. Commonly used in reinforcement learning to push a Normal policy through tanh so actions live in a bounded box (SAC and friends). Monotone increasing, bijective, event_dim = 0.

Notes

Forward: y=tanh(x)y = \tanh(x).

Inverse: x=12log ⁣((1+y)/(1y))x = \tfrac{1}{2}\log\!\bigl((1+y)/(1-y)\bigr) (the inverse hyperbolic tangent).

Log Jacobian determinant (numerically stable form):

log ⁣yx=log(1tanh2(x))=2(log2xsoftplus(2x))\log\!\left|\frac{\partial y}{\partial x}\right| = \log\bigl(1 - \tanh^2(x)\bigr) = 2 \bigl(\log 2 - x - \operatorname{softplus}(-2x)\bigr)

This formulation avoids overflow / underflow when x|x| is large, which the naive form log(1tanh2x)\log(1 - \tanh^2 x) would suffer (since tanh(x)±1\tanh(x) \to \pm 1).

Examples

>>> import lucid
>>> from lucid.distributions.transforms import TanhTransform
>>> T = TanhTransform()
>>> T(lucid.tensor(0.0))  # tanh(0) = 0
Tensor(0.0)

Methods (1)

fn

log_abs_det_jacobian

Tensor
log_abs_det_jacobian(x: Tensor, y: Tensor)
source

Numerically-stable log(1tanh2(x))=2(log2xsoftplus(2x))\log(1 - \tanh^2(x)) = 2\bigl(\log 2 - x - \operatorname{softplus}(-2x)\bigr).