
    }YhD                         d dl mZmZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZmZ d d	lmZmZmZ d
dgZ G d d
e          Z G d de
          ZdS )    )OptionalUnionN)Tensor)constraints)Distribution)TransformedDistribution)SigmoidTransform)broadcast_allclamp_probslazy_propertylogits_to_probsprobs_to_logits)_Number_sizeNumberLogitRelaxedBernoulliRelaxedBernoullic                   T    e Zd ZdZej        ej        dZej        Z	 	 	 dde	de
ee	ef                  de
ee	ef                  de
e         ddf
 fd	Zd fd
	Zd Zede	fd            Zede	fd            Zedej        fd            Z ej                    fdede	fdZd Z xZS )r   a  
    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                    || _         |d u |d u k    rt          d          |,t          |t                    }t	          |          \  | _        n/|J t          |t                    }t	          |          \  | _        || j        n| j        | _        |rt          j	                    }n| j        
                                }t                                          ||           d S )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          w/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/distributions/relaxed_bernoulli.pyr$   zLogitRelaxedBernoulli.__init__.   s     'TMv~..M   "5'22I)%00MTZZ%%%"6733I*622NT[$)$5djj4; 	-*,,KK+**,,KMBBBBB    c                    |                      t          |          }t          j        |          }| j        |_        d| j        v r+| j                            |          |_        |j        |_        d| j        v r+| j	                            |          |_	        |j	        |_        t          t          |                              |d           | j        |_        |S )Nr   r   Fr   )_get_checked_instancer   r    r!   r   __dict__r   expandr   r   r#   r$   _validate_argsr%   r'   	_instancenewr(   s       r)   r.   zLogitRelaxedBernoulli.expandH   s    (()>	JJj--*dm##
))+66CICJt}$$++K88CJCJ#S))22;e2TTT!0
r*   c                 &     | j         j        |i |S N)r   r2   )r%   argskwargss      r)   _newzLogitRelaxedBernoulli._newV   s    t{////r*   c                 .    t          | j        d          S NT)	is_binary)r   r   r%   s    r)   r   zLogitRelaxedBernoulli.logitsY   s    tzT::::r*   c                 .    t          | j        d          S r9   )r   r   r;   s    r)   r   zLogitRelaxedBernoulli.probs]   s    t{d;;;;r*   c                 4    | j                                         S r4   )r   r"   r;   s    r)   param_shapez!LogitRelaxedBernoulli.param_shapea   s    {!!!r*   sample_shapec                    |                      |          }t          | j                            |                    }t          t	          j        ||j        |j                            }|                                | 	                                z
  |                                z   | 	                                z
  | j
        z  S )N)dtypedevice)_extended_shaper   r   r.   r    randrA   rB   loglog1pr   )r%   r?   shaper   uniformss        r)   rsamplezLogitRelaxedBernoulli.rsamplee   s    $$\22DJ--e4455JuEKEEE
 
 LLNNxi..000599;;>5&AQAQQ 	r*   c                 0   | j         r|                     |           t          | j        |          \  }}||                    | j                  z
  }| j                                        |z   d|                                                                z  z
  S )N   )	r/   _validate_sampler
   r   mulr   rE   exprF   )r%   valuer   diffs       r)   log_probzLogitRelaxedBernoulli.log_probo   s     	)!!%(((%dk599		$"2333##%%,q488::3C3C3E3E/EEEr*   NNNr4   )__name__
__module____qualname____doc__r   unit_intervalrealarg_constraintssupportr   r   r   r   boolr$   r.   r7   r   r   r   propertyr    r!   r>   r   rI   rQ   __classcell__r(   s   @r)   r   r      s        & !, 9[EUVVOG
 2626(,C CC ffn-.C vv~./	C
  ~C 
C C C C C C4     0 0 0 ; ; ; ; ]; <v < < < ]< "UZ " " " X" -7EJLL  E V    F F F F F F Fr*   c                        e Zd ZU dZej        ej        dZej        ZdZ	e
ed<   	 	 	 ddedeeeef                  deeeef                  d	ee         d
df
 fdZd fd	Zed
efd            Zed
efd            Zed
efd            Z xZS )r   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                     t          |||          }t                                          |t                      |           d S )Nr   )r   r#   r$   r	   )r%   r   r   r   r   r`   r(   s         r)   r$   zRelaxedBernoulli.__init__   sB     *+ufEE	$4$6$6mTTTTTr*   c                     |                      t          |          }t                                          ||          S )N)r1   )r,   r   r#   r.   r0   s       r)   r.   zRelaxedBernoulli.expand   s3    (()99EEww~~kS~999r*   c                     | j         j        S r4   )r`   r   r;   s    r)   r   zRelaxedBernoulli.temperature   s    ~))r*   c                     | j         j        S r4   )r`   r   r;   s    r)   r   zRelaxedBernoulli.logits   s    ~$$r*   c                     | j         j        S r4   )r`   r   r;   s    r)   r   zRelaxedBernoulli.probs   s    ~##r*   rR   r4   )rS   rT   rU   rV   r   rW   rX   rY   rZ   has_rsampler   __annotations__r   r   r   r   r[   r$   r.   r\   r   r   r   r]   r^   s   @r)   r   r   w   sv         ( !, 9[EUVVO'GK$$$$
 2626(,U UU ffn-.U vv~./	U
  ~U 
U U U U U U: : : : : : *V * * * X* % % % % X% $v $ $ $ X$ $ $ $ $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    r*   r)   <module>rq      sc   " " " " " " " "        + + + + + + 9 9 9 9 9 9 P P P P P P ; ; ; ; ; ;              / . . . . . . . . . #$6
7]F ]F ]F ]F ]FL ]F ]F ]F@2$ 2$ 2$ 2$ 2$. 2$ 2$ 2$ 2$ 2$r*   