tf.random.categorical
Stay organized with collections
Save and categorize content based on your preferences.
Draws samples from a categorical distribution.
tf.random.categorical(
logits, num_samples, dtype=None, seed=None, name=None
)
Used in the notebooks
Example:
# 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] .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.random.categorical\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/random_ops.py#L394-L421) |\n\nDraws samples from a categorical distribution.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.random.categorical`](https://www.tensorflow.org/api_docs/python/tf/random/categorical)\n\n\u003cbr /\u003e\n\n tf.random.categorical(\n logits, num_samples, dtype=None, seed=None, name=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Generate music with an RNN](https://www.tensorflow.org/tutorials/audio/music_generation) - [Playing CartPole with the Actor-Critic method](https://www.tensorflow.org/tutorials/reinforcement_learning/actor_critic) - [Text generation with an RNN](https://www.tensorflow.org/text/tutorials/text_generation) - [Wiki40B Language Models](https://www.tensorflow.org/hub/tutorials/wiki40b_lm) - [Federated Learning for Text Generation](https://www.tensorflow.org/federated/tutorials/federated_learning_for_text_generation) |\n\n#### Example:\n\n # samples has shape [1, 5], where each value is either 0 or 1 with equal\n # probability.\n samples = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 5)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|-----------------------------------------------------------------------------------------------------------------------------------------|\n| `logits` | 2-D Tensor with shape `[batch_size, num_classes]`. Each slice `[i, :]` represents the unnormalized log-probabilities for all classes. |\n| `num_samples` | 0-D. Number of independent samples to draw for each row slice. |\n| `dtype` | The integer type of the output: `int32` or `int64`. Defaults to `int64`. |\n| `seed` | A Python integer. Used to create a random seed for the distribution. See [`tf.random.set_seed`](../../tf/random/set_seed) for behavior. |\n| `name` | Optional name for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| The drawn samples of shape `[batch_size, num_samples]`. ||\n\n\u003cbr /\u003e"]]