
    8h                         S SK Jr  S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
Jr  SS	/r " S
 S\	5      r " S S	\5      rg)    )OptionalN)Tensor)constraints)Categorical)Distribution)_sizeOneHotCategorical OneHotCategoricalStraightThroughc            	         ^  \ rS rSrSr\R                  \R                  S.r\R                  r
Sr   SS\\   S\\   S\\   S	S4U 4S
 jjjrSU 4S jjrS r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\R4                  4S j5       r\R4                  " 5       4S jrS rS rSS jrSr U =r!$ )r	      aa  
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                    > [        X5      U l        U R                  R                  nU R                  R                  SS  n[        TU ]  XEUS9  g )Nr   )r   _categoricalbatch_shapeparam_shapesuper__init__)selfr   r   r   r   event_shape	__class__s         a/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/distributions/one_hot_categorical.pyr   OneHotCategorical.__init__1   sM     (6''33''33BC8O    c                    > U R                  [        U5      n[        R                  " U5      nU R                  R                  U5      Ul        [        [        U]  XR                  SS9  U R                  Ul	        U$ )NFr   )
_get_checked_instancer	   torchSizer   expandr   r   r   _validate_args)r   r   	_instancenewr   s       r   r#   OneHotCategorical.expand<   sr    (():IFjj-,,33K@.)) 	/ 	
 "00
r   c                 :    U R                   R                  " U0 UD6$ N)r   _new)r   argskwargss      r   r*   OneHotCategorical._newF   s      %%t6v66r   c                 .    U R                   R                  $ r)   )r   _paramr   s    r   r/   OneHotCategorical._paramI         '''r   c                 .    U R                   R                  $ r)   r   r   r0   s    r   r   OneHotCategorical.probsM         &&&r   c                 .    U R                   R                  $ r)   )r   r   r0   s    r   r   OneHotCategorical.logitsQ   r2   r   c                 .    U R                   R                  $ r)   r4   r0   s    r   meanOneHotCategorical.meanU   r6   r   c                     U R                   R                  nUR                  SS9n[        R                  R
                  R                  X!R                  S   S9R                  U5      $ )Nr   )dim)num_classes)	r   r   argmaxr!   nn
functionalone_hotshapeto)r   r   modes      r   rE   OneHotCategorical.modeY   sU    !!''|||#xx""**4[[_*MPPQVWWr   c                 b    U R                   R                  SU R                   R                  -
  -  $ )N   r4   r0   s    r   varianceOneHotCategorical.variance_   s*      &&!d.?.?.E.E*EFFr   c                 .    U R                   R                  $ r)   )r   r   r0   s    r   r   OneHotCategorical.param_shapec   s      ,,,r   c                 ,   [         R                  " U5      nU R                  R                  nU R                  R                  nU R                  R                  U5      n[         R                  R                  R                  XC5      R                  U5      $ r)   )
r!   r"   r   r   _num_eventssampler@   rA   rB   rD   )r   sample_shaper   
num_eventsindicess        r   rO   OneHotCategorical.sampleg   sn    zz,/!!''&&22
##**<8xx""**7?BB5IIr   c                     U R                   (       a  U R                  U5        UR                  S5      S   nU R                  R	                  U5      $ )Nr   rH   )r$   _validate_samplemaxr   log_prob)r   valuerR   s      r   rW   OneHotCategorical.log_probn   sB    !!%())B-"  ))'22r   c                 6    U R                   R                  5       $ r)   )r   entropyr0   s    r   r[   OneHotCategorical.entropyt   s      ((**r   c                 P   U R                   S   n[        R                  " X R                  R                  U R                  R
                  S9nUR                  U4S[        U R                  5      -  -   U4-   5      nU(       a#  UR                  U4U R                  -   U4-   5      nU$ )Nr   )dtypedevice)rH   )
r   r!   eyer/   r^   r_   viewlenr   r#   )r   r#   nvaluess       r   enumerate_support#OneHotCategorical.enumerate_supportw   s    Q1KK$5$5dkk>P>PQaTD3t/?/?+@$@@A4GH]]A4$*:*:#:aT#ABFr   )r   )NNNr)   )T)"__name__
__module____qualname____firstlineno____doc__r   simplexreal_vectorarg_constraintsrB   supporthas_enumerate_supportr   r   boolr   r#   r*   propertyr/   r   r   r:   rE   rI   r!   r"   r   rO   rW   r[   re   __static_attributes____classcell__)r   s   @r   r	   r	      sv   : !, 3 3{?V?VWO!!G  #'#'(,		P	P  	P  ~		P
 
	P 	P7 ( ( ( 'v ' ' ( ( ( 'f ' ' Xf X X
 G& G G -UZZ - - #(**, J3+ r   c                   R    \ rS rSrSrSr\R                  " 5       4S\S\	4S jjr
Srg)	r
      z
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)
TrP   r   c                 z    U R                  U5      nU R                  R                  nX#UR                  5       -
  -   $ r)   )rO   r   r   detach)r   rP   samplesr   s       r   rsample(OneHotCategoricalStraightThrough.rsample   s5    ++l+!!''%,,.011r    N)rg   rh   ri   rj   rk   has_rsampler!   r"   r   r   rz   rs   r|   r   r   r
   r
      s/     K,1JJL 2E 2V 2 2r   )typingr   r!   r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   torch.typesr   __all__r	   r
   r|   r   r   <module>r      sC       + 7 9   B
Cn nb2'8 2r   