
    ΑiO7                    f   % S SK Jr  S SKJrJr  S SK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  S SKJr  \(       a^  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KJr  \\\   \\   \R>                  \\R@                  \RB                  4      \4   r"S\#S'    " S S\RH                  5      r%g)    )annotations)TYPE_CHECKINGUnionN)
check_typeconvert_dtype)Variable)distribution)in_dynamic_mode)multinomial)Sequence)	TypeAlias)Tensor)NestedSequence)_DTypeLiteralr   _CategoricalBoundaryc                      \ rS rSr% SrS\S'   S\S'    S     SS jjr/ 4SS	 jjrSS
 jrSS jr	SS jr
SS jrSrg)Categorical.   a  
Categorical distribution is a discrete probability distribution that
describes the possible results of a random variable that can take on
one of K possible categories, with the probability of each category
separately specified.

The probability mass function (pmf) is:

.. math::

    pmf(k; p_i) = \prod_{i=1}^{k} p_i^{[x=i]}

In the above equation:

* :math:`[x=i]` : it evaluates to 1 if :math:`x==i` , 0 otherwise.

Args:
    logits(list|tuple|numpy.ndarray|Tensor): The logits input of categorical distribution. The data type is float32 or float64.
    name(str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Categorical

        >>> paddle.seed(100) # on CPU device
        >>> x = paddle.rand([6])
        >>> print(x)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.55355281, 0.20714243, 0.01162981, 0.51577556, 0.36369765, 0.26091650])

        >>> paddle.seed(200) # on CPU device
        >>> y = paddle.rand([6])
        >>> print(y)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.77663314, 0.90824795, 0.15685187, 0.04279523, 0.34468332, 0.79557180])

        >>> cat = Categorical(x)
        >>> cat2 = Categorical(y)

        >>> paddle.seed(1000) # on CPU device
        >>> print(cat.sample([2, 3]))
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0, 1, 5],
        [3, 4, 5]])

        >>> print(cat.entropy())
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        1.77528250)

        >>> print(cat.kl_divergence(cat2))
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.07195196])

        >>> value = paddle.to_tensor([2, 1, 3])
        >>> print(cat.probs(value))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.00608027, 0.10829761, 0.26965630])

        >>> print(cat.log_prob(value))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [-5.10270691, -2.22287226, -1.31060708])
r   logitsr   dtypeNc           	     4   [        5       (       dE  [        US[        R                  [        [
        R                  R                  [        [        4S5        Ub  UOSU l
        SU l        U R                  U5      (       a!  Xl        [        UR                  5      U l        O[        U[        R                  5      (       a3  [!        UR                  5      S;   a  [        UR                  5      U l        U R#                  U5      S   U l        U R                  [        U R                  R                  5      :w  a.  [
        R$                  " U R                  U R                  S9U l        [
        R&                  " U R                  SS	S
9nU R                  U-  U l        g)a  
Args:
    logits(list|tuple|numpy.ndarray|Tensor): The logits input of categorical distribution. The data type is float32 or float64.
    name(str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
r   r   Nfloat32)r   float64r   )r   Taxiskeepdim)r
   r   npndarrayr   paddlepirValuelisttuplenamer   _validate_argsr   r   
isinstancestr
_to_tensorcastsum_prob)selfr   r%   dist_sums       _/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/categorical.py__init__Categorical.__init__s   s       Xvzz'7'7uE	 !,D-	
v&& K&v||4DJ&"**--#fll2C H 3 +6<<8
//&1!4DKzz]4;;+<+<==$kk$++TZZH::dkkDA[[8+
    c                   U R                   S-   n[        5       (       d  [        US[        [        4S5        [
        R                  " [
        R                  " U5      5      n[        U R                  R                  5      n[        U5      S:  aE  XSS -   n[        R                  " U R                  [
        R                  " USS 5      US   /5      nOUnU R                  n[        U R                  U5      US5      n[        [        UR!                  5       5      5      nUR#                  SUR%                  S5      5        UR'                  U5      n[        R                  " XuUS	9$ )
aO  Generate samples of the specified shape.

Args:
    shape (Sequence[int], optional): Shape of the generated samples.

Returns:
    Tensor: A tensor with prepended dimensions shape.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Categorical

        >>> paddle.seed(100) # on CPU device
        >>> x = paddle.rand([6])
        >>> print(x)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.55355281, 0.20714243, 0.01162981, 0.51577556, 0.36369765, 0.26091650])

        >>> # doctest: +SKIP('Random output')
        >>> cat = Categorical(x)
        >>> paddle.seed(1000) # on CPU device
        >>> print(cat.sample([2, 3]))
        Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0, 1, 5],
        [3, 4, 5]])
_sampleshapesample   Nr   Tr   r%   )r%   r
   r   r#   r$   r   prodarrayr   r5   lenr    reshaper   _logits_to_probsrangediminsertpop	transpose)	r-   r5   r%   num_sampleslogits_shapesample_shaper   sample_indexpermutes	            r/   r6   Categorical.sample   s!   : yy9$  uge}h?ggbhhuo.DKK--.|q  #44L^^bggl3B&78,r:JKF !L[[F"!!&);
 u\--/01q'++b/*#--g6~~ltDDr2   c                h   U R                   S-   n[        5       (       d  [        US[        S5        U R                  [
        R                  " U R                  SSS9-
  nUR                  [
        R                  " UR                  SSS9-
  n[
        R                  " U5      n[
        R                  " U5      n[
        R                  " USSS9n[
        R                  " USSS9nXW-  n	[
        R                  " U	U[
        R                  " U5      -
  U-
  [
        R                  " U5      -   -  SSUS9n
U
$ )a/  The KL-divergence between two Categorical distributions.

Args:
    other (Categorical): instance of Categorical. The data type is float32.

Returns:
    Tensor: kl-divergence between two Categorical distributions.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Categorical

        >>> paddle.seed(100) # on CPU device
        >>> x = paddle.rand([6])
        >>> print(x)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.55355281, 0.20714243, 0.01162981, 0.51577556, 0.36369765, 0.26091650])

        >>> paddle.seed(200) # on CPU device
        >>> y = paddle.rand([6])
        >>> print(y)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.77663314, 0.90824795, 0.15685187, 0.04279523, 0.34468332, 0.79557180])

        >>> cat = Categorical(x)
        >>> cat2 = Categorical(y)

        >>> print(cat.kl_divergence(cat2))
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.07195196])
_kl_divergenceotherkl_divergencer   Tr   )r   r   r%   )
r%   r
   r   r   r   r    maxexpr+   log)r-   rK   r%   r   other_logitse_logitsother_e_logitszother_zprobkls              r/   rL   Categorical.kl_divergence   s   D yy++  ug{ODvzz$++BMM||fjjLLr4'
 
 ::f%L1JJxb$7**^"dC|ZZ

1%4vzz'7JJL
 	r2   c                f   U R                   S-   nU R                  [        R                  " U R                  SSS9-
  n[        R                  " U5      n[        R
                  " USSS9nX4-  n[        R
                  " XR[        R                  " U5      -
  -  SS9n[        R                  " USUS9nU$ )a  Shannon entropy in nats.

Returns:
    Tensor: Shannon entropy of Categorical distribution. The data type is float32.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Categorical

        >>> paddle.seed(100) # on CPU device
        >>> x = paddle.rand([6])
        >>> print(x)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.55355281, 0.20714243, 0.01162981, 0.51577556, 0.36369765, 0.26091650])

        >>> cat = Categorical(x)

        >>> print(cat.entropy())
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        1.77528250)
_entropyr   Tr   r   g      )scaler%   )r%   r   r    rM   rN   r+   rO   r[   )r-   r%   r   rQ   rS   rU   neg_entropyentropys           r/   r]   Categorical.entropy	  s    0 yy:%vzz$++BMM::f%JJxb$7|jj&**Q-)?!@rJ,,{$TBr2   c           	        U R                   S-   n[        U R                  R                  5      S:X  aG  [        R
                  " U R                  UR                  S/US9US9R                  UR                  US9$ [        UR                  5      S:X  a\  [        R                  " U R                  [        R                  " U[        U R                  R                  5      S-
  S/-  S/-   US9SS9$ [        R                  " U R                  USS9$ )a  Probabilities of the given category (``value``).

If ``logits`` is 2-D or higher dimension, the last dimension will be regarded as
category, and the others represents the different distributions.
At the same time, if ``value`` is 1-D Tensor, ``value`` will be broadcast to the
same number of distributions as ``logits``.
If ``value`` is not 1-D Tensor, ``value`` should have the same number distributions
with ``logits. That is, ``value[:-1] = logits[:-1]``.

Args:
    value (Tensor): The input tensor represents the selected category index.

Returns:
    Tensor: probability according to the category index.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Categorical

        >>> paddle.seed(100) # on CPU device
        >>> x = paddle.rand([6])
        >>> print(x)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.55355281, 0.20714243, 0.01162981, 0.51577556, 0.36369765, 0.26091650])

        >>> cat = Categorical(x)

        >>> value = paddle.to_tensor([2, 1, 3])
        >>> print(cat.probs(value))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.00608027, 0.10829761, 0.26965630])
_probsr7   r   r8   rZ   )r%   r;   r,   r5   r    gatherr<   take_along_axisr-   valuer%   s      r/   probsCategorical.probs+  s    F yy8#tzz A%==

EMM2$TM:gekkg-. 5;;1$--JJNNTZZ--.2qc9RD@!
   --djj%bIIr2   c                h    U R                   S-   n[        R                  " U R                  U5      US9$ )a7  Log probabilities of the given category. Refer to ``probs`` method.

Args:
    value (Tensor): The input tensor represents the selected category index.

Returns:
    Tensor: Log probability.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Categorical

        >>> paddle.seed(100) # on CPU device
        >>> x = paddle.rand([6])
        >>> print(x)
        Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.55355281, 0.20714243, 0.01162981, 0.51577556, 0.36369765, 0.26091650])

        >>> cat = Categorical(x)

        >>> value = paddle.to_tensor([2, 1, 3])
        >>> print(cat.log_prob(value))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [-5.10270691, -2.22287226, -1.31060708])
	_log_probr8   )r%   r    rO   re   rc   s      r/   log_probCategorical.log_proba  s,    8 yy;&zz$**U+$77r2   )r,   r   r   r%   )N)r   r   r%   z
str | NonereturnNone)r5   zSequence[int]rk   r   )rK   r   rk   r   )rk   r   )rd   r   rk   r   )__name__
__module____qualname____firstlineno____doc____annotations__r0   r6   rL   r]   re   ri   __static_attributes__ r2   r/   r   r   .   s`    ?B N
  ",$", ", 
	",H -/ 7Er7r D4Jl8r2   r   )&
__future__r   typingr   r   numpyr   r    paddle.base.data_feederr   r   paddle.base.frameworkr   paddle.distributionr	   paddle.frameworkr
   paddle.tensorr   collections.abcr   numpy.typingnpttyping_extensionsr   r   paddle._typingr   paddle._typing.dtype_liker   floatNDArrayr   r   r   rr   Distributionr   rt   r2   r/   <module>r      s    # '   = * , , %(+-7&+uE"**bjj012	') Q8,++ Q8r2   