a
    ßñh»%  ã                   @   s<   d dl Z d dlZd dlmZ d dlmZ G dd„ deƒZdS )é    N)Úconstraints)Úlazy_propertyc                       s2  e Zd ZdZdZdZdZdZi Ze	dd„ ƒZ
e ¡ e ¡ df‡ fdd„	Zd3dd	„Zed
d„ ƒZedd„ ƒZedd„ ƒZe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!„ Zd"d#„ Zd4d%d&„Zd'd(„ Zd)d*„ Ze ¡ fd+d,„Zd-d.„ Zd5d/d0„Z d1d2„ Z!‡  Z"S )6ÚDistributionzP
    Distribution is the abstract base class for probability distributions.
    FNc                 C   s   | dvrt ‚| t_d S )N)TF)Ú
ValueErrorr   Ú_validate_args)Úvalue© r   ú`/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/distributions/distribution.pyÚset_default_validate_args   s    z&Distribution.set_default_validate_argsc                    s”   || _ || _|d ur|| _| jr‚| j ¡ D ]V\}}t |¡r>q*|| jvr^tt	t
| ƒ|ƒtƒr^q*| t	| |ƒ¡ ¡ s*td |¡ƒ‚q*tt| ƒ ¡  d S )Nz#The parameter {} has invalid values)Ú_batch_shapeÚ_event_shaper   Úarg_constraintsÚitemsr   Zis_dependentÚ__dict__Ú
isinstanceÚgetattrÚtyper   ÚcheckÚallr   ÚformatÚsuperr   Ú__init__)ÚselfÚbatch_shapeÚevent_shapeZvalidate_argsÚparamÚ
constraint©Ú	__class__r   r	   r      s    
zDistribution.__init__c                 C   s   t ‚dS )a/  
        Returns a new distribution instance (or populates an existing instance
        provided by a derived class) with batch dimensions expanded to
        `batch_shape`. This method calls :class:`~torch.Tensor.expand` on
        the distribution's parameters. As such, this does not allocate new
        memory for the expanded distribution instance. Additionally,
        this does not repeat any args checking or parameter broadcasting in
        `__init__.py`, when an instance is first created.

        Args:
            batch_shape (torch.Size): the desired expanded size.
            _instance: new instance provided by subclasses that
                need to override `.expand`.

        Returns:
            New distribution instance with batch dimensions expanded to
            `batch_size`.
        N©ÚNotImplementedError)r   r   Ú	_instancer   r   r	   Úexpand'   s    zDistribution.expandc                 C   s   | j S )zF
        Returns the shape over which parameters are batched.
        )r   ©r   r   r   r	   r   <   s    zDistribution.batch_shapec                 C   s   | j S )zJ
        Returns the shape of a single sample (without batching).
        )r   r#   r   r   r	   r   C   s    zDistribution.event_shapec                 C   s   t ‚dS )a
  
        Returns a dictionary from argument names to
        :class:`~torch.distributions.constraints.Constraint` objects that
        should be satisfied by each argument of this distribution. Args that
        are not tensors need not appear in this dict.
        Nr   r#   r   r   r	   r   J   s    zDistribution.arg_constraintsc                 C   s   t ‚dS )z‰
        Returns a :class:`~torch.distributions.constraints.Constraint` object
        representing this distribution's support.
        Nr   r#   r   r   r	   ÚsupportT   s    zDistribution.supportc                 C   s   t ‚dS )z7
        Returns the mean of the distribution.
        Nr   r#   r   r   r	   Úmean\   s    zDistribution.meanc                 C   s   t ‚dS )z;
        Returns the variance of the distribution.
        Nr   r#   r   r   r	   Úvariancec   s    zDistribution.variancec                 C   s
   | j  ¡ S )zE
        Returns the standard deviation of the distribution.
        )r&   Úsqrtr#   r   r   r	   Ústddevj   s    zDistribution.stddevc                 C   s6   t  ¡ � |  |¡W  d  ƒ S 1 s(0    Y  dS )z”
        Generates a sample_shape shaped sample or sample_shape shaped batch of
        samples if the distribution parameters are batched.
        N)ÚtorchZno_gradÚrsample©r   Zsample_shaper   r   r	   Úsampleq   s    
zDistribution.samplec                 C   s   t ‚dS )z¼
        Generates a sample_shape shaped reparameterized sample or sample_shape
        shaped batch of reparameterized samples if the distribution parameters
        are batched.
        Nr   r+   r   r   r	   r*   y   s    zDistribution.rsamplec                 C   s   t  dt¡ |  t |f¡¡S )zq
        Generates n samples or n batches of samples if the distribution
        parameters are batched.
        z6sample_n will be deprecated. Use .sample((n,)) instead)ÚwarningsÚwarnÚUserWarningr,   r)   ÚSize)r   Únr   r   r	   Úsample_n�   s    zDistribution.sample_nc                 C   s   t ‚dS )z“
        Returns the log of the probability density/mass function evaluated at
        `value`.

        Args:
            value (Tensor):
        Nr   ©r   r   r   r   r	   Úlog_prob‰   s    zDistribution.log_probc                 C   s   t ‚dS )z‡
        Returns the cumulative density/mass function evaluated at
        `value`.

        Args:
            value (Tensor):
        Nr   r3   r   r   r	   Úcdf“   s    zDistribution.cdfc                 C   s   t ‚dS )z�
        Returns the inverse cumulative density/mass function evaluated at
        `value`.

        Args:
            value (Tensor):
        Nr   r3   r   r   r	   Úicdf�   s    zDistribution.icdfTc                 C   s   t ‚dS )ar  
        Returns tensor containing all values supported by a discrete
        distribution. The result will enumerate over dimension 0, so the shape
        of the result will be `(cardinality,) + batch_shape + event_shape`
        (where `event_shape = ()` for univariate distributions).

        Note that this enumerates over all batched tensors in lock-step
        `[[0, 0], [1, 1], ...]`. With `expand=False`, enumeration happens
        along dim 0, but with the remaining batch dimensions being
        singleton dimensions, `[[0], [1], ..`.

        To iterate over the full Cartesian product use
        `itertools.product(m.enumerate_support())`.

        Args:
            expand (bool): whether to expand the support over the
                batch dims to match the distribution's `batch_shape`.

        Returns:
            Tensor iterating over dimension 0.
        Nr   )r   r"   r   r   r	   Úenumerate_support§   s    zDistribution.enumerate_supportc                 C   s   t ‚dS )z‡
        Returns entropy of distribution, batched over batch_shape.

        Returns:
            Tensor of shape batch_shape.
        Nr   r#   r   r   r	   Úentropy¿   s    zDistribution.entropyc                 C   s   t  |  ¡ ¡S )zŠ
        Returns perplexity of distribution, batched over batch_shape.

        Returns:
            Tensor of shape batch_shape.
        )r)   Úexpr8   r#   r   r   r	   Ú
perplexityÈ   s    zDistribution.perplexityc                 C   s&   t |tjƒst |¡}|| j | j S )ax  
        Returns the size of the sample returned by the distribution, given
        a `sample_shape`. Note, that the batch and event shapes of a distribution
        instance are fixed at the time of construction. If this is empty, the
        returned shape is upcast to (1,).

        Args:
            sample_shape (torch.Size): the size of the sample to be drawn.
        )r   r)   r0   r   r   r+   r   r   r	   Ú_extended_shapeÑ   s    

zDistribution._extended_shapec                 C   sÊ   t |tjƒstdƒ‚t| ¡ ƒt| jƒ }| ¡ |d… | jkrVtd | ¡ | j¡ƒ‚| ¡ }| j| j }t	t
|ƒt
|ƒƒD ]0\}}|dkr||dkr|||kr|td ||¡ƒ‚q|| j |¡ ¡ sÆtdƒ‚dS )a  
        Argument validation for distribution methods such as `log_prob`,
        `cdf` and `icdf`. The rightmost dimensions of a value to be
        scored via these methods must agree with the distribution's batch
        and event shapes.

        Args:
            value (Tensor): the tensor whose log probability is to be
                computed by the `log_prob` method.
        Raises
            ValueError: when the rightmost dimensions of `value` do not match the
                distribution's batch and event shapes.
        z/The value argument to log_prob must be a TensorNz>The right-most size of value must match event_shape: {} vs {}.é   zBValue is not broadcastable with batch_shape+event_shape: {} vs {}.z-The value argument must be within the support)r   r)   ZTensorr   ÚlenÚsizer   r   r   ÚzipÚreversedr$   r   r   )r   r   Zevent_dim_startZactual_shapeZexpected_shapeÚiÚjr   r   r	   Ú_validate_sampleß   s     
ÿÿzDistribution._validate_samplec                 C   sH   |d u r.t | ƒj|jkr.td | jj|j¡ƒ‚|d u rD|  t | ƒ¡S |S )NzcSubclass {} of {} that defines a custom __init__ method must also define a custom .expand() method.)r   r   r    r   r   Ú__name__Ú__new__)r   Úclsr!   r   r   r	   Ú_get_checked_instanceÿ   s
    
þz"Distribution._get_checked_instancec                    sD   ‡ fdd„ˆ j  ¡ D ƒ}d ‡ fdd„|D ƒ¡}ˆ jjd | d S )Nc                    s   g | ]\}}|ˆ j v r|‘qS r   )r   )Ú.0ÚkÚ_r#   r   r	   Ú
<listcomp>  ó    z)Distribution.__repr__.<locals>.<listcomp>z, c                    s>   g | ]6}d   |ˆ j|  ¡ dkr*ˆ j| nˆ j|  ¡ ¡‘qS )z{}: {}r<   )r   r   Znumelr>   )rH   Úpr#   r   r	   rK     s   þÿþú(ú))r   r   Újoinr   rD   )r   Zparam_namesÚargs_stringr   r#   r	   Ú__repr__  s
    þzDistribution.__repr__)N)T)N)#rD   Ú
__module__Ú__qualname__Ú__doc__Zhas_rsampleZhas_enumerate_supportr   r$   r   Ústaticmethodr
   r)   r0   r   r"   Úpropertyr   r   r%   r&   r(   r,   r*   r2   r4   r5   r6   r7   r8   r:   r;   rC   rG   rR   Ú__classcell__r   r   r   r	   r      sJ   




	







		 
r   )r)   r-   Ztorch.distributionsr   Ztorch.distributions.utilsr   Úobjectr   r   r   r   r	   Ú<module>   s   