o
    +ô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__)ÚselfÚbase_distributionr   r	   ÚshapeÚ	event_dimr   r   ©Ú	__class__© úr/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/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   )Ú_get_checked_instancer   ÚtorchÚSizer   Úexpandr   r   r   r   Ú_validate_args)r   r   Ú	_instanceÚnewr   r   r   r   :   s   
ÿ
ÿzIndependent.expandc                 C   ó   | j jS ©N)r   Úhas_rsample©r   r   r   r   r#   G   ó   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   Úindependent)r   Úresultr   r   r   r'   Q   s   zIndependent.supportc                 C   r!   r"   )r   Úmeanr$   r   r   r   r*   X   r%   zIndependent.meanc                 C   r!   r"   )r   Úmoder$   r   r   r   r+   \   r%   zIndependent.modec                 C   r!   r"   )r   Úvariancer$   r   r   r   r,   `   r%   zIndependent.variancec                 C   ó   | j  |¡S r"   )r   Úsample©r   Úsample_shaper   r   r   r.   d   ó   zIndependent.samplec                 C   r-   r"   )r   Úrsampler/   r   r   r   r2   g   r1   zIndependent.rsamplec                 C   s   | j  |¡}t|| jƒS r"   )r   Úlog_probr   r   )r   Úvaluer3   r   r   r   r3   j   s   zIndependent.log_probc                 C   s   | j  ¡ }t|| jƒS r"   )r   Úentropyr   r   )r   r5   r   r   r   r5   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   r7   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__r"   )T)r:   Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   Ú
ConstraintÚ__annotations__r   r   Úpropertyr#   r&   Údependent_propertyr'   r*   r+   r,   r   r   r.   r2   r3   r5   r7   r;   Ú__classcell__r   r   r   r   r      s0   
 ÿ






)Útypingr   r   Útorch.distributionsr   Ú torch.distributions.distributionr   Útorch.distributions.utilsr   Ú__all__r   r   r   r   r   Ú<module>   s    