o
    ߗi                     @   sv   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gZdd	 ZG d
d deZG dd deZdS )    N)Function)once_differentiable)constraints)ExponentialFamily)_size	Dirichletc                 C   s8   | dd|}t| ||}||| |  dd  S NT)sum	expand_astorch_dirichlet_grad)xconcentrationgrad_outputtotalgrad r   [/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/distributions/dirichlet.py_Dirichlet_backward   s   r   c                   @   s(   e Zd Zedd Zeedd ZdS )
_Dirichletc                 C   s   t |}| || |S N)r   _sample_dirichletsave_for_backward)ctxr   r   r   r   r   forward   s   
z_Dirichlet.forwardc                 C   s   | j \}}t|||S r   )saved_tensorsr   )r   r   r   r   r   r   r   backward   s   
z_Dirichlet.backwardN)__name__
__module____qualname__staticmethodr   r   r   r   r   r   r   r      s    
r   c                       s   e Zd ZdZdeejdiZejZ	dZ
d fdd	Zd fdd		ZddedejfddZdd Zedd Zedd Zedd Zdd Zedd Zdd Z  ZS )r   a  
    Creates a Dirichlet distribution parameterized by concentration :attr:`concentration`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = Dirichlet(torch.tensor([0.5, 0.5]))
        >>> m.sample()  # Dirichlet distributed with concentration [0.5, 0.5]
        tensor([ 0.1046,  0.8954])

    Args:
        concentration (Tensor): concentration parameter of the distribution
            (often referred to as alpha)
    r      TNc                    sN   |  dk r
td|| _|jd d |jdd  }}t j|||d d S )Nr"   z;`concentration` parameter must be at least one-dimensional.r	   validate_args)dim
ValueErrorr   shapesuper__init__)selfr   r$   batch_shapeevent_shape	__class__r   r   r)   7   s   zDirichlet.__init__c                    sN   |  t|}t|}| j|| j |_tt|j|| jdd | j	|_	|S )NFr#   )
_get_checked_instancer   r   Sizer   expandr,   r(   r)   _validate_args)r*   r+   	_instancenewr-   r   r   r1   @   s   

zDirichlet.expandr   sample_shapereturnc                 C   s    |  |}| j|}t|S r   )_extended_shaper   r1   r   apply)r*   r5   r'   r   r   r   r   rsampleJ   s   

zDirichlet.rsamplec                 C   sL   | j r| | t| jd |dt| jd t| jd S )N      ?r	   )r2   _validate_sampler   xlogyr   r
   lgamma)r*   valuer   r   r   log_probO   s   
zDirichlet.log_probc                 C   s   | j | j dd S r   )r   r
   r*   r   r   r   meanX   s   zDirichlet.meanc                 C   sd   | j d jdd}||dd }| j dk jdd}tjj|| jdd|j	d 
|||< |S )Nr"   g        )minr	   T)axis)r   clampr
   allr   nn
functionalone_hotargmaxr'   to)r*   concentrationm1modemaskr   r   r   rL   \   s   zDirichlet.modec                 C   s0   | j dd}| j || j   |d|d   S )Nr	   T   r"   )r   r
   pow)r*   con0r   r   r   variancef   s   zDirichlet.variancec                 C   sb   | j d}| j d}t| j dt| || t|  | j d t| j  d S )Nr	   r:   )r   sizer
   r   r=   digamma)r*   ka0r   r   r   entropyo   s   zDirichlet.entropyc                 C   s   | j fS r   )r   r@   r   r   r   _natural_paramsy   s   zDirichlet._natural_paramsc                 C   s   |  dt |d S )Nr	   )r=   r
   r   )r*   r   r   r   r   _log_normalizer}   s   zDirichlet._log_normalizerr   )r   )r   r   r    __doc__r   independentpositivearg_constraintssimplexsupporthas_rsampler)   r1   r   r   Tensorr9   r?   propertyrA   rL   rQ   rV   rW   rX   __classcell__r   r   r-   r   r   "   s(    	
	

	


)r   torch.autogradr   torch.autograd.functionr   torch.distributionsr   torch.distributions.exp_familyr   torch.typesr   __all__r   r   r   r   r   r   r   <module>   s   