
    AVhd                        d Z 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l
mZ dd	l
mZ dd
l
mZ ddl
mZ ddl ddlmZ ddlmZ ddlmZ  edddg      ej*                   ej,                  d      ddej.                  ddfd                     Z ej2                  d       ddddej.                  ddfdZ edddg      ej*                   ej,                  d      ddej.                  ddfd                     Z ej2                  d        ej2                  d        eddd g      ej*                   ej,                  d       ddej.                  ddfd!                     Z ej2                  d"        ed#d#d$g      ej*                   ej,                  d$      d8d%                     Z ej2                  d&        ed'd(g      ej*                   ej<                  dd)*      d9d+                     Z ed,      ej*                  d9d-              Z d. Z! ej2                  d/       d0 Z" ed1d1d2g      ej*                   ej,                  d2      dej.                  ddfd3                     Z# ed4d5g      ej*                   ej,                  d5      ej.                  ddfd6                     Z$ ed4g       ej*                  ej.                  ddfd7              Z%y):z)Operations for generating random numbers.    N)context)dtypes)ops)random_seed)tensor_util)	array_ops)gen_random_ops)math_ops)
shape_util)*)deprecation)dispatch)	tf_exportzrandom.normalrandom_normal)v1g        g      ?c                    t        j                  |d| ||g      5 }t        j                  |       }t        j                  ||d      }t        j                  ||d      }t        j                  |      \  }	}
t        j                  |||	|
      }||z  }t        j                  |||      }t        j                  ||        |cddd       S # 1 sw Y   yxY w)a  Outputs random values from a normal distribution.

  Example that generates a new set of random values every time:

  >>> tf.random.set_seed(5);
  >>> tf.random.normal([4], 0, 1, tf.float32)
  <tf.Tensor: shape=(4,), dtype=float32, numpy=..., dtype=float32)>

  Example that outputs a reproducible result:

  >>> tf.random.set_seed(5);
  >>> tf.random.normal([2,2], 0, 1, tf.float32, seed=1)
  <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
  array([[-1.3768897 , -0.01258316],
        [-0.169515   ,  1.0824056 ]], dtype=float32)>

  In this case, we are setting both the global and operation-level seed to
  ensure this result is reproducible.  See `tf.random.set_seed` for more
  information.

  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    mean: A Tensor or Python value of type `dtype`, broadcastable with `stddev`.
      The mean of the normal distribution.
    stddev: A Tensor or Python value of type `dtype`, broadcastable with `mean`.
      The standard deviation of the normal distribution.
    dtype: The float type of the output: `float16`, `bfloat16`, `float32`,
      `float64`. Defaults to `float32`.
    seed: A Python integer. Used to create a random seed for the distribution.
      See
      `tf.random.set_seed`
      for behavior.
    name: A name for the operation (optional).

  Returns:
    A tensor of the specified shape filled with random normal values.
  r   meandtypenamestddevseedseed2r   N)r   
name_scoper   shape_tensorconvert_to_tensorr   get_seedr	   random_standard_normalr
   addmaybe_set_static_shapeshaper   r   r   r   r   r   mean_tensorstddev_tensorseed1r   rndmulvalues                 P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/random_ops.pyr   r   '   s    \ ~~dOeT6-BC 
t**51L''EGK))&HMM''-LE5

/
/e%u6C

CLLk5E%%eU3
 
 
   B+CCRandomStandardNormalg       g       @c           
         t        j                  |d| ||||g      5 }t        j                  |       }t        j                  ||d      }	t        j                  ||d      }
t        j                  ||d      }t        j                  ||d      }t        j                  |      \  }}t        j                  ||	|
||||      }t        j                  ||        |cddd       S # 1 sw Y   yxY w)	a  Outputs random values from a truncated normal distribution.

  The generated values follow a normal distribution with specified mean and
  standard deviation, except that values whose magnitude is more than 2 standard
  deviations from the mean are dropped and re-picked.

  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    means: A 0-D Tensor or Python value of type `dtype`. The mean of the
      truncated normal distribution.
    stddevs: A 0-D Tensor or Python value of type `dtype`. The standard
      deviation of the truncated normal distribution.
    minvals: A 0-D Tensor or Python value of type `dtype`. The minimum value of
      the truncated normal distribution.
    maxvals: A 0-D Tensor or Python value of type `dtype`. The maximum value of
      the truncated normal distribution.
    dtype: The type of the output.
    seed: A Python integer. Used to create a random seed for the distribution.
      See
      `tf.random.set_seed`
      for behavior.
    name: A name for the operation (optional).

  Returns:
    A tensor of the specified shape filled with random truncated normal values.
  parameterized_truncated_normalmeansr   stddevsminvalsmaxvalsr   N)
r   r   r   r   r   r   r   r	   r/   r"   )r$   r0   r1   r2   r3   r   r   r   r   means_tensorstddevs_tensorminvals_tensormaxvals_tensorr'   r   r(   s                   r+   r/   r/   e   s    D ~~d<eWgw?A DH**51L((e'JL**7%iPN**7%iPN**7%iPN''-LE5

7
7C %%c51#  s   CC((C1zrandom.truncated_normaltruncated_normalc                    t        j                  |d| ||g      5 }t        j                  |       }t        j                  ||d      }t        j                  ||d      }t        j                  |      \  }	}
t        j                  |||	|
      }||z  }t        j                  |||      }t        j                  ||        |cddd       S # 1 sw Y   yxY w)a   Outputs random values from a truncated normal distribution.

  The values are drawn from a normal distribution with specified mean and
  standard deviation, discarding and re-drawing any samples that are more than
  two standard deviations from the mean.

  Examples:

  >>> tf.random.truncated_normal(shape=[2])
  <tf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>

  >>> tf.random.truncated_normal(shape=[2], mean=3, stddev=1, dtype=tf.float32)
  <tf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>

  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    mean: A 0-D Tensor or Python value of type `dtype`. The mean of the
      truncated normal distribution.
    stddev: A 0-D Tensor or Python value of type `dtype`. The standard deviation
      of the normal distribution, before truncation.
    dtype: The type of the output. Restricted to floating-point types:
      `tf.half`, `tf.float`, `tf.double`, etc.
    seed: A Python integer. Used to create a random seed for the distribution.
      See `tf.random.set_seed` for more information.
    name: A name for the operation (optional).

  Returns:
    A tensor of the specified shape filled with random truncated normal values.
  r8   r   r   r   r   r   N)r   r   r   r   r   r   r   r	   r8   r
   r!   r"   r#   s                 r+   r8   r8      s    N ~~d.f0EF 
$**51L''EGK))&HMM''-LE5

)
)e%u6C

CLLk5E%%eU3
 
 
r,   ParameterizedTruncatedNormalTruncatedNormalzrandom.uniformrandom_uniformc           	         t        j                  |      }t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  f}||vrt        d| d| d      ||j                  rt        d|z        d}t        j                  |d| ||g      5 }t        j                  |       } t        |t              xr |dk(  }t        |t              xr |dk(  }|r|r|j                  r0t        j                   ||d	
      }t        j                   ||d
      }t#        j$                  |      \  }	}
|j                  rt'        j(                  | |||	|
|      }nRt'        j*                  | ||	|
      }|r|s5t-        j.                  ||      }nt-        j0                  |||z
  z  ||      }t        j2                  ||        |cddd       S # 1 sw Y   yxY w)a  Outputs random values from a uniform distribution.

  The generated values follow a uniform distribution in the range
  `[minval, maxval)`. The lower bound `minval` is included in the range, while
  the upper bound `maxval` is excluded.

  For floats, the default range is `[0, 1)`.  For ints, at least `maxval` must
  be specified explicitly.

  In the integer case, the random integers are slightly biased unless
  `maxval - minval` is an exact power of two.  The bias is small for values of
  `maxval - minval` significantly smaller than the range of the output (either
  `2**32` or `2**64`).

  Examples:

  >>> tf.random.uniform(shape=[2])
  <tf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>
  >>> tf.random.uniform(shape=[], minval=-1., maxval=0.)
  <tf.Tensor: shape=(), dtype=float32, numpy=-...>
  >>> tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)
  <tf.Tensor: shape=(), dtype=int64, numpy=...>

  The `seed` argument produces a deterministic sequence of tensors across
  multiple calls. To repeat that sequence, use `tf.random.set_seed`:

  >>> tf.random.set_seed(5)
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=2>
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=0>
  >>> tf.random.set_seed(5)
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=2>
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=0>

  Without `tf.random.set_seed` but with a `seed` argument is specified, small
  changes to function graphs or previously executed operations will change the
  returned value. See `tf.random.set_seed` for details.

  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    minval: A Tensor or Python value of type `dtype`, broadcastable with
      `shape` (for integer types, broadcasting is not supported, so it needs to
      be a scalar). The lower bound on the range of random values to generate
      (inclusive).  Defaults to 0.
    maxval: A Tensor or Python value of type `dtype`, broadcastable with
      `shape` (for integer types, broadcasting is not supported, so it needs to
      be a scalar). The upper bound on the range of random values to generate
      (exclusive). Defaults to 1 if `dtype` is floating point.
    dtype: The type of the output: `float16`, `bfloat16`, `float32`, `float64`,
      `int32`, or `int64`. Defaults to `float32`.
    seed: A Python integer. Used in combination with `tf.random.set_seed` to
      create a reproducible sequence of tensors across multiple calls.
    name: A name for the operation (optional).

  Returns:
    A tensor of the specified shape filled with random uniform values.

  Raises:
    ValueError: If `dtype` is integral and `maxval` is not specified.
  #Argument `dtype` got invalid value . Accepted dtypes are .Nz(Must specify maxval for integer dtype %r   r<   r   minr   maxr   r   r   r   r   )r   as_dtypefloat16bfloat16float32float64int32int64
ValueError
is_integerr   r   r   r   
isinstanceintr   r   r   r	   random_uniform_intr<   r
   multiplyr!   r"   )r$   minvalmaxvalr   r   r   accepted_dtypesminval_is_zeromaxval_is_oner'   r   results               r+   r<   r<      s   P //%
 %^^V__fnn^^V\\6<<A/
/!

-eW4J
1	  ^AEIJJF
~~d,uff.EF $##E*E  ,<1Nvs+;!M%2B2B$$V5uEf$$V5uEf''-LE500
e5tEf ,,
U%1f	$$VV4&f8&tL
 %%fe45  s    D-G77H RandomUniformzrandom.shufflerandom_shufflec                 d    t        j                  |      \  }}t        j                  | |||      S )a  Randomly shuffles a tensor along its first dimension.

  The tensor is shuffled along dimension 0, such that each `value[j]` is mapped
  to one and only one `output[i]`. For example, a mapping that might occur for a
  3x2 tensor is:

  ```python
  [[1, 2],       [[5, 6],
   [3, 4],  ==>   [1, 2],
   [5, 6]]        [3, 4]]
  ```

  Args:
    value: A Tensor to be shuffled.
    seed: A Python integer. Used to create a random seed for the distribution.
      See
      `tf.random.set_seed`
      for behavior.
    name: A name for the operation (optional).

  Returns:
    A tensor of same shape and type as `value`, shuffled along its first
    dimension.
  rD   )r   r   r	   rY   )r*   r   r   r'   r   s        r+   rY   rY   F  s4    8 %%d+,%		&	&%u4
1 1    RandomShufflezrandom.multinomialmultinomialz$Use `tf.random.categorical` instead.)dateinstructionsc                 z    t        j                  |d| g      5  t        | |||      cddd       S # 1 sw Y   yxY w)a/  Draws samples from a multinomial distribution.

  Example:

  ```python
  # samples has shape [1, 5], where each value is either 0 or 1 with equal
  # probability.
  samples = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 5)
  ```

  Args:
    logits: 2-D Tensor with shape `[batch_size, num_classes]`.  Each slice
      `[i, :]` represents the unnormalized log-probabilities for all classes.
    num_samples: 0-D.  Number of independent samples to draw for each row slice.
    seed: A Python integer. Used to create a random seed for the distribution.
      See `tf.random.set_seed` for behavior.
    name: Optional name for the operation.
    output_dtype: The integer type of the output: `int32` or `int64`. Defaults
      to `int64`.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  r]   Nr   r   multinomial_categorical_impl)logitsnum_samplesr   r   output_dtypes        r+   r]   r]   j  s=    8 ~~dMF84 Q'\4PQ Q Q   1:zrandom.categoricalc                 z    t        j                  |d| g      5  t        | |||      cddd       S # 1 sw Y   yxY w)a(  Draws samples from a categorical distribution.

  Example:

  ```python
  # samples has shape [1, 5], where each value is either 0 or 1 with equal
  # probability.
  samples = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 5)
  ```

  Args:
    logits: 2-D Tensor with shape `[batch_size, num_classes]`.  Each slice
      `[i, :]` represents the unnormalized log-probabilities for all classes.
    num_samples: 0-D.  Number of independent samples to draw for each row slice.
    dtype: The integer type of the output: `int32` or `int64`. Defaults to
      `int64`.
    seed: A Python integer. Used to create a random seed for the distribution.
      See `tf.random.set_seed` for behavior.
    name: Optional name for the operation.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  categoricalNra   )rc   rd   r   r   r   s        r+   rh   rh     s=    4 ~~dMF84 J'UDIJ J Jrf   c                 N   t        j                  | d      } |rt        j                  |      nt        j                  }t        j
                  t        j                  f}||vrt        d| d| d      t        j                  |      \  }}t        j                  | ||||      S )zGImplementation for random.categorical (v1) and random.categorical (v2).rc   r   r>   r?   r@   )r   r   re   )r   r   r   rE   rK   rJ   rL   r   r   r	   r]   )rc   rd   r   r   rT   r'   r   s          r+   rb   rb     s      h7&$)&//%
 v||%\\6<<0/
/!

-eW4J
1	  %%d+,%		#	#kU
H Hr[   Multinomialc                 t   t        j                         st        j                         j                  r| j
                  j                         sjt        j                  |      }t        j                  |      }t        j                  |      }| j                  |j                  |j
                               y y y y )N)r   executing_eagerlyr   get_default_graphbuilding_functionr$   is_fully_definedr   r   r   constant_value_as_shaper   	set_shapeconcatenate)tensorr$   postfix_tensorconst_shapes       r+   _maybe_set_static_shape_helperrv     s    

#
#
%	//
,,
'
'
)##E*E55e<K**>:N
[,,^-A-ABC	 * 0 &r[   zrandom.gammarandom_gammac           
         t        j                  |d| ||g      5  t        j                  | dt        j                        } t        j                  |d|      }t        j                  ||ndd|      }t        j                  t        j                  |      t        j                  |            }t        j                  ||      }t        j                  |      \  }}	t        j                  t        j                  |j                  j                         j"                  t%        j&                  | |||	      |z        }
t)        |
| |       |
cddd       S # 1 sw Y   yxY w)	a6  Draws `shape` samples from each of the given Gamma distribution(s).

  `alpha` is the shape parameter describing the distribution(s), and `beta` is
  the inverse scale parameter(s).

  Note: Because internal calculations are done using `float64` and casting has
  `floor` semantics, we must manually map zero outcomes to the smallest
  possible positive floating-point value, i.e., `np.finfo(dtype).tiny`.  This
  means that `np.finfo(dtype).tiny` occurs more frequently than it otherwise
  should.  This bias can only happen for small values of `alpha`, i.e.,
  `alpha << 1` or large values of `beta`, i.e., `beta >> 1`.

  The samples are differentiable w.r.t. alpha and beta.
  The derivatives are computed using the approach described in
  (Figurnov et al., 2018).

  Example:

  ```python
  samples = tf.random.gamma([10], [0.5, 1.5])
  # samples has shape [10, 2], where each slice [:, 0] and [:, 1] represents
  # the samples drawn from each distribution

  samples = tf.random.gamma([7, 5], [0.5, 1.5])
  # samples has shape [7, 5, 2], where each slice [:, :, 0] and [:, :, 1]
  # represents the 7x5 samples drawn from each of the two distributions

  alpha = tf.constant([[1.],[3.],[5.]])
  beta = tf.constant([[3., 4.]])
  samples = tf.random.gamma([30], alpha=alpha, beta=beta)
  # samples has shape [30, 3, 2], with 30 samples each of 3x2 distributions.

  loss = tf.reduce_mean(tf.square(samples))
  dloss_dalpha, dloss_dbeta = tf.gradients(loss, [alpha, beta])
  # unbiased stochastic derivatives of the loss function
  alpha.shape == dloss_dalpha.shape  # True
  beta.shape == dloss_dbeta.shape  # True
  ```

  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output samples
      to be drawn per alpha/beta-parameterized distribution.
    alpha: A Tensor or Python value or N-D array of type `dtype`. `alpha`
      provides the shape parameter(s) describing the gamma distribution(s) to
      sample. Must be broadcastable with `beta`.
    beta: A Tensor or Python value or N-D array of type `dtype`. Defaults to 1.
      `beta` provides the inverse scale parameter(s) of the gamma
      distribution(s) to sample. Must be broadcastable with `alpha`.
    dtype: The type of alpha, beta, and the output: `float16`, `float32`, or
      `float64`.
    seed: A Python integer. Used to create a random seed for the distributions.
      See
      `tf.random.set_seed`
      for behavior.
    name: Optional name for the operation.

  Returns:
    samples: a `Tensor` of shape
      `tf.concat([shape, tf.shape(alpha + beta)], axis=0)` with values of type
      `dtype`.

  References:
    Implicit Reparameterization Gradients:
      [Figurnov et al., 2018]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients)
      ([pdf]
      (http://papers.nips.cc/paper/7326-implicit-reparameterization-gradients.pdf))
  rw   r$   r   r   alphaNrA   betar   )r   r   r   r   rJ   r   broadcast_dynamic_shaper$   broadcast_tor   r   r
   maximumnpfinfor   as_numpy_dtypetinyr	   rw   rv   )r$   rz   r{   r   r   r   broadcast_shapealpha_broadcastr'   r   rW   s              r+   rw   rw     s'   Z ~~dNUE4,@A !!%gV\\JE!!%gUCE   afECD77	 57O,,UODO''-LE5
++,11##?e	=?C	DEF #65/B  s   D4EE"zrandom.poissonrandom_poissonc                      t        || |||      S )a  Draws `shape` samples from each of the given Poisson distribution(s).

  `lam` is the rate parameter describing the distribution(s).

  Example:

  ```python
  samples = tf.random.poisson([0.5, 1.5], [10])
  # samples has shape [10, 2], where each slice [:, 0] and [:, 1] represents
  # the samples drawn from each distribution

  samples = tf.random.poisson([12.2, 3.3], [7, 5])
  # samples has shape [7, 5, 2], where each slice [:, :, 0] and [:, :, 1]
  # represents the 7x5 samples drawn from each of the two distributions
  ```

  Args:
    lam: A Tensor or Python value or N-D array of type `dtype`.
      `lam` provides the rate parameter(s) describing the poisson
      distribution(s) to sample.
    shape: A 1-D integer Tensor or Python array. The shape of the output samples
      to be drawn per "rate"-parameterized distribution.
    dtype: The type of the output: `float16`, `float32`, `float64`, `int32` or
      `int64`.
    seed: A Python integer. Used to create a random seed for the distributions.
      See
      `tf.random.set_seed`
      for behavior.
    name: Optional name for the operation.

  Returns:
    samples: a `Tensor` of shape `tf.concat([shape, tf.shape(lam)], axis=0)`
      with values of type `dtype`.
  )random_poisson_v2)lamr$   r   r   r   s        r+   r   r   !  s    L 
5#udD	99r[   c                 .   t        j                  |d|| g      5  t        j                  | dt        j                        } t        j                  |      \  }}t        j                  | ||||      }t        || |       |cddd       S # 1 sw Y   yxY w)a  Draws `shape` samples from each of the given Poisson distribution(s).

  `lam` is the rate parameter describing the distribution(s).

  Example:

  ```python
  samples = tf.random.poisson([10], [0.5, 1.5])
  # samples has shape [10, 2], where each slice [:, 0] and [:, 1] represents
  # the samples drawn from each distribution

  samples = tf.random.poisson([7, 5], [12.2, 3.3])
  # samples has shape [7, 5, 2], where each slice [:, :, 0] and [:, :, 1]
  # represents the 7x5 samples drawn from each of the two distributions
  ```

  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output samples
      to be drawn per "rate"-parameterized distribution.
    lam: A Tensor or Python value or N-D array of type `dtype`.
      `lam` provides the rate parameter(s) describing the poisson
      distribution(s) to sample.
    dtype: The type of the output: `float16`, `float32`, `float64`, `int32` or
      `int64`.
    seed: A Python integer. Used to create a random seed for the distributions.
      See
      `tf.random.set_seed`
      for behavior.
    name: Optional name for the operation.

  Returns:
    samples: a `Tensor` of shape `tf.concat([shape, tf.shape(lam)], axis=0)`
      with values of type `dtype`.
  r   r$   ry   )r   r   r   N)
r   r   r   r   rJ   r   r   r	   r   rv   )r$   r   r   r   r   r'   r   rW   s           r+   r   r   J  s    J ~~d,sEl; !!%gV\\JE''-LE5--s%e5:F"65#6  s   A'BB)NN)NNN)&__doc__numpyr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r	   r
   r   $tensorflow.python.ops.gen_random_opstensorflow.python.utilr   r    tensorflow.python.util.tf_exportr   add_dispatch_supportdeprecated_endpointsrH   r   NotDifferentiabler/   r8   r<   rY   
deprecatedr]   rh   rb   rv   rw   r   r    r[   r+   <module>r      s   0  + . + 3 3 + 0 * , 3 / + 6 ?AB	!!!/25 3  C5p   , - *-+.+/+.)/(,(,3l $(*<=?	!!!"45!>>- 6 ?-`   4 5   ' ( !13C DE	!!!"23j 4  FjZ   o & !13C DE	!!!"231 4  F1<   o & #]34		BDQD  5Q8  	J  !J8H   m $D >~~>?	!!!.1 ~~X 2  @Xv !123	!!!"23%+^^$T #: 4  4#:L #	(.T )  $)r[   