a
    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	gZd
d ZG dd deZG dd	 d	eZdS )    )OptionalN)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   K/var/www/auris/lib/python3.9/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eee dd fddZd fd	d
	Zd eedddZdd ZeedddZeedddZeedddZdd Zeee d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      TN)r   validate_argsreturnc                    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   r   )dim
ValueErrorr   shapesuper__init__)selfr   r   batch_shapeevent_shape	__class__r   r   r&   ;   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-   H   s    

zDirichlet.expandr   )sample_shaper    c                 C   s    |  |}| j|}t|S r   )Z_extended_shaper   r-   r   apply)r'   r0   r$   r   r   r   r   rsampleR   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_probW   s    
zDirichlet.log_prob)r    c                 C   s   | j | j dd S r
   )r   r   r'   r   r   r   mean`   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>   d   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   variancen   s    zDirichlet.variancec                 C   sb   | j d}| j d}t| j dt| || t|  | j d t| j  d S )Nr   r3   )r   sizer   r   r4   Zdigamma)r'   kZa0r   r   r   entropyw   s    zDirichlet.entropyc                 C   s   | j fS r   )r   r7   r   r   r   _natural_params   s    zDirichlet._natural_paramsc                 C   s   |  dt |d S )Nr   )r4   r   r   )r'   r   r   r   r   _log_normalizer   s    zDirichlet._log_normalizer)N)N)r   )r   r   r   __doc__r   ZindependentZpositiveZarg_constraintsZsimplexZsupportZhas_rsampler   r   boolr&   r-   r   r2   r6   propertyr8   r>   rB   rE   tuplerF   rG   __classcell__r   r   r*   r   r	   %   s0    
		
)typingr   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   