o
    Zh                     @   sz   d dl Z d dl mZ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 dgZG d	d deZdS )
    N)nanTensor)constraints)ExponentialFamily)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_Number	Bernoullic                       s
  e Zd ZdZejejdZejZ	dZ
dZd% fdd	Zd& fdd		Z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dejfddZe fddZdd Zdd Zd'dd Zedee fd!d"Zd#d$ Z   Z!S )(r   a1  
    Creates a Bernoulli distribution parameterized by :attr:`probs`
    or :attr:`logits` (but not both).

    Samples are binary (0 or 1). They take the value `1` with probability `p`
    and `0` with probability `1 - p`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = Bernoulli(torch.tensor([0.3]))
        >>> m.sample()  # 30% chance 1; 70% chance 0
        tensor([ 0.])

    Args:
        probs (Number, Tensor): the probability of sampling `1`
        logits (Number, Tensor): the log-odds of sampling `1`
    )probslogitsTr   Nc                    s   |d u |d u krt d|d urt|t}t|\| _nt|t}t|\| _|d ur.| jn| j| _|r9t }n| j	 }t
 j||d d S )Nz;Either `probs` or `logits` must be specified, but not both.validate_args)
ValueError
isinstancer   r   r   r   _paramtorchSizesizesuper__init__)selfr   r   r   Z	is_scalarbatch_shape	__class__ L/var/www/auris/lib/python3.10/site-packages/torch/distributions/bernoulli.pyr   ,   s   



zBernoulli.__init__c                    sv   |  t|}t|}d| jv r| j||_|j|_d| jv r+| j||_|j|_t	t|j
|dd | j|_|S )Nr   r   Fr   )Z_get_checked_instancer   r   r   __dict__r   expandr   r   r   r   _validate_args)r   r   Z	_instancenewr   r   r   r    >   s   


zBernoulli.expandc                 O   s   | j j|i |S N)r   r"   )r   argskwargsr   r   r   _newK   s   zBernoulli._newreturnc                 C   s   | j S r#   r   r   r   r   r   meanN   s   zBernoulli.meanc                 C   s$   | j dk| j }t|| j dk< |S )Ng      ?)r   tor   )r   moder   r   r   r,   R   s   zBernoulli.modec                 C   s   | j d| j   S )N   r(   r)   r   r   r   varianceX   s   zBernoulli.variancec                 C      t | jddS NT)Z	is_binary)r	   r   r)   r   r   r   r   \      zBernoulli.logitsc                 C   r/   r0   )r   r   r)   r   r   r   r   `   r1   zBernoulli.probsc                 C   s
   | j  S r#   )r   r   r)   r   r   r   param_shaped   s   
zBernoulli.param_shapec                 C   sH   |  |}t  t| j|W  d    S 1 sw   Y  d S r#   )Z_extended_shaper   Zno_gradZ	bernoullir   r    )r   Zsample_shapeshaper   r   r   sampleh   s   

$zBernoulli.samplec                 C   s0   | j r| | t| j|\}}t||dd S Nnone)Z	reduction)r!   Z_validate_sampler   r   r
   )r   valuer   r   r   r   log_probm   s   
zBernoulli.log_probc                 C   s   t | j| jddS r5   )r
   r   r   r)   r   r   r   entropys   s   
zBernoulli.entropyc                 C   sH   t jd| jj| jjd}|ddt| j  }|r"|d| j }|S )N   )dtypedevice))r-   )	r   Zaranger   r;   r<   viewlenZ_batch_shaper    )r   r    valuesr   r   r   enumerate_supportx   s
   zBernoulli.enumerate_supportc                 C   s   t | jfS r#   )r   Zlogitr   r)   r   r   r   _natural_params   r1   zBernoulli._natural_paramsc                 C   s   t t |S r#   )r   log1pexp)r   xr   r   r   _log_normalizer   s   zBernoulli._log_normalizer)NNNr#   )T)"__name__
__module____qualname____doc__r   Zunit_intervalrealZarg_constraintsbooleanZsupportZhas_enumerate_supportZ_mean_carrier_measurer   r    r&   propertyr   r*   r,   r.   r   r   r   r   r   r2   r4   r8   r9   rA   tuplerB   rF   __classcell__r   r   r   r   r      s8    
)r   r   r   Ztorch.distributionsr   Ztorch.distributions.exp_familyr   Ztorch.distributions.utilsr   r   r   r	   Ztorch.nn.functionalr
   Ztorch.typesr   __all__r   r   r   r   r   <module>   s   