o
    * 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 er$d dlm	Z	 G dd dej
ZdS )	    )annotations)TYPE_CHECKINGN)distribution)in_dynamic_mode)Tensorc                   @  s:   e Zd ZdZedd Zdd Zedd Zdd
dZdS )ExponentialFamilya  
    ExponentialFamily is the base class for probability distributions belonging
    to exponential family, whose probability mass/density function has the
    form is defined below

    ExponentialFamily is derived from `paddle.distribution.Distribution`.

    .. math::

        f_{F}(x; \theta) = \exp(\langle t(x), \theta\rangle - F(\theta) + k(x))

    where :math:`\theta` denotes the natural parameters, :math:`t(x)` denotes
    the sufficient statistic, :math:`F(\theta)` is the log normalizer function
    for a given family and :math:`k(x)` is the carrier measure.

    Distribution belongs to exponential family referring to https://en.wikipedia.org/wiki/Exponential_family
    c                 C     t NNotImplementedErrorself r   r/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/exponential_family.py_natural_parameters.      z%ExponentialFamily._natural_parametersc                 C  r   r	   r
   r   r   r   r   _log_normalizer2   s   z!ExponentialFamily._log_normalizerc                 C  r   r	   r
   r   r   r   r   _mean_carrier_measure5   r   z'ExponentialFamily._mean_carrier_measurereturnr   c                 C  s   | j  }g }| jD ]}| }d|_|| q	| j| }t r+tj|	 |dd}n	tj
|	 |}||7 }t||D ]
\}}||| 8 }q=|S )z|calculate entropy use `bregman divergence`
        https://www.lix.polytechnique.fr/~nielsen/EntropyEF-ICIP2010.pdf
        FT)Zcreate_graph)r   r   detachZstop_gradientappendr   r   paddleZgradsumZstaticZ	gradientszip)r   Zentropy_valueZnatural_parametersZ	parameterZlog_normZgradspgr   r   r   entropy9   s    


zExponentialFamily.entropyN)r   r   )	__name__
__module____qualname____doc__propertyr   r   r   r   r   r   r   r   r      s    

r   )
__future__r   typingr   r   Zpaddle.distributionr   Zpaddle.frameworkr   r   Distributionr   r   r   r   r   <module>   s   