o
    pi                     @  s   d dl mZ d dlm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 d dlmZ er@d d	lmZ d d
lmZmZ G dd dejZdS )    )annotations)TYPE_CHECKINGN)distribution)
check_typeconvert_dtype)Variable)exponential_family)in_dynamic_mode)Sequence)Tensordtypec                      s   e Zd ZU dZded< ded< ded< d% fd	d
Zed&ddZed&ddZd'ddZ	d'ddZ
d&ddZg fd(ddZg fd(ddZd)ddZd&dd Zd*d#d$Z  ZS )+Gammaa  
    Gamma distribution parameterized by :attr:`concentration` (aka "alpha") and :attr:`rate` (aka "beta").

    The probability density function (pdf) is

    .. math::

        f(x; \alpha, \beta, x > 0) = \frac{\beta^{\alpha}}{\Gamma(\alpha)} x^{\alpha-1}e^{-\beta x}

        \Gamma(\alpha)=\int_{0}^{\infty} x^{\alpha-1} e^{-x} \mathrm{~d} x, (\alpha>0)

    Args:
        concentration (float|Tensor): Concentration parameter. It supports broadcast semantics.
            The value of concentration must be positive. When the parameter is a tensor,
            it represents multiple independent distribution with
            a batch_shape(refer to :ref:`api_paddle_distribution_Distribution`).
        rate (float|Tensor): Rate parameter. It supports broadcast semantics.
            The value of rate must be positive. When the parameter is tensor,
            it represent multiple independent distribution with
            a batch_shape(refer to :ref:`api_paddle_distribution_Distribution`).

    Example:
        .. code-block:: python

            >>> import paddle

            >>> # scale input
            >>> gamma = paddle.distribution.Gamma(0.5, 0.5)
            >>> print(gamma.mean)
            Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   1.)

            >>> print(gamma.variance)
            Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   2.)

            >>> print(gamma.entropy())
            Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   0.78375685)

            >>> # tensor input with broadcast
            >>> gamma = paddle.distribution.Gamma(paddle.to_tensor([0.2, 0.4]), paddle.to_tensor(0.6))
            >>> print(gamma.mean)
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [0.33333331, 0.66666663])

            >>> print(gamma.variance)
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [0.55555552, 1.11111104])

            >>> print(gamma.entropy())
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [-1.99634242,  0.17067254])
    r   concentrationrater   float | TensorreturnNonec                   s   t  st|dtttjjfd t|dtttjjfd | ||r.|| _|| _	t
|j| _n| ||\| _| _	t | _t | jj d S )Nr   r   r   )r	   r   floatr   paddleZpirValueZ_validate_argsr   r   r   r   Z
_to_tensorZget_default_dtypesuper__init__shape)selfr   r   	__class__ `/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/gamma.pyr   \   s,   
zGamma.__init__c                 C  s   | j | j S )zVMean of gamma distribution.

        Returns:
            Tensor: mean value.
        r   r   r   r   r   r   meanz   s   z
Gamma.meanc                 C  s   | j | jd S )z^Variance of gamma distribution.

        Returns:
            Tensor: variance value.
           )r   r   powr   r   r   r   variance   s   zGamma.variancevaluec                 C  s   t | |S )zProbability density function evaluated at value

        Args:
            value (float|Tensor): Value to be evaluated.

        Returns:
            Tensor: Probability.
        )r   explog_probr   r$   r   r   r   prob   s   	z
Gamma.probc                 C  s<   | j t| j | j d t|  | j|  t| j  S )zLog probability density function evaluated at value

        Args:
            value (float|Tensor): Value to be evaluated

        Returns:
            Tensor: Log probability.
           )r   r   logr   lgammar'   r   r   r   r&      s   

zGamma.log_probc                 C  s4   | j t| j t| j  d| j  t| j   S )zUEntropy of gamma distribution

        Returns:
            Tensor: Entropy.
        g      ?)r   r   r*   r   r+   digammar   r   r   r   entropy   s   

zGamma.entropyr   Sequence[int]c                 C  s6   t   | |W  d   S 1 sw   Y  dS )zGenerate samples of the specified shape.

        Args:
            shape (Sequence[int], optional): Shape of the generated samples.

        Returns:
            Tensor, A tensor with prepended dimensions shape.The data type is float32.
        N)r   Zno_gradrsampler   r   r   r   r   sample   s   
	$zGamma.samplec                 C  s.   t jj| |d}t| j|| j| S )a  Generate reparameterized samples of the specified shape.

        Args:
            shape (Sequence[int], optional): Shape of the generated samples.

        Returns:
            Tensor: A tensor with prepended dimensions shape.The data type is float32.
        )Zsample_shape)r   DistributionZ_extend_shaper   Zstandard_gammar   expandr   r0   r   r   r   r/      s   	

zGamma.rsampleotherc                 C  s   t |tstdt| |jt| j|j  }t|jt| j }| j|j t	| j }|j| j | j| j  }|| | | S )zThe KL-divergence between two gamma distributions.

        Args:
            other (Gamma): instance of Gamma.

        Returns:
            Tensor: kl-divergence between two gamma distributions.
        z/Expected type of other is Exponential, but got )

isinstancer   	TypeErrortyper   r   r*   r   r+   r,   )r   r4   t1t2t3Zt4r   r   r   kl_divergence   s   
	zGamma.kl_divergencec                 C  s   | j d | j fS Nr)   r   r   r   r   r   _natural_parameters   s   zGamma._natural_parametersxyc                 C  s&   t |d |d t |    S r<   )r   r+   r*   Z
reciprocal)r   r>   r?   r   r   r   _log_normalizer   s   &zGamma._log_normalizer)r   r   r   r   r   r   )r   r   )r$   r   r   r   )r   r.   r   r   )r4   r   r   r   )r>   r   r?   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr    r#   r(   r&   r-   r1   r/   r;   r=   r@   __classcell__r   r   r   r   r       s$   
 7




r   )
__future__r   typingr   r   r   Zpaddle.base.data_feederr   r   Zpaddle.base.frameworkr   Zpaddle.distributionr   Zpaddle.frameworkr	   collections.abcr
   r   r   ZExponentialFamilyr   r   r   r   r   <module>   s   