3
O4W^ë  ã               @   s(   d dl Z d dlmZ G dd„ deƒZdS )é    N)ÚDistributionc               @   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 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).
    c             C   s   t ‚dS )zv
        Abstract method for natural parameters. Returns a tuple of Tensors based
        on the distribution
        N)ÚNotImplementedError)Úself© r   ú?/tmp/pip-build-e66j97b2/torch/torch/distributions/exp_family.pyÚ_natural_params   s    z!ExponentialFamily._natural_paramsc             G   s   t ‚dS )zƒ
        Abstract method for log normalizer function. Returns a log normalizer based on
        the distribution and input
        N)r   )r   Znatural_paramsr   r   r   Ú_log_normalizer"   s    z!ExponentialFamily._log_normalizerc             C   s   t ‚dS )zp
        Abstract method for expected carrier measure, which is required for computing
        entropy.
        N)r   )r   r   r   r   Ú_mean_carrier_measure)   s    z'ExponentialFamily._mean_carrier_measurec             C   sl   | j  }dd„ | jD ƒ}| j|Ž }tjj|jƒ |dd�}||jƒ 7 }x"t||ƒD ]\}}||| 8 }qPW |S )z_
        Method to compute the entropy using Bregman divergence of the log normalizer.
        c             S   s   g | ]}|j ƒ jƒ ‘qS r   )ÚdetachZrequires_grad_)Ú.0Úpr   r   r   ú
<listcomp>6   s    z-ExponentialFamily.entropy.<locals>.<listcomp>T)Zcreate_graph)	r
   r   r	   ÚtorchZautogradZgradÚsumÚcloneÚzip)r   ÚresultZnparamsZ	lg_normalZ	gradientsÚnpÚgr   r   r   Úentropy1   s    
zExponentialFamily.entropyN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr   r	   r
   r   r   r   r   r   r      s
   r   )r   Z torch.distributions.distributionr   r   r   r   r   r   Ú<module>   s   