
    ȅi                         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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Number	Bernoullic            	         ^  \ rS rSrSr\R                  \R                  S.r\R                  r
SrSr   SS\\-  S-  S\\-  S-  S	\S-  S
S4U 4S j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
\R6                  4S j5       r\R6                  " 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      aP  
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                   > US L US L :X  a  [        S5      eUb#  [        U[        5      n[        U5      u  U l        O0Uc  [        S5      e[        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 ]1  XSS9  g )Nz;Either `probs` or `logits` must be specified, but not both.zlogits is unexpectedly Noner   )
ValueError
isinstancer   r   r   AssertionErrorr   _paramtorchSizesizesuper__init__)selfr   r   r   	is_scalarbatch_shape	__class__s         W/home/james-whalen/.local/lib/python3.13/site-packages/torch/distributions/bernoulli.pyr   Bernoulli.__init__/   s     TMv~.M  "5'2I)%0MTZ~$%BCC"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.expandJ   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._newW   s    {{///r&   c                     U R                   $ r0   r   r    s    r$   meanBernoulli.meanZ   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.mode^   s5    

c!%%djj1"%ZZ3r&   c                 :    U R                   SU R                   -
  -  $ )N   r6   r7   s    r$   varianceBernoulli.varianced   s    zzQ^,,r&   c                 *    [        U R                  SS9$ NT)	is_binary)r
   r   r7   s    r$   r   Bernoulli.logitsh   s    tzzT::r&   c                 *    [        U R                  SS9$ rC   )r	   r   r7   s    r$   r   Bernoulli.probsl   s    t{{d;;r&   c                 6    U R                   R                  5       $ r0   )r   r   r7   s    r$   param_shapeBernoulli.param_shapep   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r0   )_extended_shaper   no_grad	bernoullir   r*   )r    sample_shapeshapes      r$   sampleBernoulli.samplet   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_proby   s;    !!%(%dkk590&QQQr&   c                 @    [        U R                  U R                  SS9$ rT   )r   r   r   r7   s    r$   entropyBernoulli.entropy   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`   ra   viewlen_batch_shaper*   )r    r*   valuess      r$   enumerate_supportBernoulli.enumerate_support   sl    at{{'8'8ASASTUTC0A0A,B%BBC]]54+<+<#<=Fr&   c                 D    [         R                  " U R                  5      4$ r0   )r   logitr   r7   s    r$   _natural_paramsBernoulli._natural_params   s    DJJ'))r&   c                 V    [         R                  " [         R                  " U5      5      $ r0   )r   log1pexp)r    xs     r$   _log_normalizerBernoulli._log_normalizer   s    {{599Q<((r&   )r   r   r   )NNNr0   )T)&__name__
__module____qualname____firstlineno____doc__r   unit_intervalrealarg_constraintsbooleansupporthas_enumerate_support_mean_carrier_measurer   r   boolr   r*   r3   propertyr8   r<   r@   r   r   r   r   r   rI   rQ   rY   r\   rh   tuplerl   rr   __static_attributes____classcell__)r#   s   @r$   r   r      s   * !, 9 9[EUEUVO!!G  )-)-%)	C%C $&C d{	C
 
C C6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   r   __all__r    r&   r$   <module>r      s>      + <  A ' -})! })r&   