o
    Me
                     @   s:   d dl Z d dlmZ d dlmZmZ G dd dejZdS )    N)distribution)_non_static_modein_dygraph_modec                   @   s8   e Zd ZdZedd Zdd Zedd Z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   VD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distribution/exponential_family.py_natural_parameters'      z%ExponentialFamily._natural_parametersc                 C   r   r   r   r
   r   r   r   _log_normalizer+   s   z!ExponentialFamily._log_normalizerc                 C   r   r   r   r
   r   r   r   _mean_carrier_measure.   r   z'ExponentialFamily._mean_carrier_measurec                 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|caculate 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   entropy2   s"   


zExponentialFamily.entropyN)	__name__
__module____qualname____doc__propertyr   r   r   r   r   r   r   r   r      s    

r   )r   Zpaddle.distributionr   Zpaddle.fluid.frameworkr   r   Distributionr   r   r   r   r   <module>   s   