o
    h                     @   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)sum	expand_astorch_dirichlet_grad)xconcentrationgrad_outputtotalgrad r   q/var/www/html/construction_image-detection-poc/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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$   )
_get_checked_instancer   r   Sizer   expandr-   r)   r*   _validate_args)r+   r,   	_instancenewr.   r   r   r2   B   s   

zDirichlet.expandr   sample_shapereturnc                 C   s    |  |}| j|}t|S r   )_extended_shaper   r2   r   apply)r+   r6   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
   )r3   _validate_sampler   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   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   varianceh   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   entropyq   s   zDirichlet.entropyc                 C   s   | j fS r   )r   rA   r   r   r   _natural_params{   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*   r2   r   r   r:   r@   propertyrB   rL   rQ   rV   tuplerW   rX   __classcell__r   r   r.   r   r   #   s(    	
		
)r   r   torch.autogradr   torch.autograd.functionr   torch.distributionsr   torch.distributions.exp_familyr   torch.typesr   __all__r   r   r   r   r   r   r   <module>   s   