§
    O�’j<  ã                   óŠ   — 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dS )é    N)ÚTensor)Úconstraints)ÚCategorical)ÚDistribution)Ú_sizeÚOneHotCategoricalÚ OneHotCategoricalStraightThroughc            	       ó–  ‡ — e Zd ZdZej        ej        dœZej        Z	dZ
	 	 	 ddedz  dedz  dedz  ddfˆ 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ÚlogitsTNr   r   Úvalidate_argsÚreturnc                 óº   •— t          ||¦  «        | _        | j        j        }| j        j        dd …         }t	          ¦   «                              |||¬¦  «         d S )Néÿÿÿÿ©r   )r   Ú_categoricalÚbatch_shapeÚparam_shapeÚsuperÚ__init__)Úselfr   r   r   r   Úevent_shapeÚ	__class__s         €úUC:\Projects\DSCons\.venv\Lib\site-packages\torch\distributions\one_hot_categorical.pyr   zOneHotCategorical.__init__1   s[   ø€ õ (¨¨vÑ6Ô6ˆÔØÔ'Ô3ˆØÔ'Ô3°B°C°CÔ8ˆå‰Œ×Ò˜ kÀÐÑOÔOÐOÐOÐOó    c                 ó  •— |                       t          |¦  «        }t          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.expand=   s…   ø€ Ø×(Ò(Õ):¸IÑFÔFˆÝ”j Ñ-Ô-ˆØÔ,×3Ò3°KÑ@Ô@ˆÔÝÕ Ñ%Ô%×.Ò.Ø˜Ô)¸ð 	/ñ 	
ô 	
ð 	
ð "Ô0ˆÔØˆ
r   c                 ó&   —  | j         j        |i |¤ŽS ©N)r   Ú_new)r   ÚargsÚkwargss      r   r&   zOneHotCategorical._newG   s   € Ø%ˆtÔ Ô% tÐ6¨vÐ6Ð6Ð6r   c                 ó   — | j         j        S r%   )r   Ú_param©r   s    r   r*   zOneHotCategorical._paramJ   ó   € àÔ Ô'Ð'r   c                 ó   — | j         j        S r%   ©r   r   r+   s    r   r   zOneHotCategorical.probsN   ó   € àÔ Ô&Ð&r   c                 ó   — | j         j        S r%   )r   r   r+   s    r   r   zOneHotCategorical.logitsR   r,   r   c                 ó   — | j         j        S r%   r.   r+   s    r   ÚmeanzOneHotCategorical.meanV   r/   r   c                 óÎ   — | j         j        }|                     d¬¦  «        }t          j        j                             ||j        d         ¬¦  «                             |¦  «        S )Nr   )Údim)Únum_classes)	r   r   Úargmaxr   ÚnnÚ
functionalÚone_hotÚshapeÚto)r   r   Úmodes      r   r<   zOneHotCategorical.modeZ   sS   € àÔ!Ô'ˆØ�|Š| ˆ|Ñ#Ô#ˆÝŒxÔ"×*Ò*¨4¸U¼[È¼_Ð*ÑMÔM×PÒPÐQVÑWÔWÐWr   c                 ó:   — | j         j        d| j         j        z
  z  S )Né   r.   r+   s    r   ÚvariancezOneHotCategorical.variance`   s   € àÔ Ô&¨!¨dÔ.?Ô.EÑ*EÑFÐFr   c                 ó   — | j         j        S r%   )r   r   r+   s    r   r   zOneHotCategorical.param_shaped   s   € àÔ Ô,Ð,r   c                 óþ   — t          j        |¦  «        }| j        j        }| j        j        }| j                             |¦  «        }t           j        j                             ||¦  «         	                    |¦  «        S r%   )
r   r   r   r   Ú_num_eventsÚsampler7   r8   r9   r;   )r   Úsample_shaper   Ú
num_eventsÚindicess        r   rC   zOneHotCategorical.sampleh   sf   € Ý”z ,Ñ/Ô/ˆØÔ!Ô'ˆØÔ&Ô2ˆ
ØÔ#×*Ò*¨<Ñ8Ô8ˆÝŒxÔ"×*Ò*¨7°JÑ?Ô?×BÒBÀ5ÑIÔIÐIr   c                 ó¤   — | j         r|                      |¦  «         |                     d¦  «        d         }| j                             |¦  «        S )Nr   r>   )r!   Ú_validate_sampleÚmaxr   Úlog_prob)r   ÚvaluerF   s      r   rJ   zOneHotCategorical.log_probo   sL   € ØÔð 	)Ø×!Ò! %Ñ(Ô(Ð(Ø—)’)˜B‘-”- Ô"ˆØÔ ×)Ò)¨'Ñ2Ô2Ð2r   c                 ó4   — | j                              ¦   «         S r%   )r   Úentropyr+   s    r   rM   zOneHotCategorical.entropyu   s   € ØÔ ×(Ò(Ñ*Ô*Ð*r   c                 ó"  — | j         d         }t          j        || j        j        | j        j        ¬¦  «        }|                     |fdt          | j        ¦  «        z  z   |fz   ¦  «        }|r"| 	                    |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_supportx   sŽ   € ØÔ˜QÔˆÝ”˜1 D¤KÔ$5¸d¼kÔ>PÐQÑQÔQˆØ—’˜a˜T D­3¨tÔ/?Ñ+@Ô+@Ñ$@Ñ@ÀAÀ4ÑGÑHÔHˆØð 	CØ—]’] A 4¨$Ô*:Ñ#:¸a¸TÑ#AÑBÔBˆFØˆr   )NNNr%   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚsimplexÚreal_vectorÚarg_constraintsr9   ÚsupportÚhas_enumerate_supportr   Úboolr   r    r&   Úpropertyr*   r   r   r2   r<   r?   r   r   r   rC   rJ   rM   rV   Ú__classcell__)r   s   @r   r   r      sX  ø€ € € € € ðð ð< !,Ô 3¸{Ô?VÐWÐW€OØÔ!€GØ Ðð  $Ø $Ø%)ð	
Pð 
Pà˜‰}ð
Pð ˜‘ð
Pð ˜d‘{ð	
Pð
 
ð
Pð 
Pð 
Pð 
Pð 
Pð 
Pðð ð ð ð ð ð7ð 7ð 7ð ð(˜ð (ð (ð (ñ „Xð(ð ð'�vð 'ð 'ð 'ñ „Xð'ð ð(˜ð (ð (ð (ñ „Xð(ð ð'�fð 'ð 'ð 'ñ „Xð'ð ðX�fð Xð Xð Xñ „XðXð
 ðG˜&ð Gð Gð Gñ „XðGð ð-˜UœZð -ð -ð -ñ „Xð-ð #- %¤*¡,¤,ð Jð Jð Jð Jð3ð 3ð 3ð+ð +ð +ðð ð ð ð ð ð ð r   c                   óD   — e Zd ZdZdZ ej        ¦   «         fdedefd„Z	dS )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                 óx   — |                       |¦  «        }| j        j        }|||                     ¦   «         z
  z   S r%   )rC   r   r   Údetach)r   rD   Úsamplesr   s       r   Úrsamplez(OneHotCategoricalStraightThrough.rsampleŒ   s6   € Ø—+’+˜lÑ+Ô+ˆØÔ!Ô'ˆØ˜% %§,¢,¡.¤.Ñ0Ñ1Ð1r   N)
rW   rX   rY   rZ   Úhas_rsampler   r   r   r   rg   © r   r   r	   r	   �   sW   € € € € € ðð ð €Kà,6¨E¬J©L¬Lð 2ð 2 Eð 2¸Vð 2ð 2ð 2ð 2ð 2ð 2r   )r   r   Útorch.distributionsr   Útorch.distributions.categoricalr   Ú torch.distributions.distributionr   Útorch.typesr   Ú__all__r   r	   ri   r   r   ú<module>ro      sß   ðð €€€Ø Ð Ð Ð Ð Ð Ø +Ð +Ð +Ð +Ð +Ð +Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø Ð Ð Ð Ð Ð ð Ð BÐ
C€ðpð pð pð pð p˜ñ pô pð pðf2ð 2ð 2ð 2ð 2Ð'8ñ 2ô 2ð 2ð 2ð 2r   