U
    d                     @   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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 k|d kkrt d|d k	r.t|\| _nt|\| _|d k	rF|n|}t|tr^t }n| }t	t
| j||d | jr|d k	r| j}|dk}| s|j|  }t dt|j dt|j dt|  d| 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superr
   __init___validate_argsalldatatype__name__tupleshaperepr)	selfr   r   r   Zprobs_or_logitsbatch_shapevalueZvalidZinvalid_value	__class__ A/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributions/geometric.pyr   !   s$    

*zGeometric.__init__c                    sf   |  t|}t|}d| jkr.| j||_d| jkrF| j||_tt|j	|dd | j
|_
|S )Nr   r   Fr   )Z_get_checked_instancer
   r   r   __dict__r   expandr   r   r   r   )r   r   Z	_instancenewr!   r#   r$   r&   ;   s    


zGeometric.expandc                 C   s   d| j  d S Ng      ?r   r   r#   r#   r$   meanF   s    zGeometric.meanc                 C   s   t | jS )N)r   Z
zeros_liker   r*   r#   r#   r$   modeJ   s    zGeometric.modec                 C   s   d| j  d | j  S r(   r)   r*   r#   r#   r$   varianceN   s    zGeometric.variancec                 C   s   t | jddS NT)Z	is_binary)r   r   r*   r#   r#   r$   r   R   s    zGeometric.logitsc                 C   s   t | jddS r.   )r   r   r*   r#   r#   r$   r   V   s    zGeometric.probsc              
   C   s   |  |}t| jjj}t j tj rTtj	|| jj| jj
d}|j|d}n| j||d}| | j    W  5 Q R  S Q R X d S )N)dtypedevice)min   )Z_extended_shaper   Zfinfor   r/   tinyZno_gradZ_CZ_get_tracing_stateZrandr0   clampr'   Zuniform_loglog1pfloor)r   Zsample_shaper   r3   ur#   r#   r$   sampleZ   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   r2   )	r   Z_validate_sampler   r   cloner   Zcontiguous_formatr6   r5   )r   r    r   r#   r#   r$   log_probf   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$   entropyn   s    zGeometric.entropy)NNN)N)r   
__module____qualname____doc__r   Zunit_intervalrealZarg_constraintsZnonnegative_integerZsupportr   r&   propertyr+   r,   r-   r   r   r   r   r   r9   r;   r=   __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   