
    sh                     |    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U 4S 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\R0                  4S j5       r\R0                  " 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logitsTc                    > [        X5      U l        U R                  R                  nU R                  R                  SS  n[        TU ]  XEUS9  g )Nvalidate_args)r   _categoricalbatch_shapeparam_shapesuper__init__)selfr   r   r   r   event_shape	__class__s         {/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributions/one_hot_categorical.pyr   OneHotCategorical.__init__/   sK    '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.expand5   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._new?   s      %%t6v66r   returnc                 .    U R                   R                  $ r'   )r   _paramr   s    r   r.   OneHotCategorical._paramB         '''r   c                 .    U R                   R                  $ r'   r   r   r/   s    r   r   OneHotCategorical.probsF         &&&r   c                 .    U R                   R                  $ r'   )r   r   r/   s    r   r   OneHotCategorical.logitsJ   r1   r   c                 .    U R                   R                  $ r'   r3   r/   s    r   meanOneHotCategorical.meanN   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.modeR   sU    !!''|||#xx""**4[[_*MPPQVWWr   c                 b    U R                   R                  SU R                   R                  -
  -  $ )N   r3   r/   s    r   varianceOneHotCategorical.varianceX   s*      &&!d.?.?.E.E*EFFr   c                 .    U R                   R                  $ r'   )r   r   r/   s    r   r   OneHotCategorical.param_shape\   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.sample`   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_probg   sB    !!%())B-"  ))'22r   c                 6    U R                   R                  5       $ r'   )r   entropyr/   s    r   rZ   OneHotCategorical.entropym   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_supportp   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   r!   r(   propertyr   r.   r   r   r9   rD   rH   r   r    r   rN   rV   rZ   rd   __static_attributes____classcell__)r   s   @r   r   r      s-   : !, 3 3{?V?VWO!!G 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	   y   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   rx   rq   rz   r   r   r	   r	   y   s/     K,1JJL 2E 2V 2 2r   )r   r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   torch.typesr   __all__r   r	   rz   r   r   <module>r      s@      + 7 9   B
Ci iX2'8 2r   