
    Ǆ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y)    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logitsTc                     t        ||      | _        || _        | j                  j                  }| j                  j                  dd  }t
        |   |||       y )Nvalidate_args)r   _categoricaltemperaturebatch_shapeparam_shapesuper__init__)selfr   r   r   r   r   event_shape	__class__s          o/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributions/relaxed_categorical.pyr   zExpRelaxedCategorical.__init__+   sW    'v6&''33''33BC8kO    c                 "   | j                  t        |      }t        j                  |      }| 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jj-**,,33K@#S2)) 	3 	
 "00
r   c                 :     | j                   j                  |i |S N)r   _new)r   argskwargss      r   r+   zExpRelaxedCategorical._new=   s     %t  %%t6v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                 Z   | j                  |      }t        t        j                  || j                  j
                  | j                  j                              }|j                          j                          }| j                  |z   | j                  z  }||j                  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JJuDKK$5$5dkk>P>PQ
  ||~&++-.++'4+;+;;((R(>>>r   c                    | j                   j                  }| j                  r| j                  |       t	        | j
                  |      \  }}t        j                  | j                  t        |            j                         | j                  j                         j                  |dz
         z
  }||j                  | j                        z
  }||j                  dd      z
  j                  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    ))!!%(%dkk59OOeAh

&(T%%))+//!a%9:	 4#3#344R>>CCB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      s    * !, 3 3{?V?VWO  KP	7 - - ( ( ' ' -7EJJL ?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   Tc                 X    t        ||||      }t        | 	  |t               |       y )Nr   )r   r   r   r   )r   r   r   r   r   	base_distr   s         r   r   z!RelaxedOneHotCategorical.__init__z   s.    )m
	 	LN-Pr   c                 R    | j                  t        |      }t        |   ||      S )N)r'   )r!   r   r   r$   r&   s       r   r$   zRelaxedOneHotCategorical.expand   s)    (()A9Mw~kS~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   sw    & !, 3 3{?V?VWO!!GKQ: * * % % $ $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      sF     + 7 9 P 7 @  #$>
?P!L P!f,$6 ,$r   