
    קg                         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 d dlmZ d	d
gZ G d d	e          Z G d d
e          ZdS )    N)constraints)Categorical)Distribution)TransformedDistribution)ExpTransform)broadcast_allclamp_probs)_sizeExpRelaxedCategoricalRelaxedOneHotCategoricalc                        e Zd ZdZej        ej        dZej        ZdZ	d fd	Z
d fd	Zd Zed             Zed	             Zed
             Z ej                    fdedej        fdZd Z xZS )r   a  
    Creates a ExpRelaxedCategorical parameterized by
    :attr:`temperature`, and either :attr:`probs` or :attr:`logits` (but not both).
    Returns the log of a point in the simplex. Based on the interface to
    :class:`OneHotCategorical`.

    Implementation based on [1].

    See also: :func:`torch.distributions.OneHotCategorical`

    Args:
        temperature (Tensor): relaxation temperature
        probs (Tensor): event probabilities
        logits (Tensor): unnormalized log probability for each event

    [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logitsTNc                     t          ||          | _        || _        | j        j        }| j        j        dd          }t                                          |||           d S )Nvalidate_args)r   _categoricaltemperaturebatch_shapeparam_shapesuper__init__)selfr   r   r   r   r   event_shape	__class__s          c/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/distributions/relaxed_categorical.pyr   zExpRelaxedCategorical.__init__+   sa    'v66&'3'3BCC8kOOOOO    c                 4   |                      t          |          }t          j        |          }| j        |_        | j                            |          |_        t          t          |                              || j	        d           | j
        |_
        |S )NFr   )_get_checked_instancer   torchSizer   r   expandr   r   r   _validate_argsr   r   	_instancenewr   s       r   r$   zExpRelaxedCategorical.expand2   s    (()>	JJj--*,33K@@#S))22) 	3 	
 	
 	
 "0
r   c                 &     | j         j        |i |S N)r   _new)r   argskwargss      r   r+   zExpRelaxedCategorical._new=   s    %t %t6v666r   c                     | j         j        S r*   )r   r   r   s    r   r   z!ExpRelaxedCategorical.param_shape@   s     ,,r   c                     | j         j        S r*   )r   r   r/   s    r   r   zExpRelaxedCategorical.logitsD   s     ''r   c                     | j         j        S r*   )r   r   r/   s    r   r   zExpRelaxedCategorical.probsH   s     &&r   sample_shapereturnc                 D   |                      |          }t          t          j        || j        j        | j        j                            }|                                                                  }| j        |z   | j        z  }||	                    dd          z
  S )N)dtypedevicer   Tdimkeepdim)
_extended_shaper	   r"   randr   r5   r6   logr   	logsumexp)r   r2   shapeuniformsgumbelsscoress         r   rsamplezExpRelaxedCategorical.rsampleL   s    $$\22JuDK$5dk>PQQQ
 
  ||~~&++--.+'4+;;((R(>>>>r   c                    | j         j        }| j        r|                     |           t	          | j        |          \  }}t          j        | j        t          |                    
                                | j                                                            |dz
             z
  }||                    | j                  z
  }||                    dd          z
                      d          }||z   S )N   r   Tr7   )r   _num_eventsr%   _validate_sampler   r   r"   	full_liker   floatlgammar<   mulr=   sum)r   valueKr   	log_scalescores         r   log_probzExpRelaxedCategorical.log_probU   s    ) 	)!!%(((%dk599OeAhh
 

&((T%))++//!a%99:	 4#3444R>>>CCBGGy  r   NNNr*   )__name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintssupporthas_rsampler   r$   r+   propertyr   r   r   r"   r#   r
   TensorrB   rP   __classcell__r   s   @r   r   r      s7        * !, 3{?VWWO  KP P P P P P	 	 	 	 	 	7 7 7 - - X- ( ( X( ' ' X' -7EJLL ? ?E ?U\ ? ? ? ?
! 
! 
! 
! 
! 
! 
!r   c                        e Zd ZdZej        ej        dZej        ZdZ	d
 fd	Z
d fd	Zed             Zed             Zed	             Z xZS )r   a  
    Creates a RelaxedOneHotCategorical distribution parametrized by
    :attr:`temperature`, and either :attr:`probs` or :attr:`logits`.
    This is a relaxed version of the :class:`OneHotCategorical` distribution, so
    its samples are on simplex, and are reparametrizable.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = RelaxedOneHotCategorical(torch.tensor([2.2]),
        ...                              torch.tensor([0.1, 0.2, 0.3, 0.4]))
        >>> m.sample()
        tensor([ 0.1294,  0.2324,  0.3859,  0.2523])

    Args:
        temperature (Tensor): relaxation temperature
        probs (Tensor): event probabilities
        logits (Tensor): unnormalized log probability for each event
    r   TNc                     t          ||||          }t                                          |t                      |           d S )Nr   )r   r   r   r   )r   r   r   r   r   	base_distr   s         r   r   z!RelaxedOneHotCategorical.__init__z   sK    )m
 
 
	 	LNN-PPPPPr   c                     |                      t          |          }t                                          ||          S )N)r'   )r!   r   r   r$   r&   s       r   r$   zRelaxedOneHotCategorical.expand   s3    (()A9MMww~~kS~999r   c                     | j         j        S r*   )ra   r   r/   s    r   r   z$RelaxedOneHotCategorical.temperature   s    ~))r   c                     | j         j        S r*   )ra   r   r/   s    r   r   zRelaxedOneHotCategorical.logits   s    ~$$r   c                     | j         j        S r*   )ra   r   r/   s    r   r   zRelaxedOneHotCategorical.probs   s    ~##r   rQ   r*   )rR   rS   rT   rU   r   rV   rW   rX   rY   rZ   r   r$   r[   r   r   r   r]   r^   s   @r   r   r   b   s         & !, 3{?VWWO!GKQ Q Q Q Q Q: : : : : : * * X* % % X% $ $ X$ $ $ $ $r   )r"   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   ,torch.distributions.transformed_distributionr   torch.distributions.transformsr   torch.distributions.utilsr   r	   torch.typesr
   __all__r   r    r   r   <module>ro      s    + + + + + + 7 7 7 7 7 7 9 9 9 9 9 9 P P P P P P 7 7 7 7 7 7 @ @ @ @ @ @ @ @       #$>
?P! P! P! P! P!L P! P! P!f,$ ,$ ,$ ,$ ,$6 ,$ ,$ ,$ ,$ ,$r   