o
    Zh                     @   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 dgZd	d
 ZG dd deZG dd de	ZdS )    N)Tensor)Function)once_differentiable)constraints)ExponentialFamily)_size	Dirichletc                 C   s8   | dd|}t| ||}||| |  dd  S NT)sumZ	expand_astorchZ_dirichlet_grad)xconcentrationgrad_outputtotalZgrad r   L/var/www/auris/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   Z_sample_dirichletZsave_for_backward)ctxr   r   r   r   r   forward   s   
z_Dirichlet.forwardc                 C   s   | j \}}t|||S r   )Z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fddZdd ZedefddZedefddZedefddZdd Zedee f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$   9   s   zDirichlet.__init__c                    sN   |  t|}t|}| j|| j |_tt|j|| jdd | j	|_	|S )NFr   )
Z_get_checked_instancer   r   Sizer   expandr'   r#   r$   _validate_args)r%   r&   Z	_instancenewr(   r   r   r+   B   s   

zDirichlet.expandr   sample_shapereturnc                 C   s    |  |}| j|}t|S r   )Z_extended_shaper   r+   r   apply)r%   r.   r"   r   r   r   r   rsampleL   s   

zDirichlet.rsamplec                 C   sL   | j r| | t| jd |dt| jd t| jd S )N      ?r
   )r,   Z_validate_sampler   Zxlogyr   r   lgamma)r%   valuer   r   r   log_probQ   s   
zDirichlet.log_probc                 C   s   | j | j dd S r	   )r   r   r%   r   r   r   meanZ   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)r    )r   clampr   allr   nnZ
functionalZone_hotZargmaxr"   to)r%   Zconcentrationm1modemaskr   r   r   r=   ^   s   zDirichlet.modec                 C   s0   | j dd}| j || j   |d|d   S )Nr
   T   r   )r   r   pow)r%   Zcon0r   r   r   varianceh   s   zDirichlet.variancec                 C   sb   | j d}| j d}t| j dt| || t|  | j d t| j  d S )Nr
   r2   )r   sizer   r   r3   Zdigamma)r%   kZa0r   r   r   entropyq   s   zDirichlet.entropyc                 C   s   | j fS r   )r   r6   r   r   r   _natural_params{   s   zDirichlet._natural_paramsc                 C   s   |  dt |d S )Nr
   )r3   r   r   )r%   r   r   r   r   _log_normalizer   s   zDirichlet._log_normalizerr   )r   )r   r   r   __doc__r   ZindependentZpositiveZarg_constraintsZsimplexZsupportZhas_rsampler$   r+   r   r   r1   r5   propertyr7   r=   rA   rD   tuplerE   rF   __classcell__r   r   r(   r   r   #   s(    	
		
)r   r   Ztorch.autogradr   Ztorch.autograd.functionr   Ztorch.distributionsr   Ztorch.distributions.exp_familyr   Ztorch.typesr   __all__r   r   r   r   r   r   r   <module>   s   