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) >>> Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[0.55355281, 0.20714243, 0.01162981, 0.51577556], [0.36369765, 0.26091650, 0.18905126, 0.56219709]]) >>> >>> paddle.seed(200) # on CPU device >>> out1 = paddle.multinomial(x, num_samples=5, replacement=True) >>> print(out1) >>> Tensor(shape=[2, 5], dtype=int64, place=Place(cpu), stop_gradient=True, [[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) >>> Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, [[3, 0, 1], [3, 1, 0]]) >>>