a
    MSicO                     @   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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__)selfbase_distributionr   r	   shape	event_dimr   r   	__class__ [/var/www/html/django/DPS/env/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   )_get_checked_instancer   torchSizer   expandr   r   r   r   _validate_args)r   r   	_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   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   moder$   r   r   r   r*   P   s    zIndependent.modec                 C   s   | j jS r"   )r   variancer$   r   r   r   r+   T   s    zIndependent.variancec                 C   s   | j |S r"   )r   sampler   sample_shaper   r   r   r,   X   s    zIndependent.samplec                 C   s   | j |S r"   )r   rsampler-   r   r   r   r/   [   s    zIndependent.rsamplec                 C   s   | j |}t|| jS r"   )r   log_probr   r   )r   valuer0   r   r   r   r0   ^   s    zIndependent.log_probc                 C   s   | j  }t|| jS r"   )r   entropyr   r   )r   r2   r   r   r   r2   b   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   r4   f   s    
zIndependent.enumerate_supportc                 C   s   | j jd| j| j S )Nz({}, {}))r   __name__r   r   r   r$   r   r   r   __repr__k   s    zIndependent.__repr__)N)N)T)r5   
__module____qualname____doc__r   r   strr   
Constraint__annotations__r   r   propertyr#   r%   dependent_propertyr&   r)   r*   r+   r   r   r,   r/   r0   r2   r4   r6   __classcell__r   r   r   r   r      s,   








r   )
r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   typingr   r   r   r   r   r   <module>   s
   