U
    ©Ê¹jý  ã                   @   sR   d dl mZ d dlZd dlmZ d dlmZ d dlmZ dgZ	G dd„ deƒZ
dS )é    )ÚDictN)Úconstraints)ÚDistribution)Ú_sum_rightmostÚIndependentc                       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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 )$r   a©  
    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::

        >>> from torch.distributions.multivariate_normal import MultivariateNormal
        >>> from torch.distributions.normal import Normal
        >>> 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|› dt |jƒ› �ƒ‚|j|j }|t |jƒ }|d t |ƒ| … }|t |ƒ| d … }|| _|| _tƒ j|||d� d S )NzQExpected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), actual z vs ©Úvalidate_args)ÚlenÚbatch_shapeÚ
ValueErrorÚevent_shapeÚ	base_distÚreinterpreted_batch_ndimsÚsuperÚ__init__)ÚselfZbase_distributionr   r	   ÚshapeZ	event_dimr   r   ©Ú	__class__© ú^/var/www/html/bwave/python/venv/lib/python3.8/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   :   s    
ÿ
  ÿzIndependent.expandc                 C   s   | j jS ©N)r   Úhas_rsample©r   r   r   r   r   G   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   K   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    Q   s    zIndependent.supportc                 C   s   | j jS r   )r   Úmeanr   r   r   r   r"   X   s    zIndependent.meanc                 C   s   | j jS r   )r   Úmoder   r   r   r   r#   \   s    zIndependent.modec                 C   s   | j jS r   )r   Úvariancer   r   r   r   r$   `   s    zIndependent.variancec                 C   s   | j  |¡S r   )r   Úsample©r   Zsample_shaper   r   r   r%   d   s    zIndependent.samplec                 C   s   | j  |¡S r   )r   Úrsampler&   r   r   r   r'   g   s    zIndependent.rsamplec                 C   s   | j  |¡}t|| jƒS r   )r   Úlog_probr   r   )r   Úvaluer(   r   r   r   r(   j   s    zIndependent.log_probc                 C   s   | j  ¡ }t|| jƒS r   )r   Úentropyr   r   )r   r*   r   r   r   r*   n   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,   r   s
    
ÿzIndependent.enumerate_supportc                 C   s   | j jd| j› d| j› d� S )Nú(z, ú))r   Ú__name__r   r   r   r   r   r   Ú__repr__y   s    ÿÿzIndependent.__repr__)N)N)T)r/   Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   Ú
ConstraintÚ__annotations__r   r   Úpropertyr   r   Zdependent_propertyr    r"   r#   r$   r   r   r%   r'   r(   r*   r,   r0   Ú__classcell__r   r   r   r   r      s.   
 ÿ






)Útypingr   r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   Ú__all__r   r   r   r   r   Ú<module>   s   