
    zj
                    t    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ej
                  ZdS )	    )annotations)TYPE_CHECKINGN)distribution)in_dynamic_mode)Tensorc                  L    e Zd ZdZed             Zd Ze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                    t           NNotImplementedErrorselfs    v/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/exponential_family.py_natural_parametersz%ExponentialFamily._natural_parameters.       !!    c                    t           r   r   r   s    r   _log_normalizerz!ExponentialFamily._log_normalizer2   s    !!r   c                    t           r   r   r   s    r   _mean_carrier_measurez'ExponentialFamily._mean_carrier_measure5   r   r   returnr   c                   | j          }g }| j        D ]2}|                                }d|_        |                    |           3 | j        | }t                      r*t          j        |	                                |d          }n2t          j
                            |	                                |          }||z  }t          ||          D ]\  }}|||z  z  }|S )z|calculate entropy use `bregman divergence`
        https://www.lix.polytechnique.fr/~nielsen/EntropyEF-ICIP2010.pdf
        FT)create_graph)r   r   detachstop_gradientappendr   r   paddlegradsumstatic	gradientszip)r   entropy_valuenatural_parameters	parameterlog_normgradspgs           r   entropyzExponentialFamily.entropy9   s     331 	1 	1I!((**I&+I#%%i0000'4');< 	PK 2  EE M++HLLNN<NOOE!*E22 	# 	#DAqQU"MMr   N)r   r   )	__name__
__module____qualname____doc__propertyr   r   r   r+    r   r   r	   r	      sw         $ " " X"" " " " " X"     r   r	   )
__future__r   typingr   r   paddle.distributionr   paddle.frameworkr   r   Distributionr	   r1   r   r   <module>r7      s    # " " " " "              , , , , , , , , , , , , 7 7 7 7 71 7 7 7 7 7r   