
    IЦiG"                         S SK r S SKJr  S SK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  S/r " S	 S\5      rg)
    N)Number)constraints)ExponentialFamily)broadcast_allclamp_probslazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_sizeContinuousBernoullic                     ^  \ 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	 rS
 rS r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\R6                  " 5       4S jr\R6                  " 5       4S\S\R<                  4S jjrS r S r!S r"S r#\S 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   Tc                 Z  > 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[        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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__s          g/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/distributions/continuous_bernoulli.pyr    ContinuousBernoulli.__init__5   s     TMv~.M  "5&1I)%0MTZ (++G4::4::FJJLL$%MNN$TZZ0DJ"662I*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&   r,   ContinuousBernoulli.expandP   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   r/   )r!   argskwargss      r&   _newContinuousBernoulli._new^   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_regiona   sG    yyHHTZZA/$**djjQRm1T
 	
r(   c                     [         R                  " U R                  5       U R                  U R                  S   [         R
                  " U R                  5      -  5      $ )Nr   )r   wherer=   r   r   	ones_liker<   s    r&   
_cut_probsContinuousBernoulli._cut_probsf   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'}'}@)rB   r   r@   r:   
zeros_likegerA   logabslog1ppowr   mathr=   )r!   	cut_probscut_probs_below_halfcut_probs_above_halflog_normxtaylors          r&   _cont_bern_log_norm'ContinuousBernoulli._cont_bern_log_normm   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      $ )NrF   rG   rE   gUUUUUU?gll?rH   )rB   r   rM   rK   r   rN   r@   r=   )r!   rP   musrT   rU   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      $ r2   )r   sqrtvariancer<   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      $ )NrG   rF   rH   rE   gUUUUUU?g?ggjV?)rB   r   rN   rM   rK   r   r@   r=   )r!   rP   varsrT   rU   s        r&   r^   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   r<   s    r&   r   ContinuousBernoulli.logits   s    tzzT::r(   c                 <    [        [        U R                  SS95      $ re   )r   r	   r   r<   s    r&   r   ContinuousBernoulli.probs   s    ?4;;$GHHr(   c                 6    U R                   R                  5       $ r2   )r   r   r<   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   ro   rp   no_gradicdfr!   sample_shapeshapeus       r&   sampleContinuousBernoulli.sample   sT    $$\2JJuJJ$4$4TZZ=N=NO]]_99Q< __s   $A??
Brv   returnc                     U R                  U5      n[        R                  " X R                  R                  U R                  R
                  S9nU R                  U5      $ rn   )rq   r   rr   r   ro   rp   rt   ru   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)r-   _validate_sampler   r   r   rV   )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      $ )NrG   rF   g        )r-   r   rB   r   rN   r@   r=   r:   rI   rJ   rA   )r!   r   rP   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      $ )NrF   rG   )rB   r   r@   r=   rM   rK   )r!   r   rP   s      r&   rt   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-
  $ r2   )r   rM   r   rK   rZ   rV   )r!   
log_probs0
log_probs1s      r&   entropyContinuousBernoulli.entropy   sU    [[$**-
YYtzz*
II01&&()	
r(   c                     U R                   4$ r2   )r   r<   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   rE   r8   rH   g      8@   g     @)r   r9   r:   r   r;   r@   rA   rK   rL   specialexpm1rN   )r!   rT   out_unst_regcut_nat_paramsrS   rU   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?Nr2   )(__name__
__module____qualname____firstlineno____doc__r   unit_intervalrealr   support_mean_carrier_measurehas_rsampler    r,   r5   r=   rB   rV   propertyrZ   r_   r^   r   r   r   rk   r   r   ry   r   Tensorr}   r   r   rt   r   r   r   __static_attributes____classcell__)r%   s   @r&   r   r      s?   2 !, 9 9[EUEUVO''GK KOC60


N( I I ) ) J J ; ; I I " " #(**,   -2JJL E U\\ 


 


  ; ;r(   )rO   numbersr   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r   r   r	   r
   torch.nn.functionalr   torch.typesr   __all__r    r(   r&   <module>r      s@       + <  A  !
!X;+ X;r(   