3
O4W^ð  ã               @   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mZmZmZ G dd	„ d	eƒZG d
d„ deƒZdS )é    N)ÚNumber)Úconstraints)ÚDistribution)ÚTransformedDistribution)ÚSigmoidTransform)Úbroadcast_allÚprobs_to_logitsÚlogits_to_probsÚlazy_propertyÚclamp_probsc                   sˆ   e Zd ZdZejejdœZej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jƒ fdd„Zdd„ Z‡  ZS )ÚLogitRelaxedBernoullia�  
    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ÚlogitsNc                sš   || _ |d k|d kkrtdƒ‚|d k	r>t|tƒ}t|ƒ\| _nt|tƒ}t|ƒ\| _|d k	rb| jn| j| _|rxtj	ƒ }n
| jj
ƒ }tt| ƒj||d� d S )Nz;Either `probs` or `logits` must be specified, but not both.)Úvalidate_args)ÚtemperatureÚ
ValueErrorÚ
isinstancer   r   r   r   Ú_paramÚtorchÚSizeÚsizeÚsuperr   Ú__init__)Úselfr   r   r   r   Z	is_scalarÚbatch_shape)Ú	__class__© úF/tmp/pip-build-e66j97b2/torch/torch/distributions/relaxed_bernoulli.pyr   !   s    



zLogitRelaxedBernoulli.__init__c                s~   | j t|ƒ}tj|ƒ}| j|_d| jkr>| jj|ƒ|_|j|_d| jkr^| j	j|ƒ|_	|j	|_t
t|ƒj|dd� | j|_|S )Nr   r   F)r   )Ú_get_checked_instancer   r   r   r   Ú__dict__r   Úexpandr   r   r   r   Ú_validate_args)r   r   Ú	_instanceÚnew)r   r   r   r    2   s    


zLogitRelaxedBernoulli.expandc             O   s   | j j||ŽS )N)r   r#   )r   ÚargsÚkwargsr   r   r   Ú_new@   s    zLogitRelaxedBernoulli._newc             C   s   t | jdd�S )NT)Ú	is_binary)r   r   )r   r   r   r   r   C   s    zLogitRelaxedBernoulli.logitsc             C   s   t | jdd�S )NT)r'   )r	   r   )r   r   r   r   r   G   s    zLogitRelaxedBernoulli.probsc             C   s
   | j jƒ S )N)r   r   )r   r   r   r   Úparam_shapeK   s    z!LogitRelaxedBernoulli.param_shapec             C   s\   | j |ƒ}t| jj|ƒƒ}ttj||j|jd�ƒ}|jƒ | j	ƒ  |jƒ  | j	ƒ  | j
 S )N)ÚdtypeÚdevice)Z_extended_shaper   r   r    r   Zrandr)   r*   ÚlogÚlog1pr   )r   Zsample_shapeÚshaper   Zuniformsr   r   r   ÚrsampleO   s    
zLogitRelaxedBernoulli.rsamplec             C   sN   | j r| j|ƒ t| j|ƒ\}}||j| jƒ }| jjƒ | d|jƒ jƒ   S )Né   )	r!   Z_validate_sampler   r   Úmulr   r+   Úexpr,   )r   Úvaluer   Zdiffr   r   r   Úlog_probU   s
    
zLogitRelaxedBernoulli.log_prob)NNN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úunit_intervalÚrealÚarg_constraintsÚsupportr   r    r&   r
   r   r   Úpropertyr(   r   r   r.   r3   Ú__classcell__r   r   )r   r   r   
   s   
r   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 )ÚRelaxedBernoullia®  
    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::

        >>> 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   r   TNc                s(   t |||ƒ}tt| ƒj|tƒ |d� d S )N)r   )r   r   r>   r   r   )r   r   r   r   r   Ú	base_dist)r   r   r   r   u   s    zRelaxedBernoulli.__init__c                s    | j t|ƒ}tt| ƒj||d�S )N)r"   )r   r>   r   r    )r   r   r"   r#   )r   r   r   r    {   s    zRelaxedBernoulli.expandc             C   s   | j jS )N)r?   r   )r   r   r   r   r      s    zRelaxedBernoulli.temperaturec             C   s   | j jS )N)r?   r   )r   r   r   r   r   ƒ   s    zRelaxedBernoulli.logitsc             C   s   | j jS )N)r?   r   )r   r   r   r   r   ‡   s    zRelaxedBernoulli.probs)NNN)N)r4   r5   r6   r7   r   r8   r9   r:   r;   Zhas_rsampler   r    r<   r   r   r   r=   r   r   )r   r   r>   ]   s   
r>   )r   Znumbersr   Ztorch.distributionsr   Z torch.distributions.distributionr   Z,torch.distributions.transformed_distributionr   Ztorch.distributions.transformsr   Ztorch.distributions.utilsr   r   r	   r
   r   r   r>   r   r   r   r   Ú<module>   s   S