
    9i#                         S SK r S SKJrJr  S SKrS SKJr  S SKJr  S SKJ	r	  S SK
JrJrJrJrJr  S SKJr  S SKJrJrJr  S	/r " S
 S	\	5      rg)    N)OptionalUnion)Tensor)constraints)ExponentialFamily)broadcast_allclamp_probslazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_Number_sizeNumberContinuousBernoullic                   :  ^  \ rS rSrSr\R                  \R                  S.r\R                  r	Sr
Sr    S"S\\\\4      S\\\\4      S	\\\4   S
\\   SS4
U 4S jjjrS#U 4S jjrS rS rS 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\ RB                  4S j5       r"\ RB                  " 5       4S jr#\ RB                  " 5       4S\$S\4S jjr%S r&S r'S r(S r)\S\\   4S j5       r*S  r+S!r,U =r-$ )$r      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   TNr   r   limsvalidate_argsreturnc                 d  > US L US L :X  a  [        S5      eUb  [        U[        5      n[        U5      u  U l        UbF  U R
                  S   R                  U R                  5      R                  5       (       d  [        S5      e[        U R                  5      U l        O'Uc   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X0l        [        TU ]A  XdS9  g )Nz;Either `probs` or `logits` must be specified, but not both.r   z&The parameter probs has invalid valuesr   )
ValueError
isinstancer   r   r   arg_constraintscheckallr	   r   _paramtorchSizesize_limssuper__init__)selfr   r   r   r   	is_scalarbatch_shape	__class__s          h/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/continuous_bernoulli.pyr&   ContinuousBernoulli.__init__7   s    TMv~.M  "5'2I)%0MTZ (++G4::4::FJJLL$%MNN$TZZ0DJ%%%"673I*62NT[$)$5djj4;;**,K++**,K
B    c                   > U R                  [        U5      nU R                  Ul        [        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!   r"   __dict__r   expandr    r   r%   r&   _validate_args)r'   r)   	_instancenewr*   s       r+   r1   ContinuousBernoulli.expandW   s    (()<iHJJ	jj-dmm#

))+6CICJt}}$++K8CJCJ!30E0R!00
r-   c                 :    U R                   R                  " U0 UD6$ N)r    r4   )r'   argskwargss      r+   _newContinuousBernoulli._newe   s    {{///r-   c                     [         R                  " [         R                  " U R                  U R                  S   5      [         R
                  " U R                  U R                  S   5      5      $ )Nr      )r!   maxler   r$   gtr'   s    r+   _outside_unstable_region,ContinuousBernoulli._outside_unstable_regionh   sG    yyHHTZZA/$**djjQRm1T
 	
r-   c                     [         R                  " U R                  5       U R                  U R                  S   [         R
                  " U R                  5      -  5      $ )Nr   )r!   whererB   r   r$   	ones_likerA   s    r+   
_cut_probsContinuousBernoulli._cut_probsm   sC    {{))+JJJJqMEOODJJ77
 	
r-   c           	      f   U R                  5       n[        R                  " [        R                  " US5      U[        R                  " U5      5      n[        R                  " [        R
                  " US5      U[        R                  " U5      5      n[        R                  " [        R                  " [        R                  " U* 5      [        R                  " U5      -
  5      5      [        R                  " [        R                  " US5      [        R                  " SU-  5      [        R                  " SU-  S-
  5      5      -
  n[        R                  " U R                  S-
  S5      n[        R                  " S5      SSU-  -   U-  -   n[        R                  " U R                  5       XF5      $ )zLcomputes the log normalizing constant as a function of the 'probs' parameter      ?g              @      ?   gUUUUUU?g'}'}@)rG   r!   rE   r?   
zeros_likegerF   logabslog1ppowr   mathrB   )r'   	cut_probscut_probs_below_halfcut_probs_above_halflog_normxtaylors          r+   _cont_bern_log_norm'ContinuousBernoulli._cont_bern_log_normt   s9   OO%	${{HHY$i1A1A)1L 
  %{{HHY$i1K 
 99IIekk9*-		)0DDE
KKHHY$KK334IIc00367

 IIdjj3&*#)lQ.>">!!CC{{488:HMMr-   c                 J   U R                  5       nUSU-  S-
  -  S[        R                  " U* 5      [        R                  " U5      -
  -  -   nU R                  S-
  nSSS[        R
                  " US5      -  -   U-  -   n[        R                  " U R                  5       X$5      $ )NrK   rL   rJ   gUUUUUU?gll?rM   )rG   r!   rR   rP   r   rS   rE   rB   )r'   rU   musrY   rZ   s        r+   meanContinuousBernoulli.mean   s    OO%	3?S01CKK
#eii	&::5
 
 JJ	K%))Aq/$AAQFF{{488:CHHr-   c                 B    [         R                  " U R                  5      $ r7   )r!   sqrtvariancerA   s    r+   stddevContinuousBernoulli.stddev   s    zz$--((r-   c                    U R                  5       nXS-
  -  [        R                  " SSU-  -
  S5      -  S[        R                  " [        R                  " U* 5      [        R                  " U5      -
  S5      -  -   n[        R                  " U R
                  S-
  S5      nSSSU-  -
  U-  -
  n[        R                  " U R                  5       X$5      $ )NrL   rK   rM   rJ   gUUUUUU?g?ggjV?)rG   r!   rS   rR   rP   r   rE   rB   )r'   rU   varsrY   rZ   s        r+   rc   ContinuousBernoulli.variance   s    OO%	O,uyy#	/!10
 
%))EKK
3eii	6JJANNO IIdjj3&*zMA,==BB{{488:DIIr-   c                 *    [        U R                  SS9$ NT)	is_binary)r   r   rA   s    r+   r   ContinuousBernoulli.logits   s    tzzT::r-   c                 <    [        [        U R                  SS95      $ rj   )r	   r   r   rA   s    r+   r   ContinuousBernoulli.probs   s    ?4;;$GHHr-   c                 6    U R                   R                  5       $ r7   )r    r#   rA   s    r+   param_shapeContinuousBernoulli.param_shape   s    {{!!r-   c                     U R                  U5      n[        R                  " X R                  R                  U R                  R
                  S9n[        R                  " 5          U R                  U5      sS S S 5        $ ! , (       d  f       g = fN)dtypedevice)_extended_shaper!   randr   rt   ru   no_gradicdfr'   sample_shapeshapeus       r+   sampleContinuousBernoulli.sample   sT    $$\2JJuJJ$4$4TZZ=N=NO]]_99Q< __s   $A??
Br{   c                     U R                  U5      n[        R                  " X R                  R                  U R                  R
                  S9nU R                  U5      $ rs   )rv   r!   rw   r   rt   ru   ry   rz   s       r+   rsampleContinuousBernoulli.rsample   sD    $$\2JJuJJ$4$4TZZ=N=NOyy|r-   c                     U R                   (       a  U R                  U5        [        U R                  U5      u  p![	        X!SS9* U R                  5       -   $ )Nnone)	reduction)r2   _validate_sampler   r   r   r[   )r'   valuer   s      r+   log_probContinuousBernoulli.log_prob   sN    !!%(%dkk59-fvNN&&()	
r-   c           
      6   U R                   (       a  U R                  U5        U R                  5       n[        R                  " X!5      [        R                  " SU-
  SU-
  5      -  U-   S-
  SU-  S-
  -  n[        R
                  " U R                  5       X15      n[        R
                  " [        R                  " US5      [        R                  " U5      [        R
                  " [        R                  " US5      [        R                  " U5      U5      5      $ )NrL   rK   g        )r2   r   rG   r!   rS   rE   rB   r?   rN   rO   rF   )r'   r   rU   cdfsunbounded_cdfss        r+   cdfContinuousBernoulli.cdf   s    !!%(OO%	IIi'%))C)OS5[*QQ 9_s"	$
 T%B%B%DdR{{HHUC U#KK,eooe.DnU
 	
r-   c           	      >   U R                  5       n[        R                  " U R                  5       [        R                  " U* USU-  S-
  -  -   5      [        R                  " U* 5      -
  [        R
                  " U5      [        R                  " U* 5      -
  -  U5      $ )NrK   rL   )rG   r!   rE   rB   rR   rP   )r'   r   rU   s      r+   ry   ContinuousBernoulli.icdf   s    OO%	{{))+YJ#	/C2G)HHI++yj)* yy#ekk9*&==	?
 
 	
r-   c                     [         R                  " U R                  * 5      n[         R                  " U R                  5      nU R                  X-
  -  U R                  5       -
  U-
  $ r7   )r!   rR   r   rP   r_   r[   )r'   
log_probs0
log_probs1s      r+   entropyContinuousBernoulli.entropy   sU    [[$**-
YYtzz*
II01&&()	
r-   c                     U R                   4$ r7   )r   rA   s    r+   _natural_params#ContinuousBernoulli._natural_params   s    ~r-   c                    [         R                  " [         R                  " XR                  S   S-
  5      [         R                  " XR                  S   S-
  5      5      n[         R
                  " X!U R                  S   S-
  [         R                  " U5      -  5      n[         R                  " [         R                  " [         R                  R                  U5      5      5      [         R                  " [         R                  " U5      5      -
  nSU-  [         R                  " US5      S-  -   [         R                  " US5      S-  -
  n[         R
                  " X$U5      $ )zLcomputes the log normalizing constant as a function of the natural parameterr   rJ   r=   rM   g      8@   g     @)r!   r>   r?   r$   r@   rE   rF   rP   rQ   specialexpm1rS   )r'   rY   out_unst_regcut_nat_paramsrX   rZ   s         r+   _log_normalizer#ContinuousBernoulli._log_normalizer   s    yyHHQ

1+,ehhq**Q-#:M.N
 djjmc1U__Q5GG
 99IIemm)).9:
IIeii/01 q599Q?T11EIIaOf4LL{{<6::r-   )r$   r    r   r   )NN)gV-?gx&1?Nr7   ).__name__
__module____qualname____firstlineno____doc__r   unit_intervalrealr   support_mean_carrier_measurehas_rsampler   r   r   r   tuplefloatboolr&   r1   r:   rB   rG   r[   propertyr_   rd   rc   r
   r   r   r!   r"   rp   r~   r   r   r   r   ry   r   r   r   __static_attributes____classcell__)r*   s   @r+   r   r      s   4 !, 9 9[EUEUVO''GK 2626$2(,Cffn-.C vv~./C E5L!	C
  ~C 
C C@0


N( If I I ) ) ) J& J J ; ; ; Iv I I "UZZ " " #(**,   -2JJL E V 


 


 v  ; ;r-   )rT   typingr   r   r!   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r	   r
   r   r   torch.nn.functionalr   torch.typesr   r   r   __all__r    r-   r+   <module>r      sF     "   + <  A . . !
!^;+ ^;r-   