
    [Th                         S SK r S SK JrJr  S SKJr  S SKJr  S SKJrJ	r	J
r
Jr  S SKJr  S SKJr  S/r " S	 S\5      rg)
    N)nanTensor)constraints)ExponentialFamily)broadcast_alllazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_Number	Bernoullic                     ^  \ rS rSrSr\R                  \R                  S.r\R                  r
SrSrSU 4S jjrSU 4S jjrS r\S	\4S
 j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\R2                  4S j5       r\R2                  " 5       4S jrS rS rSS jr\S	\\   4S j5       r S r!Sr"U =r#$ )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`
)probslogitsTr   c                   > US L US L :X  a  [        S5      eUb#  [        U[        5      n[        U5      u  U l        O"[        U[        5      n[        U5      u  U l        Ub  U R                  OU R
                  U l        U(       a  [        R                  " 5       nOU R                  R                  5       n[        TU ]-  XSS9  g )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__s         U/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/bernoulli.pyr   Bernoulli.__init__,   s    TMv~.M  "5'2I)%0MTZ"673I*62NT[$)$5djj4;;**,K++**,KB    c                   > U R                  [        U5      n[        R                  " U5      nSU R                  ;   a1  U R
                  R                  U5      Ul        UR
                  Ul        SU R                  ;   a1  U R                  R                  U5      Ul        UR                  Ul        [        [        U]+  USS9  U R                  Ul        U$ )Nr   r   Fr   )_get_checked_instancer   r   r   __dict__r   expandr   r   r   r   _validate_args)r   r   	_instancenewr    s       r!   r'   Bernoulli.expand>   s    ((I>jj-dmm#

))+6CICJt}}$++K8CJCJi&{%&H!00
r#   c                 :    U R                   R                  " U0 UD6$ N)r   r*   )r   argskwargss      r!   _newBernoulli._newK   s    {{///r#   returnc                     U R                   $ r-   r   r   s    r!   meanBernoulli.meanN   s    zzr#   c                     U R                   S:  R                  U R                   5      n[        XR                   S:H  '   U$ )Ng      ?)r   tor   )r   modes     r!   r:   Bernoulli.modeR   s5    

c!%%djj1"%ZZ3r#   c                 :    U R                   SU R                   -
  -  $ )N   r4   r5   s    r!   varianceBernoulli.varianceX   s    zzQ^,,r#   c                 *    [        U R                  SS9$ NT)	is_binary)r
   r   r5   s    r!   r   Bernoulli.logits\   s    tzzT::r#   c                 *    [        U R                  SS9$ rA   )r	   r   r5   s    r!   r   Bernoulli.probs`   s    t{{d;;r#   c                 6    U R                   R                  5       $ r-   )r   r   r5   s    r!   param_shapeBernoulli.param_shaped   s    {{!!r#   c                     U R                  U5      n[        R                  " 5          [        R                  " U R                  R                  U5      5      sS S S 5        $ ! , (       d  f       g = fr-   )_extended_shaper   no_grad	bernoullir   r'   )r   sample_shapeshapes      r!   sampleBernoulli.sampleh   s@    $$\2]]_??4::#4#4U#;< __s   /A  
A.c                     U R                   (       a  U R                  U5        [        U R                  U5      u  p![	        X!SS9* $ Nnone)	reduction)r(   _validate_sampler   r   r   )r   valuer   s      r!   log_probBernoulli.log_probm   s;    !!%(%dkk590&QQQr#   c                 @    [        U R                  U R                  SS9$ rR   )r   r   r   r5   s    r!   entropyBernoulli.entropys   s    /KKv
 	
r#   c                     [         R                  " SU R                  R                  U R                  R                  S9nUR                  SS[        U R                  5      -  -   5      nU(       a  UR                  SU R                  -   5      nU$ )N   )dtypedevice))r=   )	r   aranger   r^   r_   viewlen_batch_shaper'   )r   r'   valuess      r!   enumerate_supportBernoulli.enumerate_supportx   sl    at{{'8'8ASASTUTC0A0A,B%BBC]]54+<+<#<=Fr#   c                 D    [         R                  " U R                  5      4$ r-   )r   logitr   r5   s    r!   _natural_paramsBernoulli._natural_params   s    DJJ'))r#   c                 V    [         R                  " [         R                  " U5      5      $ r-   )r   log1pexp)r   xs     r!   _log_normalizerBernoulli._log_normalizer   s    {{599Q<((r#   )r   r   r   )NNNr-   )T)$__name__
__module____qualname____firstlineno____doc__r   unit_intervalrealarg_constraintsbooleansupporthas_enumerate_support_mean_carrier_measurer   r'   r0   propertyr   r6   r:   r>   r   r   r   r   r   rG   rO   rW   rZ   rf   tuplerj   rp   __static_attributes____classcell__)r    s   @r!   r   r      s6   & !, 9 9[EUEUVO!!G C$0 f   f  
 -& - - ; ; ; <v < < "UZZ " " #(**, =
R

 *v * *) )r#   )r   r   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r   r	   r
   torch.nn.functionalr   torch.typesr   __all__r    r#   r!   <module>r      s<      + <  A  -q)! q)r#   