o
    h	                     @   s:   d dl Z d dl mZ d dlmZ dgZG dd deZdS )    N)Tensor)DistributionExponentialFamilyc                   @   sL   e Zd ZdZedeedf fddZdd Zede	fdd	Z
d
d ZdS )r   a  
    ExponentialFamily is the abstract base class for probability distributions belonging to an
    exponential family, whose probability mass/density function has the form is defined below

    .. math::

        p_{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.

    Note:
        This class is an intermediary between the `Distribution` class and distributions which belong
        to an exponential family mainly to check the correctness of the `.entropy()` and analytic KL
        divergence methods. We use this class to compute the entropy and KL divergence using the AD
        framework and Bregman divergences (courtesy of: Frank Nielsen and Richard Nock, Entropies and
        Cross-entropies of Exponential Families).
    return.c                 C      t )zv
        Abstract method for natural parameters. Returns a tuple of Tensors based
        on the distribution
        NotImplementedErrorself r   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/distributions/exp_family.py_natural_params      z!ExponentialFamily._natural_paramsc                 G   r   )z
        Abstract method for log normalizer function. Returns a log normalizer based on
        the distribution and input
        r   )r
   natural_paramsr   r   r   _log_normalizer'   s   z!ExponentialFamily._log_normalizerc                 C   r   )zp
        Abstract method for expected carrier measure, which is required for computing
        entropy.
        r   r	   r   r   r   _mean_carrier_measure.   r   z'ExponentialFamily._mean_carrier_measurec                 C   sv   | j  }dd | jD }| j| }tjj| |dd}||7 }t||D ]\}}||| | j	d d8 }q%|S )z_
        Method to compute the entropy using Bregman divergence of the log normalizer.
        c                 S   s   g | ]}|   qS r   )detachrequires_grad_).0pr   r   r   
<listcomp>;   s    z-ExponentialFamily.entropy.<locals>.<listcomp>T)create_graph)r   )
r   r   r   torchautogradgradsumzipreshape_batch_shape)r
   resultnparams	lg_normal	gradientsnpgr   r   r   entropy6   s   
 zExponentialFamily.entropyN)__name__
__module____qualname____doc__propertytupler   r   r   floatr   r&   r   r   r   r   r   
   s    )r   r    torch.distributions.distributionr   __all__r   r   r   r   r   <module>   s
   