o
    2hd                     @   s  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T ddlmZ ddlmZ ddlmZ edddgdejedddejddfddZed ddddejddfddZedddgdejedddejddfddZed ed ed d d!gdejed!ddejddfd"d!Zed# ed$d$d%gdejed%d=d&d%Zed' ed(d)gdejejdd*d+d>d,d)Zed-ejd>d.d/Z d0d1 Z!ed2 d3d4 Z"ed5d5d6gdejed6dejddfd7d6Z#ed8d9gdejed9ejddfd:d9Z$ed8g dejejddfd;d<Z%dS )?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                 C      t |d| ||gA}t| }t j||dd}t j||dd}t|\}	}
tj|||	|
d}|| }t	j
|||d}t||  |W  d   S 1 sNw   Y  dS )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value r*   [/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/ops/random_ops.pyr   '   s   .
$RandomStandardNormalg       g       @c              
   C   s   t |d| ||||gH}t| }t j||dd}	t j||dd}
t j||dd}t j||dd}t|\}}tj||	|
||||d}t	||  |W  d   S 1 sWw   Y  dS )	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#   r.   r/   r0   r1   r   r   r   r   means_tensorstddevs_tensorminvals_tensormaxvals_tensorr&   r   r'   r*   r*   r+   r-   e   s,   "
$r-   zrandom.truncated_normaltruncated_normalc                 C   r   )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.
  r6   r   r   r   r   r   N)r   r   r
   r   r   r   r   r   r6   r	   r    r!   r"   r*   r*   r+   r6      s   '
$ParameterizedTruncatedNormalTruncatedNormalzrandom.uniformrandom_uniformc              	   C   sr  t |}t jt jt jt jt jt jf}||vr"td| d| d|du r1|j	r/td| d}t
|d| ||gt}t| } t|toI|dk}t|toR|dk}|rZ|rZ|j	rjt
j||d	d
}t
j||dd
}t|\}	}
|j	rtj| |||	|
|d}n tj| ||	|
d}|r|st||}ntj|||  ||d}t||  |W  d   S 1 sw   Y  dS )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   r9   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_intr9   r	   multiplyr    r!   )r#   minvalmaxvalr   r   r   accepted_dtypesminval_is_zeromaxval_is_oner&   r   resultr*   r*   r+   r9      sJ   
H
$RandomUniformzrandom.shufflerandom_shufflec                 C   s    t |\}}tj| |||d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.
  r@   )r   r   r   rU   )r)   r   r   r&   r   r*   r*   r+   rU   F  s   RandomShufflezrandom.multinomialmultinomialz$Use `tf.random.categorical` instead.)dateinstructionsc                 C   sB   t |d| g t| |||W  d   S 1 sw   Y  dS )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]`.
  rW   Nr   r   multinomial_categorical_impl)logitsnum_samplesr   r   output_dtyper*   r*   r+   rW   j  s   $zrandom.categoricalc                 C   sB   t |d| g t| |||W  d   S 1 sw   Y  dS )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]`.
  categoricalNrZ   )r\   r]   r   r   r   r*   r*   r+   r_     s   $r_   c                 C   sn   t j| dd} |rt|ntj}tjtjf}||vr&td| d| dt|\}}t	j
| ||||dS )zGImplementation for random.categorical (v1) and random.categorical (v2).r\   r   r:   r;   r<   )r   r   r^   )r   r   r   rA   rG   rF   rH   r   r   r   rW   )r\   r]   r   r   rP   r&   r   r*   r*   r+   r[     s   
r[   Multinomialc                 C   s\   t  s(t jr*| j s,t|}t	
|}t|}| ||j d S d S d S d S )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_shaper*   r*   r+   _maybe_set_static_shape_helper  s   


rk   zrandom.gammarandom_gammac              
   C   s   t |d| ||g] t j| dtjd} t j|d|d}t j|dur$|ndd|d}tt|t|}t||}t	
|\}}	tt|jjjtj| |||	d| }
t|
| | |
W  d   S 1 sjw   Y  dS )	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))
  rl   r#   r   r   alphaNr=   betar   )r   r   r   r   rF   r   broadcast_dynamic_shaper#   broadcast_tor   r   r	   maximumnpfinfor   as_numpy_dtypetinyr   rl   rk   )r#   rn   ro   r   r   r   broadcast_shapealpha_broadcastr&   r   rS   r*   r*   r+   rl     s,   M$zrandom.poissonrandom_poissonc                 C   s   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   r*   r*   r+   ry   !  s   &c                 C   sz   t |d|| g* t j| dtjd} t|\}}tj| ||||d}t	|| | |W  d   S 1 s6w   Y  d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([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`.
  ry   r#   rm   )r   r   r   N)
r   r   r   r   rF   r   r   r   rz   rk   )r#   r{   r   r   r   r&   r   rS   r*   r*   r+   rz   J  s   %
$rz   )NN)NNN)&__doc__numpyrs   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_endpointsrD   r   NotDifferentiabler-   r6   r9   rU   
deprecatedrW   r_   r[   rk   rl   ry   rz   r*   r*   r*   r+   <module>   s   
8
6
0

m


[
&