3
O4W^³  ã               @   s@   d dl Z d dlmZ d dlmZ d dlmZ G dd„ deƒZdS )é    N)Úconstraints)ÚDistribution)Ú_sum_rightmostc                   s¬   e Zd ZdZi Zd‡ fdd„	Zd‡ fdd„	Zedd„ ƒZed	d
„ ƒZ	e
jdd„ ƒZedd„ ƒZedd„ ƒZejƒ fdd„Zejƒ fdd„Zdd„ Zdd„ Zddd„Z‡  ZS )ÚIndependenta  
    Reinterprets some of the batch dims of a distribution as event dims.

    This is mainly useful for changing the shape of the result of
    :meth:`log_prob`. For example to create a diagonal Normal distribution with
    the same shape as a Multivariate Normal distribution (so they are
    interchangeable), you can::

        >>> loc = torch.zeros(3)
        >>> scale = torch.ones(3)
        >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale))
        >>> [mvn.batch_shape, mvn.event_shape]
        [torch.Size(()), torch.Size((3,))]
        >>> normal = Normal(loc, scale)
        >>> [normal.batch_shape, normal.event_shape]
        [torch.Size((3,)), torch.Size(())]
        >>> diagn = Independent(normal, 1)
        >>> [diagn.batch_shape, diagn.event_shape]
        [torch.Size(()), torch.Size((3,))]

    Args:
        base_distribution (torch.distributions.distribution.Distribution): a
            base distribution
        reinterpreted_batch_ndims (int): the number of batch dims to
            reinterpret as event dims
    Nc                sŒ   |t |jƒkr$tdj|t |jƒƒƒ‚|j|j }|t |jƒ }|d t |ƒ| … }|t |ƒ| d … }|| _|| _tt| ƒj	|||d� d S )NzYExpected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), actual {} vs {})Úvalidate_args)
ÚlenÚbatch_shapeÚ
ValueErrorÚformatÚevent_shapeÚ	base_distÚreinterpreted_batch_ndimsÚsuperr   Ú__init__)ÚselfZbase_distributionr   r   ÚshapeZ	event_dimr   r   )Ú	__class__© ú@/tmp/pip-build-e66j97b2/torch/torch/distributions/independent.pyr   $   s    zIndependent.__init__c                s`   | j t|ƒ}tj|ƒ}| jj|| jd | j…  ƒ|_| j|_tt|ƒj	|| jdd� | j
|_
|S )NF)r   )Z_get_checked_instancer   ÚtorchÚSizer   Úexpandr   r   r   r   Z_validate_args)r   r   Z	_instanceÚnew)r   r   r   r   1   s    
zIndependent.expandc             C   s   | j jS )N)r   Úhas_rsample)r   r   r   r   r   ;   s    zIndependent.has_rsamplec             C   s   | j dkrdS | jjS )Nr   F)r   r   Úhas_enumerate_support)r   r   r   r   r   ?   s    
z!Independent.has_enumerate_supportc             C   s   | j jS )N)r   Úsupport)r   r   r   r   r   E   s    zIndependent.supportc             C   s   | j jS )N)r   Úmean)r   r   r   r   r   I   s    zIndependent.meanc             C   s   | j jS )N)r   Úvariance)r   r   r   r   r   M   s    zIndependent.variancec             C   s   | j j|ƒS )N)r   Úsample)r   Úsample_shaper   r   r   r   Q   s    zIndependent.samplec             C   s   | j j|ƒS )N)r   Úrsample)r   r   r   r   r   r    T   s    zIndependent.rsamplec             C   s   | j j|ƒ}t|| jƒS )N)r   Úlog_probr   r   )r   Úvaluer!   r   r   r   r!   W   s    zIndependent.log_probc             C   s   | j jƒ }t|| jƒS )N)r   Úentropyr   r   )r   r#   r   r   r   r#   [   s    
zIndependent.entropyTc             C   s    | j dkrtdƒ‚| jj|d�S )Nr   z5Enumeration over cartesian product is not implemented)r   )r   ÚNotImplementedErrorr   Úenumerate_support)r   r   r   r   r   r%   _   s    
zIndependent.enumerate_support)N)N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zarg_constraintsr   r   Úpropertyr   r   r   Zdependent_propertyr   r   r   r   r   r   r    r!   r#   r%   Ú__classcell__r   r   )r   r   r      s   
r   )r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   r   r   r   Ú<module>   s   