Ë
    f^(hù  ã                   ó|   — 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)é    N)ÚTensor)Úconstraints)ÚCategorical)ÚDistribution)Ú_sizeÚOneHotCategoricalÚ OneHotCategoricalStraightThroughc                   ón  ‡ — e Zd ZdZej
                  ej                  dœZej                  Z	dZ
dˆ 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j.                  fd„«       Z ej.                  «       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ÚlogitsTc                 óª   •— t        ||«      | _        | j                  j                  }| j                  j                  dd  }t        ‰| �  |||¬«       y )Néÿÿÿÿ©Úvalidate_args)r   Ú_categoricalÚbatch_shapeÚparam_shapeÚsuperÚ__init__)Úselfr   r   r   r   Úevent_shapeÚ	__class__s         €úe/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/distributions/one_hot_categorical.pyr   zOneHotCategorical.__init__/   sO   ø€ Ü'¨¨vÓ6ˆÔØ×'Ñ'×3Ñ3ˆØ×'Ñ'×3Ñ3°B°CÐ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.expand5   st   ø€ Ø×(Ñ(Ô):¸IÓFˆÜ—j‘j Ó-ˆØ×,Ñ,×3Ñ3°KÓ@ˆÔÜÔ Ñ.Ø˜×)Ñ)¸ð 	/ô 	
ð "×0Ñ0ˆÔØˆ
r   c                 ó:   —  | j                   j                  |i |¤ŽS ©N)r   Ú_new)r   ÚargsÚkwargss      r   r%   zOneHotCategorical._new?   s    € Ø%ˆt× Ñ ×%Ñ% tÐ6¨vÑ6Ð6r   Úreturnc                 ó.   — | j                   j                  S r$   )r   Ú_param©r   s    r   r*   zOneHotCategorical._paramB   ó   € à× Ñ ×'Ñ'Ð'r   c                 ó.   — | j                   j                  S r$   ©r   r   r+   s    r   r   zOneHotCategorical.probsF   ó   € à× Ñ ×&Ñ&Ð&r   c                 ó.   — | j                   j                  S r$   )r   r   r+   s    r   r   zOneHotCategorical.logitsJ   r,   r   c                 ó.   — | j                   j                  S r$   r.   r+   s    r   ÚmeanzOneHotCategorical.meanN   r/   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.modeR   sW   € à×!Ñ!×'Ñ'ˆØ�|‰| ˆ|Ó#ˆÜ�x‰x×"Ñ"×*Ñ*¨4¸U¿[¹[È¹_Ð*ÓM×PÑPÐQVÓWÐWr   c                 ób   — | j                   j                  d| j                   j                  z
  z  S )Né   r.   r+   s    r   ÚvariancezOneHotCategorical.varianceX   s*   € à× Ñ ×&Ñ&¨!¨d×.?Ñ.?×.EÑ.EÑ*EÑFÐFr   c                 ó.   — | j                   j                  S r$   )r   r   r+   s    r   r   zOneHotCategorical.param_shape\   s   € à× Ñ ×,Ñ,Ð,r   c                 ó,  — t        j                  |«      }| j                  j                  }| j                  j                  }| j                  j                  |«      }t         j                  j                  j                  ||«      j                  |«      S r$   )
r   r   r   r   Ú_num_eventsÚsampler7   r8   r9   r;   )r   Úsample_shaper   Ú
num_eventsÚindicess        r   rC   zOneHotCategorical.sample`   sp   € Ü—z‘z ,Ó/ˆØ×!Ñ!×'Ñ'ˆØ×&Ñ&×2Ñ2ˆ
Ø×#Ñ#×*Ñ*¨<Ó8ˆÜ�x‰x×"Ñ"×*Ñ*¨7°JÓ?×BÑBÀ5ÓIÐIr   c                 óš   — | j                   r| j                  |«       |j                  d«      d   }| j                  j	                  |«      S )Nr   r>   )r    Ú_validate_sampleÚmaxr   Úlog_prob)r   ÚvaluerF   s      r   rJ   zOneHotCategorical.log_probg   sB   € Ø×ÒØ×!Ñ! %Ô(Ø—)‘)˜B“- Ñ"ˆØ× Ñ ×)Ñ)¨'Ó2Ð2r   c                 ó6   — | j                   j                  «       S r$   )r   Úentropyr+   s    r   rM   zOneHotCategorical.entropym   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*   rO   rP   ÚviewÚlenr   r   )r   r   ÚnÚvaluess       r   Úenumerate_supportz#OneHotCategorical.enumerate_supportp   sŽ   € Ø×Ñ˜QÑˆÜ—‘˜1 D§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   )NNNr$   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚsimplexÚreal_vectorÚarg_constraintsr9   ÚsupportÚhas_enumerate_supportr   r   r%   Úpropertyr   r*   r   r   r2   r<   r?   r   r   r   rC   rJ   rM   rV   Ú__classcell__)r   s   @r   r   r      s*  ø„ ñð: !,× 3Ñ 3¸{×?VÑ?VÑW€OØ×!Ñ!€GØ ÐõPõò7ð ð(˜ò (ó ð(ð ð'�vò 'ó ð'ð ð(˜ò (ó ð(ð ð'�fò 'ó ð'ð ðX�fò Xó ðXð
 ðG˜&ò Gó ðGð ð-˜UŸZ™Zò -ó ð-ð #- %§*¡*£,ó 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)
    TrD   r(   c                 ó|   — | j                  |«      }| j                  j                  }|||j                  «       z
  z   S r$   )rC   r   r   Údetach)r   rD   Úsamplesr   s       r   Úrsamplez(OneHotCategoricalStraightThrough.rsample„   s7   € Ø—+‘+˜lÓ+ˆØ×!Ñ!×'Ñ'ˆØ˜% %§,¡,£.Ñ0Ñ1Ð1r   N)
rW   rX   rY   rZ   Úhas_rsampler   r   r   r   rf   © r   r   r	   r	   y   s,   „ ñð €Kà,6¨E¯J©J«Lñ 2 Eð 2¸Vô 2r   )r   r   Útorch.distributionsr   Útorch.distributions.categoricalr   Ú torch.distributions.distributionr   Útorch.typesr   Ú__all__r   r	   rh   r   r   ú<module>rn      s@   ðã Ý Ý +Ý 7Ý 9Ý ð Ð BÐ
C€ôi˜ô iôX2Ð'8õ 2r   