multinomial¶
- paddle. multinomial ( x, num_samples=1, replacement=False, name=None ) [source]
-
Returns a Tensor filled with random values sampled from a Multinomical distribution. The input
x
is a tensor with probabilities for generating the random number. Each element inx
should be larger or equal to 0, but not all 0.replacement
indicates whether it is a replaceable sample. Ifreplacement
is True, a category can be sampled more than once.- Parameters
-
x (Tensor) – A tensor with probabilities for generating the random number. The data type should be float32, float64.
num_samples (int, optional) – Number of samples, default is 1.
replacement (bool, optional) – Whether it is a replaceable sample, default is False.
name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.
- Returns
-
A Tensor filled with sampled category index after
num_samples
times samples. - Return type
-
Tensor
Examples
import paddle paddle.seed(100) # on CPU device x = paddle.rand([2,4]) print(x) # [[0.5535528 0.20714243 0.01162981 0.51577556] # [0.36369765 0.2609165 0.18905126 0.5621971 ]] paddle.seed(200) # on CPU device out1 = paddle.multinomial(x, num_samples=5, replacement=True) print(out1) # [[3 3 0 0 0] # [3 3 3 1 0]] # out2 = paddle.multinomial(x, num_samples=5) # InvalidArgumentError: When replacement is False, number of samples # should be less than non-zero categories paddle.seed(300) # on CPU device out3 = paddle.multinomial(x, num_samples=3) print(out3) # [[3 0 1] # [3 1 0]]