o
    h                     @   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   	is_scalarbatch_shape	__class__ q/var/www/html/construction_image-detection-poc/venv/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   )_get_checked_instancer   r   r   __dict__r   expandr   r   r   r   _validate_args)r   r   	_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)	is_binary)r	   r   r,   r   r   r   r   \      zBernoulli.logitsc                 C   r2   r3   )r   r   r,   r   r   r   r   `   r5   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&   )_extended_shaper   no_grad	bernoullir   r"   )r   sample_shapeshaper   r   r   sampleh   s   

$zBernoulli.samplec                 C   s0   | j r| | t| j|\}}t||dd S Nnone)	reduction)r#   _validate_sampler   r   r
   )r   valuer   r   r   r   log_probm   s   
zBernoulli.log_probc                 C   s   t | j| jddS r=   )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))r0   )	r   aranger   rE   rF   viewlen_batch_shaper"   )r   r"   valuesr   r   r   enumerate_supportx   s
   zBernoulli.enumerate_supportc                 C   s   t | jfS r&   )r   logitr   r,   r   r   r   _natural_params   r5   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   unit_intervalrealarg_constraintsbooleansupporthas_enumerate_support_mean_carrier_measurer   r"   r)   propertyr   r-   r/   r1   r   r   r   r   r   r6   r<   rB   rC   rM   tuplerO   rS   __classcell__r   r   r   r   r      s8    
)r   r   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r   r   r	   torch.nn.functionalr
   torch.typesr   __all__r   r   r   r   r   <module>   s   