
    9i                     p    S SK Jr  S SKrS SKJrJr  S SKJr  S SKJr  S SK	J
r
JrJr  S/r " S S\5      rg)	    )OptionalN)nanTensor)constraints)Distribution)lazy_propertylogits_to_probsprobs_to_logitsCategoricalc            	         ^  \ rS rSrSr\R                  \R                  S.rSr	   SS\
\   S\
\   S\
\   S	S4U 4S
 jjjrSU 4S jjrS r\R                   " SSS9S 5       r\S	\4S j5       r\S	\4S j5       r\S	\R.                  4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\R.                  " 5       4S jrS rS rSS jrSr U =r!$ )r      ax  
Creates a categorical distribution parameterized by either :attr:`probs` or
:attr:`logits` (but not both).

.. note::
    It is equivalent to the distribution that :func:`torch.multinomial`
    samples from.

Samples are integers from :math:`\{0, \ldots, K-1\}` where `K` is ``probs.size(-1)``.

If `probs` is 1-dimensional with length-`K`, each element is the relative probability
of sampling the class at that index.

If `probs` is N-dimensional, the first N-1 dimensions are treated as a batch of
relative probability vectors.

.. 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.multinomial`

Example::

    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
    >>> m.sample()  # equal probability of 0, 1, 2, 3
    tensor(3)

Args:
    probs (Tensor): event probabilities
    logits (Tensor): event log probabilities (unnormalized)
)probslogitsTNr   r   validate_argsreturnc                 X  > US L US L :X  a  [        S5      eUb7  UR                  5       S:  a  [        S5      eXR                  SSS9-  U l        O;Uc   eUR                  5       S:  a  [        S5      eX"R	                  SSS9-
  U l        Ub  U R                  OU R
                  U l        U R                  R                  5       S   U l        U R                  R                  5       S:  a  U R                  R                  5       S S O[        R                  " 5       n[        TU ]5  XCS	9  g )
Nz;Either `probs` or `logits` must be specified, but not both.   z3`probs` parameter must be at least one-dimensional.T)keepdimz4`logits` parameter must be at least one-dimensional.)dimr   r   )
ValueErrorr   sumr   	logsumexpr   _paramsize_num_events
ndimensiontorchSizesuper__init__)selfr   r   r   batch_shape	__class__s        _/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/categorical.pyr"   Categorical.__init__8   s    TMv~.M  yy{Q !VWW2t!<<DJ%%%zz|a !WXX #3#3D#3#IIDK$)$5djj4;;;;++-b1'+{{'='='?!'CDKKs# 	 	B    c                   > U R                  [        U5      n[        R                  " U5      nU[        R                  " U R                  45      -   nSU R
                  ;   a1  U R                  R                  U5      Ul        UR                  Ul        SU R
                  ;   a1  U R                  R                  U5      Ul	        UR                  Ul        U R                  Ul        [        [        U]/  USS9  U R                  Ul        U$ )Nr   r   Fr   )_get_checked_instancer   r   r    r   __dict__r   expandr   r   r!   r"   _validate_args)r#   r$   	_instancenewparam_shaper%   s        r&   r,   Categorical.expandS   s    ((i@jj-!EJJ0@0@/B$CCdmm#

))+6CICJt}}$++K8CJCJ**k3(E(J!00
r(   c                 :    U R                   R                  " U0 UD6$ N)r   r/   )r#   argskwargss      r&   _newCategorical._newb   s    {{///r(   r   )is_discrete	event_dimc                 J    [         R                  " SU R                  S-
  5      $ )Nr   r   )r   integer_intervalr   r#   s    r&   supportCategorical.supporte   s     ++At/?/?!/CDDr(   c                 ,    [        U R                  5      $ r3   )r
   r   r<   s    r&   r   Categorical.logitsi   s    tzz**r(   c                 ,    [        U R                  5      $ r3   )r	   r   r<   s    r&   r   Categorical.probsm   s    t{{++r(   c                 6    U R                   R                  5       $ r3   )r   r   r<   s    r&   r0   Categorical.param_shapeq   s    {{!!r(   c                     [         R                  " U R                  5       [        U R                  R
                  U R                  R                  S9$ Ndtypedevicer   full_extended_shaper   r   rH   rI   r<   s    r&   meanCategorical.meanu   <    zz  "**""::$$	
 	
r(   c                 4    U R                   R                  SS9$ )Nr   )r   )r   argmaxr<   s    r&   modeCategorical.mode~   s    zz  R ((r(   c                     [         R                  " U R                  5       [        U R                  R
                  U R                  R                  S9$ rF   rJ   r<   s    r&   varianceCategorical.variance   rO   r(   c                 V   [        U[        R                  5      (       d  [        R                  " U5      nU R                  R	                  SU R
                  5      n[        R                  " X!R                  5       S5      R                  nUR	                  U R                  U5      5      $ )Nr   T)

isinstancer   r    r   reshaper   multinomialnumelTrL   )r#   sample_shapeprobs_2d
samples_2ds       r&   sampleCategorical.sample   sz    ,

33 ::l3L::%%b$*:*:;&&x1C1C1EtLNN
!!$"6"6|"DEEr(   c                    U R                   (       a  U R                  U5        UR                  5       R                  S5      n[        R
                  " XR                  5      u  pUSS S24   nUR                  SU5      R                  S5      $ )Nr   .r   )	r-   _validate_samplelong	unsqueezer   broadcast_tensorsr   gathersqueeze)r#   valuelog_pmfs      r&   log_probCategorical.log_prob   sr    !!%(

&&r*00Dc2A2g~~b%(0044r(   c                     [         R                  " U R                  R                  5      R                  n[         R
                  " U R                  US9nX R                  -  nUR                  S5      * $ )N)minr   )r   finfor   rH   rn   clampr   r   )r#   min_realr   p_log_ps       r&   entropyCategorical.entropy   sQ    ;;t{{00155T[[h7::%Br(   c                 ,   U R                   n[        R                  " U[        R                  U R                  R
                  S9nUR                  SS[        U R                  5      -  -   5      nU(       a  UR                  SU R                  -   5      nU$ )NrG   )r   )r   )
r   r   arangerd   r   rI   viewlen_batch_shaper,   )r#   r,   
num_eventsvaluess       r&   enumerate_supportCategorical.enumerate_support   sq    %%
j

4;;CUCUVUTC0A0A,B%BBC]]54+<+<#<=Fr(   )r   r   r   r   )NNNr3   )T)"__name__
__module____qualname____firstlineno____doc__r   simplexreal_vectorarg_constraintshas_enumerate_supportr   r   boolr"   r,   r6   dependent_propertyr=   r   r   r   propertyr   r    r0   rM   rR   rU   r`   rk   rs   r|   __static_attributes____classcell__)r%   s   @r&   r   r      so   $L !, 3 3{?V?VWO  #'#'(,	CC  C  ~	C
 
C C60 ##BE CE + + + ,v , , "UZZ " " 
f 
 
 )f ) ) 
& 
 
 #(**, F5  r(   )typingr   r   r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r	   r
   __all__r    r(   r&   <module>r      s1       + 9 U U /X, Xr(   