o
    Me                     @   sP  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm  m	Z
 d dlmZmZmZmZ g dZG dd de jZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ dS )     N)
constraintdistributiontransformed_distributionvariable)	TransformAbsTransformAffineTransformChainTransformExpTransformIndependentTransformPowerTransformReshapeTransformSigmoidTransformSoftmaxTransformStackTransformStickBreakingTransformTanhTransformc                   @   s,   e Zd ZdZdZdZdZdZedd Z	dS )	Typez!Mapping type of a transformation.Z	bijectionZ	injectionZ
surjectionotherc                 C   s   || j | jfv S )z3Both bijection and injection are injective mapping.)	BIJECTION	INJECTION)cls_type r   MD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distribution/transform.pyis_injective8   s   zType.is_injectiveN)
__name__
__module____qualname____doc__r   r   
SURJECTIONOTHERclassmethodr   r   r   r   r   r   0   s    r   c                       s   e Zd ZdZejZ f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  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 funciton
    :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 t|   d S N)superr   __init__self	__class__r   r   r&   {      zTransform.__init__c                 C   s   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   r   r   r   _is_injective~   s   zTransform._is_injectivec                 C   s:   t |tjrt|| gS t |trt| |gS | tS )ah  Make this instance as a callable object. The return value is
        depening 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   Distributionr   ZTransformedDistributionr   r	   forwardx)r(   inputr   r   r   __call__   s   

zTransform.__call__c                 C   Z   t |tjjjstdt| d| | jj	k r(t
d|  d| jj	 | |S )aC  Forward transformation with mapping :math:`y = f(x)`.

        Useful for turning one random outcome into another.

        Args:
            x (Tensos): 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 )r-   paddlefluid	frameworkVariable	TypeErrortypedim_domain
event_rank
ValueError_forwardr(   r0   r   r   r   r/      s   
zTransform.forwardc                 C   r3   )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 r4   The dimensions of y(r6   )r-   r7   r8   r9   r:   r;   r<   r=   	_codomainr?   r@   _inverser(   yr   r   r   inverse   s   

zTransform.inversec                 C   sz   t |tjjjstdt| dt |tjjjr0| | jj	k r0t
d|  d| jj	 |  s8t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.
        rC   r4   r5   r6   zJforward_log_det_jacobian can't be implemented for non-injectivetransforms.)r-   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r,   NotImplementedError_call_forward_log_det_jacobianrB   r   r   r   forward_log_det_jacobian   s"   
z"Transform.forward_log_det_jacobianc                 C   r3   )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 r4   rD   r6   )r-   r7   r8   r9   r:   r;   r<   r=   rE   r?   r@   _call_inverse_log_det_jacobianrG   r   r   r   inverse_log_det_jacobian   s   
z"Transform.inverse_log_det_jacobianc                 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 r4   )r-   typingSequencer;   r<   _forward_shaper(   shaper   r   r   forward_shape   
   	
zTransform.forward_shapec                 C   rO   )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.
        rP   r4   )r-   rQ   rR   r;   r<   _inverse_shaperT   r   r   r   inverse_shape  rW   zTransform.inverse_shapec                 C      t jS )z!The domain of this transformationr   realr'   r   r   r   r>        zTransform._domainc                 C   rZ   )z#The codomain of this transformationr[   r'   r   r   r   rE   #  r]   zTransform._codomainc                 C      t d)zInner method for publid API ``forward``, subclass should
        overwrite this method for supporting forward transformation.
        zForward not implementedrJ   rB   r   r   r   rA   (     zTransform._forwardc                 C   r^   )zInner method of public API ``inverse``, subclass should
        overwrite this method for supporting inverse transformation.
        zInverse not implementedr_   rG   r   r   r   rF   .  r`   zTransform._inversec                 C   s8   t | dr
| |S t | dr| | t 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.)hasattrra   rb   r/   rH   rJ   rB   r   r   r   rK   4     


z(Transform._call_forward_log_det_jacobianc                 C   s8   t | dr
| |S t | dr| | | S td)z3Inner method called by ``inverse_log_det_jacobian``rb   ra   zgNeither _forward_log_det_jacobian nor _inverse_log_det_jacobian is implemented. One of them is required)rc   rb   ra   rF   rJ   rG   r   r   r   rM   ?  rd   z(Transform._call_inverse_log_det_jacobianc                 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``.
        r   rT   r   r   r   rS   J     zTransform._forward_shapec                 C   re   )zInner method called by ``inverse_shape``, whic is used to infer the
        invese shape. Subclass should overwrite this method for supporting
        ``inverse_shape``.
        r   rT   r   r   r   rX   Q  rf   zTransform._inverse_shape)r   r   r   r   r   r   r   r&   r"   r,   r2   r/   rI   rL   rN   rV   rY   propertyr>   rE   rA   rF   rK   rM   rS   rX   __classcell__r   r   r)   r   r   >   s.    :


r   c                   @   sF   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 invese
      ``{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(gpu:0), stop_gradient=True,
            #        [1., 0., 1.])

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

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

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

    c                 C      |  S r$   )absrB   r   r   r   rA        zAbsTransform._forwardc                 C   s
   | |fS r$   r   rG   r   r   r   rF        
zAbsTransform._inversec                 C   s   t jdg|jd}||fS N   )dtype)r7   zerosro   )r(   rH   zeror   r   r   rb     s   z&AbsTransform._inverse_log_det_jacobianc                 C   rZ   r$   r[   r'   r   r   r   r>        zAbsTransform._domainc                 C   rZ   r$   r   Zpositiver'   r   r   r   rE     rr   zAbsTransform._codomainN)r   r   r   r   r   r    r   rA   rF   rb   rg   r>   rE   r   r   r   r   r   Y  s    2
r   c                       s~   e Zd ZdZejZ f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edd Zedd Z  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(gpu:0), stop_gradient=True,
            #        [1., 2.])
            print(affine.inverse(affine.forward(x)))
            # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [1., 2.])
            print(affine.forward_log_det_jacobian(x))
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0.])
    c                    sb   t |tjjjstdt| t |tjjjs"tdt| || _|| _t	t
|   d S )Nz$Expected 'loc' is a Tensor, but got z$Expected scale is a Tensor, but got )r-   r7   r8   r9   r:   r;   r<   _loc_scaler%   r   r&   )r(   locscaler)   r   r   r&     s   zAffineTransform.__init__c                 C      | j S r$   )rt   r'   r   r   r   rv     rr   zAffineTransform.locc                 C   rx   r$   )ru   r'   r   r   r   rw     rr   zAffineTransform.scalec                 C   s   | j | j|  S r$   rt   ru   rB   r   r   r   rA        zAffineTransform._forwardc                 C   s   || j  | j S r$   ry   rG   r   r   r   rF     rz   zAffineTransform._inversec                 C   s   t | j S r$   )r7   rj   ru   logrB   r   r   r   ra     rz   z)AffineTransform._forward_log_det_jacobianc                 C       t tt|| jj| jjS r$   tupler7   broadcast_shapert   rU   ru   rT   r   r   r   rS        zAffineTransform._forward_shapec                 C   r|   r$   r}   rT   r   r   r   rX     r   zAffineTransform._inverse_shapec                 C   rZ   r$   r[   r'   r   r   r   r>     rr   zAffineTransform._domainc                 C   rZ   r$   r[   r'   r   r   r   rE     rr   zAffineTransform._codomain)r   r   r   r   r   r   r   r&   rg   rv   rw   rA   rF   ra   rS   rX   r>   rE   rh   r   r   r)   r   r     s"    


r   c                       sp   e Zd ZdZ f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  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(gpu:0), stop_gradient=True,
            #        [1.         , 2.71828175 , 7.38905621 , 20.08553696])
            print(chain.inverse(chain.forward(x)))
            # Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0., 1., 2., 3.])
            print(chain.forward_log_det_jacobian(x))
            # Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0., 1., 2., 3.])
            print(chain.inverse_log_det_jacobian(chain.forward(x)))
            # Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [ 0., -1., -2., -3.])
    c                    sP   t |tjstdt| tdd |D std|| _tt| 	  d S )Nz3Expected type of 'transforms' is Sequence, but got c                 s       | ]}t |tV  qd S r$   r-   r   .0tr   r   r   	<genexpr>      z*ChainTransform.__init__.<locals>.<genexpr>z4All elements of transforms should be Transform type.)
r-   rQ   rR   r;   r<   all
transformsr%   r	   r&   )r(   r   r)   r   r   r&     s   zChainTransform.__init__c                 C      t dd | jD S )Nc                 s       | ]}|  V  qd S r$   r,   r   r   r   r   r   #      z/ChainTransform._is_injective.<locals>.<genexpr>)r   r   r'   r   r   r   r,   "     zChainTransform._is_injectivec                 C      | j D ]}||}q|S r$   )r   r/   )r(   r0   	transformr   r   r   rA   %     
zChainTransform._forwardc                 C   s   t | jD ]}||}q|S r$   )reversedr   rI   )r(   rH   r   r   r   r   rF   *  s   zChainTransform._inversec                 C   sX   d}| j j}| jD ] }|| ||||j j 7 }||}||jj|j j 7 }q	|S )N        )r>   r?   r   _sum_rightmostrL   r/   rE   )r(   r0   valuer?   r   r   r   r   ra   /  s   

z(ChainTransform._forward_log_det_jacobianc                 C   r   r$   )r   rV   r(   rU   r   r   r   r   rS   :  r   zChainTransform._forward_shapec                 C   r   r$   )r   rY   r   r   r   r   rX   ?  r   zChainTransform._inverse_shapec                 C   s"   |dkr| tt| dS |S )zsum value along rightmost n dimr   )sumlistrange)r(   r   nr   r   r   r   D  s   "zChainTransform._sum_rightmostc                 C   s^   | j d j}| j d jj}t| j D ]}||jj|jj 8 }t||jj}qt|||j S )Nr   )r   r>   rE   r?   r   maxr   Independent)r(   domainr?   r   r   r   r   r>   H  s   zChainTransform._domainc                 C   sZ   | j d j}| j d jj}| j D ]}||jj|jj 7 }t||jj}qt|||j S )Nr   r   )r   rE   r>   r?   r   r   r   )r(   Zcodomainr?   r   r   r   r   rE   d  s   
zChainTransform._codomain)r   r   r   r   r&   r,   rA   rF   ra   rS   rX   r   rg   r>   rE   rh   r   r   r)   r   r	     s    !
r	   c                       sV   e Zd ZdZejZ fddZedd Z	edd Z
dd	 Zd
d Zdd Z  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(gpu:0), 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(gpu:0), 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(gpu:0), 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(gpu:0), stop_gradient=True,
            #        [ 0.        , -0.69314718, -1.09861231])
    c                    r#   r$   )r%   r
   r&   r'   r)   r   r   r&     r+   zExpTransform.__init__c                 C   rZ   r$   r[   r'   r   r   r   r>     rr   zExpTransform._domainc                 C   rZ   r$   rs   r'   r   r   r   rE     rr   zExpTransform._codomainc                 C   ri   r$   )exprB   r   r   r   rA     rk   zExpTransform._forwardc                 C   ri   r$   r{   rG   r   r   r   rF     rk   zExpTransform._inversec                 C   re   r$   r   rB   r   r   r   ra     s   z&ExpTransform._forward_log_det_jacobian)r   r   r   r   r   r   r   r&   rg   r>   rE   rA   rF   ra   rh   r   r   r)   r   r
   p  s    

r
   c                       sh   e Zd ZdZ f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  ZS )r   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 transformaion, but does sum out the
    ``reinterpretd_bach_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
    paritioned-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(gpu:0), 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(gpu:0), stop_gradient=True,
            #        [6. , 15.])
    c                    sP   t |tstdt| |dkrtd| || _|| _tt| 	  d S )Nz+Expected 'base' is Transform type, but get r   zAExpected 'reinterpreted_batch_rank' is grater than zero, but got )
r-   r   r;   r<   r@   _base_reinterpreted_batch_rankr%   r   r&   )r(   baseZreinterpreted_batch_rankr)   r   r   r&     s   
zIndependentTransform.__init__c                 C   s
   | j  S r$   )r   r,   r'   r   r   r   r,     rl   z"IndependentTransform._is_injectivec                 C   $   |  | jjk rtd| j|S Nz/Input dimensions is less than event dimensions.)r=   r>   r?   r@   r   r/   rB   r   r   r   rA        zIndependentTransform._forwardc                 C   r   r   )r=   rE   r?   r@   r   rI   rG   r   r   r   rF     r   zIndependentTransform._inversec                 C   s    | j |tt| j dS )Nr   )r   rL   r   r   r   r   rB   r   r   r   ra     s   z.IndependentTransform._forward_log_det_jacobianc                 C      | j |S r$   )r   rV   rT   r   r   r   rS        z#IndependentTransform._forward_shapec                 C   r   r$   )r   rY   rT   r   r   r   rX     r   z#IndependentTransform._inverse_shapec                 C      t | jj| jS r$   )r   r   r   r>   r   r'   r   r   r   r>        
zIndependentTransform._domainc                 C   r   r$   )r   r   r   rE   r   r'   r   r   r   rE     r   zIndependentTransform._codomain)r   r   r   r   r&   r,   rA   rF   ra   rS   rX   rg   r>   rE   rh   r   r   r)   r   r     s    +
r   c                       sr   e Zd ZdZejZ f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  ZS )r   aC  
    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(gpu:0), stop_gradient=True,
            #        [1., 4.])
            print(power.inverse(power.forward(x)))
            # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [1., 2.])
            print(power.forward_log_det_jacobian(x))
            # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0.69314718, 1.38629436])
    c                    s:   t |tjjjstdt| || _tt	| 
  d S )Nz&Expected 'power' is a tensor, but got )r-   r7   r8   r9   r:   r;   r<   _powerr%   r   r&   )r(   powerr)   r   r   r&     s   zPowerTransform.__init__c                 C   rx   r$   )r   r'   r   r   r   r   $  rr   zPowerTransform.powerc                 C   rZ   r$   r[   r'   r   r   r   r>   (  rr   zPowerTransform._domainc                 C   rZ   r$   rs   r'   r   r   r   rE   ,  rr   zPowerTransform._codomainc                 C   s   | | jS r$   powr   rB   r   r   r   rA   0  r   zPowerTransform._forwardc                 C   s   | d| j S Nrn   r   rG   r   r   r   rF   3  rz   zPowerTransform._inversec                 C   s   | j || j d    S r   )r   r   rj   r{   rB   r   r   r   ra   6  s   z(PowerTransform._forward_log_det_jacobianc                 C      t t|| jjS r$   r~   r7   r   r   rU   rT   r   r   r   rS   9  r   zPowerTransform._forward_shapec                 C   r   r$   r   rT   r   r   r   rX   <  r   zPowerTransform._inverse_shape)r   r   r   r   r   r   r   r&   rg   r   r>   rE   rA   rF   ra   rS   rX   rh   r   r   r)   r   r      s    


r   c                       s~   e Zd ZdZejZ fd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  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)))
            # (5, 2, 6)
            print(reshape_transform.forward(x))
            # Tensor(shape=[1, 3, 2], dtype=float32, place=Place(gpu:0), 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(gpu:0), stop_gradient=True,
            #        [[[1., 1., 1.],
            #          [1., 1., 1.]]])
            print(reshape_transform.forward_log_det_jacobian(x))
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0.])
    c                    s   t |tjrt |tjstd| d| ttj|ttj|kr8tdttj| dttj| t	|| _
t	|| _tt|   d S )NzcExpected type of 'in_event_shape' and 'out_event_shape' is Squence[int], but got 'in_event_shape': z, 'out_event_shape': zCThe numel of 'in_event_shape' should be 'out_event_shape', but got z!=)r-   rQ   rR   r;   	functoolsreduceoperatormulr@   r~   _in_event_shape_out_event_shaper%   r   r&   )r(   in_event_shapeout_event_shaper)   r   r   r&   c  s.   

zReshapeTransform.__init__c                 C   rx   r$   )r   r'   r   r   r   r   x  rr   zReshapeTransform.in_event_shapec                 C   rx   r$   )r   r'   r   r   r   r   |  rr   z ReshapeTransform.out_event_shapec                 C      t t jt| jS r$   )r   r   r\   lenr   r'   r   r   r   r>        zReshapeTransform._domainc                 C   r   r$   )r   r   r\   r   r   r'   r   r   r   rE     r   zReshapeTransform._codomainc                 C   ,   | t|jd | t| j  | j S r$   )reshaper~   rU   r=   r   r   r   rB   r   r   r   rA     
   zReshapeTransform._forwardc                 C   r   r$   )r   r~   rU   r=   r   r   r   rG   r   r   r   rF     r   zReshapeTransform._inversec                 C      t |t | jk rtdt | j dt | |t | j d  | jkr8td| j d|t | j d   t|d t | j  | j S )Nz,Expected length of 'shape' is not less than 
, but got  Event shape mismatch, expected: )r   r   r@   r~   r   rT   r   r   r   rS         zReshapeTransform._forward_shapec                 C   r   )Nz)Expected 'shape' length is not less than r   r   )r   r   r@   r~   r   rT   r   r   r   rX     r   zReshapeTransform._inverse_shapec                 C   s2   |j d | t| j  pdg}tj||jdS rm   )rU   r=   r   r   r7   rp   ro   )r(   r0   rU   r   r   r   ra     s   "z*ReshapeTransform._forward_log_det_jacobian)r   r   r   r   r   r   r   r&   rg   r   r   r>   rE   rA   rF   rS   rX   ra   rh   r   r   r)   r   r   @  s"     



r   c                   @   s@   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(gpu:0), 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(gpu:0), 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(gpu:0), stop_gradient=True,
            #        [[-1.62652326, -1.62652326, -1.62652326],
            #         [-1.62652326, -1.62652326, -1.62652326]])
    c                 C   rZ   r$   r[   r'   r   r   r   r>     rr   zSigmoidTransform._domainc                 C      t ddtddS )NFr   r         ?r   r:   r   Ranger'   r   r   r   rE        zSigmoidTransform._codomainc                 C   s
   t |S r$   )FsigmoidrB   r   r   r   rA     rl   zSigmoidTransform._forwardc                 C   s   |  |   S r$   )r{   log1prG   r   r   r   rF     r+   zSigmoidTransform._inversec                 C   s   t |  t | S r$   )r   softplusrB   r   r   r   ra     s   z*SigmoidTransform._forward_log_det_jacobianN)
r   r   r   r   rg   r>   rE   rA   rF   ra   r   r   r   r   r     s    

r   c                   @   sN   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(gpu:0), 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(gpu:0), stop_gradient=True,
            #        [[-1.09861231, -1.09861231, -1.09861231],
            #         [-1.09861231, -1.09861231, -1.09861231]])
    c                 C      t t jdS r   r   r   r\   r'   r   r   r   r>        zSoftmaxTransform._domainc                 C      t ddtjS NFrn   r   r:   r   Zsimplexr'   r   r   r   rE        zSoftmaxTransform._codomainc                 C   s,   ||j dddd   }||jddd S )Nr   T)Zkeepdimr   )r   r   r   rB   r   r   r   rA     s   zSoftmaxTransform._forwardc                 C   ri   r$   r   rG   r   r   r   rF     rk   zSoftmaxTransform._inversec                 C   "   t |dk rtdt | |S Nrn   z3Expected length of shape is grater than 1, but got r   r@   rT   r   r   r   rS     
   zSoftmaxTransform._forward_shapec                 C   r   r   r   rT   r   r   r   rX     r   zSoftmaxTransform._inverse_shapeN)r   r   r   r   r   r!   r   rg   r>   rE   rA   rF   rS   rX   r   r   r   r   r     s    

r   c                   @   sr   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(gpu:0), 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(gpu:0), stop_gradient=True,
            #        [[1., 1.],
            #         [2., 2.],
            #         [3., 3.]])

            print(t.forward_log_det_jacobian(x))
            # Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[1.        , 0.69314718],
            #         [2.        , 1.38629436],
            #         [3.        , 1.79175949]])
    r   c                 C   sl   |rt |tjstdt| dtdd |D stdt |ts.tdt| d|| _|| _d S )Nz6Expected 'transforms' is Sequence[Transform], but got r4   c                 s   r   r$   r   r   r   r   r   r   E  r   z*StackTransform.__init__.<locals>.<genexpr>z5Expected all element in transforms is Transform Type.zExpected 'axis' is int, but got)	r-   rQ   rR   r;   r<   r   int_transforms_axis)r(   r   axisr   r   r   r&   @  s   

zStackTransform.__init__c                 C   r   )Nc                 s   r   r$   r   r   r   r   r   r   P  r   z/StackTransform._is_injective.<locals>.<genexpr>)r   r   r'   r   r   r   r,   O  r   zStackTransform._is_injectivec                 C   rx   r$   )r   r'   r   r   r   r   R  rr   zStackTransform.transformsc                 C   rx   r$   )r   r'   r   r   r   r   V  rr   zStackTransform.axisc                 C   4   |  | tdd tt|| j| jD | jS )Nc                 S      g | ]	\}}| |qS r   )r/   r   vr   r   r   r   
<listcomp>]      z+StackTransform._forward.<locals>.<listcomp>_check_sizer7   stackzipZunstackr   r   rB   r   r   r   rA   Z     
zStackTransform._forwardc                 C   r   )Nc                 S   r   r   )rI   r   r   r   r   r   g  r   z+StackTransform._inverse.<locals>.<listcomp>r   rG   r   r   r   rF   d  r   zStackTransform._inversec                 C   r   )Nc                 S   r   r   )rL   r   r   r   r   r   q  r   z<StackTransform._forward_log_det_jacobian.<locals>.<listcomp>r   rB   r   r   r   ra   n  r   z(StackTransform._forward_log_det_jacobianc                 C   sj   |   | j  kr|  k sn td|   d| j d|j| j t| jkr3td| j dd S )NzInput dimensions z, should be grater than stack transform axis r4   zInput size along z- should be equal to the length of transforms.)r=   r   r@   rU   r   r   )r(   r   r   r   r   r   x  s   "zStackTransform._check_sizec                 C      t dd | jD | jS )Nc                 S      g | ]}|j qS r   )r>   r   r   r   r   r         z*StackTransform._domain.<locals>.<listcomp>r   Stackr   r   r'   r   r   r   r>     s   zStackTransform._domainc                 C   r   )Nc                 S   r   r   )rE   r   r   r   r   r     r   z,StackTransform._codomain.<locals>.<listcomp>r   r'   r   r   r   rE     s   zStackTransform._codomainN)r   )r   r   r   r   r&   r,   rg   r   r   rA   rF   ra   r   r>   rE   r   r   r   r   r     s     
(





r   c                   @   sV   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   aV  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(gpu:0), stop_gradient=True,
            #        [0.47536686, 0.41287899, 0.10645414, 0.00530004])
            print(t.inverse(t.forward(x)))
            # Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0.99999988, 2.        , 2.99999881])
            print(t.forward_log_det_jacobian(x))
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [-9.10835075])
    c                 C   s   |j d d t|j d gd }t||  }d| d}tj|dgd t	|j d  ddg ddtj|dgd t	|j d  ddg dd S )Nr   rn   r      )r   )
rU   r7   onescumsumr   r   r{   Zcumprodpadr   )r(   r0   offsetzZ	z_cumprodr   r   r   rA     s   &."zStickBreakingTransform._forwardc                 C   s\   |dd df }|j d t|j d gd }d|d }| |  |  }|S )N.r   rn   )rU   r7   r   r   r{   )r(   rH   Zy_cropr   Zsfr0   r   r   r   rF     s
   "zStickBreakingTransform._inversec                 C   sf   |  |}|jd d t|jd gd }||  }| t| |dd df   dS )Nr   rn   .)	r/   rU   r7   r   r   r{   r   Zlog_sigmoidr   )r(   r0   rH   r   r   r   r   ra     s   
&*z0StickBreakingTransform._forward_log_det_jacobianc                 C   s,   |s	t d| |d d |d d f S Nz'Expected 'shape' is not empty, but got r   rn   r@   rT   r   r   r   rS        z%StickBreakingTransform._forward_shapec                 C   s,   |s	t d| |d d |d d f S r   r   rT   r   r   r   rX     r   z%StickBreakingTransform._inverse_shapec                 C   r   r   r   r'   r   r   r   r>     r   zStickBreakingTransform._domainc                 C   r   r   r   r'   r   r   r   rE     r   z StickBreakingTransform._codomainN)r   r   r   r   r   r   r   rA   rF   ra   rS   rX   rg   r>   rE   r   r   r   r   r     s    
r   c                   @   sF   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   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.]])

            print(tanh.forward(x))
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[0.76159418, 0.96402758, 0.99505478],
            #         [0.99932933, 0.99990922, 0.99998772]])
            print(tanh.inverse(tanh.forward(x)))
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[1.00000012, 2.        , 3.00000286],
            #         [4.00002146, 5.00009823, 6.00039864]])
            print(tanh.forward_log_det_jacobian(x))
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[-0.86756170 , -2.65000558 , -4.61865711 ],
            #         [-6.61437654 , -8.61379623 , -10.61371803]])
            print(tanh.inverse_log_det_jacobian(tanh.forward(x)))
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[0.86756176 , 2.65000558 , 4.61866283 ],
            #         [6.61441946 , 8.61399269 , 10.61451530]])
    c                 C   rZ   r$   r[   r'   r   r   r   r>     rr   zTanhTransform._domainc                 C   r   )NFr   g      r   r   r'   r   r   r   rE     r   zTanhTransform._codomainc                 C   ri   r$   )tanhrB   r   r   r   rA     rk   zTanhTransform._forwardc                 C   ri   r$   )atanhrG   r   r   r   rF     rk   zTanhTransform._inversec                 C   s    dt d| td|   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{   r   r   rB   r   r   r   ra     s    z'TanhTransform._forward_log_det_jacobianN)r   r   r   r   r   r   r   rg   r>   rE   rA   rF   ra   r   r   r   r   r     s    

r   )!enumr   r   numbersr   rQ   r7   Zpaddle.nn.functionalnnZ
functionalr   Zpaddle.distributionr   r   r   r   __all__Enumr   objectr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s4     HR}1_@t+8xB