
    9iD                         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
  S SKJr  S SKJrJrJrJrJr  S S	KJrJrJr  S
S/r " S S
\5      r " S S\
5      rg)    )OptionalUnionN)Tensor)constraints)Distribution)TransformedDistribution)SigmoidTransform)broadcast_allclamp_probslazy_propertylogits_to_probsprobs_to_logits)_Number_sizeNumberLogitRelaxedBernoulliRelaxedBernoullic                   l  ^  \ rS rSrSr\R                  \R                  S.r\R                  r	   SS\
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	\
4S j5       r\S	\
4S j5       r\S	\R.                  4S j5       r\R.                  " 5       4S\S	\
4S jjrS rSrU =r$ )r      aO  
Creates a LogitRelaxedBernoulli distribution parameterized by :attr:`probs`
or :attr:`logits` (but not both), which is the logit of a RelaxedBernoulli
distribution.

Samples are logits of values in (0, 1). See [1] for more details.

Args:
    temperature (Tensor): relaxation temperature
    probs (Number, Tensor): the probability of sampling `1`
    logits (Number, Tensor): the log-odds of sampling `1`

[1] The Concrete Distribution: A Continuous Relaxation of Discrete Random
Variables (Maddison et al., 2017)

[2] Categorical Reparametrization with Gumbel-Softmax
(Jang et al., 2017)
probslogitsNtemperaturer   r   validate_argsreturnc                   > Xl         US L US L :X  a  [        S5      eUb#  [        U[        5      n[	        U5      u  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[        TU ]1  XdS9  g )Nz;Either `probs` or `logits` must be specified, but not both.r   )r   
ValueError
isinstancer   r
   r   r   _paramtorchSizesizesuper__init__)selfr   r   r   r   	is_scalarbatch_shape	__class__s          e/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/relaxed_bernoulli.pyr%   LogitRelaxedBernoulli.__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U R                  Ul        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   LogitRelaxedBernoulli.expandH   s    (()>	Jjj-**dmm#

))+6CICJt}}$++K8CJCJ#S2;e2T!00
r,   c                 :    U R                   R                  " U0 UD6$ N)r    r4   )r&   argskwargss      r*   _newLogitRelaxedBernoulli._newV   s    {{///r,   c                 *    [        U R                  SS9$ NT)	is_binary)r   r   r&   s    r*   r   LogitRelaxedBernoulli.logitsY   s    tzzT::r,   c                 *    [        U R                  SS9$ r=   )r   r   r?   s    r*   r   LogitRelaxedBernoulli.probs]   s    t{{d;;r,   c                 6    U R                   R                  5       $ r7   )r    r#   r?   s    r*   param_shape!LogitRelaxedBernoulli.param_shapea   s    {{!!r,   sample_shapec                 t   U R                  U5      n[        U R                  R                  U5      5      n[        [        R
                  " X#R                  UR                  S95      nUR                  5       U* R                  5       -
  UR                  5       -   U* R                  5       -
  U R                  -  $ )N)dtypedevice)_extended_shaper   r   r0   r!   randrH   rI   loglog1pr   )r&   rF   shaper   uniformss        r*   rsampleLogitRelaxedBernoulli.rsamplee   s    $$\2DJJ--e45JJuKKE
 LLNxi..00599;>5&AQQ 	r,   c                 .   U R                   (       a  U R                  U5        [        U R                  U5      u  p!X!R	                  U R
                  5      -
  nU R
                  R                  5       U-   SUR                  5       R                  5       -  -
  $ )N   )	r1   _validate_sampler
   r   mulr   rL   exprM   )r&   valuer   diffs       r*   log_probLogitRelaxedBernoulli.log_probo   su    !!%(%dkk59		$"2"233##%,q488:3C3C3E/EEEr,   )r    r   r   r   NNNr7   )__name__
__module____qualname____firstlineno____doc__r   unit_intervalrealarg_constraintssupportr   r   r   r   boolr%   r0   r:   r   r   r   propertyr!   r"   rD   r   rP   rY   __static_attributes____classcell__r)   s   @r*   r   r      s    & !, 9 9[EUEUVOG
 2626(,CC ffn-.C vv~./	C
  ~C 
C C40 ; ; ; <v < < "UZZ " " -2JJL E V F Fr,   c                   &  ^  \ rS rSr% Sr\R                  \R                  S.r\R                  r	Sr
\\S'      SS\S\\\\4      S	\\\\4      S
\\   SS4
U 4S jjjrSU 4S jjr\S\4S j5       r\S\4S j5       r\S\4S j5       rSrU =r$ )r   w   a  
Creates a RelaxedBernoulli distribution, parametrized by
:attr:`temperature`, and either :attr:`probs` or :attr:`logits`
(but not both). This is a relaxed version of the `Bernoulli` distribution,
so the values are in (0, 1), and has reparametrizable samples.

Example::

    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> m = RelaxedBernoulli(torch.tensor([2.2]),
    ...                      torch.tensor([0.1, 0.2, 0.3, 0.99]))
    >>> m.sample()
    tensor([ 0.2951,  0.3442,  0.8918,  0.9021])

Args:
    temperature (Tensor): relaxation temperature
    probs (Number, Tensor): the probability of sampling `1`
    logits (Number, Tensor): the log-odds of sampling `1`
r   T	base_distNr   r   r   r   r   c                 L   > [        XU5      n[        TU ]	  U[        5       US9  g )Nr   )r   r$   r%   r	   )r&   r   r   r   r   rl   r)   s         r*   r%   RelaxedBernoulli.__init__   s)     *+fE	$4$6mTr,   c                 J   > U R                  [        U5      n[        TU ]  XS9$ )N)r3   )r.   r   r$   r0   r2   s       r*   r0   RelaxedBernoulli.expand   s'    (()99Ew~k~99r,   c                 .    U R                   R                  $ r7   )rl   r   r?   s    r*   r   RelaxedBernoulli.temperature   s    ~~)))r,   c                 .    U R                   R                  $ r7   )rl   r   r?   s    r*   r   RelaxedBernoulli.logits   s    ~~$$$r,   c                 .    U R                   R                  $ r7   )rl   r   r?   s    r*   r   RelaxedBernoulli.probs   s    ~~###r,    r[   r7   )r\   r]   r^   r_   r`   r   ra   rb   rc   rd   has_rsampler   __annotations__r   r   r   r   re   r%   r0   rf   r   r   r   rg   rh   ri   s   @r*   r   r   w   s    ( !, 9 9[EUEUVO''GK$$
 2626(,UU ffn-.U vv~./	U
  ~U 
U U: *V * * % % % $v $ $r,   )typingr   r   r!   r   torch.distributionsr    torch.distributions.distributionr   ,torch.distributions.transformed_distributionr   torch.distributions.transformsr	   torch.distributions.utilsr
   r   r   r   r   torch.typesr   r   r   __all__r   r   rw   r,   r*   <module>r      sW    "   + 9 P ;  / . #$6
7]FL ]F@2$. 2$r,   