
    IЦ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JrJ	r	  S/r
 " S S\5      rg)    N)nan)constraints)Distribution)lazy_propertylogits_to_probsprobs_to_logitsCategoricalc                   J  ^  \ rS rSrSr\R                  \R                  S.rSr	SU 4S jjr
SU 4S jjrS r\R                  " SSS	9S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\R0                  " 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logitsTc                 N  > 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        O6U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   validate_args)
ValueErrorr   sumr   	logsumexpr   _paramsize_num_events
ndimensiontorchSizesuper__init__)selfr   r   r   batch_shape	__class__s        ^/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/distributions/categorical.pyr   Categorical.__init__5   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.expandJ   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._newY   s    {{///r%   r   )is_discrete	event_dimc                 J    [         R                  " SU R                  S-
  5      $ )Nr   r   )r   integer_intervalr   r    s    r#   supportCategorical.support\   s     ++At/?/?!/CDDr%   c                 ,    [        U R                  5      $ r0   )r   r   r9   s    r#   r   Categorical.logits`   s    tzz**r%   c                 ,    [        U R                  5      $ r0   )r   r   r9   s    r#   r   Categorical.probsd   s    t{{++r%   c                 6    U R                   R                  5       $ r0   )r   r   r9   s    r#   r-   Categorical.param_shapeh   s    {{!!r%   c                     [         R                  " U R                  5       [        U R                  R
                  U R                  R                  S9$ Ndtypedevicer   full_extended_shaper   r   rE   rF   r9   s    r#   meanCategorical.meanl   <    zz  "**""::$$	
 	
r%   c                 4    U R                   R                  SS9$ )Nr   )axis)r   argmaxr9   s    r#   modeCategorical.modeu   s    zz  b ))r%   c                     [         R                  " U R                  5       [        U R                  R
                  U R                  R                  S9$ rC   rG   r9   s    r#   varianceCategorical.variancey   rL   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TrI   )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   rE   rl   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$ )NrD   )r   )r   )
r   r   arangerb   r   rF   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   )NNNr0   )T)__name__
__module____qualname____firstlineno____doc__r   simplexreal_vectorarg_constraintshas_enumerate_supportr   r)   r3   dependent_propertyr:   r   r   r   propertyr-   rJ   rP   rS   r   r   r^   ri   rq   rz   __static_attributes____classcell__)r"   s   @r#   r	   r	      s    $J !, 3 3{?V?VWO C*0 ##BE CE + + , , " " 
 
 * * 
 
 #(**, F5  r%   )r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   r   __all__r	    r%   r#   <module>r      s.      + 9 U U /Q, Qr%   