o
    h"                     @   s   d dl 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mZ d dlmZ d dlmZmZ dgZG d	d deZdS )
    N)Tensor)constraints)ExponentialFamily)broadcast_allclamp_probslazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_Number_sizeContinuousBernoullic                       sH  e Zd ZdZejejdZejZdZ	dZ
	d1	d2 fdd	Zd3 fd
d	Zdd Zdd Z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e fd"edefd#d$Zd%d& Zd'd( Z d)d* Z!d+d, Z"ede#e fd-d.Z$d/d0 Z%  Z&S )4r   a  
    Creates a continuous Bernoulli distribution parameterized by :attr:`probs`
    or :attr:`logits` (but not both).

    The distribution is supported in [0, 1] and parameterized by 'probs' (in
    (0,1)) or 'logits' (real-valued). Note that, unlike the Bernoulli, 'probs'
    does not correspond to a probability and 'logits' does not correspond to
    log-odds, but the same names are used due to the similarity with the
    Bernoulli. See [1] for more details.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = ContinuousBernoulli(torch.tensor([0.3]))
        >>> m.sample()
        tensor([ 0.2538])

    Args:
        probs (Number, Tensor): (0,1) valued parameters
        logits (Number, Tensor): real valued parameters whose sigmoid matches 'probs'

    [1] The continuous Bernoulli: fixing a pervasive error in variational
    autoencoders, Loaiza-Ganem G and Cunningham JP, NeurIPS 2019.
    https://arxiv.org/abs/1907.06845
    )probslogitsr   TNgV-?gx&1?returnc                    s   |d u |d u krt d|d ur5t|t}t|\| _|d ur.| jd | j s.t dt| j| _nt|t}t|\| _	|d urG| jn| j	| _
|rRt }n| j
 }|| _t j||d d S )Nz;Either `probs` or `logits` must be specified, but not both.r   z&The parameter probs has invalid valuesvalidate_args)
ValueError
isinstancer   r   r   arg_constraintscheckallr   r   _paramtorchSizesize_limssuper__init__)selfr   r   limsr   	is_scalarbatch_shape	__class__ |/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/distributions/continuous_bernoulli.pyr   6   s&   



zContinuousBernoulli.__init__c                    s~   |  t|}| j|_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   r   __dict__r   expandr   r   r   r   _validate_args)r    r#   	_instancenewr$   r&   r'   r*   Q   s   


zContinuousBernoulli.expandc                 O   s   | j j|i |S N)r   r-   )r    argskwargsr&   r&   r'   _new_   s   zContinuousBernoulli._newc                 C   s,   t t | j| jd t | j| jd S )Nr      )r   maxler   r   gtr    r&   r&   r'   _outside_unstable_regionb   s   $z,ContinuousBernoulli._outside_unstable_regionc                 C   s&   t |  | j| jd t | j S )Nr   )r   wherer7   r   r   	ones_liker6   r&   r&   r'   
_cut_probsg   s
   zContinuousBernoulli._cut_probsc              	   C   s   |   }tt|d|t|}tt|d|t|}ttt	| t| tt|dt	d| td| d  }t
| jd d}tddd|  |  }t|  ||S )zLcomputes the log normalizing constant as a function of the 'probs' parameter      ?g              @      ?   gUUUUUU?g'}'}@)r:   r   r8   r4   
zeros_likeger9   logabslog1ppowr   mathr7   )r    	cut_probscut_probs_below_halfcut_probs_above_halflog_normxtaylorr&   r&   r'   _cont_bern_log_normn   s&   
z'ContinuousBernoulli._cont_bern_log_normc                 C   sj   |   }|d| d  dt| t|   }| jd }dddt|d  |  }t|  ||S )Nr<   r=   r;   gUUUUUU?gll?r>   )r:   r   rC   rA   r   rD   r8   r7   )r    rF   musrJ   rK   r&   r&   r'   mean   s   
zContinuousBernoulli.meanc                 C   s   t | jS r.   )r   sqrtvariancer6   r&   r&   r'   stddev   s   zContinuousBernoulli.stddevc                 C   s   |   }||d  tdd|  d dtt| t| d  }t| jd d}ddd|  |  }t|  ||S )Nr=   r<   r>   r;   gUUUUUU?g?ggjV?)r:   r   rD   rC   rA   r   r8   r7   )r    rF   varsrJ   rK   r&   r&   r'   rP      s    zContinuousBernoulli.variancec                 C   s   t | jddS NT)	is_binary)r	   r   r6   r&   r&   r'   r      s   zContinuousBernoulli.logitsc                 C   s   t t| jddS rS   )r   r   r   r6   r&   r&   r'   r      s   zContinuousBernoulli.probsc                 C   s
   | j  S r.   )r   r   r6   r&   r&   r'   param_shape   s   
zContinuousBernoulli.param_shapec                 C   sX   |  |}tj|| jj| jjd}t  | |W  d    S 1 s%w   Y  d S N)dtypedevice)_extended_shaper   randr   rW   rX   no_gradicdfr    sample_shapeshapeur&   r&   r'   sample   s
   

$zContinuousBernoulli.sampler^   c                 C   s,   |  |}tj|| jj| jjd}| |S rV   )rY   r   rZ   r   rW   rX   r\   r]   r&   r&   r'   rsample   s   

zContinuousBernoulli.rsamplec                 C   s8   | j r| | t| j|\}}t||dd |   S )Nnone)	reduction)r+   _validate_sampler   r   r
   rL   )r    valuer   r&   r&   r'   log_prob   s   
zContinuousBernoulli.log_probc              
   C   s   | j r| | |  }t||td| d|  | d d| d  }t|  ||}tt|dt|tt	|dt
||S )Nr=   r<   g        )r+   re   r:   r   rD   r8   r7   r4   r?   r@   r9   )r    rf   rF   cdfsunbounded_cdfsr&   r&   r'   cdf   s    


zContinuousBernoulli.cdfc              	   C   sT   |   }t|  t| |d| d   t|  t|t|   |S )Nr<   r=   )r:   r   r8   r7   rC   rA   )r    rf   rF   r&   r&   r'   r\      s   
zContinuousBernoulli.icdfc                 C   s4   t | j }t | j}| j||  |   | S r.   )r   rC   r   rA   rN   rL   )r    
log_probs0
log_probs1r&   r&   r'   entropy   s   zContinuousBernoulli.entropyc                 C   s   | j fS r.   )r   r6   r&   r&   r'   _natural_params   s   z#ContinuousBernoulli._natural_paramsc                 C   s   t t || jd d t || jd d }t ||| jd d t | }t t t j	
|t t | }d| t |dd  t |dd  }t |||S )zLcomputes the log normalizing constant as a function of the natural parameterr   r;   r2   r>   g      8@   g     @)r   r3   r4   r   r5   r8   r9   rA   rB   specialexpm1rD   )r    rJ   out_unst_regcut_nat_paramsrI   rK   r&   r&   r'   _log_normalizer   s   ((z#ContinuousBernoulli._log_normalizer)NNr   N)r   Nr.   )'__name__
__module____qualname____doc__r   unit_intervalrealr   support_mean_carrier_measurehas_rsampler   r*   r1   r7   r:   rL   propertyr   rN   rQ   rP   r   r   r   r   r   rU   ra   r   rb   rg   rj   r\   rm   tuplern   rt   __classcell__r&   r&   r$   r'   r      sH    				)rE   r   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r   r   r   r	   torch.nn.functionalr
   torch.typesr   r   __all__r   r&   r&   r&   r'   <module>   s   