
    ȅi                     |    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)    N)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\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	\R2                  4S j5       r\R2                  " 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/home/james-whalen/.local/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._newG   s      %%t6v66r   c                 .    U R                   R                  $ r(   )r   _paramr   s    r   r.   OneHotCategorical._paramJ         '''r   c                 .    U R                   R                  $ r(   r   r   r/   s    r   r   OneHotCategorical.probsN         &&&r   c                 .    U R                   R                  $ r(   )r   r   r/   s    r   r   OneHotCategorical.logitsR   r1   r   c                 .    U R                   R                  $ r(   r3   r/   s    r   meanOneHotCategorical.meanV   r5   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   rD   OneHotCategorical.modeZ   sU    !!''|||#xx""**4[[_*MPPQVWWr   c                 b    U R                   R                  SU R                   R                  -
  -  $ )N   r3   r/   s    r   varianceOneHotCategorical.variance`   s*      &&!d.?.?.E.E*EFFr   c                 .    U R                   R                  $ r(   )r   r   r/   s    r   r   OneHotCategorical.param_shaped   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?   r@   rA   rC   )r   sample_shaper   
num_eventsindicess        r   rN   OneHotCategorical.sampleh   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   rG   )r#   _validate_samplemaxr   log_prob)r   valuerQ   s      r   rV   OneHotCategorical.log_probo   sB    !!%())B-"  ))'22r   c                 6    U R                   R                  5       $ r(   )r   entropyr/   s    r   rZ   OneHotCategorical.entropyu   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)rG   )
r   r    eyer.   r]   r^   viewlenr   r"   )r   r"   nvaluess       r   enumerate_support#OneHotCategorical.enumerate_supportx   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_constraintsrA   supporthas_enumerate_supportr   boolr   r"   r)   propertyr.   r   r   r9   rD   rH   r    r!   r   rN   rV   rZ   rd   __static_attributes____classcell__)r   s   @r   r   r      st   < !, 3 3{?V?VWO!!G   $ $%)	
P}
P 
P d{	
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)
TrO   r   c                 z    U R                  U5      nU R                  R                  nX#UR                  5       -
  -   $ r(   )rN   r   r   detach)r   rO   samplesr   s       r   rsample(OneHotCategoricalStraightThrough.rsample   s5    ++l+!!''%,,.011r    N)rf   rg   rh   ri   rj   has_rsampler    r!   r   r   ry   rr   r{   r   r   r	   r	      s/     K,1JJL 2E 2V 2 2r   )r    r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   torch.typesr   __all__r   r	   r{   r   r   <module>r      sB      + 7 9   B
Cp pf2'8 2r   