3
O4W^  ã               @   sT   d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
 G dd„ deƒZdS )é    N)Únan)Úconstraints)ÚDistribution)Úprobs_to_logitsÚlogits_to_probsÚlazy_propertyc                   s¾   e Zd ZdZejejdœZdZd‡ fdd„	Z	d ‡ fdd„	Z
d	d
„ Zejdd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZejƒ fdd„Zdd„ Zdd„ Zd!dd„Z‡  ZS )"ÚCategoricalaÐ  
    Creates a categorical distribution parameterized by either :attr:`probs` or
    :attr:`logits` (but not both).

    .. note::
        It is equivalent to the distribution that :func:`torch.multinomial`
        samples from.

    Samples are integers from :math:`\{0, \ldots, K-1\}` where `K` is ``probs.size(-1)``.

    If :attr:`probs` is 1D with length-`K`, each element is the relative
    probability of sampling the class at that index.

    If :attr:`probs` is 2D, it is treated as a batch of relative probability
    vectors.

    .. note:: :attr:`probs` must be non-negative, finite and have a non-zero sum,
              and it will be normalized to sum to 1.

    See also: :func:`torch.multinomial`

    Example::

        >>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
        >>> m.sample()  # equal probability of 0, 1, 2, 3
        tensor(3)

    Args:
        probs (Tensor): event probabilities
        logits (Tensor): event log-odds
    )ÚprobsÚlogitsTNc                sØ   |d k|d kkrt dƒ‚|d k	rJ|jƒ dk r4t dƒ‚||jd	dd� | _n(|jƒ dk r^t dƒ‚||jd
dd� | _|d k	r€| jn| j| _| jjƒ d | _| jj	ƒ dkr¸| jjƒ d d… nt
jƒ }tt| ƒj||d� d S )Nz;Either `probs` or `logits` must be specified, but not both.é   z3`probs` parameter must be at least one-dimensional.T)Úkeepdimz4`logits` parameter must be at least one-dimensional.)Údimr   )Úvalidate_argséÿÿÿÿr   r   r   )Ú
ValueErrorr   Úsumr	   Z	logsumexpr
   Ú_paramÚsizeÚ_num_eventsZ
ndimensionÚtorchÚSizeÚsuperr   Ú__init__)Úselfr	   r
   r   Úbatch_shape)Ú	__class__© ú@/tmp/pip-build-e66j97b2/torch/torch/distributions/categorical.pyr   ,   s    (zCategorical.__init__c                s�   | j t|ƒ}tj|ƒ}|tj| jfƒ }d| jkrH| jj|ƒ|_|j|_d| jkrh| j	j|ƒ|_	|j	|_| j|_t
t|ƒj|dd� | j|_|S )Nr	   r
   F)r   )Z_get_checked_instancer   r   r   r   Ú__dict__r	   Úexpandr   r
   r   r   Ú_validate_args)r   r   Z	_instanceÚnewÚparam_shape)r   r   r   r   <   s    


zCategorical.expandc             O   s   | j j||ŽS )N)r   r!   )r   ÚargsÚkwargsr   r   r   Ú_newK   s    zCategorical._newc             C   s   t jd| jd ƒS )Nr   r   )r   Zinteger_intervalr   )r   r   r   r   ÚsupportN   s    zCategorical.supportc             C   s
   t | jƒS )N)r   r	   )r   r   r   r   r
   R   s    zCategorical.logitsc             C   s
   t | jƒS )N)r   r
   )r   r   r   r   r	   V   s    zCategorical.probsc             C   s
   | j jƒ S )N)r   r   )r   r   r   r   r"   Z   s    zCategorical.param_shapec             C   s   t j| jƒ t| jj| jjd�S )N)ÚdtypeÚdevice)r   ÚfullÚ_extended_shaper   r	   r'   r(   )r   r   r   r   Úmean^   s    zCategorical.meanc             C   s   t j| jƒ t| jj| jjd�S )N)r'   r(   )r   r)   r*   r   r	   r'   r(   )r   r   r   r   Úvarianceb   s    zCategorical.variancec             C   sN   | j |ƒ}|tj| jfƒ }| jj|ƒ}|jd| jƒ}tj|ddƒ}|j|ƒS )Nr   Tr   )r*   r   r   r   r	   r   ZreshapeZmultinomial)r   Zsample_shaper"   r	   Zprobs_2dZ	sample_2dr   r   r   Úsamplef   s    
zCategorical.samplec             C   sR   | j r| j|ƒ |jƒ jdƒ}tj|| jƒ\}}|dd d…f }|jd|ƒjdƒS )Nr   .r   r   r   )	r    Z_validate_sampleÚlongZ	unsqueezer   Zbroadcast_tensorsr
   ZgatherZsqueeze)r   ÚvalueZlog_pmfr   r   r   Úlog_probn   s    
zCategorical.log_probc             C   s   | j | j }|jdƒ S )Nr   r   )r
   r	   r   )r   Zp_log_pr   r   r   Úentropyv   s    zCategorical.entropyc             C   sL   | j }tj|tj| jjd�}|jddt| jƒ  ƒ}|rH|j	d| j ƒ}|S )N)r'   r(   r   r   )r   )r   r   )r   )
r   r   Zaranger.   r   r(   ÚviewÚlenZ_batch_shaper   )r   r   Z
num_eventsÚvaluesr   r   r   Úenumerate_supportz   s    zCategorical.enumerate_support)NNN)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZsimplexÚrealZarg_constraintsZhas_enumerate_supportr   r   r%   Zdependent_propertyr&   r   r
   r	   Úpropertyr"   r+   r,   r   r   r-   r0   r1   r5   Ú__classcell__r   r   )r   r   r      s"   
r   )r   Z
torch._sixr   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   r   r   r   r   r   Ú<module>   s
   