can anyone give a tiny example to explain the params of tf.random.categorical?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
tf.random.categorical samples class indices from one or more categorical distributions. The function is small, but the parameters matter because the input must be logits, the output shape depends on num_samples, and each row of the input tensor is treated as a separate distribution.
The core parameters
The most important arguments are:
- '
logits: a 2D tensor shaped likebatch_size x num_classes' - '
num_samples: how many draws to take from each row' - '
dtype: integer type of the returned class IDs' - '
seed: optional seed for reproducibility'
The function does not expect probabilities directly. It expects logits, which are unnormalized scores.
A tiny working example
Here are two categorical distributions, each with three classes:
The output shape is 2 x 5 because there are two rows in logits and five samples requested per row.
How to read the rows
Each row of logits is one distribution:
- first row favors class
1 - second row favors class
0
That means most samples from the first row should be 1, and most samples from the second row should be 0. The function does not mix the rows together. Sampling happens row by row.
Why logits are used
TensorFlow uses logits because many models already produce raw scores before a softmax layer. Passing logits directly avoids unnecessary conversions and can be more numerically stable.
If you already have probabilities, convert them carefully:
Passing raw probabilities as if they were logits changes the distribution, which is a common mistake.
What num_samples really means
num_samples is not the number of classes and not the batch size. It is the number of draws from each categorical distribution.
For example:
The shape is 1 x 10 because there is one distribution and ten draws.
dtype and seed
The returned samples are class IDs, so the output dtype is an integer type such as tf.int64 or tf.int32.
The seed parameter helps when you want repeatable experiments or tests. It does not make the result "more correct." It just makes the randomness easier to reproduce.
Shape mistakes are very common
Even when you only want to sample from one distribution, logits still needs to be 2D. That means a single distribution should look like [[...]], not just [...].
For example:
That shape rule explains a lot of first-time confusion with this API, especially in quick experiments and notebooks for beginners who expect simpler inputs. It is a small but important detail.
Common Pitfalls
- Passing probabilities directly instead of logits.
- Forgetting that
logitsmust be 2D, even when you have only one distribution. - Misreading
num_samplesas the number of classes. - Expecting the output values to be probabilities instead of sampled class indices.
Summary
- '
tf.random.categoricalsamples class IDs from each row of a logits tensor.' - '
logitsmust be shaped likebatch_size x num_classes.' - '
num_samplescontrols how many draws are taken per row.' - Use
seedfor reproducibility anddtypefor the integer output type you want.
Related reading
- Can cond support TF ops with side effects?
- Can Dataset.map somehow parallelize over tf.py_func calls?
- Can flow_from_directory get train and validation data from the same directory in Keras?
- Can I apply tf.map_fn... to multiple inputs/outputs?
- Can consumer groups span different nodes in a cluster?
- Can evolutionary computation be a method of reinforcement learning?
- Can anyone please tell me what are the differences between pika and kombu messaging library in python?
- Can Flask have optional URL parameters?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.