a
    –¥»a	  ã                   @   sL   d dl Z d dlm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_rightmost)ÚDictc                       sÈ   e Zd ZU dZi Zeeejf e	d< 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 ¡ fdd„Ze ¡ fdd„Zdd„ Zdd„ Zd!dd„Z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
    Úarg_constraintsNc                    sŒ   |t |jƒkr$td |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__© úY/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/distributions/independent.pyr   $   s    þzIndependent.__init__c                    s`   |   t|¡}t |¡}| j || jd | j…  ¡|_| j|_tt|ƒj	|| jdd� | j
|_
|S )NFr   )Z_get_checked_instancer   ÚtorchÚSizer   Úexpandr   r   r   r   Z_validate_args)r   r   Z	_instanceÚnewr   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_supportr   r   r   r   r    ?   s    
z!Independent.has_enumerate_supportc                 C   s    | j j}| jrt || j¡}|S r   )r   Úsupportr   r   Zindependent)r   Úresultr   r   r   r!   E   s    zIndependent.supportc                 C   s   | j jS r   )r   Úmeanr   r   r   r   r#   L   s    zIndependent.meanc                 C   s   | j jS r   )r   Úvariancer   r   r   r   r$   P   s    zIndependent.variancec                 C   s   | j  |¡S r   )r   Úsample©r   Zsample_shaper   r   r   r%   T   s    zIndependent.samplec                 C   s   | j  |¡S r   )r   Úrsampler&   r   r   r   r'   W   s    zIndependent.rsamplec                 C   s   | j  |¡}t|| jƒS r   )r   Úlog_probr   r   )r   Úvaluer(   r   r   r   r(   Z   s    zIndependent.log_probc                 C   s   | j  ¡ }t|| jƒS r   )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,   b   s    
zIndependent.enumerate_supportc                 C   s   | j jd | j| j¡ S )Nz({}, {}))r   Ú__name__r   r   r   r   r   r   r   Ú__repr__g   s    zIndependent.__repr__)N)N)T)r-   Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   Z
ConstraintÚ__annotations__r   r   Úpropertyr   r    Zdependent_propertyr!   r#   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   Útypingr   r   r   r   r   r   Ú<module>   s
   