o
    +ôj,  ã                   @   sj   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 dgZG dd„ deƒZdS )	é    )ÚNumberN)Úconstraints)ÚDistribution)Úbroadcast_allÚlazy_propertyÚlogits_to_probsÚprobs_to_logits)Ú binary_cross_entropy_with_logitsÚ	Geometricc                       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dd„ ƒZe ¡ fdd„Zdd„ Zdd„ Z‡  ZS )r
   a€  
    Creates a Geometric distribution parameterized by :attr:`probs`,
    where :attr:`probs` is the probability of success of Bernoulli trials.

    .. math::

        P(X=k) = (1-p)^{k} p, k = 0, 1, ...

    .. note::
        :func:`torch.distributions.geometric.Geometric` :math:`(k+1)`-th trial is the first success
        hence draws samples in :math:`\{0, 1, \ldots\}`, whereas
        :func:`torch.Tensor.geometric_` `k`-th trial is the first success hence draws samples in :math:`\{1, 2, \ldots\}`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> 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rt|ƒ\| _nt|ƒ\| _|d ur#|n|}t|tƒr/t ¡ }n| ¡ }t	ƒ j
||d� | jrk|d urm| j}|dk}| ¡ so|j|  }t dt|ƒj› dt|jƒ› dt| ƒ› d|› �ƒ‚d S d S d S )Nz;Either `probs` or `logits` must be specified, but not both.©Úvalidate_argsr   zExpected parameter probs (z
 of shape z) of distribution z* to be positive but found invalid values:
)Ú
ValueErrorr   r   r   Ú
isinstancer   ÚtorchÚSizeÚsizeÚsuperÚ__init__Ú_validate_argsÚallÚdataÚtypeÚ__name__ÚtupleÚshapeÚrepr)	Úselfr   r   r   Zprobs_or_logitsÚbatch_shapeÚvalueÚvalidZinvalid_value©Ú	__class__© úp/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torch/distributions/geometric.pyr   -   s<   ÿ

ÿÿþýÿúzGeometric.__init__c                    sf   |   t|¡}t |¡}d| jv r| j |¡|_d| jv r#| j |¡|_tt|ƒj	|dd� | j
|_
|S )Nr   r   Fr   )Ú_get_checked_instancer
   r   r   Ú__dict__r   Úexpandr   r   r   r   )r   r   Ú	_instanceÚnewr"   r$   r%   r(   I   s   


zGeometric.expandc                 C   s   d| j  d S ©Ng      ð?©r   ©r   r$   r$   r%   ÚmeanT   ó   zGeometric.meanc                 C   s   t  | j¡S ©N)r   Ú
zeros_liker   r-   r$   r$   r%   ÚmodeX   s   zGeometric.modec                 C   s   d| j  d | j  S r+   r,   r-   r$   r$   r%   Úvariance\   s   zGeometric.variancec                 C   ó   t | jdd�S ©NT)Ú	is_binary)r   r   r-   r$   r$   r%   r   `   r/   zGeometric.logitsc                 C   r4   r5   )r   r   r-   r$   r$   r%   r   d   r/   zGeometric.probsc                 C   s¢   |   |¡}t | jj¡j}t ¡ �6 tj ¡ r*tj	|| jj| jj
d�}|j|d�}n
| j |¡ |d¡}| ¡ | j  ¡   ¡ W  d   ƒ S 1 sJw   Y  d S )N)ÚdtypeÚdevice)Úminé   )Ú_extended_shaper   Úfinfor   r7   ÚtinyÚno_gradÚ_CÚ_get_tracing_stateÚrandr8   Úclampr*   Úuniform_ÚlogÚlog1pÚfloor)r   Úsample_shaper   r=   Úur$   r$   r%   Úsampleh   s   


$ùzGeometric.samplec                 C   sZ   | j r|  |¡ t|| jƒ\}}|jtjd�}d||dk|dk@ < ||  ¡  | j ¡  S )N)Úmemory_formatr   r:   )	r   Ú_validate_sampler   r   Úcloner   Úcontiguous_formatrE   rD   )r   r    r   r$   r$   r%   Úlog_probt   s   
zGeometric.log_probc                 C   s   t | j| jdd�| j S )NÚnone)Ú	reduction)r	   r   r   r-   r$   r$   r%   Úentropy|   s   ÿÿzGeometric.entropy)NNNr0   )r   Ú
__module__Ú__qualname__Ú__doc__r   Úunit_intervalÚrealÚarg_constraintsÚnonnegative_integerÚsupportr   r(   Úpropertyr.   r2   r3   r   r   r   r   r   rI   rN   rQ   Ú__classcell__r$   r$   r"   r%   r
      s&    




)Únumbersr   r   Ztorch.distributionsr   Ú torch.distributions.distributionr   Útorch.distributions.utilsr   r   r   r   Ztorch.nn.functionalr	   Ú__all__r
   r$   r$   r$   r%   Ú<module>   s    