
    ǄgH                     p    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e      Zy)	    )NumberN)constraints)Distribution)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits	Geometricc                        e Zd ZdZej
                  ej                  dZej                  Z	d fd	Z
d fd	Zed        Zed        Zed        Zed        Zed	        Z ej(                         fd
Zd Zd Z x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logitsc           
      "   |d u |d u k(  rt        d      |t        |      \  | _        nt        |      \  | _        ||n|}t	        |t
              rt        j                         }n|j                         }t        	| )  ||       | j                  r{|x| j                  }|dkD  }|j                         sV|j                  |    }t        dt        |      j                   dt!        |j"                         dt%        |        d|       y y y )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   probs_or_logitsbatch_shapevaluevalidinvalid_value	__class__s
            e/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributions/geometric.pyr   zGeometric.__init__/   s    TMv~.M  )%0MTZ*62NT[#(#4%&ov.**,K)..0KMB5#4JJEAIE99; %

E6 2 U,,-Zekk8J7K L''+Dzl 3AANQ  	 $5    c                 b   | j                  t        |      }t        j                  |      }d| j                  v r | j
                  j                  |      |_        d| j                  v r | j                  j                  |      |_        t        t        |'  |d       | j                  |_
        |S )Nr   r   Fr   )_get_checked_instancer   r   r   __dict__r   expandr   r   r   r   )r!   r#   	_instancenewr'   s       r(   r-   zGeometric.expandK   s    ((I>jj-dmm#

))+6CIt}}$++K8CJi&{%&H!00
r)   c                 &    d| j                   z  dz
  S Ng      ?r   r!   s    r(   meanzGeometric.meanV   s    TZZ#%%r)   c                 @    t        j                  | j                        S N)r   
zeros_liker   r3   s    r(   modezGeometric.modeZ   s    

++r)   c                 @    d| j                   z  dz
  | j                   z  S r1   r2   r3   s    r(   variancezGeometric.variance^   s    djj 3&$**44r)   c                 0    t        | j                  d      S NT)	is_binary)r	   r   r3   s    r(   r   zGeometric.logitsb   s    tzzT::r)   c                 0    t        | j                  d      S r<   )r   r   r3   s    r(   r   zGeometric.probsf   s    t{{d;;r)   c                    | j                  |      }t        j                  | j                  j                        j
                  }t        j                         5  t        j                  j                         rSt        j                  || j                  j                  | j                  j                        }|j                  |      }n+| j                  j                  |      j                  |d      }|j                         | j                   j                         z  j!                         cd d d        S # 1 sw Y   y xY w)N)dtypedevice)min   )_extended_shaper   finfor   r@   tinyno_grad_C_get_tracing_staterandrA   clampr/   uniform_loglog1pfloor)r!   sample_shaper   rF   us        r(   samplezGeometric.samplej   s    $$\2{{4::++,11]]_ 	=xx**,JJuDJJ,<,<TZZEVEVWGGG%JJNN5)224;EEG

{1133::<	= 	= 	=s   CD99Ec                 (   | j                   r| j                  |       t        || j                        \  }}|j	                  t
        j                        }d||dk(  |dk(  z  <   || j                         z  | j                  j                         z   S )N)memory_formatr   rC   )	r   _validate_sampler   r   cloner   contiguous_formatrN   rM   )r!   r$   r   s      r(   log_probzGeometric.log_probv   s~    !!%($UDJJ7u%*A*AB-.uzeqj)*~~''$**..*:::r)   c                 `    t        | j                  | j                  d      | j                  z  S )Nnone)	reduction)r
   r   r   r3   s    r(   entropyzGeometric.entropy~   s(    ,T[[$**PVWjj	
r)   )NNNr6   )r   
__module____qualname____doc__r   unit_intervalrealarg_constraintsnonnegative_integersupportr   r-   propertyr4   r8   r:   r   r   r   r   r   rR   rX   r\   __classcell__)r'   s   @r(   r   r      s    0 !, 9 9[EUEUVO--G8	 & & , , 5 5 ; ; < < #-%**, 
=;
r)   )numbersr   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   r   r	   torch.nn.functionalr
   __all__r    r)   r(   <module>rn      s8      + 9  A -o
 o
r)   