# TanhTransform¶

class paddle.distribution. TanhTransform [source]

Tanh transformation with mapping \(y = \tanh(x)\).

Examples

```import paddle

tanh = paddle.distribution.TanhTransform()

x = paddle.to_tensor([[1., 2., 3.], [4., 5., 6.]])

print(tanh.forward(x))
# Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [[0.76159418, 0.96402758, 0.99505478],
#         [0.99932933, 0.99990922, 0.99998772]])
print(tanh.inverse(tanh.forward(x)))
# Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [[1.00000012, 2.        , 3.00000286],
#         [4.00002146, 5.00009823, 6.00039864]])
print(tanh.forward_log_det_jacobian(x))
# Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [[-0.86756170 , -2.65000558 , -4.61865711 ],
#         [-6.61437654 , -8.61379623 , -10.61371803]])
print(tanh.inverse_log_det_jacobian(tanh.forward(x)))
# Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [[0.86756176 , 2.65000558 , 4.61866283 ],
#         [6.61441946 , 8.61399269 , 10.61451530]])
```
forward ( x )

Forward transformation with mapping \(y = f(x)\).

Useful for turning one random outcome into another.

Parameters

x (Tensos) – Input parameter, generally is a sample generated from `Distribution`.

Returns

Outcome of forward transformation.

Return type

Tensor

forward_log_det_jacobian ( x )

The log of the absolute value of the determinant of the matrix of all first-order partial derivatives of the inverse function.

Parameters

x (Tensor) – Input tensor, generally is a sample generated from `Distribution`

Returns

The log of the absolute value of Jacobian determinant.

Return type

Tensor

forward_shape ( shape )

Infer the shape of forward transformation.

Parameters

shape (Sequence[int]) – The input shape.

Returns

The output shape.

Return type

Sequence[int]

inverse ( y )

Inverse transformation \(x = f^{-1}(y)\). It’s useful for “reversing” a transformation to compute one probability in terms of another.

Parameters

y (Tensor) – Input parameter for inverse transformation.

Returns

Outcome of inverse transform.

Return type

Tensor

inverse_log_det_jacobian ( y )

Compute \(log|det J_{f^{-1}}(y)|\). Note that `forward_log_det_jacobian` is the negative of this function, evaluated at \(f^{-1}(y)\).

Parameters

y (Tensor) – The input to the `inverse` Jacobian determinant evaluation.

Returns

The value of \(log|det J_{f^{-1}}(y)|\).

Return type

Tensor

inverse_shape ( shape )

Infer the shape of inverse transformation.

Parameters

shape (Sequence[int]) – The input shape of inverse transformation.

Returns

The output shape of inverse transformation.

Return type

Sequence[int]