
    rh	                         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 d	lmZmZ d d
lmZ ddgZ G d de	      Z G d de      Zy)    )OptionalN)Tensor)constraints)Categorical)Distribution)TransformedDistribution)ExpTransform)broadcast_allclamp_probs)_sizeExpRelaxedCategoricalRelaxedOneHotCategoricalc                   @    e Zd ZdZej
                  ej                  dZej                  ZdZ		 	 	 dde
dee
   dee
   dee   d	df
 fd
Zd fd	Zd Zed	ej$                  fd       Zed	e
fd       Zed	e
fd       Z ej$                         fded	e
fdZd Z xZS )r   a  
    Creates a ExpRelaxedCategorical parameterized by
    :attr:`temperature`, and either :attr:`probs` or :attr:`logits` (but not both).
    Returns the log of a point in the simplex. Based on the interface to
    :class:`OneHotCategorical`.

    Implementation based on [1].

    See also: :func:`torch.distributions.OneHotCategorical`

    Args:
        temperature (Tensor): relaxation temperature
        probs (Tensor): event probabilities
        logits (Tensor): unnormalized log probability for each event

    [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logitsTNtemperaturer   r   validate_argsreturnc                     t        ||      | _        || _        | j                  j                  }| j                  j                  dd  }t
        |   |||       y )Nr   )r   _categoricalr   batch_shapeparam_shapesuper__init__)selfr   r   r   r   r   event_shape	__class__s          z/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/distributions/relaxed_categorical.pyr   zExpRelaxedCategorical.__init__/   sY     (v6&''33''33BC8kO    c                 "   | j                  t        |      }t        j                  |      }| j                  |_        | j
                  j                  |      |_        t        t        |#  || j                  d       | j                  |_
        |S )NFr   )_get_checked_instancer   torchSizer   r   expandr   r   r   _validate_argsr   r   	_instancenewr    s       r!   r'   zExpRelaxedCategorical.expand<   s    (()>	Jjj-**,,33K@#S2)) 	3 	
 "00
r"   c                 :     | j                   j                  |i |S N)r   _new)r   argskwargss      r!   r.   zExpRelaxedCategorical._newG   s     %t  %%t6v66r"   c                 .    | j                   j                  S r-   )r   r   r   s    r!   r   z!ExpRelaxedCategorical.param_shapeJ   s      ,,,r"   c                 .    | j                   j                  S r-   )r   r   r2   s    r!   r   zExpRelaxedCategorical.logitsN   s      '''r"   c                 .    | j                   j                  S r-   )r   r   r2   s    r!   r   zExpRelaxedCategorical.probsR   s      &&&r"   sample_shapec                 Z   | j                  |      }t        t        j                  || j                  j
                  | j                  j                              }|j                          j                          }| j                  |z   | j                  z  }||j                  dd      z
  S )N)dtypedevicer   Tdimkeepdim)
_extended_shaper   r%   randr   r7   r8   logr   	logsumexp)r   r5   shapeuniformsgumbelsscoress         r!   rsamplezExpRelaxedCategorical.rsampleV   s    $$\2JJuDKK$5$5dkk>P>PQ
  ||~&++-.++'4+;+;;((R(>>>r"   c                    | j                   j                  }| j                  r| j                  |       t	        | j
                  |      \  }}t        j                  | j                  t        |            j                         | j                  j                         j                  |dz
         z
  }||j                  | j                        z
  }||j                  dd      z
  j                  d      }||z   S )N   r   Tr9   )r   _num_eventsr(   _validate_sampler
   r   r%   	full_liker   floatlgammar>   mulr?   sum)r   valueKr   	log_scalescores         r!   log_probzExpRelaxedCategorical.log_prob_   s    ))!!%(%dkk59OOeAh

&(T%%))+//!a%9:	 4#3#344R>>CCBGy  r"   NNNr-   )__name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintssupporthas_rsampler   r   boolr   r'   r.   propertyr%   r&   r   r   r   r   rD   rR   __classcell__r    s   @r!   r   r      s   , !, 3 3{?V?VWO  K
 #'#'(,PP P  	P
  ~P 
P	7 -UZZ - - ( ( ( 'v ' ' -7EJJL ?E ?V ?
!r"   c                        e Zd ZU dZej
                  ej                  dZej
                  ZdZ	e
ed<   	 	 	 ddedee   dee   d	ee   d
df
 fdZd fd	Zed
efd       Zed
efd       Zed
efd       Z xZS )r   a  
    Creates a RelaxedOneHotCategorical distribution parametrized by
    :attr:`temperature`, and either :attr:`probs` or :attr:`logits`.
    This is a relaxed version of the :class:`OneHotCategorical` distribution, so
    its samples are on simplex, and are reparametrizable.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = RelaxedOneHotCategorical(torch.tensor([2.2]),
        ...                              torch.tensor([0.1, 0.2, 0.3, 0.4]))
        >>> m.sample()
        tensor([ 0.1294,  0.2324,  0.3859,  0.2523])

    Args:
        temperature (Tensor): relaxation temperature
        probs (Tensor): event probabilities
        logits (Tensor): unnormalized log probability for each event
    r   T	base_distNr   r   r   r   r   c                 X    t        ||||      }t        | 	  |t               |       y )Nr   )r   r   r   r	   )r   r   r   r   r   rb   r    s         r!   r   z!RelaxedOneHotCategorical.__init__   s0     *m
	 	LN-Pr"   c                 R    | j                  t        |      }t        |   ||      S )N)r*   )r$   r   r   r'   r)   s       r!   r'   zRelaxedOneHotCategorical.expand   s)    (()A9Mw~kS~99r"   c                 .    | j                   j                  S r-   )rb   r   r2   s    r!   r   z$RelaxedOneHotCategorical.temperature   s    ~~)))r"   c                 .    | j                   j                  S r-   )rb   r   r2   s    r!   r   zRelaxedOneHotCategorical.logits   s    ~~$$$r"   c                 .    | j                   j                  S r-   )rb   r   r2   s    r!   r   zRelaxedOneHotCategorical.probs   s    ~~###r"   rS   r-   )rT   rU   rV   rW   r   rX   rY   rZ   r[   r\   r   __annotations__r   r   r]   r   r'   r^   r   r   r   r_   r`   s   @r!   r   r   l   s    ( !, 3 3{?V?VWO!!GK$$
 #'#'(,
Q
Q 
Q  	
Q
  ~
Q 

Q: *V * * % % % $v $ $r"   )typingr   r%   r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   ,torch.distributions.transformed_distributionr   torch.distributions.transformsr	   torch.distributions.utilsr
   r   torch.typesr   __all__r   r    r"   r!   <module>rs      sL       + 7 9 P 7 @  #$>
?W!L W!t4$6 4$r"   