o
    eie                     @   sz   d dl Z d dl m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 dlmZmZ dgZG d	d deZdS )
    N)Tensor)constraints)Distribution)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_NumberNumber	Geometricc                	       s   e Zd ZdZejejdZejZ				dde
eB dB de
eB dB dedB ddf fdd	Zd fd
d	Zede
fddZede
fddZede
fddZede
fddZede
f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logitsNr   r   validate_argsreturnc           	   	      s  |d u |d u krt d|d urt|\| _n|d u rtdt|\| _|d ur+|n|}t|tr7t }n|d u r?td|	 }t
 j||d | jr{|d ur}| j}|dk}| s|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.zlogits is unexpectedly Nonez$probs_or_logits is unexpectedly Noner   r   zExpected parameter probs (z
 of shape z) of distribution z* to be positive but found invalid values:
)
ValueErrorr   r   AssertionErrorr   
isinstancer
   torchSizesizesuper__init___validate_argsalldatatype__name__tupleshaperepr)	selfr   r   r   probs_or_logitsbatch_shapevaluevalidinvalid_value	__class__ g/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/distributions/geometric.pyr   2   sD   

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.   Y   s   


zGeometric.expandc                 C   s   d| j  d S Ng      ?r   r"   r*   r*   r+   meand      zGeometric.meanc                 C   s   t | jS N)r   
zeros_liker   r3   r*   r*   r+   modeh   s   zGeometric.modec                 C   s   d| j  d | j  S r1   r2   r3   r*   r*   r+   variancel   s   zGeometric.variancec                 C      t | jddS NT)	is_binary)r   r   r3   r*   r*   r+   r   p   r5   zGeometric.logitsc                 C   r:   r;   )r   r   r3   r*   r*   r+   r   t   r5   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   r=   tinyno_grad_C_get_tracing_staterandr>   clampr0   uniform_loglog1pfloor)r"   sample_shaper    rC   ur*   r*   r+   samplex   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_formatrK   rJ   )r"   r%   r   r*   r*   r+   log_prob   s   
zGeometric.log_probc                 C   s   t | j| jdd| j S )Nnone)	reduction)r	   r   r   r3   r*   r*   r+   entropy   s   zGeometric.entropy)NNNr6   )r   
__module____qualname____doc__r   unit_intervalrealarg_constraintsnonnegative_integersupportr   r   boolr   r.   propertyr4   r8   r9   r   r   r   r   r   rO   rT   rW   __classcell__r*   r*   r(   r+   r      s<    

')r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   r   r   torch.nn.functionalr	   torch.typesr
   r   __all__r   r*   r*   r*   r+   <module>   s   