ó
    Eñ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         €Úd/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/distributions/one_hot_categorical.pyr   ÚOneHotCategorical.__init__1   sM   ø€ ô (¨Ó6ˆÔØ×'Ñ'×3Ñ3ˆØ×'Ñ'×3Ñ3°B°CÐ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ˆÜ—j’j Ó-ˆØ×,Ñ,×3Ñ3°KÓ@ˆÔÜÔ Ñ.Ø×)Ñ)¸ð 	/ñ 	
ð "×0Ñ0ˆÔØˆ
r   c                 ó:   • U R                   R                  " U0 UD6$ ©N)r   Ú_new)r   ÚargsÚkwargss      r   r)   ÚOneHotCategorical._newG   s   € Ø× Ñ ×%Ò% tÐ6¨vÑ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   € à×!Ñ!×'Ñ'ˆØ�|‰| ˆ|Ð#ˆÜ�x‰x×"Ñ"×*Ñ*¨4¿[¹[È¹_Ð*ÐM×PÑPÐQVÓ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   € Ü—z’z ,Ó/ˆØ×!Ñ!×'Ñ'ˆØ×&Ñ&×2Ñ2ˆ
Ø×#Ñ#×*Ñ*¨<Ó8ˆÜ�x‰x×"Ñ"×*Ñ*¨7Ó?×BÑBÀ5Ó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ÑˆÜ—’˜1§K¡K×$5Ñ$5¸d¿k¹k×>PÑ>PÑQˆØ—‘˜a˜T D¬3¨t×/?Ñ/?Ó+@Ñ$@Ñ@ÀAÀ4ÑGÓHˆÞØ—]‘] A 4¨$×*:Ñ*:Ñ#:¸a¸TÑ#AÓBˆFØˆ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ÑW€OØ×!Ñ!€GØ Ðð  $Ø $Ø%)ñ	
Pà˜‰}ð
Pð ˜‘ð
Pð ˜d‘{ð	
Pð
 
÷
Pð 
P÷ò7ð ð(˜ó (ó ð(ð ð'�vó 'ó ð'ð ð(˜ó (ó ð(ð ð'�fó 'ó ð'ð ðX�fó Xó ðXð
 ðG˜&ó Gó ðGð ð-˜UŸZ™Zó -ó ð-ð #(§*¢*£,ô 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à,1¯JªJ«Lñ 2 Eð 2¸V÷ 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   