
    zj                    4   d dl mZ d dlZd dlZd dlZd dlmZmZmZ d dlZd dl	m
c mZ d dlmZmZmZmZ erd dlmZ d dlmZ d dlmZmZ g dZ G d	 d
ej                  Z G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z# G d d e          Z$ G d! d"e          Z% G d# d$e          Z&dS )%    )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    e Zd ZdZdZdZdZdZed             Z	dS )Typez!Mapping type of a transformation.	bijection	injection
surjectionotherc                "    || j         | j        fv S )z3Both bijection and injection are injective mapping.)	BIJECTION	INJECTION)cls_types     m/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/transform.pyis_injectivezType.is_injectiveA   s     666    N)
__name__
__module____qualname____doc__r#   r$   
SURJECTIONOTHERclassmethodr(    r)   r'   r   r   9   sG        ++IIJE7 7 [7 7 7r)   r   c                  (    e Zd ZdZej        Zd# fdZed             Z	e
d$d            Ze
d%d            Ze
d&d            Zd'dZd(dZd)dZd(dZd)dZd*dZd*dZed+d            Zed+d            Zd(dZd)dZd(dZd)d Zd*d!Zd*d"Z xZS ),r   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

    returnNonec                H    t                                                       d S Nsuper__init__self	__class__s    r'   r9   zTransform.__init__       r)   c                @    t                               | j                  S )zIs the transformation type one-to-one or not.

        Returns:
            bool: ``True`` denotes injective. ``False`` denotes non-injective.
        )r   r(   r&   )r%   s    r'   _is_injectivezTransform._is_injective   s       +++r)   inputr   c                    d S r6   r1   r;   r@   s     r'   __call__zTransform.__call__   s    14r)   r   r   c                    d S r6   r1   rB   s     r'   rC   zTransform.__call__   s    HKr)   r   c                    d S r6   r1   rB   s     r'   rC   zTransform.__call__   s    <?Cr)   r   c                    t          |t          j                  rt          j        || g          S t          |t
                    rt          | |g          S |                     |          S )ai  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   forwardrB   s     r'   rC   zTransform.__call__   sm    " e\677 	+Cv   eY'' 	1!4-000||E"""r)   xc                |   t          |t          j        j        j        t          j        j        f          s t          dt          |           d          |	                                | j
        j        k     r1t          d|	                                 d| j
        j                   |                     |          S )aC  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 )rG   paddlebase	frameworkVariablepirValue	TypeErrortypedim_domain
event_rank
ValueError_forwardr;   rI   s     r'   rH   zTransform.forward   s     %.
0@A
 
 	 GT!WWGGG   5577T\,,,Equuww E E+/<+BE E   }}Qr)   yc                |   t          |t          j        j        j        t          j        j        f          s t          dt          |           d          |	                                | j
        j        k     r1t          d|	                                 d| j
        j                   |                     |          S )a2  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 rK   The dimensions of y(rM   )rG   rN   rO   rP   rQ   rR   rS   rT   rU   rV   	_codomainrX   rY   _inverser;   r\   s     r'   inversezTransform.inverse   s     %.
0@A
 
 	 GT!WWGGG   5577T^...Gquuww G G+/>+DG G   }}Qr)   c                ,   t          |t          j        j        j        t          j        j        f          s t          dt          |           d          t          |t          j        j        j        t          j        j        f          rS|	                                | j
        j        k     r1t          d|	                                 d| j
        j                   |                                 st          d          |                     |          S )ag  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.
        r^   rK   rL   rM   zJforward_log_det_jacobian can't be implemented for non-injectivetransforms.)rG   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r?   NotImplementedError_call_forward_log_det_jacobianr[   s     r'   forward_log_det_jacobianz"Transform.forward_log_det_jacobian   s    %.
0@A
 
 	 GT!WWGGG   q6;096:;KLMM	$,111Equuww E E+/<+BE E   !!## 	%  
 221555r)   c                |   t          |t          j        j        j        t          j        j        f          s t          dt          |           d          |	                                | j
        j        k     r1t          d|	                                 d| j
        j                   |                     |          S )aq  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 rK   r_   rM   )rG   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r`   rX   rY   _call_inverse_log_det_jacobianrb   s     r'   inverse_log_det_jacobianz"Transform.inverse_log_det_jacobian  s     %.
0@A
 
 	M KaKKKLLL5577T^...Gquuww G G+/>+DG G   221555r)   shapeSequence[int]c                    t          |t          j                  s t          dt	          |           d          |                     |          S )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 rK   )rG   typingr   rT   rU   _forward_shaper;   rk   s     r'   forward_shapezTransform.forward_shape  T     %11 	OeOOO   ""5)))r)   c                    t          |t          j                  s t          dt	          |           d          |                     |          S )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.
        rn   rK   )rG   ro   r   rT   rU   _inverse_shaperq   s     r'   inverse_shapezTransform.inverse_shape*  rs   r)   variable.Variablec                    t           j        S )z!The domain of this transformationr
   realr;   s    r'   rW   zTransform._domain9       }r)   c                    t           j        S )z#The codomain of this transformationry   r{   s    r'   r`   zTransform._codomain>  r|   r)   c                     t          d          )zInner method for public API ``forward``, subclass should
        overwrite this method for supporting forward transformation.
        zForward not implementedre   r[   s     r'   rZ   zTransform._forwardC       "";<<<r)   c                     t          d          )zInner method of public API ``inverse``, subclass should
        overwrite this method for supporting inverse transformation.
        zInverse not implementedr   rb   s     r'   ra   zTransform._inverseI  r   r)   c                    t          | d          r|                     |          S t          | d          r)|                     |                     |                     S t	          d          )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   rH   re   r[   s     r'   rf   z(Transform._call_forward_log_det_jacobianO  su    4455 	511!4444455 	D224<<??CCCC!7
 
 	
r)   c                    t          | d          r|                     |          S t          | d          r)|                     |                     |                     S t	          d          )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   ra   re   rb   s     r'   ri   z(Transform._call_inverse_log_det_jacobianZ  sw    4455 	511!4444455 	E224==3C3CDDDD!6
 
 	
r)   c                    |S )zInner method called by ``forward_shape``, which is used to infer the
        forward shape. Subclass should overwrite this method for supporting
        ``forward_shape``.
        r1   rq   s     r'   rp   zTransform._forward_shapee  	    
 r)   c                    |S )zInner method called by ``inverse_shape``, which is used to infer the
        inverse shape. Subclass should overwrite this method for supporting
        ``inverse_shape``.
        r1   rq   s     r'   ru   zTransform._inverse_shapel  r   r)   r3   r4   )r@   r   r3   r   )r@   r   r3   r   )r@   r   r3   r   )r3   r   rI   r   r3   r   r\   r   r3   r   rk   rl   r3   rl   r3   rw   )r*   r+   r,   r-   r   r$   r&   r9   r0   r?   r   rC   rH   rc   rg   rj   rr   rv   propertyrW   r`   rZ   ra   rf   ri   rp   ru   __classcell__r<   s   @r'   r   r   G   s       9 9v NE      , , [, 444 X4KKK XK??? X?# # # #2       2       .6 6 6 6B6 6 6 6.* * * ** * * *    X    X= = = == = = =	
 	
 	
 	
	
 	
 	
 	
          r)   r   c                  h    e Zd ZdZej        ZddZddZdd	Z	e
dd            Ze
dd            ZdS )r   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.))

    rI   r   r3   c                *    |                                 S r6   )absr[   s     r'   rZ   zAbsTransform._forward      uuwwr)   r\   tuple[Tensor, Tensor]c                    | |fS r6   r1   rb   s     r'   ra   zAbsTransform._inverse  s    r1ur)   c                @    t          j        g |j                  }||fS N)dtype)rN   zerosr   )r;   r\   zeros      r'   r   z&AbsTransform._inverse_log_det_jacobian  s"    |Bag...Tzr)   variable.Realc                    t           j        S r6   ry   r{   s    r'   rW   zAbsTransform._domain  
    }r)   variable.Positivec                    t           j        S r6   r
   positiver{   s    r'   r`   zAbsTransform._codomain        r)   Nr   )r\   r   r3   r   r3   r   r3   r   )r*   r+   r,   r-   r   r.   r&   rZ   ra   r   r   rW   r`   r1   r)   r'   r   r   t  s        1 1f OE             X ! ! ! X! ! !r)   r   c                       e Zd ZdZej        Zd fdZedd            Z	edd	            Z
ddZddZddZddZddZedd            Zedd            Z xZS )r   a  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.)
    locr   scaler3   r4   c                   t          |t          j        j        j        t          j        j        f          st          dt          |                     t          |t          j        j        j        t          j        j        f          st          dt          |                     || _	        || _
        t                                                       d S )Nz$Expected 'loc' is a Tensor, but got z$Expected scale is a Tensor, but got )rG   rN   rO   rP   rQ   rR   rS   rT   rU   _loc_scaler8   r9   )r;   r   r   r<   s      r'   r9   zAffineTransform.__init__  s    &+'0&*2BC
 
 	P N499NNOOOFK)2FJ4DE
 
 	 DtE{{DD   	r)   c                    | j         S r6   )r   r{   s    r'   r   zAffineTransform.loc  s
    yr)   c                    | j         S r6   )r   r{   s    r'   r   zAffineTransform.scale  
    {r)   rI   c                &    | j         | j        |z  z   S r6   r   r   r[   s     r'   rZ   zAffineTransform._forward  s    y4;?**r)   r\   c                &    || j         z
  | j        z  S r6   r   rb   s     r'   ra   zAffineTransform._inverse  s    DI,,r)   c                X    t          j        | j                                                  S r6   )rN   r   r   logr[   s     r'   r   z)AffineTransform._forward_log_det_jacobian  s     z$+&&**,,,r)   rk   rl   c                    t          t          j        t          j        || j        j                  | j        j                            S r6   tuplerN   broadcast_shaper   rk   r   rq   s     r'   rp   zAffineTransform._forward_shape  @    "&udio>>! 
 
 	
r)   c                    t          t          j        t          j        || j        j                  | j        j                            S r6   r   rq   s     r'   ru   zAffineTransform._inverse_shape  r   r)   r   c                    t           j        S r6   ry   r{   s    r'   rW   zAffineTransform._domain  r   r)   c                    t           j        S r6   ry   r{   s    r'   r`   zAffineTransform._codomain  r   r)   )r   r   r   r   r3   r4   r3   r   r   r   r   r   )r*   r+   r,   r-   r   r#   r&   r9   r   r   r   rZ   ra   r   rp   ru   rW   r`   r   r   s   @r'   r   r     s4        6 NE         X    X+ + + +- - - -- - - -
 
 
 

 
 
 
    X    X    r)   r   c                       e Zd ZdZd fdZddZddZddZddZd dZ	d dZ
d!dZed"d            Zed"d            Z xZS )#r   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.])
    
transformsSequence[Transform]r3   r4   c                   t          |t          j                  st          dt	          |                     t          d |D                       st          d          || _        t                                                       d S )Nz3Expected type of 'transforms' is Sequence, but got c              3  @   K   | ]}t          |t                    V  d S r6   rG   r   .0ts     r'   	<genexpr>z*ChainTransform.__init__.<locals>.<genexpr>;  ,      @@:a++@@@@@@r)   z4All elements of transforms should be Transform type.)	rG   ro   r   rT   rU   allr   r8   r9   )r;   r   r<   s     r'   r9   zChainTransform.__init__6  s    *fo66 	Xd:FVFVXX   @@Z@@@@@ 	F   %r)   boolc                >    t          d | j        D                       S )Nc              3  >   K   | ]}|                                 V  d S r6   r?   r   s     r'   r   z/ChainTransform._is_injective.<locals>.<genexpr>D  s,      >>1??$$>>>>>>r)   )r   r   r{   s    r'   r?   zChainTransform._is_injectiveC  s!    >>do>>>>>>r)   rI   r   c                D    | j         D ]}|                    |          }|S r6   )r   rH   )r;   rI   	transforms      r'   rZ   zChainTransform._forwardF  s-     	% 	%I!!!$$AAr)   r\   c                ^    t          | j                  D ]}|                    |          }|S r6   )reversedr   rc   )r;   r\   r   s      r'   ra   zChainTransform._inverseK  s5    !$/22 	% 	%I!!!$$AAr)   floatc                
   d}| j         j        }| j        D ]l}||                     |                    |          ||j         j        z
            z  }|                    |          }||j        j        |j         j        z
  z  }m|S )N        )rW   rX   r   _sum_rightmostrg   rH   r`   )r;   rI   valuerX   r   s        r'   r   z(ChainTransform._forward_log_det_jacobianP  s    \,
 	H 	HAT((**1--zAI<P/P  E 		!A!+0193GGGJJr)   rk   rl   c                D    | j         D ]}|                    |          }|S r6   )r   rr   r;   rk   r   s      r'   rp   zChainTransform._forward_shape[  -     	3 	3I++E22EEr)   c                D    | j         D ]}|                    |          }|S r6   )r   rv   r   s      r'   ru   zChainTransform._inverse_shape`  r   r)   r   nintc                t    |dk    r1|                     t          t          | d                              n|S )zsum value along rightmost n dimr   )sumlistrange)r;   r   r   s      r'   r   zChainTransform._sum_rightmoste  s3    01AuyyeQBll++,,,5@r)   variable.Independentc                (   | j         d         j        }| j         d         j        j        }t	          | j                   D ]8}||j        j        |j        j        z
  z  }t          ||j        j                  }9t          j        |||j        z
            S )Nr   )r   rW   r`   rX   r   maxr
   Independent)r;   domainrX   r   s       r'   rW   zChainTransform._domaini  s    #+& _R(2=
$/** 	? 	?A!+0193GGGJZ)=>>JJ#FJ9J,JKKKr)   c                   | j         d         j        }| j         d         j        j        }| j         D ]8}||j        j        |j        j        z
  z  }t	          ||j        j                  }9t          j        |||j        z
            S )Nr   r   )r   r`   rW   rX   r   r
   r   )r;   codomainrX   r   s       r'   r`   zChainTransform._codomain  s    ?2&0_Q'/:
 	A 	AA!+0193GGGJZ)?@@JJ#Hj8;N.NOOOr)   )r   r   r3   r4   r3   r   r   r   )rI   r   r3   r   r   )r   r   r   r   r3   r   r3   r   )r*   r+   r,   r-   r9   r?   rZ   ra   r   rp   ru   r   r   rW   r`   r   r   s   @r'   r   r     s$        B     ? ? ? ?   
   
	 	 	 	   
   
A A A A L L L XL6 P P P XP P P P Pr)   r   c                  z     e Zd ZdZej        Zd fdZedd            Z	edd            Z
ddZddZddZ xZS )r   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])
    r3   r4   c                H    t                                                       d S r6   r7   r:   s    r'   r9   zExpTransform.__init__  r=   r)   r   c                    t           j        S r6   ry   r{   s    r'   rW   zExpTransform._domain  r   r)   r   c                    t           j        S r6   r   r{   s    r'   r`   zExpTransform._codomain  r   r)   rI   r   c                *    |                                 S r6   )expr[   s     r'   rZ   zExpTransform._forward  r   r)   r\   c                *    |                                 S r6   r   rb   s     r'   ra   zExpTransform._inverse  r   r)   c                    |S r6   r1   r[   s     r'   r   z&ExpTransform._forward_log_det_jacobian  s    r)   r   r   r   r   r   )r*   r+   r,   r-   r   r#   r&   r9   r   rW   r`   rZ   ra   r   r   r   s   @r'   r   r     s         4 NE         X ! ! ! X!             r)   r   c                       e Zd ZdZd fdZdd
ZddZddZddZddZ	ddZ
edd            Zedd            Z xZS )r   ap  
    ``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.])
    rO   r   reinterpreted_batch_rankr   r3   r4   c                    t          |t                    st          dt          |                     |dk    rt	          d|           || _        || _        t                                                       d S )Nz+Expected 'base' is Transform type, but get r   zAExpected 'reinterpreted_batch_rank' is grater than zero, but got )	rG   r   rT   rU   rY   _base_reinterpreted_batch_rankr8   r9   )r;   rO   r   r<   s      r'   r9   zIndependentTransform.__init__  s    $	** 	Jd4jjJJ   $q((nTlnn   
)A&r)   r   c                4    | j                                         S r6   )r   r?   r{   s    r'   r?   z"IndependentTransform._is_injective  s    z'')))r)   rI   r   c                    |                                 | j        j        k     rt          d          | j                            |          S Nz/Input dimensions is less than event dimensions.)rV   rW   rX   rY   r   rH   r[   s     r'   rZ   zIndependentTransform._forward   s?    5577T\,,,NOOOz!!!$$$r)   r\   c                    |                                 | j        j        k     rt          d          | j                            |          S r   )rV   r`   rX   rY   r   rc   rb   s     r'   ra   zIndependentTransform._inverse  s?    5577T^...NOOOz!!!$$$r)   c                    | j                             |                              t          t	          | j         d                              S )Nr   )r   rg   r   r   r   r   r[   s     r'   r   z.IndependentTransform._forward_log_det_jacobian
  sE    z221559966::;;
 
 	
r)   rk   rl   c                6    | j                             |          S r6   )r   rr   rq   s     r'   rp   z#IndependentTransform._forward_shape      z''...r)   c                6    | j                             |          S r6   )r   rv   rq   s     r'   ru   z#IndependentTransform._inverse_shape  r   r)   r   c                J    t          j        | j        j        | j                  S r6   )r
   r   r   rW   r   r{   s    r'   rW   zIndependentTransform._domain  s$    #J >
 
 	
r)   c                J    t          j        | j        j        | j                  S r6   )r
   r   r   r`   r   r{   s    r'   r`   zIndependentTransform._codomain  s$    #J $"@
 
 	
r)   )rO   r   r   r   r3   r4   r   r   r   r   r   )r*   r+   r,   r-   r9   r?   rZ   ra   r   rp   ru   r   rW   r`   r   r   s   @r'   r   r     s        ) )V     * * * *% % % %
% % % %

 
 
 

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

 
 
 
 X
 
 
 
 
r)   r   c                       e Zd ZdZej        Zd fdZedd            Z	edd	            Z
edd            ZddZddZddZddZddZ xZS )r   aL  
    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])
    powerr   r3   r4   c                    t          |t          j        j        j        t          j        j        f          st          dt          |                     || _	        t                                                       d S )Nz&Expected 'power' is a tensor, but got )rG   rN   rO   rP   rQ   rR   rS   rT   rU   _powerr8   r9   )r;   r  r<   s     r'   r9   zPowerTransform.__init__?  sv    FK)2FJ4DE
 
 	 FeFF   r)   c                    | j         S r6   )r  r{   s    r'   r  zPowerTransform.powerI  r   r)   r   c                    t           j        S r6   ry   r{   s    r'   rW   zPowerTransform._domainM  r   r)   r   c                    t           j        S r6   r   r{   s    r'   r`   zPowerTransform._codomainQ  r   r)   rI   c                6    |                     | j                  S r6   powr  r[   s     r'   rZ   zPowerTransform._forwardU  s    uuT[!!!r)   r\   c                <    |                     d| j        z            S N   r  rb   s     r'   ra   zPowerTransform._inverseX  s    uuQ_%%%r)   c                    | j         |                    | j         dz
            z                                                                  S r  )r  r  r   r   r[   s     r'   r   z(PowerTransform._forward_log_det_jacobian[  s9    aeeDK!O44499;;??AAAr)   rk   rl   c                Z    t          t          j        || j        j                            S r6   r   rN   r   r  rk   rq   s     r'   rp   zPowerTransform._forward_shape^  "    V+E4;3DEEFFFr)   c                Z    t          t          j        || j        j                            S r6   r  rq   s     r'   ru   zPowerTransform._inverse_shapea  r  r)   )r  r   r3   r4   r   r   r   r   r   r   )r*   r+   r,   r-   r   r#   r&   r9   r   r  rW   r`   rZ   ra   r   rp   ru   r   r   s   @r'   r   r   "  s$        4 NE         X    X ! ! ! X!" " " "& & & &B B B BG G G GG G G G G G G Gr)   r   c                       e Zd ZdZej        Zd fdZedd	            Z	edd
            Z
edd            Zedd            ZddZddZddZddZddZ xZS )r   a  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.])
    in_event_shaperl   out_event_shaper3   r4   c                   t          |t          j                  rt          |t          j                  st          d| d|           d}|D ]}||z  }d}|D ]}||z  }||k    rt	          d| d|           t          |          | _        t          |          | _        t                      	                                 d S )NzdExpected type of 'in_event_shape' and 'out_event_shape' is Sequence[int], but got 'in_event_shape': z, 'out_event_shape': r  zCThe numel of 'in_event_shape' should be 'out_event_shape', but got z!=)
rG   ro   r   rT   rY   r   _in_event_shape_out_event_shaper8   r9   )r;   r  r  in_sizeeout_sizer<   s         r'   r9   zReshapeTransform.__init__  s     .&/:: 	*V_C
 C
 	 8<J8 8&58 8  
  	 	AqLGG  	 	AMHHh1"1 1&.1 1  
  %^44 %o 6 6r)   tuple[Sequence[int]]c                    | j         S r6   )r  r{   s    r'   r  zReshapeTransform.in_event_shape  s    ##r)   c                    | j         S r6   )r  r{   s    r'   r  z ReshapeTransform.out_event_shape  s    $$r)   r   c                d    t          j        t           j        t          | j                            S r6   )r
   r   rz   lenr  r{   s    r'   rW   zReshapeTransform._domain  s"    #HM3t7K3L3LMMMr)   c                d    t          j        t           j        t          | j                            S r6   )r
   r   rz   r"  r  r{   s    r'   r`   zReshapeTransform._codomain  s"    #HM3t7L3M3MNNNr)   rI   r   c                    |                     t          |j                  d |                                t	          | j                  z
           | j        z             S r6   )reshaper   rk   rV   r"  r  r  r[   s     r'   rZ   zReshapeTransform._forward  sQ    yy!'NN@QUUWWs4+?'@'@@@A#$
 
 	
r)   r\   c                    |                     t          |j                  d |                                t	          | j                  z
           | j        z             S r6   )r%  r   rk   rV   r"  r  r  rb   s     r'   ra   zReshapeTransform._inverse  sQ    yy!'NNAQUUWWs4+@'A'AAAB"#
 
 	
r)   rk   c           	        t          |          t          | j                  k     r4t          dt          | j                   dt          |                     t          |t          | j                   d                    t          | j                  k    r5t          d| j         d|t          | j                   d                     t          |d t          | j                                      | j        z   S )Nz,Expected length of 'shape' is not less than 
, but got  Event shape mismatch, expected: )r"  r  rY   r   r  rq   s     r'   rp   zReshapeTransform._forward_shape  s   u::D01111ps4CW?X?Xppdghmdndnpp   D011133455 :
 :
 
 
 x43GxxSXZ]^b^rZsZsYsYuYuSvxx   %43t344445669NN	
r)   c           	        t          |          t          | j                  k     r4t          dt          | j                   dt          |                     t          |t          | j                   d                    t          | j                  k    r5t          d| j         d|t          | j                   d                     t          |d t          | j                                      | j        z   S )Nz)Expected 'shape' length is not less than r(  r)  )r"  r  rY   r   r  rq   s     r'   ru   zReshapeTransform._inverse_shape  s   u::D12222nC@U<V<Vnnbefkblblnn   D122244566%!;
 ;
 
 
 z43HzzTY[^_c_t[u[uZuZwZwTxzz   %53t45555677$:NN	
r)   c                    |j         d |                                t          | j                  z
           }t	          j        ||j                  S r   )rk   rV   r"  r  rN   r   r   )r;   rI   rk   s      r'   r   z*ReshapeTransform._forward_log_det_jacobian  sC    =!%%''C(<$=$===>|E1111r)   )r  rl   r  rl   r3   r4   )r3   r  r   r   r   r   )r*   r+   r,   r-   r   r#   r&   r9   r   r  r  rW   r`   rZ   ra   rp   ru   r   r   r   s   @r'   r   r   e  s=        B NE     6 $ $ $ X$ % % % X% N N N XN O O O XO
 
 
 

 
 
 

 
 
 

 
 
 
2 2 2 2 2 2 2 2r)   r   c                  Z    e Zd ZdZedd            Zedd            Zdd	ZddZddZ	dS )r   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]])
    r3   r   c                    t           j        S r6   ry   r{   s    r'   rW   zSigmoidTransform._domain  r   r)   rw   c                T    t          j        ddt          j        dd                    S )NFr   r         ?r
   rQ   r   Ranger{   s    r'   r`   zSigmoidTransform._codomain   s$     :+;C+E+EFFFr)   rI   r   c                *    t          j        |          S r6   )Fsigmoidr[   s     r'   rZ   zSigmoidTransform._forward  s    y||r)   r\   c                V    |                                 |                                 z
  S r6   )r   log1prb   s     r'   ra   zSigmoidTransform._inverse  s    uuww1"%%r)   c                X    t          j        |            t          j        |          z
  S r6   )r3  softplusr[   s     r'   r   z*SigmoidTransform._forward_log_det_jacobian
  s!    
A2A..r)   Nr   r   r   r   )
r*   r+   r,   r-   r   rW   r`   rZ   ra   r   r1   r)   r'   r   r     s         0    X G G G XG   & & & &/ / / / / /r)   r   c                  p    e Zd ZdZej        Zedd            Zedd            Z	dd	Z
ddZddZddZdS )r   a  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]])
    r3   r   c                @    t          j        t           j        d          S r  r
   r   rz   r{   s    r'   rW   zSoftmaxTransform._domain)      #HM1555r)   rw   c                B    t          j        ddt          j                  S NFr  r
   rQ   r   simplexr{   s    r'   r`   zSoftmaxTransform._codomain-       :+=>>>r)   rI   r   c                    ||                     dd          d         z
                                  }||                    dd          z  S )Nr   T)keepdimr   )r   r   r   r[   s     r'   rZ   zSoftmaxTransform._forward1  sH    r4((++0022155T5****r)   r\   c                *    |                                 S r6   r   rb   s     r'   ra   zSoftmaxTransform._inverse5  r   r)   rk   rl   c                j    t          |          dk     rt          dt          |                     |S Nr  z3Expected length of shape is grater than 1, but got r"  rY   rq   s     r'   rp   zSoftmaxTransform._forward_shape8  ;    u::>>Rc%jjRR   r)   c                j    t          |          dk     rt          dt          |                     |S rF  rG  rq   s     r'   ru   zSoftmaxTransform._inverse_shape?  rH  r)   Nr   r   r   r   r   )r*   r+   r,   r-   r   r/   r&   r   rW   r`   rZ   ra   rp   ru   r1   r)   r'   r   r     s         0 JE6 6 6 X6 ? ? ? X?+ + + +           r)   r   c                      e Zd ZdZdddZdd
Zedd            Zedd            ZddZ	d dZ
ddZd!dZed"d            Zed"d            ZdS )#r   a  ``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]])
    r   r   r   axisr   c                T   |rt          |t          j                  s t          dt	          |           d          t          d |D                       st          d          t          |t                    s t          dt	          |           d          || _        || _        d S )Nz6Expected 'transforms' is Sequence[Transform], but got rK   c              3  @   K   | ]}t          |t                    V  d S r6   r   r   s     r'   r   z*StackTransform.__init__.<locals>.<genexpr>u  r   r)   z5Expected all element in transforms is Transform Type.zExpected 'axis' is int, but got)	rG   ro   r   rT   rU   r   r   _transforms_axis)r;   r   rK  s      r'   r9   zStackTransform.__init__p  s     	J!H!H 	\jIYIY\\\   @@Z@@@@@ 	G   $$$ 	MKd4jjKKKLLL%


r)   r3   r   c                >    t          d | j        D                       S )Nc              3  >   K   | ]}|                                 V  d S r6   r   r   s     r'   r   z/StackTransform._is_injective.<locals>.<genexpr>  s,      ??1??$$??????r)   )r   rN  r{   s    r'   r?   zStackTransform._is_injective  s"    ??d.>??????r)   c                    | j         S r6   )rN  r{   s    r'   r   zStackTransform.transforms  s    r)   c                    | j         S r6   )rO  r{   s    r'   rK  zStackTransform.axis  s
    zr)   rI   r   c           	         |                      |           t          j        d t          t          j        || j                  | j                  D             | j                  S )Nc                >    g | ]\  }}|                     |          S r1   )rH   r   vr   s      r'   
<listcomp>z+StackTransform._forward.<locals>.<listcomp>  6       Aq 		!  r)   _check_sizerN   stackzipunstackrO  rN  r[   s     r'   rZ   zStackTransform._forward  h    | q$* = =t?OPP   J
 
 	
r)   r\   c           	         |                      |           t          j        d t          t          j        || j                  | j                  D             | j                  S )Nc                >    g | ]\  }}|                     |          S r1   )rc   rV  s      r'   rX  z+StackTransform._inverse.<locals>.<listcomp>  rY  r)   rZ  rb   s     r'   ra   zStackTransform._inverse  r_  r)   c           	         |                      |           t          j        d t          t          j        || j                  | j                  D             | j                  S )Nc                >    g | ]\  }}|                     |          S r1   )rg   rV  s      r'   rX  z<StackTransform._forward_log_det_jacobian.<locals>.<listcomp>  s:       Aq **1--  r)   rZ  r[   s     r'   r   z(StackTransform._forward_log_det_jacobian  r_  r)   rW  r4   c                R   |                                  | j        cxk    r|                                 k     s/n t          d|                                  d| j         d          |j        | j                 t	          | j                  k    rt          d| j         d          d S )NzInput dimensions z, should be grater than stack transform axis rK   zInput size along z- should be equal to the length of transforms.)rV   rO  rY   rk   r"  rN  )r;   rW  s     r'   r[  zStackTransform._check_size  s    DJ000000000AEEGG 0 0"&*0 0 0   74:#d&6"7"777)DJ ) ) )   87r)   variable.Stackc                T    t          j        d | j        D             | j                  S )Nc                    g | ]	}|j         
S r1   )rW   r   s     r'   rX  z*StackTransform._domain.<locals>.<listcomp>  s    CCCQqyCCCr)   r
   StackrN  rO  r{   s    r'   rW   zStackTransform._domain  s(    ~CC$2BCCCTZPPPr)   c                T    t          j        d | j        D             | j                  S )Nc                    g | ]	}|j         
S r1   )r`   r   s     r'   rX  z,StackTransform._codomain.<locals>.<listcomp>  s    333QQ[333r)   rh  r{   s    r'   r`   zStackTransform._codomain  s.    ~33$"2333TZ
 
 	
r)   N)r   )r   r   rK  r   r   )r3   r   )r3   r   r   r   )rW  r   r3   r4   )r3   re  )r*   r+   r,   r-   r9   r?   r   r   rK  rZ   ra   r   r[  rW   r`   r1   r)   r'   r   r   G  s$       & &P    @ @ @ @       X     X
 
 
 

 
 
 

 
 
 

 
 
 
 Q Q Q XQ 
 
 
 X
 
 
r)   r   c                  x    e Zd ZdZej        ZddZddZddZ	ddZ
ddZedd            Zedd            ZdS )r   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)
    rI   r   r3   c                   |j         d         dz   t          j        |j         d         g                              d          z
  }t	          j        ||                                z
            }d|z
                      d          }t	          j        |dgdz  t          |j                   dz
  z  ddgz   d          t	          j        |dgdz  t          |j                   dz
  z  ddgz   d          z  S )Nr   r  r      )r   )
rk   rN   onescumsumr3  r4  r   cumprodpadr"  )r;   rI   offsetz	z_cumprods        r'   rZ   zStickBreakingTransform._forward  s    q6;}#=#=#D#DR#H#HHIa&**,,&''UOOB''	uQa3qw<<!#341v=QGGG!%sQw#ag,,"23q!f<AK
 K
 K
 
 	
r)   r\   c                L   |dd df         }|j         d         t          j        |j         d         g                              d          z
  }d|                    d          z
  }|                                |                                z
  |                                z   }|S )N.r   r  )rk   rN   ro  rp  r   )r;   r\   y_croprs  sfrI   s         r'   ra   zStickBreakingTransform._inverse  s    38v{FL,<+=>>EEbIIIr"""JJLL26688#fjjll2r)   c                v   |                      |          }|j        d         dz   t          j        |j        d         g                              d          z
  }||                                z
  }| t          j        |          z   |dd df                                         z                       d          S )Nr   r  .)	rH   rk   rN   ro  rp  r   r3  log_sigmoidr   )r;   rI   r\   rs  s       r'   r   z0StickBreakingTransform._forward_log_det_jacobian  s    LLOOq6;}#=#=#D#DR#H#HH

Q]1%%%#ss((9(99>>rBBBr)   rk   rl   c                Z    |st          d|           g |d d         |d         dz   R S Nz'Expected 'shape' is not empty, but got r   r  rY   rq   s     r'   rp   z%StickBreakingTransform._forward_shape  E     	PNuNNOOO+ss+U2Y]+++r)   c                Z    |st          d|           g |d d         |d         dz
  R S r|  r}  rq   s     r'   ru   z%StickBreakingTransform._inverse_shape  r~  r)   r   c                @    t          j        t           j        d          S r  r;  r{   s    r'   rW   zStickBreakingTransform._domain  r<  r)   rw   c                B    t          j        ddt          j                  S r>  r?  r{   s    r'   r`   z StickBreakingTransform._codomain  rA  r)   Nr   r   r   r   r   )r*   r+   r,   r-   r   r#   r&   rZ   ra   r   rp   ru   r   rW   r`   r1   r)   r'   r   r     s         . NE
 
 
 
   C C C C, , , ,
, , , ,
 6 6 6 X6 ? ? ? X? ? ?r)   r   c                  h    e Zd ZdZej        Zedd            Zedd            Z	dd	Z
ddZddZdS )r   as  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
    r3   r   c                    t           j        S r6   ry   r{   s    r'   rW   zTanhTransform._domain$  r   r)   rw   c                T    t          j        ddt          j        dd                    S )NFr   g      r/  r0  r{   s    r'   r`   zTanhTransform._codomain(  s$     :+;D#+F+FGGGr)   rI   r   c                *    |                                 S r6   )tanhr[   s     r'   rZ   zTanhTransform._forward,  s    vvxxr)   r\   c                *    |                                 S r6   )atanhrb   s     r'   ra   zTanhTransform._inverse/  s    wwyyr)   c                f    dt          j        d          |z
  t          j        d|z            z
  z  S )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   r3  r8  r[   s     r'   r   z'TanhTransform._forward_log_det_jacobian2  s/     dhsmma'!*TAX*>*>>??r)   Nr   r   r   r   )r*   r+   r,   r-   r   r#   r&   r   rW   r`   rZ   ra   r   r1   r)   r'   r   r     s         @ NE   X H H H XH      @ @ @ @ @ @r)   r   )'
__future__r   enumr  ro   r   r   r   rN   paddle.nn.functionalnn
functionalr3  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   r1   r)   r'   <module>r     s   # " " " " "                                            J((((((IIIIIIII  "7 7 7 7 749 7 7 7j j j j j j j jZ	F! F! F! F! F!9 F! F! F!RT T T T Ti T T TnzP zP zP zP zPY zP zP zPz/ / / / /9 / / /d\
 \
 \
 \
 \
9 \
 \
 \
~@G @G @G @G @GY @G @G @GF{2 {2 {2 {2 {2y {2 {2 {2|(/ (/ (/ (/ (/y (/ (/ (/V6 6 6 6 6y 6 6 6ru
 u
 u
 u
 u
Y u
 u
 u
p?? ?? ?? ?? ??Y ?? ?? ??D8@ 8@ 8@ 8@ 8@I 8@ 8@ 8@ 8@ 8@r)   