예제 #1
0
def multinomial(logits, num_samples, seed=None, name=None):
  """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.multinomial(tf.log([[10., 10.]]), 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.set_random_seed}
      for behavior.
    name: Optional name for the operation.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  """
  with ops.name_scope(name, "multinomial", [logits]):
    logits = ops.convert_to_tensor(logits, name="logits")
    seed1, seed2 = random_seed.get_seed(seed)
    return gen_random_ops.multinomial(
        logits, num_samples, seed=seed1, seed2=seed2)
예제 #2
0
def multinomial(logits, num_samples, seed=None, name=None, output_dtype=None):
    """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.multinomial(tf.log([[10., 10.]]), 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.set_random_seed}
      for behavior.
    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, "multinomial", [logits]):
        logits = ops.convert_to_tensor(logits, name="logits")
        seed1, seed2 = random_seed.get_seed(seed)
        return gen_random_ops.multinomial(logits,
                                          num_samples,
                                          seed=seed1,
                                          seed2=seed2,
                                          output_dtype=output_dtype)
예제 #3
0
def multinomial(logits, num_samples, seed=None, name=None):
  """Draws samples from a multinomial distribution.

  Example:

    samples = tf.multinomial(tf.log([[0.5, 0.5]]), 10)
    # samples has shape [1, 10], where each value is either 0 or 1.

    samples = tf.multinomial([[1, -1, -1]], 10)
    # samples is equivalent to tf.zeros([1, 10], dtype=tf.int64).

  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
      [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
      for behavior.
    name: Optional name for the operation.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  """
  with ops.op_scope([logits], name, "multinomial"):
    logits = ops.convert_to_tensor(logits, name="logits")
    seed1, seed2 = random_seed.get_seed(seed)
    return gen_random_ops.multinomial(logits, num_samples, seed=seed1,
                                      seed2=seed2)
예제 #4
0
def multinomial(logits, num_samples, seed=None, name=None):
    """Draws samples from a multinomial distribution.

  Example:

    samples = tf.multinomial(tf.log([[0.5, 0.5]]), 10)
    # samples has shape [1, 10], where each value is either 0 or 1.

    samples = tf.multinomial([[1, -1, -1]], 10)
    # samples is equivalent to tf.zeros([1, 10], dtype=tf.int64).

  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
      [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
      for behavior.
    name: Optional name for the operation.

  Returns:
    The drawn samples of shape `[batch_size, num_samples]`.
  """
    with ops.op_scope([logits], name, "multinomial"):
        logits = ops.convert_to_tensor(logits, name="logits")
        seed1, seed2 = random_seed.get_seed(seed)
        return gen_random_ops.multinomial(logits,
                                          num_samples,
                                          seed=seed1,
                                          seed2=seed2)
예제 #5
0
def multinomial_categorical_impl(logits, num_samples, dtype, seed):
    """Implementation for random.categorical (v1) and random.categorical (v2)."""
    logits = ops.convert_to_tensor(logits, name="logits")
    seed1, seed2 = random_seed.get_seed(seed)
    return gen_random_ops.multinomial(logits,
                                      num_samples,
                                      seed=seed1,
                                      seed2=seed2,
                                      output_dtype=dtype)
예제 #6
0
def multinomial_categorical_impl(logits, num_samples, dtype, seed):
    """Implementation for random.categorical (v1) and random.categorical (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}.")
    seed1, seed2 = random_seed.get_seed(seed)
    return gen_random_ops.multinomial(logits,
                                      num_samples,
                                      seed=seed1,
                                      seed2=seed2,
                                      output_dtype=dtype)
예제 #7
0
def multinomial_categorical_impl(logits, num_samples, dtype, seed):
  """Implementation for random.multinomial (v1) and random.categorical (v2)."""
  logits = ops.convert_to_tensor(logits, name="logits")
  seed1, seed2 = random_seed.get_seed(seed)
  return gen_random_ops.multinomial(
      logits, num_samples, seed=seed1, seed2=seed2, output_dtype=dtype)