a
    ßñhñ  ã                   @   sd   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ G dd„ deƒZdS )	é    )ÚNumberN)Úconstraints)ÚDistribution)Úbroadcast_allÚprobs_to_logitsÚlogits_to_probsÚlazy_property)Ú binary_cross_entropy_with_logitsc                       s”   e Zd ZdZejejdœZejZ	d‡ fdd„	Z
d‡ fdd„	Zedd	„ ƒZed
d„ ƒZedd„ ƒZedd„ ƒZe ¡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú	GeometricaŸ  
    Creates a Geometric distribution parameterized by :attr:`probs`,
    where :attr:`probs` is the probability of success of Bernoulli trials.
    It represents the probability that in :math:`k + 1` Bernoulli trials, the
    first :math:`k` trials failed, before seeing a success.

    Samples are non-negative integers [0, :math:`\inf`).

    Example::

        >>> m = Geometric(torch.tensor([0.3]))
        >>> m.sample()  # underlying Bernoulli has 30% chance 1; 70% chance 0
        tensor([ 2.])

    Args:
        probs (Number, Tensor): the probability of sampling `1`. Must be in range (0, 1]
        logits (Number, Tensor): the log-odds of sampling `1`.
    )ÚprobsÚlogitsNc                    s–   |d u |d u krt dƒ‚|d urFt|ƒ\| _| j d¡ ¡ sRt dƒ‚nt|ƒ\| _|d ur^|n|}t|tƒrvt 	¡ }n| 
¡ }tt| ƒj||d� d S )Nz;Either `probs` or `logits` must be specified, but not both.r   z,All elements of probs must be greater than 0©Úvalidate_args)Ú
ValueErrorr   r   ÚgtÚallr   Ú
isinstancer   ÚtorchÚSizeÚsizeÚsuperr
   Ú__init__)Úselfr   r   r   Zprobs_or_logitsÚbatch_shape©Ú	__class__© ú]/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/distributions/geometric.pyr   !   s    


zGeometric.__init__c                    sf   |   t|¡}t |¡}d| jv r.| j |¡|_d| jv rF| j |¡|_tt|ƒj	|dd� | j
|_
|S )Nr   r   Fr   )Z_get_checked_instancer
   r   r   Ú__dict__r   Úexpandr   r   r   Ú_validate_args)r   r   Z	_instanceÚnewr   r   r   r   1   s    


zGeometric.expandc                 C   s   d| j  d S ©Ng      ð?©r   ©r   r   r   r   Úmean<   s    zGeometric.meanc                 C   s   d| j  d | j  S r"   r#   r$   r   r   r   Úvariance@   s    zGeometric.variancec                 C   s   t | jdd�S ©NT)Z	is_binary)r   r   r$   r   r   r   r   D   s    zGeometric.logitsc                 C   s   t | jdd�S r'   )r   r   r$   r   r   r   r   H   s    zGeometric.probsc                 C   s¢   |   |¡}t | jj¡j}t ¡ �l tj ¡ rTtj	|| jj| jj
d�}|j|d�}n| j |¡ |d¡}| ¡ | j  ¡   ¡ W  d   ƒ S 1 s”0    Y  d S )N)ÚdtypeÚdevice)Úminé   )Z_extended_shaper   Zfinfor   r(   ÚtinyZno_gradZ_CZ_get_tracing_stateZrandr)   Úclampr!   Zuniform_ÚlogÚlog1pÚfloor)r   Zsample_shapeÚshaper,   Úur   r   r   ÚsampleL   s    


zGeometric.samplec                 C   sZ   | j r|  |¡ t|| jƒ\}}|jtjd�}d||dk|dk@ < ||  ¡  | j ¡  S )N)Zmemory_formatr   r+   )	r    Z_validate_sampler   r   Úcloner   Zcontiguous_formatr/   r.   )r   Úvaluer   r   r   r   Úlog_probX   s    
zGeometric.log_probc                 C   s   t | j| jdd�| j S )NÚnone)Z	reduction)r	   r   r   r$   r   r   r   Úentropy`   s    zGeometric.entropy)NNN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Zunit_intervalÚrealZarg_constraintsZnonnegative_integerZsupportr   r   Úpropertyr%   r&   r   r   r   r   r   r3   r6   r8   Ú__classcell__r   r   r   r   r
   
   s$   ÿ



r
   )Znumbersr   r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   r   Ztorch.nn.functionalr	   r
   r   r   r   r   Ú<module>   s   