Esempio n. 1
0
def stateless_multinomial_categorical_impl(logits, num_samples, dtype, seed):
    """Implementation for stateless multinomial/categorical ops (v1/v2)."""
    logits = ops.convert_to_tensor(logits, name="logits")
    return gen_stateless_random_ops.stateless_multinomial(logits,
                                                          num_samples,
                                                          seed,
                                                          output_dtype=dtype)
Esempio n. 2
0
def stateless_multinomial_categorical_impl(logits, num_samples, dtype, seed):
  """Implementation for stateless multinomial/categorical ops (v1/v2)."""
  logits = ops.convert_to_tensor(logits, name="logits")
  dtype = dtypes.as_dtype(dtype) if dtype else dtypes.int64
  accepted_dtypes = (dtypes.int32, dtypes.int64)
  if dtype not in accepted_dtypes:
    raise ValueError(
        f"Argument `dtype` got invalid value {dtype}. Accepted dtypes are "
        f"{accepted_dtypes}.")
  return gen_stateless_random_ops.stateless_multinomial(
      logits, num_samples, seed, output_dtype=dtype)
def stateless_multinomial(logits,
                          num_samples,
                          seed,
                          output_dtype=dtypes.int64,
                          name=None):
  """Draws deterministic pseudorandom samples from a multinomial distribution.

  This is a stateless version of `tf.multinomial`: if run twice with the
  same seeds, it will produce the same pseudorandom numbers.  The output is
  consistent across multiple runs on the same hardware (and between CPU
  and GPU), but may change between versions of TensorFlow or on non-CPU/GPU
  hardware.

  Example:

  ```python
  # samples has shape [1, 5], where each value is either 0 or 1 with equal
  # probability.
  samples = tf.random.stateless_multinomial(
      tf.log([[10., 10.]]), 5, seed=[7, 17])
  ```

  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 shape [2] integer Tensor of seeds to the random number generator.
    name: Optional name for the operation.
    output_dtype: integer type to use for the output. Defaults to int64.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  """
  with ops.name_scope(name, "stateless_multinomial", [logits, seed]):
    logits = ops.convert_to_tensor(logits, name="logits")
    return gen_stateless_random_ops.stateless_multinomial(
        logits, num_samples, seed, output_dtype=output_dtype)
Esempio n. 4
0
def stateless_multinomial(logits,
                          num_samples,
                          seed,
                          output_dtype=dtypes.int64,
                          name=None):
    """Draws deterministic pseudorandom samples from a multinomial distribution.

  This is a stateless version of `tf.multinomial`: if run twice with the
  same seeds, it will produce the same pseudorandom numbers.  The output is
  consistent across multiple runs on the same hardware (and between CPU
  and GPU), but may change between versions of TensorFlow or on non-CPU/GPU
  hardware.

  Example:

  ```python
  # samples has shape [1, 5], where each value is either 0 or 1 with equal
  # probability.
  samples = tf.random.stateless_multinomial(
      tf.log([[10., 10.]]), 5, seed=[7, 17])
  ```

  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 shape [2] integer Tensor of seeds to the random number generator.
    name: Optional name for the operation.
    output_dtype: integer type to use for the output. Defaults to int64.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  """
    with ops.name_scope(name, "stateless_multinomial", [logits, seed]):
        logits = ops.convert_to_tensor(logits, name="logits")
        return gen_stateless_random_ops.stateless_multinomial(
            logits, num_samples, seed, output_dtype=output_dtype)
Esempio n. 5
0
 def select_dataset_varying_logits(logits, seed):
     return array_ops.squeeze(
         gen_stateless_random_ops.stateless_multinomial(logits,
                                                        1,
                                                        seed=seed),
         axis=[0, 1])
def stateless_multinomial_categorical_impl(logits, num_samples, dtype, seed):
  """Implementation for stateless multinomial/categorical ops (v1/v2)."""
  logits = ops.convert_to_tensor(logits, name="logits")
  return gen_stateless_random_ops.stateless_multinomial(
      logits, num_samples, seed, output_dtype=dtype)
Esempio n. 7
0
 def select_dataset_varying_logits(logits, seed):
   return array_ops.squeeze(
       gen_stateless_random_ops.stateless_multinomial(logits, 1, seed=seed),
       axis=[0, 1])