U
    ©Ê¹j*  ã                   @   s€   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	gZG d
d„ deƒZG dd	„ d	eƒZdS )é    N)Úconstraints)ÚCategorical)ÚDistribution)ÚTransformedDistribution)ÚExpTransform)Úbroadcast_allÚclamp_probsÚExpRelaxedCategoricalÚRelaxedOneHotCategoricalc                       sŒ   e Zd ZdZejejdœZejZdZ	d‡ fdd„	Z
d‡ fdd„	Zd	d
„ Zedd„ ƒZedd„ ƒZedd„ ƒZ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ÚlogitsTNc                    s@   t ||ƒ| _|| _| jj}| jjdd … }tƒ j|||d� d S )Néÿÿÿÿ©Úvalidate_args)r   Ú_categoricalÚtemperatureÚbatch_shapeÚparam_shapeÚsuperÚ__init__)Úselfr   r   r   r   r   Úevent_shape©Ú	__class__© úf/var/www/html/bwave/python/venv/lib/python3.8/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||ŽS ©N)r   Ú_new)r   ÚargsÚkwargsr   r   r   r&   :   s    zExpRelaxedCategorical._newc                 C   s   | j jS r%   )r   r   ©r   r   r   r   r   =   s    z!ExpRelaxedCategorical.param_shapec                 C   s   | j jS r%   )r   r   r)   r   r   r   r   A   s    zExpRelaxedCategorical.logitsc                 C   s   | j jS r%   )r   r   r)   r   r   r   r   E   s    zExpRelaxedCategorical.probsc                 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   r*   r+   Úlogr   Ú	logsumexp)r   Zsample_shapeÚshapeZuniformsZgumbelsZscoresr   r   r   ÚrsampleI   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   Tr,   )r   Z_num_eventsr!   Z_validate_sampler   r   r   Z	full_liker   ÚfloatÚlgammar.   Úmulr/   Úsum)r   ÚvalueÚKr   Z	log_scaleZscorer   r   r   Úlog_probR   s    
 ÿþzExpRelaxedCategorical.log_prob)NNN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚsimplexÚreal_vectorÚarg_constraintsÚsupportÚhas_rsampler   r    r&   Úpropertyr   r   r   r   r   r1   r9   Ú__classcell__r   r   r   r   r	      s    ÿ


	c                       sl   e Zd ZdZejejdœZejZdZ	d‡ fdd„	Z
d‡ fdd„	Zed	d
„ ƒZedd„ ƒZe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-deterinistic")
        >>> 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                    s(   t ||||d�}tƒ j|tƒ |d� d S )Nr   )r	   r   r   r   )r   r   r   r   r   Ú	base_distr   r   r   r   w   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   s   | j jS r%   )rE   r   r)   r   r   r   r   �   s    z$RelaxedOneHotCategorical.temperaturec                 C   s   | j jS r%   )rE   r   r)   r   r   r   r   …   s    zRelaxedOneHotCategorical.logitsc                 C   s   | j jS r%   )rE   r   r)   r   r   r   r   ‰   s    zRelaxedOneHotCategorical.probs)NNN)N)r:   r;   r<   r=   r   r>   r?   r@   rA   rB   r   r    rC   r   r   r   rD   r   r   r   r   r
   _   s   

)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   Ú__all__r	   r
   r   r   r   r   Ú<module>   s   S