o
    h	                     @   s   d dl 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 d d	lmZmZ d d
lmZ ddgZG dd de	ZG dd deZdS )    )OptionalN)Tensor)constraints)Categorical)Distribution)TransformedDistribution)ExpTransform)broadcast_allclamp_probs)_sizeExpRelaxedCategoricalRelaxedOneHotCategoricalc                       s   e Zd ZdZejejdZejZdZ				dde
dee
 dee
 dee d	df
 fd
dZd fdd	Zdd Zed	ejfddZed	e
fddZed	e
fddZe fded	e
fddZdd Z  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logitsTNtemperaturer   r   validate_argsreturnc                    s@   t ||| _|| _| jj}| jjdd  }t j|||d d S )Nr   )r   _categoricalr   batch_shapeparam_shapesuper__init__)selfr   r   r   r   r   event_shape	__class__ m/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torch/distributions/relaxed_categorical.pyr   /   s
   zExpRelaxedCategorical.__init__c                    sP   |  t|}t|}| j|_| j||_tt|j|| j	dd | j
|_
|S )NFr   )_get_checked_instancer   torchSizer   r   expandr   r   r   _validate_argsr   r   	_instancenewr   r   r    r$   <   s   

zExpRelaxedCategorical.expandc                 O   s   | j j|i |S N)r   _new)r   argskwargsr   r   r    r*   G   s   zExpRelaxedCategorical._newc                 C      | j jS r)   )r   r   r   r   r   r    r   J      z!ExpRelaxedCategorical.param_shapec                 C   r-   r)   )r   r   r.   r   r   r    r   N   r/   zExpRelaxedCategorical.logitsc                 C   r-   r)   )r   r   r.   r   r   r    r   R   r/   zExpRelaxedCategorical.probssample_shapec                 C   sX   |  |}ttj|| jj| jjd}|    }| j| | j }||j	ddd S )N)dtypedevicer   TdimZkeepdim)
Z_extended_shaper
   r"   Zrandr   r1   r2   logr   	logsumexp)r   r0   shapeZuniformsZgumbelsZscoresr   r   r    rsampleV   s   
zExpRelaxedCategorical.rsamplec                 C   s   | j j}| jr| | t| j|\}}t| jt	|
 | j |d   }||| j }||jddd d}|| S )N   r   Tr3   )r   Z_num_eventsr%   Z_validate_sampler	   r   r"   Z	full_liker   floatlgammar5   mulr6   sum)r   valueKr   Z	log_scaleZscorer   r   r    log_prob_   s   

zExpRelaxedCategorical.log_probNNNr)   )__name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintssupporthas_rsampler   r   boolr   r$   r*   propertyr"   r#   r   r   r   r   r8   r@   __classcell__r   r   r   r    r      s<    	c                       s   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 dee d	ee d
df
 fddZd fdd	Zed
efddZed
efddZed
efddZ  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   T	base_distNr   r   r   r   r   c                    s(   t ||||d}t j|t |d d S )Nr   )r   r   r   r   )r   r   r   r   r   rN   r   r   r    r      s   z!RelaxedOneHotCategorical.__init__c                    s   |  t|}t j||dS )N)r'   )r!   r   r   r$   r&   r   r   r    r$      s   zRelaxedOneHotCategorical.expandc                 C   r-   r)   )rN   r   r.   r   r   r    r      r/   z$RelaxedOneHotCategorical.temperaturec                 C   r-   r)   )rN   r   r.   r   r   r    r      r/   zRelaxedOneHotCategorical.logitsc                 C   r-   r)   )rN   r   r.   r   r   r    r      r/   zRelaxedOneHotCategorical.probsrA   r)   )rB   rC   rD   rE   r   rF   rG   rH   rI   rJ   r   __annotations__r   r   rK   r   r$   rL   r   r   r   rM   r   r   r   r    r   l   s6   
 )typingr   r"   r   Ztorch.distributionsr   Ztorch.distributions.categoricalr   Z torch.distributions.distributionr   Z,torch.distributions.transformed_distributionr   Ztorch.distributions.transformsr   Ztorch.distributions.utilsr	   r
   Ztorch.typesr   __all__r   r   r   r   r   r    <module>   s   Z