
    }Yh                         d dl mZmZ 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	e          ZdS )    )OptionalUnionN)Tensor)constraints)Distribution)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_NumberNumber	Geometricc            	       t    e Zd ZdZej        ej        dZej        Z		 	 	 dde
eeef                  de
eeef                  de
e         ddf fdZd fd		Zedefd
            Zedefd            Zedefd            Zedefd            Zedef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logitsNr   r   validate_argsreturnc           
         |d u |d u k    rt          d          |t          |          \  | _        n|J t          |          \  | _        ||n|}t	          |t
                    rt          j                    }n|J |                                }t                      
                    ||           | j        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.r   r   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
            o/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/distributions/geometric.pyr   zGeometric.__init__2   s    TMv~..M   )%00MTZZ%%%*622NT[#(#4%%&ow// 	1*,,KK"...)..00KMBBB 	5#4JEAIE99;;  %
E6 2 QU,Q Q8=ek8J8JQ Q'+DzzQ Q BOQ Q  	 	#4#4     c                 r   |                      t          |          }t          j        |          }d| j        v r| j                            |          |_        d| j        v r| 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-   r2   zGeometric.expandU   s    ((I>>j--dm##
))+66CIt}$$++K88CJi&&{%&HHH!0
r.   c                     d| j         z  dz
  S Ng      ?r   r&   s    r-   meanzGeometric.mean`   s    TZ#%%r.   c                 4    t          j        | j                  S N)r   
zeros_liker   r8   s    r-   modezGeometric.moded   s    
+++r.   c                 ,    d| j         z  dz
  | j         z  S r6   r7   r8   s    r-   variancezGeometric.varianceh   s    dj 3&$*44r.   c                 .    t          | j        d          S NT)	is_binary)r   r   r8   s    r-   r   zGeometric.logitsl   s    tzT::::r.   c                 .    t          | j        d          S rA   )r
   r   r8   s    r-   r   zGeometric.probsp   s    t{d;;;;r.   c                 j   |                      |          }t          j        | j        j                  j        }t          j                    5  t          j                                        rBt          j	        || j        j        | j        j
                  }|                    |          }n.| j                            |                              |d          }|                                | j                                         z                                  cd d d            S # 1 swxY w Y   d S )N)dtypedevice)min   )_extended_shaper   finfor   rE   tinyno_grad_C_get_tracing_staterandrF   clampr4   uniform_loglog1pfloor)r&   sample_shaper$   rK   us        r-   samplezGeometric.samplet   s6   $$\22{4:+,,1]__ 	= 	=x**,, <JuDJ,<TZEVWWWGGG%%JNN5))224;;EEGG
{11333::<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s   CD((D,/D,c                 .   | j         r|                     |           t          || j                  \  }}|                    t
          j                  }d||dk    |dk    z  <   ||                                 z  | j                                        z   S )N)memory_formatr   rH   )	r   _validate_sampler   r   cloner   contiguous_formatrS   rR   )r&   r)   r   s      r-   log_probzGeometric.log_prob   s     	)!!%((($UDJ77u%*ABB-.uzeqj)*~~'''$*..*:*:::r.   c                 J    t          | j        | j        d          | j        z  S )Nnone)	reduction)r   r   r   r8   s    r-   entropyzGeometric.entropy   s(    ,T[$*PVWWWj	
r.   )NNNr;   )r"   
__module____qualname____doc__r   unit_intervalrealarg_constraintsnonnegative_integersupportr   r   r   r   boolr   r2   propertyr9   r=   r?   r	   r   r   r   r   rW   r]   ra   __classcell__)r,   s   @r-   r   r      s        2 !, 9[EUVVO-G 2626(,	! !ffn-.! vv~./!  ~	!
 
! ! ! ! ! !F	 	 	 	 	 	 &f & & & X& ,f , , , X, 5& 5 5 5 X5 ; ; ; ; ]; <v < < < ]< #-%*,, 
= 
= 
= 
=; ; ;
 
 
 
 
 
 
r.   )typingr   r   r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r	   r
   r   torch.nn.functionalr   torch.typesr   r   __all__r    r.   r-   <module>ru      s   " " " " " " " "        + + + + + + 9 9 9 9 9 9            A @ @ @ @ @ ' ' ' ' ' ' ' ' -w
 w
 w
 w
 w
 w
 w
 w
 w
 w
r.   