AbsTransform

class paddle.distribution. AbsTransform [源代码]

取绝对值变换 \(y = |x|\) .

AbsTransform 不是双射变换,其逆变换处理逻辑如下:

  • \(y \in (0, +\infty )\) , AbsTransform.inverse(y) 返回元组 (-y, y) .

  • \(y=0\) , AbsTransform.inverse(y) 返回 (0, 0) .

  • \(y \in (-\infty, 0)\) , 为了避免对 Tensor 数据进行判断带来性能下降, AbsTransform.inverse(y) 仍返回 (-y, y) , 注意这本质上是一个错误结果,仅仅出于 性能考虑。

代码示例

import paddle

abs = paddle.distribution.AbsTransform()

print(abs.forward(paddle.to_tensor([-1., 0., 1.])))
# Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [1., 0., 1.])

print(abs.inverse(paddle.to_tensor(1.)))
# (Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [-1.]), Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [1.]))

# The |dX/dY| is constant 1. So Log|dX/dY| == 0
print(abs.inverse_log_det_jacobian(paddle.to_tensor(1.)))
# (Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        0.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        0.))

#Special case handling of 0.
print(abs.inverse(paddle.to_tensor(0.)))
# (Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [0.]), Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        [0.]))
print(abs.inverse_log_det_jacobian(paddle.to_tensor(0.)))
# (Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        0.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
#        0.))

方法

forward(x)

计算正变换 \(y=f(x)\) 的结果。

参数

  • x (Tensor) - 正变换输入参数,通常为 Distribution 的随机采样结果。

返回

  • y (Tensor) - 正变换的计算结果。

inverse(y)

计算逆变换 \(x = f^{-1}(y)\)

参数

  • y (Tensor) - 逆变换的输入参数。

返回

  • x (Tensor) - 逆变换的计算结果。

forward_log_det_jacobian(x)

计算正变换雅可比行列式绝对值的对数。

如果变换不是一一映射,则雅可比矩阵不存在,返回 NotImplementedError .

参数

  • x (Tensor) - 输入参数。

返回

  • Tensor - 正变换雅可比行列式绝对值的对数。

inverse_log_det_jacobian(y)

计算逆变换雅可比行列式绝对值的对数。

forward_log_det_jacobian 互为负数。

参数

  • y (Tensor) - 输入参数。

返回

  • Tensor - 逆变换雅可比行列式绝对值的对数。

forward_shape(shape)

推断正变换输出形状。

参数

  • shape (Sequence[int]) - 正变换输入的形状。

返回

  • Sequence[int] - 正变换输出的形状。

inverse_shape(shape)

推断逆变换输出形状。

参数

  • shape (Sequence[int]) - 逆变换输入的形状。

返回

  • Sequence[int] - 逆变换输出的形状。