o
    Me(                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZ d dlmZmZmZ d dlmZmZmZmZmZmZmZmZ d dlmZmZm Z m!Z! G d	d
 d
ej"Z#dS )    N)_C_ops_legacy_C_ops)distribution)core)check_dtype
check_typecheck_variable_and_dtypeconvert_dtype)_non_static_modein_dygraph_mode_in_legacy_dygraph)control_flowelementwise_addelementwise_divelementwise_mulelementwise_subnnopstensor)arangeconcat	gather_ndmultinomialc                   @   s<   e Zd ZdZdddZdddZdd	 Zd
d Zdd ZdS )Uniforma  Uniform distribution with `low` and `high` parameters.

    Mathematical Details

    The probability density function (pdf) is

    .. math::

        pdf(x; a, b) = \frac{1}{Z}, \ a <=x <b

    .. math::

        Z = b - a

    In the above equation:

    * :math:`low = a`,
    * :math:`high = b`,
    * :math:`Z`: is the normalizing constant.

    The parameters `low` and `high` must be shaped in a way that supports
    :ref:`user_guide_broadcasting` (e.g., `high - low` is a valid operation).

    Args:
        low(int|float|list|tuple|numpy.ndarray|Tensor): The lower boundary of
            uniform distribution.The data type is float32 and float64.
        high(int|float|list|tuple|numpy.ndarray|Tensor): The higher boundary
            of uniform distribution.The data type is float32 and float64.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Examples:
        .. code-block:: python

            import paddle
            from paddle.distribution import Uniform

            # Without broadcasting, a single uniform distribution [3, 4]:
            u1 = Uniform(low=3.0, high=4.0)
            # 2 distributions [1, 3], [2, 4]
            u2 = Uniform(low=[1.0, 2.0], high=[3.0, 4.0])
            # 4 distributions
            u3 = Uniform(low=[[1.0, 2.0], [3.0, 4.0]],
                        high=[[1.5, 2.5], [3.5, 4.5]])

            # With broadcasting:
            u4 = Uniform(low=3.0, high=[5.0, 6.0, 7.0])

            # Complete example
            value_tensor = paddle.to_tensor([0.8], dtype="float32")

            uniform = Uniform([0.], [2.])

            sample = uniform.sample([2])
            # a random tensor created by uniform distribution with shape: [2, 1]
            entropy = uniform.entropy()
            # [0.6931472] with shape: [1]
            lp = uniform.log_prob(value_tensor)
            # [-0.6931472] with shape: [1]
            p = uniform.probs(value_tensor)
            # [0.5] with shape: [1]
    Nc              	   C   sn  t  s!t|dtttjtjtt	fd t|dtttjtjtt	fd d| _
d| _|d ur-|nd| _d| _t|tr<t|}t|trEt|}| ||r\d| _|| _|| _t|j| _d S t|trit|trid| _
t|tjr{t|jdv r{|j| _nt|tjrt|jdv r|j| _| ||\| _| _| jt| jjkrtj| j| jd| _tj| j| jd| _d S d S )	Nlowr   highFfloat32T)r   Zfloat64dtype)r
   r   intfloatnpZndarrayr   Variablelisttupleall_arg_is_floatbatch_size_unknownnamer   
isinstanceZ_validate_argsr   r   r	   strZ
_to_tensorcast)selfr   r   r'    r,   KD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distribution/uniform.py__init__m   sJ   


zUniform.__init__r   c                 C   s(  t  st|dtd t|dtd | jd }t| j| j j}| jrb|| }t	
| j| j || | jd}tj||j| jdd|d}t||}t||}	|	|| j | j  }
t|
| j|d}
|
S || }tj|| jdd|dt	j|| jd	| j| j   }
t|
| j|d}
| jrtj|
||dS |
S )
a   Generate samples of the specified shape.

        Args:
            shape (list): 1D `int32`. Shape of the generated samples.
            seed (int): Python integer number.

        Returns:
            Tensor, A tensor with prepended dimensions shape. The data type is float32.

        shapesampleseedZ_sampleg        g      ?)r   minmaxr1   r'   r   )r
   r   r#   r   r'   r   r   r/   r&   r   Zfill_constant_batch_size_liker   r   Zuniform_random_batch_size_likeZreshaper   Zuniform_randomZzerosr%   )r+   r/   r1   r'   Zbatch_shapeZoutput_shapeZzero_tmpZuniform_random_tmpZzero_tmp_reshapeZuniform_random_tmp_reshapeoutputr,   r,   r-   r0      sN   

zUniform.samplec                 C   s   |  | j|}t r^| j|k }|| jk }t r5t||j}t||j}t	|| t	| j| j  S t
 r^t|d|jd|j}t|d|jd|j}t	|| t	| j| j  S | jd }| j|k }|| jk }tj||jd}tj||jd}tt	|| t	| j| j |dS )zLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor, log probability.The data type is same with value.

        in_dtype	out_dtypeZ	_log_probr   r4   )_check_values_dtype_in_probsr   r
   r   r   r   r*   r   r   logr   r   r'   r   r   r+   valueZlb_boolZub_boolZlbZubr'   r,   r,   r-   log_prob   s0   


  


zUniform.log_probc                 C   s   |  | j|}t rR| j|k }|| jk }t r/t||j}t||j}|| | j| j  S t rRt	|d|jd|j}t	|d|jd|j}|| | j| j  S | j
d }| j|k }|| jk }tj||jd}tj||jd}t|| | j| j |dS )zProbability density/mass function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor, probability. The data type is same with value.

        r6   r7   Z_probsr   r4   )r8   r   r
   r   r   r   r*   r   r   r   r'   r   r   r:   r,   r,   r-   probs   s,   





zUniform.probsc                 C   s    | j d }tj| j| j |dS )zShannon entropy in nats.

        The entropy is

        .. math::

            entropy(low, high) = \\log (high - low)

        Returns:
            Tensor, Shannon entropy of uniform distribution.The data type is float32.

        Z_entropyr4   )r'   r   r9   r   r   )r+   r'   r,   r,   r-   entropy  s   
zUniform.entropy)N)r   )	__name__
__module____qualname____doc__r.   r0   r<   r=   r>   r,   r,   r,   r-   r   .   s    
>
15'$r   )$mathwarningsnumpyr!   Zpaddler   r   Zpaddle.distributionr   Zpaddle.fluidr   Zpaddle.fluid.data_feederr   r   r   r	   Zpaddle.fluid.frameworkr
   r   r   Zpaddle.fluid.layersr   r   r   r   r   r   r   r   Zpaddle.tensorr   r   r   r   Distributionr   r,   r,   r,   r-   <module>   s   (
