
    AVh0                     "   d 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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lmZ ddlmZ ddlmZ d Z edg       G d dej*                               Z ej.                  ee      dd       Zy)z#The Categorical distribution class.    )constant_op)dtypes)ops)tensor_shape)	array_ops)math_ops)nn_ops)
random_ops)distribution)kullback_leibler)util)deprecation)	tf_exportc                 :   | j                   j                  rnV| j                   j                  r&t        j                  | t
        j                        } nt        dj                  |            |j                  j                  duxr9 |j                  dd j                         xr | j                  j                         }|r|j                  dd | j                  k7  r|t        j                  | dt        j                  f   |j                         z  }t        j                  |      dd }| t        j                  || j                         z  } |j                  j                  1| j!                  t#        j$                  |j                  dd              | |fS )z0Broadcasts the event or distribution parameters.dtypez8`value` should have integer `dtype` or `self.dtype` ({})N.)r   
is_integeris_floatingr   castr   int32	TypeErrorformatshapendimsis_fully_definedr   	ones_likenewaxisones	set_shaper   TensorShape)eventparams
base_dtypeshape_known_staticallyparams_shapes        _/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/distributions/categorical.py_broadcast_cat_event_and_paramsr(       sP   
[[{{ MM%v||4E
 ((.z(:< < ll$ %ll3B((*%kk""$  
 6<<#4#C
i!!%Y->->(>"?(.6 6F??6*3B/L	Y^^L<<E||%ool..v||CR/@AB	    zdistributions.Categorical)v1c                        e Zd ZdZ ej
                  ddd      ddej                  ddd f fd	       Ze	d	        Z
e	d
        Ze	d        Zd Zd Zd Zd ZddZd Zd Zd Zd Z xZS )Categoricalas	  Categorical distribution.

  The Categorical distribution is parameterized by either probabilities or
  log-probabilities of a set of `K` classes. It is defined over the integers
  `{0, 1, ..., K}`.

  The Categorical distribution is closely related to the `OneHotCategorical` and
  `Multinomial` distributions.  The Categorical distribution can be intuited as
  generating samples according to `argmax{ OneHotCategorical(probs) }` itself
  being identical to `argmax{ Multinomial(probs, total_count=1) }`.

  #### Mathematical Details

  The probability mass function (pmf) is,

  ```none
  pmf(k; pi) = prod_j pi_j**[k == j]
  ```

  #### Pitfalls

  The number of classes, `K`, must not exceed:
  - the largest integer representable by `self.dtype`, i.e.,
    `2**(mantissa_bits+1)` (IEEE 754),
  - the maximum `Tensor` index, i.e., `2**31-1`.

  In other words,

  ```python
  K <= min(2**31-1, {
    tf.float16: 2**11,
    tf.float32: 2**24,
    tf.float64: 2**53 }[param.dtype])
  ```

  Note: This condition is validated only when `self.validate_args = True`.

  #### Examples

  Creates a 3-class distribution with the 2nd class being most likely.

  ```python
  dist = Categorical(probs=[0.1, 0.5, 0.4])
  n = 1e4
  empirical_prob = tf.cast(
      tf.histogram_fixed_width(
        dist.sample(int(n)),
        [0., 2],
        nbins=3),
      dtype=tf.float32) / n
  # ==> array([ 0.1005,  0.5037,  0.3958], dtype=float32)
  ```

  Creates a 3-class distribution with the 2nd class being most likely.
  Parameterized by [logits](https://en.wikipedia.org/wiki/Logit) rather than
  probabilities.

  ```python
  dist = Categorical(logits=np.log([0.1, 0.5, 0.4])
  n = 1e4
  empirical_prob = tf.cast(
      tf.histogram_fixed_width(
        dist.sample(int(n)),
        [0., 2],
        nbins=3),
      dtype=tf.float32) / n
  # ==> array([0.1045,  0.5047, 0.3908], dtype=float32)
  ```

  Creates a 3-class distribution with the 3rd class being most likely.
  The distribution functions can be evaluated on counts.

  ```python
  # counts is a scalar.
  p = [0.1, 0.4, 0.5]
  dist = Categorical(probs=p)
  dist.prob(0)  # Shape []

  # p will be broadcast to [[0.1, 0.4, 0.5], [0.1, 0.4, 0.5]] to match counts.
  counts = [1, 0]
  dist.prob(counts)  # Shape [2]

  # p will be broadcast to shape [3, 5, 7, 3] to match counts.
  counts = [[...]] # Shape [5, 7, 3]
  dist.prob(counts)  # Shape [5, 7, 3]
  ```

  z
2019-01-01zThe TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.T)	warn_onceNFc           	         t        t                     }t        j                  |||g      5 }t	        j
                  |||d|      \  | _        | _        |r$t	        j                  | j                        | _        | j                  j                         j                  d      }|j                  9t        j                  |j                  dz
  t        j                  d      | _        nFt        j                  d      5  t!        j"                  | j                        dz
  | _        ddd       t!        j$                  | j                  d	      }	t'        j(                  |d
         Ct        j                  |j*                  d
   j,                  t        j                  d      | _        n3t        j                  d      5  |	| j                     | _        ddd       |dd
 j1                         r=t3        j4                  |dd
 j7                         t        j                  d      | _        n)t        j                  d      5  |	dd
 | _        ddd       ddd       t:        t<        |   |t@        jB                  |||| j                  | j                  g|       y# 1 sw Y   yxY w# 1 sw Y   xY w# 1 sw Y   ixY w# 1 sw Y   mxY w)a  Initialize Categorical distributions using class log-probabilities.

    Args:
      logits: An N-D `Tensor`, `N >= 1`, representing the log probabilities
        of a set of Categorical distributions. The first `N - 1` dimensions
        index into a batch of independent distributions and the last dimension
        represents a vector of logits for each class. Only one of `logits` or
        `probs` should be passed in.
      probs: An N-D `Tensor`, `N >= 1`, representing the probabilities
        of a set of Categorical distributions. The first `N - 1` dimensions
        index into a batch of independent distributions and the last dimension
        represents a vector of probabilities for each class. Only one of
        `logits` or `probs` should be passed in.
      dtype: The type of the event samples (default: int32).
      validate_args: Python `bool`, default `False`. When `True` distribution
        parameters are checked for validity despite possibly degrading runtime
        performance. When `False` invalid inputs may silently render incorrect
        outputs.
      allow_nan_stats: Python `bool`, default `True`. When `True`, statistics
        (e.g., mean, mode, variance) use the value "`NaN`" to indicate the
        result is undefined. When `False`, an exception is raised if one or
        more of the statistic's batch members are undefined.
      name: Python `str` name prefixed to Ops created by this class.
    valuesT)logitsprobsvalidate_argsmultidimensionalname   N
batch_rank)r   r5   r5   logits_shaper   
event_sizebatch_shape)r   reparameterization_typer3   allow_nan_stats
parametersgraph_parentsr5   )"dictlocalsr   
name_scopedistribution_utilget_logits_and_probs_logits_probs#embed_check_categorical_event_shape	get_shapewith_rank_at_leastr   convert_to_tensorr   r   _batch_rankr   rankr   r   dimension_valuedimsvalue_event_sizer   r   constantas_list_batch_shape_valsuperr,   __init__r   NOT_REPARAMETERIZED)selfr1   r2   r   r3   r=   r5   r>   logits_shape_staticr9   	__class__s             r'   rU   zCategorical.__init__   sc   P fhJ	fe_	5 '4"3"H"H%#dlDK 
(LLLL !LL224GGJ		"	"	.00%%),,
 ^^. 	>&^^DLL9A=$
	> __T\\Gl		%	%&9"&=	>	J00$$R(..,,
 ^^. 	<)$*:*:;$
	< 
Sb	!	2	2	4 + 4 4$,,.,,! 
 ^^/ 	4".s"3$
	4M'4P 
+t% , @ @#'||{{$ & +	> 	>	< 	<	4 	4M'4 '4sV   CK (J(BKJ,A.KJ8KJ)	$K,J5	1K8K	=KKc                     | j                   S )z-Scalar `int32` tensor: the number of classes.)rP   rW   s    r'   r:   zCategorical.event_size   s     r)   c                     | j                   S )z Vector of coordinatewise logits.)rE   r[   s    r'   r1   zCategorical.logits   s     <<r)   c                     | j                   S )z'Vector of coordinatewise probabilities.)rF   r[   s    r'   r2   zCategorical.probs   s     ;;r)   c                 @    t        j                  | j                        S N)r   identityrS   r[   s    r'   _batch_shape_tensorzCategorical._batch_shape_tensor   s    d3344r)   c                 <    | j                   j                         d d S )Nr   )r1   rH   r[   s    r'   _batch_shapezCategorical._batch_shape  s    ;;  "3B''r)   c                 L    t        j                  g t        j                        S )Nr   )r   rQ   r   r   r[   s    r'   _event_shape_tensorzCategorical._event_shape_tensor  s    &,,77r)   c                 ,    t        j                  g       S r_   )r   r!   r[   s    r'   _event_shapezCategorical._event_shape  s    ##B''r)   c                 B   | j                   j                         j                  dk(  r| j                   }n,t        j                  | j                   d| j
                  g      }| j                  j                  dkD  rt        j                  nt        j                  }t        j                  ||||      }t        j                  t        j                  |      t        j                  |g| j                         gd            }t!        j"                  || j                        S )N   r      )seedoutput_dtyper   )r1   rH   r   r   reshaper:   r   sizer   int64r   r
   multinomial	transposeconcatbatch_shape_tensorr   r   )rW   nrk   	logits_2dsample_dtypedrawss         r'   	_sample_nzCategorical._sample_n  s    {{$$)++i##DKK"doo1FGi#'::??Q#66<<FLLL""14l<EE"1#t66891=?E ==

++r)   c                 r   t        j                  |d      }| j                  r%t        j                  |t
        j                        }t        || j                  | j                  j                        \  }}t        j                  |d| j                  f      }t        j                  |dg      }t        j                  t        j                  || j                        |t        j                   |            }t#        j$                  |d      }t        j                  |t        j&                  |            S )Nkr8   target_dtyper$   r   axis)r   rJ   r3   rC   "embed_check_integer_casting_closedr   r   r(   r2   r   r$   r   rm   rP   wheresequence_mask
zeros_liker   
reduce_sumr   )rW   rz   r2   batch_flattened_probsbatch_flattened_kto_sum_overbatch_flattened_cdfs          r'   _cdfzCategorical._cdf  s    ac*A

>
>
&,,(a /	4::$**"7"79HAu &--e/143C3C.DF!))!bT2// 143C3CD235K #--kC0)//!2DEEr)   c                     t        j                  |d      }| j                  r%t        j                  |t
        j                        }t        || j                  | j                  j                        \  }}t        j                  ||       S )Nrz   r8   r{   r}   )labelsr1   )r   rJ   r3   rC   r   r   r   r(   r1   r   r$   r	   (sparse_softmax_cross_entropy_with_logits)rW   rz   r1   s      r'   	_log_probzCategorical._log_prob.  sy    ac*A

>
>
&,,(a/	4;;4::#8#8:IAv ;;  r)   c                     t        j                  t        j                  | j                        | j
                  z  d       S )Nr   r~   )r   r   r	   log_softmaxr1   r2   r[   s    r'   _entropyzCategorical._entropy;  s7    4;;'$**42? ? ?r)   c                     t        j                  | j                  | j                        }t        j                  || j
                        }|j                  | j                         |S )Nr~   )r   argmaxr1   rK   r   r   r    r;   )rW   rets     r'   _modezCategorical._mode?  sG    
//$++D,<,<
=C
--TZZ
(CMM$""#Jr)   r_   )__name__
__module____qualname____doc__r   
deprecatedr   r   rU   propertyr:   r1   r2   ra   rc   re   rg   rx   r   r   r   r   __classcell__)rY   s   @r'   r,   r,   :   s    Wr ;'
  LLQQf      5(8(,F,?r)   r,   Nc                 v   t        j                  |d| j                  |j                  g      5  t        j                  | j                        t        j                  |j                        z
  }t        j                  t        j                  | j                        |z  d      cddd       S # 1 sw Y   yxY w)aM  Calculate the batched KL divergence KL(a || b) with a and b Categorical.

  Args:
    a: instance of a Categorical distribution object.
    b: instance of a Categorical distribution object.
    name: (optional) Name to use for created operations.
      default is "kl_categorical_categorical".

  Returns:
    Batchwise KL(a || b)
  kl_categorical_categoricalr/   r   r~   N)r   rB   r1   r	   r   r   r   softmax)abr5   delta_log_probs1s       r'   _kl_categorical_categoricalr   F  s     ~~d8hh13 ( **1884**18845v~~ahh7:JJ$&(( ( (s   A6B//B8r_   )r   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r   r	   r
   #tensorflow.python.ops.distributionsr   r   r   rC   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   r(   Distributionr,   
RegisterKLr    r)   r'   <module>r      s    * 3 . + 4 + * ( , < @ I . 64 *+,H,++ H -HV [+6( 7(r)   