o
    "j
"                     @   s^   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZ G dd dZdS )    N)_C_ops)check_variable_and_dtypeconvert_dtype)Variable)in_dynamic_modec                       s   e Zd ZdZd( fdd	Zedd Zedd Zed	d
 Zedd Z	d)ddZ
d)ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd*d$d%Zd*d&d'Z  ZS )+Distributiona>  
    The abstract base class for probability distributions. Functions are
    implemented in specific distributions.

    Args:
        batch_shape(Sequence[int], optional):  independent, not identically
            distributed draws, aka a "collection" or "bunch" of distributions.
        event_shape(Sequence[int], optional): the shape of a single
            draw from the distribution; it may be dependent across dimensions.
            For scalar distributions, the event shape is []. For n-dimension
            multivariate distribution, the event shape is [n].
     c                    s>   t |tr|nt|| _t |tr|nt|| _t   d S )N)
isinstancetuple_batch_shape_event_shapesuper__init__)selfbatch_shapeevent_shape	__class__r   a/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distribution/distribution.pyr   /   s   zDistribution.__init__c                 C      | j S )zeReturns batch shape of distribution

        Returns:
            Sequence[int]: batch shape
        )r   r   r   r   r   r   =      zDistribution.batch_shapec                 C   r   )zeReturns event shape of distribution

        Returns:
            Sequence[int]: event shape
        )r   r   r   r   r   r   F   r   zDistribution.event_shapec                 C      t )zMean of distributionNotImplementedErrorr   r   r   r   meanO      zDistribution.meanc                 C   r   )zVariance of distributionr   r   r   r   r   varianceT   r   zDistribution.variancec                 C   r   )zSampling from the distribution.r   r   shaper   r   r   sampleY      zDistribution.samplec                 C   r   )zreparameterized sampler   r   r   r   r   rsample]   r!   zDistribution.rsamplec                 C   r   )z The entropy of the distribution.r   r   r   r   r   entropya   r!   zDistribution.entropyc                 C   r   )z7The KL-divergence between self distributions and other.r   )r   otherr   r   r   kl_divergencee   r!   zDistribution.kl_divergencec                 C   s   |  | S )zProbability density/mass function evaluated at value.

        Args:
            value (Tensor): value which will be evaluated
        )log_probexpr   valuer   r   r   probi   s   zDistribution.probc                 C   r   )z&Log probability density/mass function.r   r(   r   r   r   r&   q   r!   zDistribution.log_probc                 C   r   )zProbability density/mass function.

        Note:

            This method will be deprecated in the future, please use `prob`
            instead.
        r   r(   r   r   r   probsu   s   zDistribution.probsc                 C   s   t |t | j t | j S )zcompute shape of the sample

        Args:
            sample_shape (Tensor): sample shape

        Returns:
            Tensor: generated sample data shape
        )r
   r   r   )r   Zsample_shaper   r   r   _extend_shape   s   
zDistribution._extend_shapec                 G   s:   d}d}|D ]}t |trd}qd}q|r|rtd|S )z
        Argument validation for distribution args
        Args:
            value (float, list, numpy.ndarray, Tensor)
        Raises
            ValueError: if one argument is Tensor, all arguments should be Tensor
        FTz9if one argument is Tensor, all arguments should be Tensor)r	   r   
ValueError)r   argsZis_variableZ	is_numberargr   r   r   _validate_args   s   
zDistribution._validate_argsc                 G   s   g }g }d}|D ]=}t |ttttjtfstdt	|t
|}|j}t|dkr<t|dkr7td |d}|| }|| q|j}|D ]}t||\}	}
tjj|d}t|	| || qKt|S )z
        Argument convert args to Tensor

        Args:
            value (float, list, numpy.ndarray, Tensor)
        Returns:
            Tensor of args.
        g        z\Type of input args must be float, list, tuple, numpy.ndarray or Tensor, but received type {}float32float64zadata type of argument only support float32 and float64, your argument will be convert to float32.dtype)r	   floatlistr
   npZndarrayr   	TypeErrorformattypearrayr4   strwarningswarnZastypeappendZbroadcast_arrayspaddleZtensorZcreate_tensorZassign)r   r.   Z
numpy_argsZvariable_argstmpr/   Zarg_npZ	arg_dtyper4   Zarg_broadcasted_Zarg_variabler   r   r   
_to_tensor   s6   	

zDistribution._to_tensorc                 C   sx   t  r|j|jkrt|jdv rtd t||jS |S t|dddgd |j|jkr:td tj||jdS |S )a  
        Log_prob and probs methods have input ``value``, if value's dtype is different from param,
        convert value's dtype to be consistent with param's dtype.

        Args:
            param (Tensor): low and high in Uniform class, loc and scale in Normal class.
            value (Tensor): The input tensor.

        Returns:
            value (Tensor): Change value's dtype if value's dtype is different from param.
        )r1   r2   ztdtype of input 'value' needs to be the same as parameters of distribution class. dtype of 'value' will be converted.r)   r1   r2   r&   r3   )	r   r4   r   r=   r>   r   castr   r@   )r   paramr)   r   r   r   _check_values_dtype_in_probs   s    z)Distribution._check_values_dtype_in_probsFc                 C   s$   |rt |t |  S t |S )a  
        Converts probabilities into logits. For the binary, probs denotes the
        probability of occurrence of the event indexed by `1`. For the
        multi-dimensional, values of last axis denote the probabilities of
        occurrence of each of the events.
        )r@   loglog1p)r   r+   	is_binaryr   r   r   _probs_to_logits   s
   	zDistribution._probs_to_logitsc                 C   s$   |r	t jj|S t jjj|ddS )z
        Converts logits into probabilities. For the binary, each value denotes
        log odds, whereas for the multi-dimensional case, the values along the
        last dimension denote the log probabilities of the events.
        )Zaxis)r@   nnZ
functionalZsigmoidZsoftmax)r   ZlogitsrI   r   r   r   _logits_to_probs   s
   zDistribution._logits_to_probs)r   r   )r   )F)__name__
__module____qualname____doc__r   propertyr   r   r   r   r    r"   r#   r%   r*   r&   r+   r,   r0   rC   rF   rJ   rM   __classcell__r   r   r   r   r   !   s0    






,
!r   )r=   numpyr7   r@   r   Zpaddle.base.data_feederr   r   Zpaddle.base.frameworkr   Zpaddle.frameworkr   r   r   r   r   r   <module>   s   