
    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	gZ G d
 de	      Z G d d	e      Zy)    )OptionalN)Tensor)constraints)Categorical)Distribution)_sizeOneHotCategorical OneHotCategoricalStraightThroughc            	           e Zd ZdZej
                  ej                  dZej                  Z	dZ
	 	 	 ddee   dee   dee   ddf fd	Zd fd
	Zd Zedefd       Zedefd       Zedefd       Zedefd       Zedefd       Zedefd       Zedej2                  fd       Z ej2                         fdZd Zd ZddZ xZS )r	   a  
    Creates a one-hot categorical distribution parameterized by :attr:`probs` or
    :attr:`logits`.

    Samples are one-hot coded vectors of size ``probs.size(-1)``.

    .. note:: The `probs` argument must be non-negative, finite and have a non-zero sum,
              and it will be normalized to sum to 1 along the last dimension. :attr:`probs`
              will return this normalized value.
              The `logits` argument will be interpreted as unnormalized log probabilities
              and can therefore be any real number. It will likewise be normalized so that
              the resulting probabilities sum to 1 along the last dimension. :attr:`logits`
              will return this normalized value.

    See also: :func:`torch.distributions.Categorical` for specifications of
    :attr:`probs` and :attr:`logits`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = OneHotCategorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
        >>> m.sample()  # equal probability of 0, 1, 2, 3
        tensor([ 0.,  0.,  0.,  1.])

    Args:
        probs (Tensor): event probabilities
        logits (Tensor): event log probabilities (unnormalized)
    )probslogitsTNr   r   validate_argsreturnc                     t        ||      | _        | j                  j                  }| j                  j                  dd  }t        |   |||       y )Nr   )r   _categoricalbatch_shapeparam_shapesuper__init__)selfr   r   r   r   event_shape	__class__s         z/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/distributions/one_hot_categorical.pyr   zOneHotCategorical.__init__1   sQ     (v6''33''33BC8kO    c                     | j                  t        |      }t        j                  |      }| j                  j                  |      |_        t        t        |  || j                  d       | j                  |_	        |S )NFr   )
_get_checked_instancer	   torchSizer   expandr   r   r   _validate_args)r   r   	_instancenewr   s       r   r!   zOneHotCategorical.expand<   st    (():IFjj-,,33K@.)) 	/ 	
 "00
r   c                 :     | j                   j                  |i |S N)r   _new)r   argskwargss      r   r'   zOneHotCategorical._newF   s     %t  %%t6v66r   c                 .    | j                   j                  S r&   )r   _paramr   s    r   r+   zOneHotCategorical._paramI         '''r   c                 .    | j                   j                  S r&   r   r   r,   s    r   r   zOneHotCategorical.probsM         &&&r   c                 .    | j                   j                  S r&   )r   r   r,   s    r   r   zOneHotCategorical.logitsQ   r-   r   c                 .    | j                   j                  S r&   r/   r,   s    r   meanzOneHotCategorical.meanU   r0   r   c                     | j                   j                  }|j                  d      }t        j                  j
                  j                  ||j                  d         j                  |      S )Nr   )dim)num_classes)	r   r   argmaxr   nn
functionalone_hotshapeto)r   r   modes      r   r=   zOneHotCategorical.modeY   sW    !!''|||#xx""**4U[[_*MPPQVWWr   c                 b    | j                   j                  d| j                   j                  z
  z  S )N   r/   r,   s    r   variancezOneHotCategorical.variance_   s*      &&!d.?.?.E.E*EFFr   c                 .    | j                   j                  S r&   )r   r   r,   s    r   r   zOneHotCategorical.param_shapec   s      ,,,r   c                 ,   t        j                  |      }| j                  j                  }| j                  j                  }| j                  j                  |      }t         j                  j                  j                  ||      j                  |      S r&   )
r   r    r   r   _num_eventssampler8   r9   r:   r<   )r   sample_shaper   
num_eventsindicess        r   rD   zOneHotCategorical.sampleg   sp    zz,/!!''&&22
##**<8xx""**7J?BB5IIr   c                     | j                   r| j                  |       |j                  d      d   }| j                  j	                  |      S )Nr   r?   )r"   _validate_samplemaxr   log_prob)r   valuerG   s      r   rK   zOneHotCategorical.log_probn   sB    !!%())B-"  ))'22r   c                 6    | j                   j                         S r&   )r   entropyr,   s    r   rN   zOneHotCategorical.entropyt   s      ((**r   c                 L   | j                   d   }t        j                  || j                  j                  | j                  j
                        }|j                  |fdt        | j                        z  z   |fz         }|r#|j                  |f| j                  z   |fz         }|S )Nr   )dtypedevice)r?   )
r   r   eyer+   rP   rQ   viewlenr   r!   )r   r!   nvaluess       r   enumerate_supportz#OneHotCategorical.enumerate_supportw   s    Q1DKK$5$5dkk>P>PQaTD3t/?/?+@$@@A4GH]]A4$*:*:#:aT#ABFr   )NNNr&   )T) __name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintsr:   supporthas_enumerate_supportr   r   boolr   r!   r'   propertyr+   r   r   r3   r=   r@   r   r    r   rD   rK   rN   rW   __classcell__)r   s   @r   r	   r	      sm   : !, 3 3{?V?VWO!!G  #'#'(,		P	P  	P  ~		P
 
	P7 ( ( ( 'v ' ' ( ( ( 'f ' ' Xf X X
 G& G G -UZZ - - #-%**, J3+r   c                   F    e Zd ZdZdZ ej                         fdedefdZ	y)r
   a  
    Creates a reparameterizable :class:`OneHotCategorical` distribution based on the straight-
    through gradient estimator from [1].

    [1] Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation
    (Bengio et al., 2013)
    TrE   r   c                 |    | j                  |      }| j                  j                  }|||j                         z
  z   S r&   )rD   r   r   detach)r   rE   samplesr   s       r   rsamplez(OneHotCategoricalStraightThrough.rsample   s7    ++l+!!''%%,,.011r   N)
rX   rY   rZ   r[   has_rsampler   r    r   r   rh    r   r   r
   r
      s,     K,6EJJL 2E 2V 2r   )typingr   r   r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   torch.typesr   __all__r	   r
   rj   r   r   <module>rq      sC       + 7 9   B
Cn nb2'8 2r   