o
    ,h                     @   s   d dl mZmZ 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mZ d	gZG d
d	 d	e	ZdS )    )OptionalUnionN)nanTensor)constraints)ExponentialFamily)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_NumberNumber	Bernoullic                	       s>  e Zd ZdZejejdZejZ	dZ
dZ			d(deeeef  deeeef  dee d	df 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   a  
    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`
        validate_args (bool, optional): whether to validate arguments, None by default
    )probslogitsTr   Nr   r   validate_argsreturnc                    s   |d u |d u krt d|d urt|t}t|\| _n|d us"J t|t}t|\| _|d ur4| jn| j| _|r?t }n| j	 }t
 j||d d S )Nz;Either `probs` or `logits` must be specified, but not both.r   )
ValueError
isinstancer   r   r   r   _paramtorchSizesizesuper__init__)selfr   r   r   	is_scalarbatch_shape	__class__ X/var/www/html/scripts/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&   G   s   


zBernoulli.expandc                 O   s   | j j|i |S N)r   r)   )r   argskwargsr"   r"   r#   _newT   s   zBernoulli._newc                 C   s   | j S r*   r   r   r"   r"   r#   meanW   s   zBernoulli.meanc                 C   s$   | j dk| j }t|| j dk< |S )Ng      ?)r   tor   )r   moder"   r"   r#   r2   [   s   zBernoulli.modec                 C   s   | j d| j   S )N   r.   r/   r"   r"   r#   variancea   s   zBernoulli.variancec                 C      t | jddS NT)	is_binary)r   r   r/   r"   r"   r#   r   e      zBernoulli.logitsc                 C   r5   r6   )r
   r   r/   r"   r"   r#   r   i   r8   zBernoulli.probsc                 C   s
   | j  S r*   )r   r   r/   r"   r"   r#   param_shapem   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#   sampleq   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_probv   s   
zBernoulli.log_probc                 C   s   t | j| jddS r@   )r   r   r   r/   r"   r"   r#   entropy|   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))r3   )	r   aranger   rH   rI   viewlen_batch_shaper&   )r   r&   valuesr"   r"   r#   enumerate_support   s
   zBernoulli.enumerate_supportc                 C   s   t | jfS r*   )r   logitr   r/   r"   r"   r#   _natural_params   r8   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   r   boolr   r&   r-   propertyr0   r2   r4   r	   r   r   r   r   r9   r?   rE   rF   rP   tuplerR   rV   __classcell__r"   r"   r    r#   r      sN    
)typingr   r   r   r   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r	   r
   r   torch.nn.functionalr   torch.typesr   r   __all__r   r"   r"   r"   r#   <module>   s   