
    Αi                       S SK Jr  S SKrS SKrS SKrS SKJrJrJr  S SKrS SK	J
s  Jr  S SKJrJrJrJr  \(       a  S SKJr  S SKJr  S SKJrJr  / SQr " S	 S
\R0                  5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S\5      r# " S S \5      r$ " S! S"\5      r% " S# S$\5      r&g)%    )annotationsN)TYPE_CHECKINGAnyoverload)
constraintdistributiontransformed_distributionvariable)Sequence)Tensor)DistributionTransformedDistribution)	TransformAbsTransformAffineTransformChainTransformExpTransformIndependentTransformPowerTransformReshapeTransformSigmoidTransformSoftmaxTransformStackTransformStickBreakingTransformTanhTransformc                  8    \ rS rSrSrSrSrSrSr\	S 5       r
Srg	)
Type9   z!Mapping type of a transformation.	bijection	injection
surjectionotherc                6    XR                   U R                  4;   $ )z3Both bijection and injection are injective mapping.)	BIJECTION	INJECTION)cls_types     ]/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/transform.pyis_injectiveType.is_injectiveA   s     666     N)__name__
__module____qualname____firstlineno____doc__r$   r%   
SURJECTIONOTHERclassmethodr)   __static_attributes__r,   r+   r(   r   r   9   s+    +IIJE7 7r+   r   c                  >  ^  \ rS rSrSr\R                  rSU 4S jjr\	S 5       r
\SS j5       r\SS j5       r\SS j5       rSS jrSS	 jrSS
 jrSS jrSS jrSS jrSS jr\S S j5       r\S S j5       rSS jrSS jrSS jrSS jrSS jrSS jrSrU =r$ )!r   G   a[  Base class for the transformations of random variables.

``Transform`` can be used to represent any differentiable and injective
function from the subset of :math:`R^n` to subset of :math:`R^m`, generally
used for transforming a random sample generated by ``Distribution``
instance.

Suppose :math:`X` is a K-dimensional random variable with probability
density function :math:`p_X(x)`. A new random variable :math:`Y = f(X)` may
be defined by transforming :math:`X` with a suitably well-behaved function
:math:`f`. It suffices for what follows to note that if `f` is one-to-one and
its inverse :math:`f^{-1}` have a well-defined Jacobian, then the density of
:math:`Y` is

.. math::

    p_Y(y) = p_X(f^{-1}(y)) |det J_{f^{-1}}(y)|

where det is the matrix determinant operation and :math:`J_{f^{-1}}(y)` is
the Jacobian matrix of :math:`f^{-1}` evaluated at :math:`y`.
Taking :math:`x = f^{-1}(y)`, the Jacobian matrix is defined by

.. math::

    J(y) = \begin{bmatrix}
    {\frac{\partial x_1}{\partial y_1}} &{\frac{\partial x_1}{\partial y_2}}
    &{\cdots} &{\frac{\partial x_1}{\partial y_K}} \\
    {\frac{\partial x_2}{\partial y_1}}  &{\frac{\partial x_2}
    {\partial y_2}}&{\cdots} &{\frac{\partial x_2}{\partial y_K}} \\
    {\vdots} &{\vdots} &{\ddots} &{\vdots}\\
    {\frac{\partial x_K}{\partial y_1}} &{\frac{\partial x_K}{\partial y_2}}
    &{\cdots} &{\frac{\partial x_K}{\partial y_K}}
    \end{bmatrix}

A ``Transform`` can be characterized by three operations:

    #. forward
       Forward implements :math:`x \rightarrow f(x)`, and is used to convert
       one random outcome into another.
    #. inverse
       Undoes the transformation :math:`y \rightarrow f^{-1}(y)`.
    #. log_det_jacobian
       The log of the absolute value of the determinant of the matrix of all
       first-order partial derivatives of the inverse function.

Subclass typically implement follow methods:

    * _forward
    * _inverse
    * _forward_log_det_jacobian
    * _inverse_log_det_jacobian (optional)

If the transform changes the shape of the input, you must also implemented:

    * _forward_shape
    * _inverse_shape

c                "   > [         TU ]  5         g Nsuper__init__self	__class__s    r(   r<   Transform.__init__       r+   c                @    [         R                  U R                  5      $ )zIs the transformation type one-to-one or not.

Returns:
    bool: ``True`` denotes injective. ``False`` denotes non-injective.
)r   r)   r'   )r&   s    r(   _is_injectiveTransform._is_injective   s       ++r+   c                    g r9   r,   r>   inputs     r(   __call__Transform.__call__   s    14r+   c                    g r9   r,   rF   s     r(   rH   rI      s    HKr+   c                    g r9   r,   rF   s     r(   rH   rI      s    <?r+   c                    [        U[        R                  5      (       a  [        R                  " X/5      $ [        U[
        5      (       a  [        X/5      $ U R                  U5      $ )a	  Make this instance as a callable object. The return value is
depending on the input type.

* If the input is a ``Tensor`` instance, return
  ``self.forward(input)`` .
* If the input is a ``Distribution`` instance, return
  ``TransformedDistribution(base=input, transforms=[self])`` .
* If the input is a ``Transform`` instance, return
  ``ChainTransform([self, input])`` .

Args:
    input (Tensor|Distribution|Transform): The input value.

Returns:
    [Tensor|TransformedDistribution|ChainTransform]: The return value.
)
isinstancer   r   r	   r   r   r   forwardrF   s     r(   rH   rI      sZ    " e\6677+CCv  eY''!4-00||E""r+   c                   [        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       S35      eUR                  5       U R                  R                  :  a3  [        SUR                  5        SU R                  R                   35      eU R                  U5      $ )a  Forward transformation with mapping :math:`y = f(x)`.

Useful for turning one random outcome into another.

Args:
    x (Tensor): Input parameter, generally is a sample generated
        from ``Distribution``.

Returns:
    Tensor: Outcome of forward transformation.
z*Expected 'x' is a Tensor or Real, but got .The dimensions of x($) should be grater than or equal to )rM   paddlebase	frameworkVariablepirValue	TypeErrortypedim_domain
event_rank
ValueError_forwardr>   xs     r(   rN   Transform.forward   s     %%..

0@0@A
 
 <T!WIQG  557T\\,,,&quuwi 0++/<<+B+B*CE  }}Qr+   c                   [        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       S35      eUR                  5       U R                  R                  :  a3  [        SUR                  5        SU R                  R                   35      eU R                  U5      $ )a  Inverse transformation :math:`x = f^{-1}(y)`. It's useful for "reversing"
a transformation to compute one probability in terms of another.

Args:
    y (Tensor): Input parameter for inverse transformation.

Returns:
    Tensor: Outcome of inverse transform.
*Expected 'y' is a Tensor or Real, but got rP   The dimensions of y(rR   )rM   rS   rT   rU   rV   rW   rX   rY   rZ   r[   	_codomainr]   r^   _inverser>   ys     r(   inverseTransform.inverse   s     %%..

0@0@A
 
 <T!WIQG  557T^^...&quuwi 0++/>>+D+D*EG  }}Qr+   c                ~   [        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       S35      e[        U[        R                  R                  R                  [        R
                  R                  45      (       a[  UR                  5       U R                  R                  :  a3  [        SUR                  5        SU R                  R                   35      eU R                  5       (       d  [        S5      eU R                  U5      $ )a/  The log of the absolute value of the determinant of the matrix of all
first-order partial derivatives of the inverse function.

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

Returns:
    Tensor: The log of the absolute value of Jacobian determinant.
rd   rP   rQ   rR   zJforward_log_det_jacobian can't be implemented for non-injectivetransforms.)rM   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   rC   NotImplementedError_call_forward_log_det_jacobianr`   s     r(   forward_log_det_jacobian"Transform.forward_log_det_jacobian   s    %%..

0@0@A
 
 <T!WIQG  q6;;00996::;K;KLMM$,,111&quuwi 0++/<<+B+B*CE  !!##% 
 22155r+   c                   [        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       S35      eUR                  5       U R                  R                  :  a3  [        SUR                  5        SU R                  R                   35      eU R                  U5      $ )a1  Compute :math:`log|det J_{f^{-1}}(y)|`.
Note that ``forward_log_det_jacobian`` is the negative of this function,
evaluated at :math:`f^{-1}(y)`.

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

Returns:
    Tensor: The value of :math:`log|det J_{f^{-1}}(y)|`.
z"Expected 'y' is a Tensor, but got rP   re   rR   )rM   rS   rT   rU   rV   rW   rX   rY   rZ   r[   rf   r]   r^   _call_inverse_log_det_jacobianrh   s     r(   inverse_log_det_jacobian"Transform.inverse_log_det_jacobian  s     %%..

0@0@A
 
 @a	KLL557T^^...&quuwi 0++/>>+D+D*EG  22155r+   c                    [        U[        R                  5      (       d  [        S[	        U5       S35      eU R                  U5      $ )zInfer the shape of forward transformation.

Args:
    shape (Sequence[int]): The input shape.

Returns:
    Sequence[int]: The output shape.
.Expected shape is Sequence[int] type, but got rP   )rM   typingr   rY   rZ   _forward_shaper>   shapes     r(   forward_shapeTransform.forward_shape  E     %11@eQO  ""5))r+   c                    [        U[        R                  5      (       d  [        S[	        U5       S35      eU R                  U5      $ )zInfer the shape of inverse transformation.

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

Returns:
    Sequence[int]: The output shape of inverse transformation.
rv   rP   )rM   rw   r   rY   rZ   _inverse_shapery   s     r(   inverse_shapeTransform.inverse_shape*  r}   r+   c                "    [         R                  $ )z!The domain of this transformationr
   realr>   s    r(   r\   Transform._domain9       }}r+   c                "    [         R                  $ )z#The codomain of this transformationr   r   s    r(   rf   Transform._codomain>  r   r+   c                    [        S5      e)zvInner method for public API ``forward``, subclass should
overwrite this method for supporting forward transformation.
zForward not implementedrm   r`   s     r(   r_   Transform._forwardC       "";<<r+   c                    [        S5      e)zuInner method of public API ``inverse``, subclass should
overwrite this method for supporting inverse transformation.
zInverse not implementedr   rh   s     r(   rg   Transform._inverseI  r   r+   c                    [        U S5      (       a  U R                  U5      $ [        U S5      (       a!  U R                  U R                  U5      5      * $ [	        S5      e)z4Inner method called by ``forward_log_det_jacobian``._forward_log_det_jacobian_inverse_log_det_jacobianzgNeither _forward_log_det_jacobian nor _inverse_log_det_jacobianis implemented. One of them is required.)hasattrr   r   rN   rm   r`   s     r(   rn   (Transform._call_forward_log_det_jacobianO  s]    445511!444455224<<?CCC!7
 	
r+   c                    [        U S5      (       a  U R                  U5      $ [        U S5      (       a!  U R                  U R                  U5      5      * $ [	        S5      e)z3Inner method called by ``inverse_log_det_jacobian``r   r   zgNeither _forward_log_det_jacobian nor _inverse_log_det_jacobian is implemented. One of them is required)r   r   r   rg   rm   rh   s     r(   rr   (Transform._call_inverse_log_det_jacobianZ  s^    445511!444455224==3CDDD!6
 	
r+   c                    U$ )zInner method called by ``forward_shape``, which is used to infer the
forward shape. Subclass should overwrite this method for supporting
``forward_shape``.
r,   ry   s     r(   rx   Transform._forward_shapee  	    
 r+   c                    U$ )zInner method called by ``inverse_shape``, which is used to infer the
inverse shape. Subclass should overwrite this method for supporting
``inverse_shape``.
r,   ry   s     r(   r   Transform._inverse_shapel  r   r+   r,   returnNone)rG   r   r   r   )rG   r   r   r   )rG   r   r   r   )r   r   ra   r   r   r   ri   r   r   r   rz   Sequence[int]r   r   r   zvariable.Variable)r-   r.   r/   r0   r1   r   r%   r'   r<   r4   rC   r   rH   rN   rj   ro   rs   r{   r   propertyr\   rf   r_   rg   rn   rr   rx   r   r5   __classcell__r?   s   @r(   r   r   G   s    9v NNE , , 4 4K K? ?#2 2 .6B6.**    ==	
	
 r+   r   c                  v    \ rS rSrSr\R                  rS
S jrSS jr	SS jr
\SS j5       r\SS j5       rSrg	)r   it  a  Absolute transformation with formula :math:`y = f(x) = abs(x)`,
element-wise.

This non-injective transformation allows for transformations of scalar
distributions with the absolute value function, which maps ``(-inf, inf)``
to ``[0, inf)`` .

* For ``y`` in ``(0, inf)`` , ``AbsTransform.inverse(y)`` returns the set inverse
  ``{x  in (-inf, inf) : |x| = y}`` as a tuple, ``-y, y`` .
* For ``y`` equal ``0`` , ``AbsTransform.inverse(0)`` returns ``0, 0``, which is not
  the set inverse (the set inverse is the singleton {0}), but "works" in
  conjunction with ``TransformedDistribution`` to produce a left
  semi-continuous pdf.
* For ``y`` in ``(-inf, 0)`` , ``AbsTransform.inverse(y)`` returns the
  wrong thing ``-y, y``. This is done for efficiency.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> abs = paddle.distribution.AbsTransform()

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

        >>> print(abs.inverse(paddle.to_tensor([1.])))
        (Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [-1.]), Tensor(shape=[1], dtype=float32, place=Place(cpu), 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(cpu), stop_gradient=True,
                0.), Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                0.))

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

c                "    UR                  5       $ r9   )absr`   s     r(   r_   AbsTransform._forward      uuwr+   c                    U* U4$ r9   r,   rh   s     r(   rg   AbsTransform._inverse  s    r1ur+   c                F    [         R                  " / UR                  S9nX"4$ N)dtype)rS   zerosr   )r>   ri   zeros      r(   r   &AbsTransform._inverse_log_det_jacobian  s    ||Bagg.zr+   c                "    [         R                  $ r9   r   r   s    r(   r\   AbsTransform._domain      }}r+   c                "    [         R                  $ r9   r
   positiver   s    r(   rf   AbsTransform._codomain         r+   r,   Nr   )ri   r   r   ztuple[Tensor, Tensor]r   zvariable.Realr   zvariable.Positive)r-   r.   r/   r0   r1   r   r2   r'   r_   rg   r   r   r\   rf   r5   r,   r+   r(   r   r   t  sJ    1f OOE   ! !r+   r   c                     ^  \ rS rSrSr\R                  rSU 4S jjr\	SS j5       r
\	SS j5       rSS jrSS jrSS jrSS	 jrSS
 jr\	SS j5       r\	SS j5       rSrU =r$ )r   i  aE  Affine transformation with mapping
:math:`y = \text{loc} + \text{scale} \times x`.

Args:
    loc (Tensor): The location parameter.
    scale (Tensor): The scale parameter.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([1., 2.])
        >>> affine = paddle.distribution.AffineTransform(paddle.to_tensor(0.), paddle.to_tensor(1.))

        >>> print(affine.forward(x))
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1., 2.])
        >>> print(affine.inverse(affine.forward(x)))
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1., 2.])
        >>> print(affine.forward_log_det_jacobian(x))
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                0.)
c                  > [        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       35      e[        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       35      eXl	        X l
        [        TU ]1  5         g )Nz$Expected 'loc' is a Tensor, but got z$Expected scale is a Tensor, but got )rM   rS   rT   rU   rV   rW   rX   rY   rZ   _loc_scaler;   r<   )r>   locscaler?   s      r(   r<   AffineTransform.__init__  s    &++''00&**2B2BC
 
 B49+NOOFKK))22FJJ4D4DE
 
 6tE{mD  	r+   c                    U R                   $ r9   )r   r   s    r(   r   AffineTransform.loc  s    yyr+   c                    U R                   $ r9   )r   r   s    r(   r   AffineTransform.scale      {{r+   c                :    U R                   U R                  U-  -   $ r9   r   r   r`   s     r(   r_   AffineTransform._forward  s    yy4;;?**r+   c                8    XR                   -
  U R                  -  $ r9   r   rh   s     r(   rg   AffineTransform._inverse  s    II,,r+   c                ^    [         R                  " U R                  5      R                  5       $ r9   )rS   r   r   logr`   s     r(   r   )AffineTransform._forward_log_det_jacobian  s    zz$++&**,,r+   c                    [        [        R                  " [        R                  " XR                  R                  5      U R
                  R                  5      5      $ r9   tuplerS   broadcast_shaper   rz   r   ry   s     r(   rx   AffineTransform._forward_shape  @    ""&&uiioo>!!
 	
r+   c                    [        [        R                  " [        R                  " XR                  R                  5      U R
                  R                  5      5      $ r9   r   ry   s     r(   r   AffineTransform._inverse_shape  r   r+   c                "    [         R                  $ r9   r   r   s    r(   r\   AffineTransform._domain  r   r+   c                "    [         R                  $ r9   r   r   s    r(   rf   AffineTransform._codomain  r   r+   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r-   r.   r/   r0   r1   r   r$   r'   r<   r   r   r   r_   rg   r   rx   r   r\   rf   r5   r   r   s   @r(   r   r     s    6 NNE    +--

    r+   r   c                     ^  \ rS rSrSrSU 4S jjrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jr\SS j5       r\SS j5       rSrU =r$ )r   i  a  Composes multiple transforms in a chain.

Args:
    transforms (Sequence[Transform]): A sequence of transformations.

Examples:

    .. code-block:: python

        >>> import paddle


        >>> x = paddle.to_tensor([0., 1., 2., 3.])

        >>> chain = paddle.distribution.ChainTransform((
        ...     paddle.distribution.AffineTransform(
        ...         paddle.to_tensor(0.), paddle.to_tensor(1.)),
        ...     paddle.distribution.ExpTransform()
        >>> ))
        >>> print(chain.forward(x))
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1.         , 2.71828175 , 7.38905621 , 20.08553696])
        >>> print(chain.inverse(chain.forward(x)))
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0., 1., 2., 3.])
        >>> print(chain.forward_log_det_jacobian(x))
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0., 1., 2., 3.])
        >>> print(chain.inverse_log_det_jacobian(chain.forward(x)))
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
                [ 0., -1., -2., -3.])
c                   > [        U[        R                  5      (       d  [        S[	        U5       35      e[        S U 5       5      (       d  [        S5      eXl        [        TU ]!  5         g )Nz3Expected type of 'transforms' is Sequence, but got c              3  B   #    U  H  n[        U[        5      v   M     g 7fr9   rM   r   .0ts     r(   	<genexpr>*ChainTransform.__init__.<locals>.<genexpr>;       @Z:a++Z   z4All elements of transforms should be Transform type.)	rM   rw   r   rY   rZ   all
transformsr;   r<   )r>   r   r?   s     r(   r<   ChainTransform.__init__6  sf    *foo66Ed:FVEWX  @Z@@@F  %r+   c                :    [        S U R                   5       5      $ )Nc              3  @   #    U  H  oR                  5       v   M     g 7fr9   rC   r   s     r(   r   /ChainTransform._is_injective.<locals>.<genexpr>D  s     >o??$$o   )r   r   r   s    r(   rC   ChainTransform._is_injectiveC  s    >doo>>>r+   c                N    U R                    H  nUR                  U5      nM     U$ r9   )r   rN   )r>   ra   	transforms      r(   r_   ChainTransform._forwardF  s%    I!!!$A )r+   c                `    [        U R                  5       H  nUR                  U5      nM     U$ r9   )reversedr   rj   )r>   ri   r   s      r(   rg   ChainTransform._inverseK  s*    !$//2I!!!$A 3r+   c                N   SnU R                   R                  nU R                   H|  nX R                  UR	                  U5      X4R                   R                  -
  5      -  nUR                  U5      nX4R                  R                  UR                   R                  -
  -  nM~     U$ )N        )r\   r]   r   _sum_rightmostro   rN   rf   )r>   ra   valuer]   r   s        r(   r   (ChainTransform._forward_log_det_jacobianP  s    \\,,
A((**1-zII<P<P/P E 		!A++001993G3GGGJ ! r+   c                N    U R                    H  nUR                  U5      nM     U$ r9   )r   r{   r>   rz   r   s      r(   rx   ChainTransform._forward_shape[  %    I++E2E )r+   c                N    U R                    H  nUR                  U5      nM     U$ r9   )r   r   r   s      r(   r   ChainTransform._inverse_shape`  r  r+   c                \    US:  a%  UR                  [        [        U* S5      5      5      $ U$ )zsum value along rightmost n dimr   )sumlistrange)r>   r   ns      r(   r   ChainTransform._sum_rightmoste  s)    01AuyyeQBl+,@5@r+   c                   U R                   S   R                  nU R                   S   R                  R                  n[	        U R                   5       HQ  nX#R                  R                  UR                  R                  -
  -  n[        X#R                  R                  5      nMS     [        R                  " XUR                  -
  5      $ )Nr   )r   r\   rf   r]   r   maxr
   Independent)r>   domainr]   r   s       r(   r\   ChainTransform._domaini  s    #++& __R(22==
$//*A++001993G3GGGJZ)=)=>J + ##F9J9J,JKKr+   c                   U R                   S   R                  nU R                   S   R                  R                  nU R                    HQ  nX#R                  R                  UR                  R                  -
  -  n[	        X#R                  R                  5      nMS     [
        R                  " XUR                  -
  5      $ )Nr  r   )r   rf   r\   r]   r  r
   r  )r>   codomainr]   r   s       r(   rf   ChainTransform._codomain  s    ??2&00__Q'//::
A++001993G3GGGJZ)?)?@J ! ##H8;N;N.NOOr+   )r   )r   Sequence[Transform]r   r   r   boolr   r   )ra   r   r   floatr   )r   r   r  intr   r   r   zvariable.Independent)r-   r.   r/   r0   r1   r<   rC   r_   rg   r   rx   r   r   r   r\   rf   r5   r   r   s   @r(   r   r     s^    B?

	

A L L6 P Pr+   r   c                     ^  \ rS rSrSr\R                  rS
U 4S jjr\	SS j5       r
\	SS j5       rSS jrSS jrSS jrS	rU =r$ )r   i  a  Exponent transformation with mapping :math:`y = \exp(x)`.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> exp = paddle.distribution.ExpTransform()
        >>> print(exp.forward(paddle.to_tensor([1., 2., 3.])))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [2.71828175 , 7.38905621 , 20.08553696])

        >>> print(exp.inverse(paddle.to_tensor([1., 2., 3.])))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.        , 0.69314718, 1.09861231])

        >>> print(exp.forward_log_det_jacobian(paddle.to_tensor([1., 2., 3.])))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1., 2., 3.])

        >>> print(exp.inverse_log_det_jacobian(paddle.to_tensor([1., 2., 3.])))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [ 0.        , -0.69314718, -1.09861231])
c                "   > [         TU ]  5         g r9   r:   r=   s    r(   r<   ExpTransform.__init__  rA   r+   c                "    [         R                  $ r9   r   r   s    r(   r\   ExpTransform._domain  r   r+   c                "    [         R                  $ r9   r   r   s    r(   rf   ExpTransform._codomain  r   r+   c                "    UR                  5       $ r9   )expr`   s     r(   r_   ExpTransform._forward  r   r+   c                "    UR                  5       $ r9   r   rh   s     r(   rg   ExpTransform._inverse  r   r+   c                    U$ r9   r,   r`   s     r(   r   &ExpTransform._forward_log_det_jacobian  s    r+   r,   r   r   r   r   r   )r-   r.   r/   r0   r1   r   r$   r'   r<   r   r\   rf   r_   rg   r   r5   r   r   s   @r(   r   r     sT    4 NNE   ! ! r+   r   c                     ^  \ rS rSrSrSU 4S jjrSS jrSS jrSS jrSS jr	SS jr
SS	 jr\SS
 j5       r\SS j5       rSrU =r$ )r   i  a  
``IndependentTransform`` wraps a base transformation, reinterprets
some of the rightmost batch axes as event axes.

Generally, it is used to expand the event axes. This has no effect on the
forward or inverse transformation, but does sum out the
``reinterpreted_batch_rank`` rightmost dimensions in computing the determinant
of Jacobian matrix.

To see this, consider the ``ExpTransform`` applied to a Tensor which has
sample, batch, and event ``(S,B,E)`` shape semantics. Suppose the Tensor's
partitioned-shape is ``(S=[4], B=[2, 2], E=[3])`` , reinterpreted_batch_rank
is 1. Then the reinterpreted Tensor's shape  is ``(S=[4], B=[2], E=[2, 3])`` .
The shape returned by ``forward`` and ``inverse`` is unchanged, ie,
``[4,2,2,3]`` . However the shape returned by ``inverse_log_det_jacobian``
is ``[4,2]``, because the Jacobian determinant is a reduction over the
event dimensions.

Args:
    base (Transform): The base transformation.
    reinterpreted_batch_rank (int): The num of rightmost batch rank that
        will be reinterpreted as event rank.

Examples:

    .. code-block:: python

        >>> import paddle

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

        >>> # Exponential transform with event_rank = 1
        >>> multi_exp = paddle.distribution.IndependentTransform(
        ...     paddle.distribution.ExpTransform(), 1)
        >>> print(multi_exp.forward(x))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[2.71828175  , 7.38905621  , 20.08553696 ],
                 [54.59814835 , 148.41316223, 403.42880249]])
        >>> print(multi_exp.forward_log_det_jacobian(x))
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [6. , 15.])
c                   > [        U[        5      (       d  [        S[        U5       35      eUS::  a  [	        SU 35      eXl        X l        [        TU ]!  5         g )Nz+Expected 'base' is Transform type, but get r   zAExpected 'reinterpreted_batch_rank' is grater than zero, but got )	rM   r   rY   rZ   r^   _base_reinterpreted_batch_rankr;   r<   )r>   rT   reinterpreted_batch_rankr?   s      r(   r<   IndependentTransform.__init__  sf    $	**=d4j\J  $q(STlSmn  
)A&r+   c                6    U R                   R                  5       $ r9   )r*  rC   r   s    r(   rC   "IndependentTransform._is_injective  s    zz''))r+   c                    UR                  5       U R                  R                  :  a  [        S5      eU R                  R                  U5      $ Nz/Input dimensions is less than event dimensions.)r[   r\   r]   r^   r*  rN   r`   s     r(   r_   IndependentTransform._forward   s;    557T\\,,,NOOzz!!!$$r+   c                    UR                  5       U R                  R                  :  a  [        S5      eU R                  R                  U5      $ r1  )r[   rf   r]   r^   r*  rj   rh   s     r(   rg   IndependentTransform._inverse  s;    557T^^...NOOzz!!!$$r+   c                    U R                   R                  U5      R                  [        [	        U R
                  * S5      5      5      $ )Nr   )r*  ro   r  r  r  r+  r`   s     r(   r   .IndependentTransform._forward_log_det_jacobian
  s<    zz221599666:;
 	
r+   c                8    U R                   R                  U5      $ r9   )r*  r{   ry   s     r(   rx   #IndependentTransform._forward_shape      zz''..r+   c                8    U R                   R                  U5      $ r9   )r*  r   ry   s     r(   r   #IndependentTransform._inverse_shape  r9  r+   c                l    [         R                  " U R                  R                  U R                  5      $ r9   )r
   r  r*  r\   r+  r   s    r(   r\   IndependentTransform._domain  s*    ##JJ > >
 	
r+   c                l    [         R                  " U R                  R                  U R                  5      $ r9   )r
   r  r*  rf   r+  r   s    r(   rf   IndependentTransform._codomain  s*    ##JJ  $"@"@
 	
r+   )r*  r+  )rT   r   r,  r  r   r   r  r   r   r   r  )r-   r.   r/   r0   r1   r<   rC   r_   rg   r   rx   r   r   r\   rf   r5   r   r   s   @r(   r   r     sT    )V*%
%


// 
 

 
 
r+   r   c                     ^  \ rS rSrSr\R                  rSU 4S jjr\	SS j5       r
\	SS j5       r\	SS j5       rSS jrSS jrSS	 jrSS
 jrSS jrSrU =r$ )r   i"  a  
Power transformation with mapping :math:`y = x^{\text{power}}`.

Args:
    power (Tensor): The power parameter.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([1., 2.])
        >>> power = paddle.distribution.PowerTransform(paddle.to_tensor(2.))

        >>> print(power.forward(x))
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1., 4.])
        >>> print(power.inverse(power.forward(x)))
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1., 2.])
        >>> print(power.forward_log_det_jacobian(x))
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.69314718, 1.38629436])
c                   > [        U[        R                  R                  R                  [        R
                  R                  45      (       d  [        S[        U5       35      eXl	        [        TU ]-  5         g )Nz&Expected 'power' is a tensor, but got )rM   rS   rT   rU   rV   rW   rX   rY   rZ   _powerr;   r<   )r>   powerr?   s     r(   r<   PowerTransform.__init__?  sc    FKK))22FJJ4D4DE
 
 8eF  r+   c                    U R                   $ r9   rB  r   s    r(   rC  PowerTransform.powerI  r   r+   c                "    [         R                  $ r9   r   r   s    r(   r\   PowerTransform._domainM  r   r+   c                "    [         R                  $ r9   r   r   s    r(   rf   PowerTransform._codomainQ  r   r+   c                8    UR                  U R                  5      $ r9   powrB  r`   s     r(   r_   PowerTransform._forwardU  s    uuT[[!!r+   c                >    UR                  SU R                  -  5      $ N   rM  rh   s     r(   rg   PowerTransform._inverseX  s    uuQ_%%r+   c                    U R                   UR                  U R                   S-
  5      -  R                  5       R                  5       $ rQ  )rB  rN  r   r   r`   s     r(   r   (PowerTransform._forward_log_det_jacobian[  s4    aeeDKK!O4499;??AAr+   c                h    [        [        R                  " XR                  R                  5      5      $ r9   r   rS   r   rB  rz   ry   s     r(   rx   PowerTransform._forward_shape^  "    V++E;;3D3DEFFr+   c                h    [        [        R                  " XR                  R                  5      5      $ r9   rW  ry   s     r(   r   PowerTransform._inverse_shapea  rY  r+   rF  )rC  r   r   r   r   r   r   r   r   r   )r-   r.   r/   r0   r1   r   r$   r'   r<   r   rC  r\   rf   r_   rg   r   rx   r   r5   r   r   s   @r(   r   r   "  sv    4 NNE     ! !"&BGG Gr+   r   c                     ^  \ rS rSrSr\R                  r      SU 4S jjr\	SS j5       r
\	SS j5       r\	SS j5       r\	SS j5       rSS jrSS	 jrSS
 jrSS jrSS jrSrU =r$ )r   ie  ag  Reshape the event shape of a tensor.

Note that ``in_event_shape`` and ``out_event_shape`` must have the same
number of elements.

Args:
    in_event_shape(Sequence[int]): The input event shape.
    out_event_shape(Sequence[int]): The output event shape.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.ones((1,2,3))
        >>> reshape_transform = paddle.distribution.ReshapeTransform((2, 3), (3, 2))
        >>> print(reshape_transform.forward_shape((1,2,3)))
        (1, 3, 2)
        >>> print(reshape_transform.forward(x))
        Tensor(shape=[1, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[[1., 1.],
                [1., 1.],
                [1., 1.]]])
        >>> print(reshape_transform.inverse(reshape_transform.forward(x)))
        Tensor(shape=[1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[[1., 1., 1.],
                    [1., 1., 1.]]])
        >>> print(reshape_transform.forward_log_det_jacobian(x))
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.])
c                h  > [        U[        R                  5      (       a  [        U[        R                  5      (       d  [        SU SU 35      eSnU H  nX4-  nM	     SnU H  nXT-  nM	     X5:w  a  [	        SU SU 35      e[        U5      U l        [        U5      U l        [        TU ]%  5         g )NzdExpected type of 'in_event_shape' and 'out_event_shape' is Sequence[int], but got 'in_event_shape': z, 'out_event_shape': rR  zCThe numel of 'in_event_shape' should be 'out_event_shape', but got z!=)
rM   rw   r   rY   r^   r   _in_event_shape_out_event_shaper;   r<   )r>   in_event_shapeout_event_shapein_sizeeout_sizer?   s         r(   r<   ReshapeTransform.__init__  s     .&//::*V__C
 C
 <<J;K L&&5%68 
 ALG   AMH !")2hZ1 
  %^4 %o 6r+   c                    U R                   $ r9   )r^  r   s    r(   r`  ReshapeTransform.in_event_shape  s    ###r+   c                    U R                   $ r9   )r_  r   s    r(   ra   ReshapeTransform.out_event_shape  s    $$$r+   c                r    [         R                  " [         R                  [        U R                  5      5      $ r9   )r
   r  r   lenr^  r   s    r(   r\   ReshapeTransform._domain  s$    ##HMM3t7K7K3LMMr+   c                r    [         R                  " [         R                  [        U R                  5      5      $ r9   )r
   r  r   rk  r_  r   s    r(   rf   ReshapeTransform._codomain  s$    ##HMM3t7L7L3MNNr+   c                    UR                  [        UR                  5      S UR                  5       [	        U R
                  5      -
   U R                  -   5      $ r9   )reshaper   rz   r[   rk  r^  r_  r`   s     r(   r_   ReshapeTransform._forward  sJ    yy!''N@QUUWs4+?+?'@@A##$
 	
r+   c                    UR                  [        UR                  5      S UR                  5       [	        U R
                  5      -
   U R                  -   5      $ r9   )rp  r   rz   r[   rk  r_  r^  rh   s     r(   rg   ReshapeTransform._inverse  sJ    yy!''NAQUUWs4+@+@'AAB""#
 	
r+   c           	        [        U5      [        U R                  5      :  a-  [        S[        U R                  5       S[        U5       35      e[        U[        U R                  5      * S  5      [        U R                  5      :w  a2  [        SU R                   SU[        U R                  5      * S   35      e[        US [        U R                  5      *  5      U R                  -   $ )Nz,Expected length of 'shape' is not less than 
, but got  Event shape mismatch, expected: )rk  r^  r^   r   r_  ry   s     r(   rx   ReshapeTransform._forward_shape  s    u:D0011>s4CWCW?X>YYcdghmdncop  D0011345  :
 
 243G3G2H
SXZ]^b^r^rZsYsYuSvRwx  %43t3344569N9NN	
r+   c           	        [        U5      [        U R                  5      :  a-  [        S[        U R                  5       S[        U5       35      e[        U[        U R                  5      * S  5      [        U R                  5      :w  a2  [        SU R                   SU[        U R                  5      * S   35      e[        US [        U R                  5      *  5      U R                  -   $ )Nz)Expected 'shape' length is not less than ru  rv  )rk  r_  r^   r   r^  ry   s     r(   r   ReshapeTransform._inverse_shape  s    u:D1122;C@U@U<V;WWabefkblamn  D1122456%!!;
 
 243H3H2ITY[^_c_t_t[uZuZwTxSyz  %53t445567$:N:NN	
r+   c                    UR                   S UR                  5       [        U R                  5      -
   n[        R
                  " X!R                  S9$ r   )rz   r[   rk  r^  rS   r   r   )r>   ra   rz   s      r(   r   *ReshapeTransform._forward_log_det_jacobian  s<    =!%%'C(<(<$==>||E11r+   )r^  r_  )r`  r   ra  r   r   r   )r   ztuple[Sequence[int]]r  r   r   r   )r-   r.   r/   r0   r1   r   r$   r'   r<   r   r`  ra  r\   rf   r_   rg   rx   r   r   r5   r   r   s   @r(   r   r   e  s    B NNE+>K	6 $ $ % % N N O O



2 2r+   r   c                  ^    \ rS rSrSr\S
S j5       r\SS j5       rSS jrSS jr	SS jr
Srg	)r   i  a  Sigmoid transformation with mapping :math:`y = \frac{1}{1 + \exp(-x)}` and :math:`x = \text{logit}(y)`.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.ones((2,3))
        >>> t = paddle.distribution.SigmoidTransform()
        >>> print(t.forward(x))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[0.73105860, 0.73105860, 0.73105860],
                 [0.73105860, 0.73105860, 0.73105860]])
        >>> print(t.inverse(t.forward(x)))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[1.00000012, 1.00000012, 1.00000012],
                 [1.00000012, 1.00000012, 1.00000012]])
        >>> print(t.forward_log_det_jacobian(x))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[-1.62652326, -1.62652326, -1.62652326],
                 [-1.62652326, -1.62652326, -1.62652326]])
c                "    [         R                  $ r9   r   r   s    r(   r\   SigmoidTransform._domain  r   r+   c                \    [         R                  " SS[        R                  " SS5      5      $ )NFr   r         ?r
   rV   r   Ranger   s    r(   rf   SigmoidTransform._codomain   s$      :+;+;C+EFFr+   c                .    [         R                  " U5      $ r9   )Fsigmoidr`   s     r(   r_   SigmoidTransform._forward  s    yy|r+   c                F    UR                  5       U* R                  5       -
  $ r9   )r   log1prh   s     r(   rg   SigmoidTransform._inverse  s    uuw1"%%r+   c                `    [         R                  " U* 5      * [         R                  " U5      -
  $ r9   )r  softplusr`   s     r(   r   *SigmoidTransform._forward_log_det_jacobian
  s!    

A2A..r+   r,   Nr   r   r   r   )r-   r.   r/   r0   r1   r   r\   rf   r_   rg   r   r5   r,   r+   r(   r   r     s@    0   G G&/r+   r   c                      \ rS rSrSr\R                  r\SS j5       r	\SS j5       r
SS jrSS jrSS jrSS jrS	rg
)r   i  am  Softmax transformation with mapping :math:`y=\exp(x)` then normalizing.

It's generally used to convert unconstrained space to simplex. This mapping
is not injective, so ``forward_log_det_jacobian`` and
``inverse_log_det_jacobian`` are not implemented.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.ones((2,3))
        >>> t = paddle.distribution.SoftmaxTransform()
        >>> print(t.forward(x))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[0.33333334, 0.33333334, 0.33333334],
                 [0.33333334, 0.33333334, 0.33333334]])
        >>> print(t.inverse(t.forward(x)))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[-1.09861231, -1.09861231, -1.09861231],
                 [-1.09861231, -1.09861231, -1.09861231]])
c                L    [         R                  " [         R                  S5      $ rQ  r
   r  r   r   s    r(   r\   SoftmaxTransform._domain)      ##HMM155r+   c                N    [         R                  " SS[        R                  5      $ NFrR  r
   rV   r   simplexr   s    r(   rf   SoftmaxTransform._codomain-        :+=+=>>r+   c                l    XR                  SSS9S   -
  R                  5       nXR                  SSS9-  $ )Nr  T)keepdimr   )r  r!  r  r`   s     r(   r_   SoftmaxTransform._forward1  s;    r4(++00255T5***r+   c                "    UR                  5       $ r9   r$  rh   s     r(   rg   SoftmaxTransform._inverse5  r   r+   c                R    [        U5      S:  a  [        S[        U5       35      eU$ NrR  z3Expected length of shape is grater than 1, but got rk  r^   ry   s     r(   rx   SoftmaxTransform._forward_shape8  /    u:>Ec%j\R  r+   c                R    [        U5      S:  a  [        S[        U5       35      eU$ r  r  ry   s     r(   r   SoftmaxTransform._inverse_shape?  r  r+   r,   Nr  r   r   r   r   )r-   r.   r/   r0   r1   r   r3   r'   r   r\   rf   r_   rg   rx   r   r5   r,   r+   r(   r   r     sL    0 JJE6 6 ? ?+r+   r   c                      \ rS rSrSrSSS jjrSS jr\SS j5       r\SS j5       r	SS jr
SS jrSS	 jrSS
 jr\SS j5       r\SS j5       rSrg)r   iG  aY  ``StackTransform`` applies a sequence of transformations along the
specific axis.

Args:
    transforms (Sequence[Transform]): The sequence of transformations.
    axis (int, optional): The axis along which will be transformed. default
        value is 0.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> x = paddle.stack(
        ...     (paddle.to_tensor([1., 2., 3.]), paddle.to_tensor([1, 2., 3.])), 1)
        >>> t = paddle.distribution.StackTransform(
        ...     (paddle.distribution.ExpTransform(),
        ...     paddle.distribution.PowerTransform(paddle.to_tensor(2.))),
        ...     1
        >>> )
        >>> print(t.forward(x))
        Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[2.71828175 , 1.         ],
                 [7.38905621 , 4.         ],
                 [20.08553696, 9.         ]])

        >>> print(t.inverse(t.forward(x)))
        Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[1., 1.],
                 [2., 2.],
                 [3., 3.]])

        >>> print(t.forward_log_det_jacobian(x))
        Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[1.        , 0.69314718],
                 [2.        , 1.38629436],
                 [3.        , 1.79175949]])
c                6   U(       a  [        U[        R                  5      (       d  [        S[	        U5       S35      e[        S U 5       5      (       d  [        S5      e[        U[        5      (       d  [        S[	        U5       S35      eXl        X l        g )Nz6Expected 'transforms' is Sequence[Transform], but got rP   c              3  B   #    U  H  n[        U[        5      v   M     g 7fr9   r   r   s     r(   r   *StackTransform.__init__.<locals>.<genexpr>u  r   r   z5Expected all element in transforms is Transform Type.zExpected 'axis' is int, but got)	rM   rw   r   rY   rZ   r   r  _transforms_axis)r>   r   axiss      r(   r<   StackTransform.__init__p  s    J!H!HHjIYHZZ[\  @Z@@@G  $$$=d4j\KLL%
r+   c                :    [        S U R                   5       5      $ )Nc              3  @   #    U  H  oR                  5       v   M     g 7fr9   r   r   s     r(   r   /StackTransform._is_injective.<locals>.<genexpr>  s     ?.>??$$.>r   )r   r  r   s    r(   rC   StackTransform._is_injective  s    ?d.>.>???r+   c                    U R                   $ r9   )r  r   s    r(   r   StackTransform.transforms  s    r+   c                    U R                   $ r9   )r  r   s    r(   r  StackTransform.axis  s    zzr+   c           	        U R                  U5        [        R                  " [        [        R                  " XR
                  5      U R                  5       VVs/ s H  u  p#UR                  U5      PM     snnU R
                  5      $ s  snnf r9   )_check_sizerS   stackzipunstackr  r  rN   r>   ra   vr   s       r(   r_   StackTransform._forward  p    ||  q** =t?O?OPPDA 		!P JJ
 	
   B	
c           	        U R                  U5        [        R                  " [        [        R                  " XR
                  5      U R                  5       VVs/ s H  u  p#UR                  U5      PM     snnU R
                  5      $ s  snnf r9   )r  rS   r  r  r  r  r  rj   )r>   ri   r  r   s       r(   rg   StackTransform._inverse  r  r  c           	        U R                  U5        [        R                  " [        [        R                  " XR
                  5      U R                  5       VVs/ s H  u  p#UR                  U5      PM     snnU R
                  5      $ s  snnf r9   )r  rS   r  r  r  r  r  ro   r  s       r(   r   (StackTransform._forward_log_det_jacobian  ss    ||  q** =t?O?OPPDA **1-P JJ
 	
r  c                V   UR                  5       * U R                  s=::  a  UR                  5       :  d,  O  [        SUR                  5        SU R                   S35      eUR                  U R                     [	        U R
                  5      :w  a  [        SU R                   S35      eg )NzInput dimensions z, should be grater than stack transform axis rP   zInput size along z- should be equal to the length of transforms.)r[   r  r^   rz   rk  r  )r>   r  s     r(   r  StackTransform._check_size  s    DJJ00#AEEG9 -""&**Q0  774::#d&6&6"77#DJJ< 0( )  8r+   c                    [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  5      $ s  snf r9   )r
   Stackr  r\   r  r>   r   s     r(   r\   StackTransform._domain  s3    ~~$2B2BC2BQyy2BCTZZPPC   Ac                    [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  5      $ s  snf r9   )r
   r  r  rf   r  r  s     r(   rf   StackTransform._codomain  s8    ~~"&"2"23"2Q[["23TZZ
 	
3r  )r  r  N)r   )r   r  r  r  r  )r   r  )r   r  r   r   )r  r   r   r   )r   zvariable.Stack)r-   r.   r/   r0   r1   r<   rC   r   r   r  r_   rg   r   r  r\   rf   r5   r,   r+   r(   r   r   G  sy    &P@      



 Q Q 
 
r+   r   c                      \ rS rSrSr\R                  rSS jrSS jr	SS jr
SS jrSS jr\SS j5       r\SS	 j5       rS
rg)r   i  a  Convert an unconstrained vector to the simplex with one additional
dimension by the stick-breaking construction.

Examples:

    .. code-block:: python

        >>> import paddle


        >>> x = paddle.to_tensor([1.,2.,3.])
        >>> t = paddle.distribution.StickBreakingTransform()
        >>> print(t.forward(x))
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.47536686, 0.41287899, 0.10645414, 0.00530004])
        >>> print(t.inverse(t.forward(x)))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.99999988, 2.        , 2.99999881])
        >>> print(t.forward_log_det_jacobian(x))
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                -9.10835075)
c                   UR                   S   S-   [        R                  " UR                   S   /5      R                  S5      -
  n[        R
                  " XR                  5       -
  5      nSU-
  R                  S5      n[        R                  " US/S-  [        UR                   5      S-
  -  SS/-   SS9[        R                  " US/S-  [        UR                   5      S-
  -  SS/-   SS9-  $ )Nr  rR  r      )r   )
rz   rS   onescumsumr  r  r   cumprodpadrk  )r>   ra   offsetz	z_cumprods        r(   r_   StickBreakingTransform._forward  s    q6;;}#=#D#DR#HHIIa**,&'UOOB'	uuQa3qww<!#341v=QG!%%sQw#agg,"23q!f<AK
 
 	
r+   c                *   USS S24   nUR                   S   [        R                  " UR                   S   /5      R                  S5      -
  nSUR                  S5      -
  nUR	                  5       UR	                  5       -
  UR	                  5       -   nU$ )N.r  rR  )rz   rS   r  r  r   )r>   ri   y_cropr  sfra   s         r(   rg   StickBreakingTransform._inverse  s~    38v{{FLL,<+=>EEbIIr""JJL2668#fjjl2r+   c                V   U R                  U5      nUR                  S   S-   [        R                  " UR                  S   /5      R	                  S5      -
  nXR                  5       -
  nU* [        R                  " U5      -   USS S24   R                  5       -   R                  S5      $ )Nr  rR  .)	rN   rz   rS   r  r  r   r  log_sigmoidr  )r>   ra   ri   r  s       r(   r   0StickBreakingTransform._forward_log_det_jacobian  s    LLOq6;;}#=#D#DR#HH

Q]]1%%#ss((99>>rBBr+   c                L    U(       d  [        SU 35      e/ US S QUS   S-   P7$ Nz'Expected 'shape' is not empty, but got r  rR  r^   ry   s     r(   rx   %StickBreakingTransform._forward_shape  6    FugNOO+s+U2Y]++r+   c                L    U(       d  [        SU 35      e/ US S QUS   S-
  P7$ r  r  ry   s     r(   r   %StickBreakingTransform._inverse_shape  r  r+   c                L    [         R                  " [         R                  S5      $ rQ  r  r   s    r(   r\   StickBreakingTransform._domain  r  r+   c                N    [         R                  " SS[        R                  5      $ r  r  r   s    r(   rf    StickBreakingTransform._codomain  r  r+   r,   Nr   r   r   r  r   )r-   r.   r/   r0   r1   r   r$   r'   r_   rg   r   rx   r   r   r\   rf   r5   r,   r+   r(   r   r     sT    . NNE
C,
,
 6 6 ? ?r+   r   c                  v    \ rS rSrSr\R                  r\S
S j5       r	\SS j5       r
SS jrSS jrSS jrSrg	)r   i  a  Tanh transformation with mapping :math:`y = \tanh(x)`.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> tanh = paddle.distribution.TanhTransform()

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

        >>> # doctest: +SKIP('random sample')
        >>> print(tanh.forward(x))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.76159418, 0.96402758, 0.99505472],
                [0.99932921, 0.99990916, 0.99998784]])
        >>> print(tanh.inverse(tanh.forward(x)))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1.        , 2.        , 2.99999666],
                [3.99993253, 4.99977016, 6.00527668]])
        >>> print(tanh.forward_log_det_jacobian(x))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), 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(cpu), stop_gradient=True,
                [[0.86756176 , 2.65000558 , 4.61866283 ],
                 [6.61441946 , 8.61399269 , 10.61451530]])
        >>> # doctest: -SKIP
c                "    [         R                  $ r9   r   r   s    r(   r\   TanhTransform._domain$  r   r+   c                \    [         R                  " SS[        R                  " SS5      5      $ )NFr   g      r  r  r   s    r(   rf   TanhTransform._codomain(  s$      :+;+;D#+FGGr+   c                "    UR                  5       $ r9   )tanhr`   s     r(   r_   TanhTransform._forward,  s    vvxr+   c                "    UR                  5       $ r9   )atanhrh   s     r(   rg   TanhTransform._inverse/  s    wwyr+   c                n    S[         R                  " S5      U-
  [        R                  " SU-  5      -
  -  $ )aA  We implicitly rely on _forward_log_det_jacobian rather than
explicitly implement ``_inverse_log_det_jacobian`` since directly using
``-tf.math.log1p(-tf.square(y))`` has lower numerical precision.

See details: https://github.com/tensorflow/probability/blob/master/tensorflow_probability/python/bijectors/tanh.py#L69-L80
g       @g       )mathr   r  r  r`   s     r(   r   'TanhTransform._forward_log_det_jacobian2  s.     dhhsma'!**TAX*>>??r+   r,   Nr   r   r   r   )r-   r.   r/   r0   r1   r   r$   r'   r   r\   rf   r_   rg   r   r5   r,   r+   r(   r   r     sK    @ NNE  H H@r+   r   )'
__future__r   enumr  rw   r   r   r   rS   paddle.nn.functionalnn
functionalr  paddle.distributionr   r   r	   r
   collections.abcr   r   r   r   __all__Enumr   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r+   r(   <module>r      s   #           (I"7499 7j jZ	F!9 F!RTi TnzPY zPz/9 /d\
9 \
~@GY @GF{2y {2|(/y (/V6y 6ru
Y u
p??Y ??D8@I 8@r+   