
    ȅiR$                         S SK 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)Tensor)constraints)ExponentialFamily)broadcast_allclamp_probslazy_propertylogits_to_probsprobs_to_logits) binary_cross_entropy_with_logits)_Number_sizeNumberContinuousBernoullic                   2  ^  \ rS rSrSr\R                  \R                  S.r\R                  r	Sr
Sr    S"S\\-  S-  S\\-  S-  S	\\\4   S
\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\R>                  4S j5       r \R>                  " 5       4S jr!\R>                  " 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                 v  > 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        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X0l        [         TU ]E  XdS9  g )Nz;Either `probs` or `logits` must be specified, but not both.r   z&The parameter probs has invalid valueszlogits is unexpectedly Noner   )
ValueError
isinstancer   r   r   arg_constraintscheckallr   AssertionErrorr   _paramtorchSizesize_limssuper__init__)selfr   r   r   r   	is_scalarbatch_shape	__class__s          b/home/james-whalen/.local/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~$%BCC"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*   r0   ContinuousBernoulli.expand[   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   r3   )r&   argskwargss      r*   _newContinuousBernoulli._newi   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_regionl   sG    yyHHTZZA/$**djjQRm1T
 	
r,   c                     [         R                  " U R                  5       U R                  U R                  S   [         R
                  " U R                  5      -  5      $ )Nr   )r    whererA   r   r#   	ones_liker@   s    r*   
_cut_probsContinuousBernoulli._cut_probsq   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'}'}@)rF   r    rD   r>   
zeros_likegerE   logabslog1ppowr   mathrA   )r&   	cut_probscut_probs_below_halfcut_probs_above_halflog_normxtaylors          r*   _cont_bern_log_norm'ContinuousBernoulli._cont_bern_log_normx   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      $ )NrJ   rK   rI   gUUUUUU?gll?rL   )rF   r    rQ   rO   r   rR   rD   rA   )r&   rT   musrX   rY   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      $ r6   )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      $ )NrK   rJ   rL   rI   gUUUUUU?g?ggjV?)rF   r    rR   rQ   rO   r   rD   rA   )r&   rT   varsrX   rY   s        r*   rb   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      $ ri   )r   r	   r   r@   s    r*   r   ContinuousBernoulli.probs   s    ?4;;$GHHr,   c                 6    U R                   R                  5       $ r6   )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   rs   rt   no_gradicdfr&   sample_shapeshapeus       r*   sampleContinuousBernoulli.sample   sT    $$\2JJuJJ$4$4TZZ=N=NO]]_99Q< __s   $A??
Brz   c                     U R                  U5      n[        R                  " X R                  R                  U R                  R
                  S9nU R                  U5      $ rr   )ru   r    rv   r   rs   rt   rx   ry   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)r1   _validate_sampler   r   r   rZ   )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      $ )NrK   rJ   g        )r1   r   rF   r    rR   rD   rA   r>   rM   rN   rE   )r&   r   rT   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      $ )NrJ   rK   )rF   r    rD   rA   rQ   rO   )r&   r   rT   s      r*   rx   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-
  $ r6   )r    rQ   r   rO   r^   rZ   )r&   
log_probs0
log_probs1s      r*   entropyContinuousBernoulli.entropy   sU    [[$**-
YYtzz*
II01&&()	
r,   c                     U R                   4$ r6   )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   rI   r<   rL   g      8@   g     @)r    r=   r>   r#   r?   rD   rE   rO   rP   specialexpm1rR   )r&   rX   out_unst_regcut_nat_paramsrW   rY   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?Nr6   ),__name__
__module____qualname____firstlineno____doc__r   unit_intervalrealr   support_mean_carrier_measurehas_rsampler   r   tuplefloatboolr%   r0   r9   rA   rF   rZ   propertyr^   rc   rb   r   r   r   r    r!   ro   r}   r   r   r   r   rx   r   r   r   __static_attributes____classcell__)r)   s   @r*   r   r      s   6 !, 9 9[EUEUVO''GK )-)-$2%)"C%"C $&"C E5L!	"C
 d{"C 
"C "CH0


N( If I I ) ) ) J& J J ; ; ; Iv I I "UZZ " " #(**,   -2JJL E V 


 


 v  ; ;r,   )rS   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      sC       + <  A . . !
!d;+ d;r,   