From 9128de9e773d8539870c64407218316af3c6b8fd Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 30 Sep 2020 03:44:01 +0000 Subject: [PATCH 01/12] fix multinomial doc --- python/paddle/tensor/random.py | 54 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index a46946cea868a7..20ad025da24530 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -57,19 +57,17 @@ def bernoulli(x, name=None): Examples: .. code-block:: python - import paddle - - paddle.disable_static() + import paddle - x = paddle.rand([2, 3]) - print(x.numpy()) - # [[0.11272584 0.3890902 0.7730957 ] - # [0.10351662 0.8510418 0.63806665]] + x = paddle.rand([2, 3]) + print(x.numpy()) + # [[0.11272584 0.3890902 0.7730957 ] + # [0.10351662 0.8510418 0.63806665]] - out = paddle.bernoulli(x) - print(out.numpy()) - # [[0. 0. 1.] - # [0. 0. 1.]] + out = paddle.bernoulli(x) + print(out.numpy()) + # [[0. 0. 1.] + # [0. 0. 1.]] """ @@ -108,28 +106,26 @@ def multinomial(x, num_samples=1, replacement=False, name=None): Examples: .. code-block:: python - import paddle - - paddle.disable_static() + import paddle - x = paddle.rand([2,4]) - print(x.numpy()) - # [[0.7713825 0.4055941 0.433339 0.70706886] - # [0.9223313 0.8519825 0.04574518 0.16560672]] + x = paddle.rand([2,4]) + print(x.numpy()) + # [[0.7713825 0.4055941 0.433339 0.70706886] + # [0.9223313 0.8519825 0.04574518 0.16560672]] - out1 = paddle.multinomial(x, num_samples=5, replacement=True) - print(out1.numpy()) - # [[3 3 1 1 0] - # [0 0 0 0 1]] + out1 = paddle.multinomial(x, num_samples=5, replacement=True) + print(out1.numpy()) + # [[3 3 1 1 0] + # [0 0 0 0 1]] - # out2 = paddle.multinomial(x, num_samples=5) - # OutOfRangeError: When replacement is False, number of samples - # should be less than non-zero categories + # out2 = paddle.multinomial(x, num_samples=5) + # OutOfRangeError: When replacement is False, number of samples + # should be less than non-zero categories - out3 = paddle.multinomial(x, num_samples=3) - print(out3.numpy()) - # [[0 2 3] - # [0 1 3]] + out3 = paddle.multinomial(x, num_samples=3) + print(out3.numpy()) + # [[0 2 3] + # [0 1 3]] """ From 2144fb66259257300ea611e8c5fc03a359d0d686 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 14 Oct 2020 10:57:56 +0000 Subject: [PATCH 02/12] fix multinomial error message --- paddle/fluid/operators/multinomial_op.cc | 6 ++++ paddle/fluid/operators/multinomial_op.cu | 9 +++++ .../tests/unittests/test_multinomial_op.py | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/paddle/fluid/operators/multinomial_op.cc b/paddle/fluid/operators/multinomial_op.cc index 94c9fc2d9742b3..60980378027178 100644 --- a/paddle/fluid/operators/multinomial_op.cc +++ b/paddle/fluid/operators/multinomial_op.cc @@ -53,12 +53,18 @@ class MultinomialOp : public framework::OperatorWithKernel { auto x_dim = ctx->GetInputDim("X"); int64_t x_rank = x_dim.size(); + PADDLE_ENFORCE_EQ( + x_rank > 0 && x_rank <= 2, true, + platform::errors::PreconditionNotMet( + "Input probability distribution should be 1 or 2 dimension")); std::vector out_dims(x_rank); for (int64_t i = 0; i < x_rank - 1; i++) { out_dims[i] = x_dim[i]; } int64_t num_samples = ctx->Attrs().Get("num_samples"); + PADDLE_ENFORCE_GT(num_samples, 0, platform::errors::OutOfRange( + "Number of samples should be > 0")); out_dims[x_rank - 1] = num_samples; ctx->SetOutputDim("Out", framework::make_ddim(out_dims)); diff --git a/paddle/fluid/operators/multinomial_op.cu b/paddle/fluid/operators/multinomial_op.cu index 2762f0ce9bd46a..336c14d2c922bc 100644 --- a/paddle/fluid/operators/multinomial_op.cu +++ b/paddle/fluid/operators/multinomial_op.cu @@ -21,6 +21,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/multinomial_op.h" +#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/transform.h" namespace paddle { @@ -31,6 +32,14 @@ __global__ void NormalizeProbability(T* norm_probs, const T* in_data, T* sum_rows) { int id = threadIdx.x + blockIdx.x * blockDim.x + blockIdx.y * gridDim.x * blockDim.x; + PADDLE_ENFORCE(in_data[id] >= 0.0, + "The input of multinomial distribution should be >= 0"); + PADDLE_ENFORCE( + !std::isinf(static_cast(in_data[id])) && + !std::isnan(static_cast(in_data[id])), + "The input of multinomial distribution shoud not be infinity or NaN"); + PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, + "The sum of input should not be 0"); norm_probs[id] = in_data[id] / sum_rows[blockIdx.y]; } diff --git a/python/paddle/fluid/tests/unittests/test_multinomial_op.py b/python/paddle/fluid/tests/unittests/test_multinomial_op.py index 7cca7738efd050..db4978930e049f 100644 --- a/python/paddle/fluid/tests/unittests/test_multinomial_op.py +++ b/python/paddle/fluid/tests/unittests/test_multinomial_op.py @@ -17,12 +17,14 @@ import unittest import paddle import paddle.fluid as fluid +from paddle.fluid import core from op_test import OpTest import numpy as np class TestMultinomialOp(OpTest): def setUp(self): + paddle.enable_static() self.op_type = "multinomial" self.init_data() self.inputs = {"X": self.input_np} @@ -175,5 +177,39 @@ def test_alias(self): paddle.tensor.random.multinomial(x, num_samples=10, replacement=True) +class TestMultinomialError(unittest.TestCase): + def setUp(self): + paddle.disable_static() + + def test_num_sample(self): + def test_num_sample_less_than_0(): + x = paddle.rand([4]) + paddle.multinomial(x, num_samples=-2) + + self.assertRaises(core.EnforceNotMet, test_num_sample_less_than_0) + + def test_replacement_False(self): + def test_samples_larger_than_categories(): + x = paddle.rand([4]) + paddle.multinomial(x, num_samples=5, replacement=False) + + self.assertRaises(core.EnforceNotMet, + test_samples_larger_than_categories) + + def test_input_probs_dim(self): + def test_dim_larger_than_2(): + x = paddle.rand([2, 3, 3]) + paddle.multinomial(x) + + self.assertRaises(core.EnforceNotMet, test_dim_larger_than_2) + + def test_dim_less_than_1(): + x_np = np.random.random([]) + x = paddle.to_tensor(x_np) + paddle.multinomial(x) + + self.assertRaises(core.EnforceNotMet, test_dim_less_than_1) + + if __name__ == "__main__": unittest.main() From e6e54eaa95b5f94ada0f96c5f1636b75d7907ec8 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 14 Oct 2020 11:08:50 +0000 Subject: [PATCH 03/12] little doc change --- python/paddle/tensor/random.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index 20ad025da24530..ffd9f10f5403e5 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -115,8 +115,8 @@ def multinomial(x, num_samples=1, replacement=False, name=None): out1 = paddle.multinomial(x, num_samples=5, replacement=True) print(out1.numpy()) - # [[3 3 1 1 0] - # [0 0 0 0 1]] + # [[3, 3, 1, 1, 0] + # [0, 0, 0, 0, 1]] # out2 = paddle.multinomial(x, num_samples=5) # OutOfRangeError: When replacement is False, number of samples @@ -124,8 +124,8 @@ def multinomial(x, num_samples=1, replacement=False, name=None): out3 = paddle.multinomial(x, num_samples=3) print(out3.numpy()) - # [[0 2 3] - # [0 1 3]] + # [[0, 2, 3] + # [0, 1, 3]] """ From 1bb315b2fd6ea3d2fe13019ceb9bb3172c1b2985 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 14 Oct 2020 12:08:07 +0000 Subject: [PATCH 04/12] fix Categorical class doc --- python/paddle/distribution.py | 184 +++++++++++++++++----------------- 1 file changed, 93 insertions(+), 91 deletions(-) diff --git a/python/paddle/distribution.py b/python/paddle/distribution.py index ff3e882229ae8c..7e7a81730e3b10 100644 --- a/python/paddle/distribution.py +++ b/python/paddle/distribution.py @@ -662,48 +662,50 @@ class Categorical(Distribution): Args: logits(list|numpy.ndarray|Tensor): The logits input of categorical distribution. The data type is float32 or float64. + name(str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Examples: .. code-block:: python - import paddle - from paddle.distribution import Categorical + import paddle + from paddle.distribution import Categorical - x = paddle.rand([6]) - print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] - y = paddle.rand([6]) - print(y.numpy()) - # [0.6365463 , 0.7278677 , 0.90260243, - # 0.5226815 , 0.35837543, 0.13981032] + x = paddle.rand([6]) + print(x.numpy()) + # [0.32564053, 0.99334985, 0.99034804, + # 0.09053693, 0.30820143, 0.19095989] + y = paddle.rand([6]) + print(y.numpy()) + # [0.6365463 , 0.7278677 , 0.90260243, + # 0.5226815 , 0.35837543, 0.13981032] - cat = Categorical(x) - cat2 = Categorical(y) + cat = Categorical(x) + cat2 = Categorical(y) - cat.sample([2,3]) - # [[5, 1, 1], - # [0, 1, 2]] + cat.sample([2,3]) + # [[5, 1, 1], + # [0, 1, 2]] - cat.entropy() - # [1.71887] + cat.entropy() + # [1.71887] - cat.kl_divergence(cat2) - # [0.0278455] + cat.kl_divergence(cat2) + # [0.0278455] - value = paddle.to_tensor([2,1,3]) - cat.probs(value) - # [0.341613 0.342648 0.03123] + value = paddle.to_tensor([2,1,3]) + cat.probs(value) + # [0.341613 0.342648 0.03123] - cat.log_prob(value) - # [-1.07408 -1.07105 -3.46638] + cat.log_prob(value) + # [-1.07408 -1.07105 -3.46638] """ def __init__(self, logits, name=None): """ Args: - logits(list|numpy.ndarray|Variable): The logits input of categorical distribution. The data type is float32 or float64. + logits(list|numpy.ndarray|Tensor): The logits input of categorical distribution. The data type is float32 or float64. + name(str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. """ if not in_dygraph_mode(): check_type(logits, 'logits', (np.ndarray, tensor.Variable, list), @@ -727,27 +729,27 @@ def sample(self, shape): """Generate samples of the specified shape. Args: - shape (list): Shape of the generated samples. + shape (list): Shape of the generated samples. Returns: - Tensor: A tensor with prepended dimensions shape. + Tensor: A tensor with prepended dimensions shape. Examples: - .. code-block:: python + .. code-block:: python - import paddle - from paddle.distribution import Categorical + import paddle + from paddle.distribution import Categorical - x = paddle.rand([6]) - print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + x = paddle.rand([6]) + print(x.numpy()) + # [0.32564053, 0.99334985, 0.99034804, + # 0.09053693, 0.30820143, 0.19095989] - cat = Categorical(x) + cat = Categorical(x) - cat.sample([2,3]) - # [[5, 1, 1], - # [0, 1, 2]] + cat.sample([2,3]) + # [[5, 1, 1], + # [0, 1, 2]] """ name = self.name + '_sample' @@ -775,28 +777,28 @@ def kl_divergence(self, other): other (Categorical): instance of Categorical. The data type is float32. Returns: - Variable: kl-divergence between two Categorical distributions. + Tensor: kl-divergence between two Categorical distributions. Examples: - .. code-block:: python + .. code-block:: python - import paddle - from paddle.distribution import Categorical + import paddle + from paddle.distribution import Categorical - x = paddle.rand([6]) - print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] - y = paddle.rand([6]) - print(y.numpy()) - # [0.6365463 , 0.7278677 , 0.90260243, - # 0.5226815 , 0.35837543, 0.13981032] + x = paddle.rand([6]) + print(x.numpy()) + # [0.32564053, 0.99334985, 0.99034804, + # 0.09053693, 0.30820143, 0.19095989] + y = paddle.rand([6]) + print(y.numpy()) + # [0.6365463 , 0.7278677 , 0.90260243, + # 0.5226815 , 0.35837543, 0.13981032] - cat = Categorical(x) - cat2 = Categorical(y) + cat = Categorical(x) + cat2 = Categorical(y) - cat.kl_divergence(cat2) - # [0.0278455] + cat.kl_divergence(cat2) + # [0.0278455] """ name = self.name + '_kl_divergence' @@ -823,23 +825,23 @@ def entropy(self): """Shannon entropy in nats. Returns: - Variable: Shannon entropy of Categorical distribution. The data type is float32. + Tensor: Shannon entropy of Categorical distribution. The data type is float32. Examples: - .. code-block:: python + .. code-block:: python - import paddle - from paddle.distribution import Categorical + import paddle + from paddle.distribution import Categorical - x = paddle.rand([6]) - print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + x = paddle.rand([6]) + print(x.numpy()) + # [0.32564053, 0.99334985, 0.99034804, + # 0.09053693, 0.30820143, 0.19095989] - cat = Categorical(x) + cat = Categorical(x) - cat.entropy() - # [1.71887] + cat.entropy() + # [1.71887] """ name = self.name + '_entropy' @@ -864,27 +866,27 @@ def probs(self, value): with ``logits. That is, ``value[:-1] = logits[:-1]``. Args: - value (Tensor): The input tensor represents the selected category index. + value (Tensor): The input tensor represents the selected category index. Returns: - Tensor: probability according to the category index. + Tensor: probability according to the category index. Examples: - .. code-block:: python + .. code-block:: python - import paddle - from paddle.distribution import Categorical + import paddle + from paddle.distribution import Categorical - x = paddle.rand([6]) - print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + x = paddle.rand([6]) + print(x.numpy()) + # [0.32564053, 0.99334985, 0.99034804, + # 0.09053693, 0.30820143, 0.19095989] - cat = Categorical(x) + cat = Categorical(x) - value = paddle.to_tensor([2,1,3]) - cat.probs(value) - # [0.341613 0.342648 0.03123] + value = paddle.to_tensor([2,1,3]) + cat.probs(value) + # [0.341613 0.342648 0.03123] """ name = self.name + '_probs' @@ -929,28 +931,28 @@ def log_prob(self, value): """Log probabilities of the given category. Refer to ``probs`` method. Args: - value (Tensor): The input tensor represents the selected category index. + value (Tensor): The input tensor represents the selected category index. Returns: - Tensor: Log probability. + Tensor: Log probability. Examples: - .. code-block:: python + .. code-block:: python - import paddle - from paddle.distribution import Categorical + import paddle + from paddle.distribution import Categorical - x = paddle.rand([6]) - print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + x = paddle.rand([6]) + print(x.numpy()) + # [0.32564053, 0.99334985, 0.99034804, + # 0.09053693, 0.30820143, 0.19095989] - cat = Categorical(x) + cat = Categorical(x) - value = paddle.to_tensor([2,1,3]) + value = paddle.to_tensor([2,1,3]) - cat.log_prob(value) - # [-1.07408 -1.07105 -3.46638] + cat.log_prob(value) + # [-1.07408 -1.07105 -3.46638] """ name = self.name + '_log_prob' From 8f35154e0441d5bf453a575163cd3b67c7891eeb Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 14 Oct 2020 12:36:56 +0000 Subject: [PATCH 05/12] optimize format of error message --- paddle/fluid/operators/multinomial_op.cc | 19 +++++++++++++------ paddle/fluid/operators/multinomial_op.cu | 12 +++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cc b/paddle/fluid/operators/multinomial_op.cc index 60980378027178..7e32881be2fa2c 100644 --- a/paddle/fluid/operators/multinomial_op.cc +++ b/paddle/fluid/operators/multinomial_op.cc @@ -53,18 +53,25 @@ class MultinomialOp : public framework::OperatorWithKernel { auto x_dim = ctx->GetInputDim("X"); int64_t x_rank = x_dim.size(); - PADDLE_ENFORCE_EQ( - x_rank > 0 && x_rank <= 2, true, - platform::errors::PreconditionNotMet( - "Input probability distribution should be 1 or 2 dimension")); + PADDLE_ENFORCE_GT(x_rank, 0, platform::errors::PreconditionNotMet( + "Input probability distribution should be " + "1 or 2 dimension, but got %d", + x_rank)); + PADDLE_ENFORCE_LE(x_rank, 2, platform::errors::PreconditionNotMet( + "Input probability distribution should be " + "1 or 2 dimension, but got %d", + x_rank)); + std::vector out_dims(x_rank); for (int64_t i = 0; i < x_rank - 1; i++) { out_dims[i] = x_dim[i]; } int64_t num_samples = ctx->Attrs().Get("num_samples"); - PADDLE_ENFORCE_GT(num_samples, 0, platform::errors::OutOfRange( - "Number of samples should be > 0")); + PADDLE_ENFORCE_GT( + num_samples, 0, + platform::errors::OutOfRange( + "The number of samples should be > 0, but got %d", num_samples)); out_dims[x_rank - 1] = num_samples; ctx->SetOutputDim("Out", framework::make_ddim(out_dims)); diff --git a/paddle/fluid/operators/multinomial_op.cu b/paddle/fluid/operators/multinomial_op.cu index 336c14d2c922bc..4bd974e9f1c590 100644 --- a/paddle/fluid/operators/multinomial_op.cu +++ b/paddle/fluid/operators/multinomial_op.cu @@ -32,12 +32,14 @@ __global__ void NormalizeProbability(T* norm_probs, const T* in_data, T* sum_rows) { int id = threadIdx.x + blockIdx.x * blockDim.x + blockIdx.y * gridDim.x * blockDim.x; - PADDLE_ENFORCE(in_data[id] >= 0.0, - "The input of multinomial distribution should be >= 0"); PADDLE_ENFORCE( - !std::isinf(static_cast(in_data[id])) && - !std::isnan(static_cast(in_data[id])), - "The input of multinomial distribution shoud not be infinity or NaN"); + in_data[id] >= 0.0, + "The input of multinomial distribution should be >= 0, but got %f", + in_data[id]); + PADDLE_ENFORCE(!std::isinf(static_cast(in_data[id])), + "The input of multinomial distribution shoud not be infinity"); + PADDLE_ENFORCE(!std::isnan(static_cast(in_data[id])), + "The input of multinomial distribution shoud not be NaN"); PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, "The sum of input should not be 0"); norm_probs[id] = in_data[id] / sum_rows[blockIdx.y]; From d5fe719d123eb6bf8cf8db56ca31c1675cda301e Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 14 Oct 2020 13:06:37 +0000 Subject: [PATCH 06/12] fix CPU Kernel error message format --- paddle/fluid/operators/multinomial_op.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.h b/paddle/fluid/operators/multinomial_op.h index 420d2cd11e37df..4ad6b9f0d97494 100644 --- a/paddle/fluid/operators/multinomial_op.h +++ b/paddle/fluid/operators/multinomial_op.h @@ -46,13 +46,18 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, prob_value = in_data[i * num_categories + j]; PADDLE_ENFORCE_GE( prob_value, 0.0, + platform::errors::OutOfRange("The input of multinomial distribution " + "should be >= 0, but got %f", + prob_value)); + PADDLE_ENFORCE_EQ( + std::isinf(static_cast(prob_value)), false, platform::errors::OutOfRange( - "The input of multinomial distribution should be >= 0")); - PADDLE_ENFORCE_EQ((std::isinf(static_cast(prob_value)) || - std::isnan(static_cast(prob_value))), - false, platform::errors::OutOfRange( - "The input of multinomial distribution " - "shoud not be infinity or NaN")); + "The input of multinomial distribution shoud not be infinity")); + PADDLE_ENFORCE_EQ( + std::isnan(static_cast(prob_value)), false, + platform::errors::OutOfRange( + "The input of multinomial distribution shoud not be NaN")); + probs_sum += prob_value; if (prob_value == 0) { num_zeros += 1; From f616d5b3407484f4bc577d93fe401773da11078c Mon Sep 17 00:00:00 2001 From: pangyoki Date: Thu, 15 Oct 2020 08:53:33 +0000 Subject: [PATCH 07/12] fix isinf and isnan error in WindowsOPENBLAS CI --- paddle/fluid/operators/multinomial_op.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cu b/paddle/fluid/operators/multinomial_op.cu index 4bd974e9f1c590..9f79d0ee1b7407 100644 --- a/paddle/fluid/operators/multinomial_op.cu +++ b/paddle/fluid/operators/multinomial_op.cu @@ -36,9 +36,9 @@ __global__ void NormalizeProbability(T* norm_probs, const T* in_data, in_data[id] >= 0.0, "The input of multinomial distribution should be >= 0, but got %f", in_data[id]); - PADDLE_ENFORCE(!std::isinf(static_cast(in_data[id])), + PADDLE_ENFORCE(in_data[id] != INFINITY, "The input of multinomial distribution shoud not be infinity"); - PADDLE_ENFORCE(!std::isnan(static_cast(in_data[id])), + PADDLE_ENFORCE(in_data[id] != NAN, "The input of multinomial distribution shoud not be NaN"); PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, "The sum of input should not be 0"); From 994713c6b76fd94597b12fbd484d34eee6da0b63 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Fri, 16 Oct 2020 03:02:12 +0000 Subject: [PATCH 08/12] delete inf and nan --- paddle/fluid/operators/multinomial_op.cc | 18 ++++++++++-------- paddle/fluid/operators/multinomial_op.cu | 7 +------ paddle/fluid/operators/multinomial_op.h | 8 -------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cc b/paddle/fluid/operators/multinomial_op.cc index 7e32881be2fa2c..f39b0624042914 100644 --- a/paddle/fluid/operators/multinomial_op.cc +++ b/paddle/fluid/operators/multinomial_op.cc @@ -53,14 +53,16 @@ class MultinomialOp : public framework::OperatorWithKernel { auto x_dim = ctx->GetInputDim("X"); int64_t x_rank = x_dim.size(); - PADDLE_ENFORCE_GT(x_rank, 0, platform::errors::PreconditionNotMet( - "Input probability distribution should be " - "1 or 2 dimension, but got %d", - x_rank)); - PADDLE_ENFORCE_LE(x_rank, 2, platform::errors::PreconditionNotMet( - "Input probability distribution should be " - "1 or 2 dimension, but got %d", - x_rank)); + PADDLE_ENFORCE_GT(x_rank, 0, + platform::errors::PreconditionNotMet( + "The number of dimensions of the input probability " + "distribution should be > 0, but got %d.", + x_rank)); + PADDLE_ENFORCE_LE(x_rank, 2, + platform::errors::PreconditionNotMet( + "The number of dimensions of the input probability " + "distribution should be <= 2, but got %d.", + x_rank)); std::vector out_dims(x_rank); for (int64_t i = 0; i < x_rank - 1; i++) { diff --git a/paddle/fluid/operators/multinomial_op.cu b/paddle/fluid/operators/multinomial_op.cu index 9f79d0ee1b7407..6e844b9603fddd 100644 --- a/paddle/fluid/operators/multinomial_op.cu +++ b/paddle/fluid/operators/multinomial_op.cu @@ -36,12 +36,7 @@ __global__ void NormalizeProbability(T* norm_probs, const T* in_data, in_data[id] >= 0.0, "The input of multinomial distribution should be >= 0, but got %f", in_data[id]); - PADDLE_ENFORCE(in_data[id] != INFINITY, - "The input of multinomial distribution shoud not be infinity"); - PADDLE_ENFORCE(in_data[id] != NAN, - "The input of multinomial distribution shoud not be NaN"); - PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, - "The sum of input should not be 0"); + PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, "The sum of input should be > 0"); norm_probs[id] = in_data[id] / sum_rows[blockIdx.y]; } diff --git a/paddle/fluid/operators/multinomial_op.h b/paddle/fluid/operators/multinomial_op.h index 4ad6b9f0d97494..2b8670537949a5 100644 --- a/paddle/fluid/operators/multinomial_op.h +++ b/paddle/fluid/operators/multinomial_op.h @@ -49,14 +49,6 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, platform::errors::OutOfRange("The input of multinomial distribution " "should be >= 0, but got %f", prob_value)); - PADDLE_ENFORCE_EQ( - std::isinf(static_cast(prob_value)), false, - platform::errors::OutOfRange( - "The input of multinomial distribution shoud not be infinity")); - PADDLE_ENFORCE_EQ( - std::isnan(static_cast(prob_value)), false, - platform::errors::OutOfRange( - "The input of multinomial distribution shoud not be NaN")); probs_sum += prob_value; if (prob_value == 0) { From 01f331d87a564382a1a4df49fae74ca9a04d06b6 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Fri, 16 Oct 2020 06:20:57 +0000 Subject: [PATCH 09/12] add manual_seed in sample code --- paddle/fluid/operators/multinomial_op.cc | 4 +- paddle/fluid/operators/multinomial_op.h | 2 +- python/paddle/distribution.py | 67 ++++++++++++++---------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cc b/paddle/fluid/operators/multinomial_op.cc index f39b0624042914..5086c71ac494db 100644 --- a/paddle/fluid/operators/multinomial_op.cc +++ b/paddle/fluid/operators/multinomial_op.cc @@ -55,12 +55,12 @@ class MultinomialOp : public framework::OperatorWithKernel { int64_t x_rank = x_dim.size(); PADDLE_ENFORCE_GT(x_rank, 0, platform::errors::PreconditionNotMet( - "The number of dimensions of the input probability " + "The number of dimensions of the input probability " "distribution should be > 0, but got %d.", x_rank)); PADDLE_ENFORCE_LE(x_rank, 2, platform::errors::PreconditionNotMet( - "The number of dimensions of the input probability " + "The number of dimensions of the input probability " "distribution should be <= 2, but got %d.", x_rank)); diff --git a/paddle/fluid/operators/multinomial_op.h b/paddle/fluid/operators/multinomial_op.h index 2b8670537949a5..37844fcf1061a3 100644 --- a/paddle/fluid/operators/multinomial_op.h +++ b/paddle/fluid/operators/multinomial_op.h @@ -57,7 +57,7 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, cumulative_probs[j] = probs_sum; } PADDLE_ENFORCE_GT(probs_sum, 0.0, platform::errors::OutOfRange( - "The sum of input should not be 0")); + "The sum of input should be > 0")); PADDLE_ENFORCE_EQ( (replacement || (num_categories - num_zeros >= num_samples)), true, platform::errors::OutOfRange("When replacement is False, number of " diff --git a/python/paddle/distribution.py b/python/paddle/distribution.py index 7e7a81730e3b10..f5535b9fc05092 100644 --- a/python/paddle/distribution.py +++ b/python/paddle/distribution.py @@ -670,34 +670,37 @@ class Categorical(Distribution): import paddle from paddle.distribution import Categorical + paddle.manual_seed(100) x = paddle.rand([6]) print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + # [0.00224779 0.50324494 0.13526054 + # 0.1611277 0.7955702 0.96897715] + + paddle.manual_seed(200) y = paddle.rand([6]) print(y.numpy()) - # [0.6365463 , 0.7278677 , 0.90260243, - # 0.5226815 , 0.35837543, 0.13981032] + # [0.00449559 0.00648983 0.27052107 + # 0.3222554 0.5911404 0.93795437] cat = Categorical(x) cat2 = Categorical(y) cat.sample([2,3]) - # [[5, 1, 1], - # [0, 1, 2]] + # [[4, 5, 5], + # [4, 2, 3]] cat.entropy() - # [1.71887] + # [1.72595] cat.kl_divergence(cat2) - # [0.0278455] + # [0.0218145] value = paddle.to_tensor([2,1,3]) cat.probs(value) - # [0.341613 0.342648 0.03123] + # [0.0527038 0.196088 0.0627829] cat.log_prob(value) - # [-1.07408 -1.07105 -3.46638] + # [-2.94307 -1.62919 -2.76807] """ @@ -740,16 +743,17 @@ def sample(self, shape): import paddle from paddle.distribution import Categorical + paddle.manual_seed(100) x = paddle.rand([6]) print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + # [0.00224779 0.50324494 0.13526054 + # 0.1611277 0.7955702 0.96897715] cat = Categorical(x) cat.sample([2,3]) - # [[5, 1, 1], - # [0, 1, 2]] + # [[4, 5, 5], + # [4, 2, 3]] """ name = self.name + '_sample' @@ -785,20 +789,23 @@ def kl_divergence(self, other): import paddle from paddle.distribution import Categorical + paddle.manual_seed(100) x = paddle.rand([6]) print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + # [0.00224779 0.50324494 0.13526054 + # 0.1611277 0.7955702 0.96897715] + + paddle.manual_seed(200) y = paddle.rand([6]) print(y.numpy()) - # [0.6365463 , 0.7278677 , 0.90260243, - # 0.5226815 , 0.35837543, 0.13981032] + # [0.00449559 0.00648983 0.27052107 + # 0.3222554 0.5911404 0.93795437] cat = Categorical(x) cat2 = Categorical(y) cat.kl_divergence(cat2) - # [0.0278455] + # [0.0218145] """ name = self.name + '_kl_divergence' @@ -833,15 +840,16 @@ def entropy(self): import paddle from paddle.distribution import Categorical + paddle.manual_seed(100) x = paddle.rand([6]) print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + # [0.00224779 0.50324494 0.13526054 + # 0.1611277 0.7955702 0.96897715] cat = Categorical(x) cat.entropy() - # [1.71887] + # [1.72595] """ name = self.name + '_entropy' @@ -877,16 +885,17 @@ def probs(self, value): import paddle from paddle.distribution import Categorical + paddle.manual_seed(100) x = paddle.rand([6]) print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + # [0.00224779 0.50324494 0.13526054 + # 0.1611277 0.7955702 0.96897715] cat = Categorical(x) value = paddle.to_tensor([2,1,3]) cat.probs(value) - # [0.341613 0.342648 0.03123] + # [0.0527038 0.196088 0.0627829] """ name = self.name + '_probs' @@ -942,17 +951,17 @@ def log_prob(self, value): import paddle from paddle.distribution import Categorical + paddle.manual_seed(100) x = paddle.rand([6]) print(x.numpy()) - # [0.32564053, 0.99334985, 0.99034804, - # 0.09053693, 0.30820143, 0.19095989] + # [0.00224779 0.50324494 0.13526054 + # 0.1611277 0.7955702 0.96897715] cat = Categorical(x) value = paddle.to_tensor([2,1,3]) - cat.log_prob(value) - # [-1.07408 -1.07105 -3.46638] + # [-2.94307 -1.62919 -2.76807] """ name = self.name + '_log_prob' From 8250e582913eea0c199090b8da94bd52095ac6c4 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Fri, 16 Oct 2020 06:37:01 +0000 Subject: [PATCH 10/12] little error message change --- paddle/fluid/operators/multinomial_op.cu | 5 ++++- paddle/fluid/operators/multinomial_op.h | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cu b/paddle/fluid/operators/multinomial_op.cu index 6e844b9603fddd..d8c1496c12f9bf 100644 --- a/paddle/fluid/operators/multinomial_op.cu +++ b/paddle/fluid/operators/multinomial_op.cu @@ -36,7 +36,10 @@ __global__ void NormalizeProbability(T* norm_probs, const T* in_data, in_data[id] >= 0.0, "The input of multinomial distribution should be >= 0, but got %f", in_data[id]); - PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, "The sum of input should be > 0"); + PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, + "The sum of one multinomial distribution probability should " + "be > 0, but got %f", + sum_rows[blockIdx.y]); norm_probs[id] = in_data[id] / sum_rows[blockIdx.y]; } diff --git a/paddle/fluid/operators/multinomial_op.h b/paddle/fluid/operators/multinomial_op.h index 37844fcf1061a3..885dde94c4fc5d 100644 --- a/paddle/fluid/operators/multinomial_op.h +++ b/paddle/fluid/operators/multinomial_op.h @@ -56,8 +56,11 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, } cumulative_probs[j] = probs_sum; } - PADDLE_ENFORCE_GT(probs_sum, 0.0, platform::errors::OutOfRange( - "The sum of input should be > 0")); + PADDLE_ENFORCE_GT( + probs_sum, 0.0, + platform::errors::OutOfRange("The sum of one multinomial distribution " + "probability should be > 0, but got %f", + probs_sum)); PADDLE_ENFORCE_EQ( (replacement || (num_categories - num_zeros >= num_samples)), true, platform::errors::OutOfRange("When replacement is False, number of " From 0b7f5cf7528cf2ecbe57d33e207ece6916db94e1 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Fri, 16 Oct 2020 06:50:13 +0000 Subject: [PATCH 11/12] change error message to InvalidArgument --- paddle/fluid/operators/multinomial_op.cc | 6 +++--- paddle/fluid/operators/multinomial_op.h | 27 ++++++++++++------------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cc b/paddle/fluid/operators/multinomial_op.cc index 5086c71ac494db..1dba8608f078e6 100644 --- a/paddle/fluid/operators/multinomial_op.cc +++ b/paddle/fluid/operators/multinomial_op.cc @@ -54,12 +54,12 @@ class MultinomialOp : public framework::OperatorWithKernel { auto x_dim = ctx->GetInputDim("X"); int64_t x_rank = x_dim.size(); PADDLE_ENFORCE_GT(x_rank, 0, - platform::errors::PreconditionNotMet( + platform::errors::InvalidArgument( "The number of dimensions of the input probability " "distribution should be > 0, but got %d.", x_rank)); PADDLE_ENFORCE_LE(x_rank, 2, - platform::errors::PreconditionNotMet( + platform::errors::InvalidArgument( "The number of dimensions of the input probability " "distribution should be <= 2, but got %d.", x_rank)); @@ -72,7 +72,7 @@ class MultinomialOp : public framework::OperatorWithKernel { int64_t num_samples = ctx->Attrs().Get("num_samples"); PADDLE_ENFORCE_GT( num_samples, 0, - platform::errors::OutOfRange( + platform::errors::InvalidArgument( "The number of samples should be > 0, but got %d", num_samples)); out_dims[x_rank - 1] = num_samples; diff --git a/paddle/fluid/operators/multinomial_op.h b/paddle/fluid/operators/multinomial_op.h index 885dde94c4fc5d..8a7762c72d8025 100644 --- a/paddle/fluid/operators/multinomial_op.h +++ b/paddle/fluid/operators/multinomial_op.h @@ -44,11 +44,11 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, int64_t num_zeros = 0; for (int64_t j = 0; j < num_categories; j++) { prob_value = in_data[i * num_categories + j]; - PADDLE_ENFORCE_GE( - prob_value, 0.0, - platform::errors::OutOfRange("The input of multinomial distribution " - "should be >= 0, but got %f", - prob_value)); + PADDLE_ENFORCE_GE(prob_value, 0.0, + platform::errors::InvalidArgument( + "The input of multinomial distribution " + "should be >= 0, but got %f", + prob_value)); probs_sum += prob_value; if (prob_value == 0) { @@ -56,16 +56,17 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, } cumulative_probs[j] = probs_sum; } - PADDLE_ENFORCE_GT( - probs_sum, 0.0, - platform::errors::OutOfRange("The sum of one multinomial distribution " - "probability should be > 0, but got %f", - probs_sum)); + PADDLE_ENFORCE_GT(probs_sum, 0.0, + platform::errors::InvalidArgument( + "The sum of one multinomial distribution " + "probability should be > 0, but got %f", + probs_sum)); PADDLE_ENFORCE_EQ( (replacement || (num_categories - num_zeros >= num_samples)), true, - platform::errors::OutOfRange("When replacement is False, number of " - "samples should be less than non-zero " - "categories")); + platform::errors::InvalidArgument( + "When replacement is False, number of " + "samples should be less than non-zero " + "categories")); for (int64_t j = 0; j < num_categories; j++) { cumulative_probs[j] /= probs_sum; From 9a3b6e411bb3968be5d33d5c401064ace116351a Mon Sep 17 00:00:00 2001 From: pangyoki Date: Fri, 16 Oct 2020 13:36:28 +0000 Subject: [PATCH 12/12] add full point for error message and add manual_seed in CPU environment --- paddle/fluid/operators/multinomial_op.cc | 2 +- paddle/fluid/operators/multinomial_op.cu | 4 +- paddle/fluid/operators/multinomial_op.h | 6 +- python/paddle/distribution.py | 74 ++++++++++++------------ python/paddle/tensor/random.py | 29 ++++++---- 5 files changed, 61 insertions(+), 54 deletions(-) diff --git a/paddle/fluid/operators/multinomial_op.cc b/paddle/fluid/operators/multinomial_op.cc index 1dba8608f078e6..165d402342162f 100644 --- a/paddle/fluid/operators/multinomial_op.cc +++ b/paddle/fluid/operators/multinomial_op.cc @@ -73,7 +73,7 @@ class MultinomialOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_GT( num_samples, 0, platform::errors::InvalidArgument( - "The number of samples should be > 0, but got %d", num_samples)); + "The number of samples should be > 0, but got %d.", num_samples)); out_dims[x_rank - 1] = num_samples; ctx->SetOutputDim("Out", framework::make_ddim(out_dims)); diff --git a/paddle/fluid/operators/multinomial_op.cu b/paddle/fluid/operators/multinomial_op.cu index d8c1496c12f9bf..92f7c992ed9763 100644 --- a/paddle/fluid/operators/multinomial_op.cu +++ b/paddle/fluid/operators/multinomial_op.cu @@ -34,11 +34,11 @@ __global__ void NormalizeProbability(T* norm_probs, const T* in_data, blockIdx.y * gridDim.x * blockDim.x; PADDLE_ENFORCE( in_data[id] >= 0.0, - "The input of multinomial distribution should be >= 0, but got %f", + "The input of multinomial distribution should be >= 0, but got %f.", in_data[id]); PADDLE_ENFORCE(sum_rows[blockIdx.y] > 0.0, "The sum of one multinomial distribution probability should " - "be > 0, but got %f", + "be > 0, but got %f.", sum_rows[blockIdx.y]); norm_probs[id] = in_data[id] / sum_rows[blockIdx.y]; } diff --git a/paddle/fluid/operators/multinomial_op.h b/paddle/fluid/operators/multinomial_op.h index 8a7762c72d8025..14cfbd268389ec 100644 --- a/paddle/fluid/operators/multinomial_op.h +++ b/paddle/fluid/operators/multinomial_op.h @@ -47,7 +47,7 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, PADDLE_ENFORCE_GE(prob_value, 0.0, platform::errors::InvalidArgument( "The input of multinomial distribution " - "should be >= 0, but got %f", + "should be >= 0, but got %f.", prob_value)); probs_sum += prob_value; @@ -59,14 +59,14 @@ void MultinomialFunctor(int64_t* out_data, const T* in_data, PADDLE_ENFORCE_GT(probs_sum, 0.0, platform::errors::InvalidArgument( "The sum of one multinomial distribution " - "probability should be > 0, but got %f", + "probability should be > 0, but got %f.", probs_sum)); PADDLE_ENFORCE_EQ( (replacement || (num_categories - num_zeros >= num_samples)), true, platform::errors::InvalidArgument( "When replacement is False, number of " "samples should be less than non-zero " - "categories")); + "categories.")); for (int64_t j = 0; j < num_categories; j++) { cumulative_probs[j] /= probs_sum; diff --git a/python/paddle/distribution.py b/python/paddle/distribution.py index f5535b9fc05092..da70bd702dac10 100644 --- a/python/paddle/distribution.py +++ b/python/paddle/distribution.py @@ -670,37 +670,38 @@ class Categorical(Distribution): import paddle from paddle.distribution import Categorical - paddle.manual_seed(100) + paddle.manual_seed(100) # on CPU device x = paddle.rand([6]) print(x.numpy()) - # [0.00224779 0.50324494 0.13526054 - # 0.1611277 0.7955702 0.96897715] + # [0.5535528 0.20714243 0.01162981 + # 0.51577556 0.36369765 0.2609165 ] - paddle.manual_seed(200) + paddle.manual_seed(200) # on CPU device y = paddle.rand([6]) print(y.numpy()) - # [0.00449559 0.00648983 0.27052107 - # 0.3222554 0.5911404 0.93795437] + # [0.77663314 0.90824795 0.15685187 + # 0.04279523 0.34468332 0.7955718 ] cat = Categorical(x) cat2 = Categorical(y) + paddle.manual_seed(1000) # on CPU device cat.sample([2,3]) - # [[4, 5, 5], - # [4, 2, 3]] + # [[0, 0, 5], + # [3, 4, 5]] cat.entropy() - # [1.72595] + # [1.77528] cat.kl_divergence(cat2) - # [0.0218145] + # [0.071952] value = paddle.to_tensor([2,1,3]) cat.probs(value) - # [0.0527038 0.196088 0.0627829] + # [0.00608027 0.108298 0.269656] cat.log_prob(value) - # [-2.94307 -1.62919 -2.76807] + # [-5.10271 -2.22287 -1.31061] """ @@ -743,17 +744,18 @@ def sample(self, shape): import paddle from paddle.distribution import Categorical - paddle.manual_seed(100) + paddle.manual_seed(100) # on CPU device x = paddle.rand([6]) print(x.numpy()) - # [0.00224779 0.50324494 0.13526054 - # 0.1611277 0.7955702 0.96897715] + # [0.5535528 0.20714243 0.01162981 + # 0.51577556 0.36369765 0.2609165 ] cat = Categorical(x) + paddle.manual_seed(1000) # on CPU device cat.sample([2,3]) - # [[4, 5, 5], - # [4, 2, 3]] + # [[0, 0, 5], + # [3, 4, 5]] """ name = self.name + '_sample' @@ -789,23 +791,23 @@ def kl_divergence(self, other): import paddle from paddle.distribution import Categorical - paddle.manual_seed(100) + paddle.manual_seed(100) # on CPU device x = paddle.rand([6]) print(x.numpy()) - # [0.00224779 0.50324494 0.13526054 - # 0.1611277 0.7955702 0.96897715] + # [0.5535528 0.20714243 0.01162981 + # 0.51577556 0.36369765 0.2609165 ] - paddle.manual_seed(200) + paddle.manual_seed(200) # on CPU device y = paddle.rand([6]) print(y.numpy()) - # [0.00449559 0.00648983 0.27052107 - # 0.3222554 0.5911404 0.93795437] + # [0.77663314 0.90824795 0.15685187 + # 0.04279523 0.34468332 0.7955718 ] cat = Categorical(x) cat2 = Categorical(y) cat.kl_divergence(cat2) - # [0.0218145] + # [0.071952] """ name = self.name + '_kl_divergence' @@ -840,16 +842,16 @@ def entropy(self): import paddle from paddle.distribution import Categorical - paddle.manual_seed(100) + paddle.manual_seed(100) # on CPU device x = paddle.rand([6]) print(x.numpy()) - # [0.00224779 0.50324494 0.13526054 - # 0.1611277 0.7955702 0.96897715] + # [0.5535528 0.20714243 0.01162981 + # 0.51577556 0.36369765 0.2609165 ] cat = Categorical(x) cat.entropy() - # [1.72595] + # [1.77528] """ name = self.name + '_entropy' @@ -885,17 +887,17 @@ def probs(self, value): import paddle from paddle.distribution import Categorical - paddle.manual_seed(100) + paddle.manual_seed(100) # on CPU device x = paddle.rand([6]) print(x.numpy()) - # [0.00224779 0.50324494 0.13526054 - # 0.1611277 0.7955702 0.96897715] + # [0.5535528 0.20714243 0.01162981 + # 0.51577556 0.36369765 0.2609165 ] cat = Categorical(x) value = paddle.to_tensor([2,1,3]) cat.probs(value) - # [0.0527038 0.196088 0.0627829] + # [0.00608027 0.108298 0.269656] """ name = self.name + '_probs' @@ -951,17 +953,17 @@ def log_prob(self, value): import paddle from paddle.distribution import Categorical - paddle.manual_seed(100) + paddle.manual_seed(100) # on CPU device x = paddle.rand([6]) print(x.numpy()) - # [0.00224779 0.50324494 0.13526054 - # 0.1611277 0.7955702 0.96897715] + # [0.5535528 0.20714243 0.01162981 + # 0.51577556 0.36369765 0.2609165 ] cat = Categorical(x) value = paddle.to_tensor([2,1,3]) cat.log_prob(value) - # [-2.94307 -1.62919 -2.76807] + # [-5.10271 -2.22287 -1.31061] """ name = self.name + '_log_prob' diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index ffd9f10f5403e5..5ddceb51071968 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -59,15 +59,17 @@ def bernoulli(x, name=None): import paddle - x = paddle.rand([2, 3]) + paddle.manual_seed(100) # on CPU device + x = paddle.rand([2,3]) print(x.numpy()) - # [[0.11272584 0.3890902 0.7730957 ] - # [0.10351662 0.8510418 0.63806665]] + # [[0.5535528 0.20714243 0.01162981] + # [0.51577556 0.36369765 0.2609165 ]] + paddle.manual_seed(200) # on CPU device out = paddle.bernoulli(x) print(out.numpy()) - # [[0. 0. 1.] - # [0. 0. 1.]] + # [[0. 0. 0.] + # [1. 1. 0.]] """ @@ -108,24 +110,27 @@ def multinomial(x, num_samples=1, replacement=False, name=None): import paddle + paddle.manual_seed(100) # on CPU device x = paddle.rand([2,4]) print(x.numpy()) - # [[0.7713825 0.4055941 0.433339 0.70706886] - # [0.9223313 0.8519825 0.04574518 0.16560672]] + # [[0.5535528 0.20714243 0.01162981 0.51577556] + # [0.36369765 0.2609165 0.18905126 0.5621971 ]] + paddle.manual_seed(200) # on CPU device out1 = paddle.multinomial(x, num_samples=5, replacement=True) print(out1.numpy()) - # [[3, 3, 1, 1, 0] - # [0, 0, 0, 0, 1]] + # [[3 3 0 0 0] + # [3 3 3 1 0]] # out2 = paddle.multinomial(x, num_samples=5) - # OutOfRangeError: When replacement is False, number of samples + # InvalidArgumentError: When replacement is False, number of samples # should be less than non-zero categories + paddle.manual_seed(300) # on CPU device out3 = paddle.multinomial(x, num_samples=3) print(out3.numpy()) - # [[0, 2, 3] - # [0, 1, 3]] + # [[3 0 1] + # [3 1 0]] """